KMPreferenceWindowController.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // KMPreferenceWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/2.
  6. //
  7. import Cocoa
  8. protocol KMPreferenceDealDataChangeProtocol: NSObjectProtocol {
  9. func refreshUI()
  10. }
  11. class KMPreferenceWindowController: NSWindowController {
  12. static let shared = KMPreferenceWindowController(windowNibName: "KMPreferenceWindowController")
  13. @IBOutlet weak var titleBarBox: NSBox!
  14. @IBOutlet weak var contentBox: NSBox!
  15. @IBOutlet weak var bottomBarBox: NSBox!
  16. @IBOutlet weak var resetButton: NSButton!
  17. @IBOutlet weak var resetAllButton: NSButton!
  18. @IBOutlet weak var contentHeightConst: NSLayoutConstraint!
  19. @IBOutlet weak var contentWidthConst: NSLayoutConstraint!
  20. var currentController: KMPreferenceDealDataChangeProtocol?
  21. var currentItemIndex: Int = 0
  22. deinit {
  23. NotificationCenter.default.removeObserver(self)
  24. }
  25. override func loadWindow() {
  26. super.loadWindow()
  27. self.adjectWindowSize(NSSize(width: 512, height: 504), false)
  28. }
  29. override func showWindow(_ sender: Any?) {
  30. super.showWindow(sender)
  31. self.currentController?.refreshUI()
  32. }
  33. override func windowDidLoad() {
  34. super.windowDidLoad()
  35. self.window?.title = NSLocalizedString("Preferences", comment: "")
  36. self.window?.titlebarAppearsTransparent = true
  37. self.window?.appearance = NSAppearance(named: .aqua)
  38. self.titleBarBox.fillColor = NSColor(white: 245/255.0, alpha: 1.0)
  39. self.window?.backgroundColor = NSColor(white: 245/255.0, alpha: 1.0)
  40. self.contentBox.fillColor = NSColor(white: 241/255.0, alpha: 1.0)
  41. self.bottomBarBox.fillColor = NSColor(white: 241/255.0, alpha: 1.0)
  42. let segmentControl = KMPreferenceSegementControl()
  43. segmentControl.frame = self.titleBarBox.contentView!.bounds
  44. segmentControl.autoresizingMask = [.width, .height]
  45. self.titleBarBox.contentView?.addSubview(segmentControl)
  46. segmentControl.target = self
  47. segmentControl.action = #selector(segmentControlSelectionDidChange)
  48. let titles = [NSLocalizedString("General", comment: ""),NSLocalizedString("Display", comment: ""), NSLocalizedString("Annotation", comment: "")]
  49. let selectedImages = ["KMImageNamePreferenceGeneralSelected", "KMImageNamePreferenceDisplaySelected", "KMImageNamePreferenceAnnotationSelected"]
  50. let unselectedImages = ["KMImageNamePreferenceGeneralUnSelected", "KMImageNamePreferenceDisplayUnSelected", "KMImageNamePreferenceAnnotationUnSelected"]
  51. var i: Int = 0
  52. for item in segmentControl.items {
  53. item.label.font = NSFont.systemFont(ofSize: 11)
  54. item.label.stringValue = titles[i]
  55. item.unSelectedTextColor = NSColor(red: 97/255.0, green: 100/255.0, blue: 105/255.0, alpha: 1.0)
  56. item.selectedTextColor = NSColor(red: 23/255.0, green: 112/255.0, blue: 244/255.0, alpha: 1.0)
  57. item.selectedImage = NSImage(named: selectedImages[i])
  58. item.unSelectedImage = NSImage(named: unselectedImages[i])
  59. i += 1
  60. }
  61. segmentControl.selectItem(segmentControl.items.first!.itemID)
  62. self.showContent(segmentControl.items.first!.itemID)
  63. resetButton.title = NSLocalizedString("Reset", comment: "")
  64. resetButton.target = self
  65. resetButton.action = #selector(resetAction)
  66. resetAllButton.title = NSLocalizedString("Reset All", comment: "")
  67. resetAllButton.target = self
  68. resetAllButton.action = #selector(resetAllAction)
  69. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  70. }
  71. @objc func resetAction() {
  72. if (self.currentItemIndex == 1) {
  73. KMPreferenceManager.shared.resetData(.general)
  74. } else if (self.currentItemIndex == 2) {
  75. KMPreferenceManager.shared.resetData(.display)
  76. } else if (self.currentItemIndex == 3) {
  77. KMPreferenceManager.shared.resetData(.markup)
  78. }
  79. }
  80. @objc func resetAllAction() {
  81. KMPreferenceManager.shared.resetAllData()
  82. }
  83. @objc func segmentControlSelectionDidChange(sender: KMPreferenceSegementControl) {
  84. if (self.currentItemIndex == sender.selectedIndex) {
  85. return
  86. }
  87. self.showContent(sender.selectedIndex)
  88. }
  89. @objc private func preferenceDidChangeNotification(sender: Notification) {
  90. if (self.currentController == nil) {
  91. return
  92. }
  93. self.currentController?.refreshUI()
  94. }
  95. private func adjectWindowSize(_ size: NSSize) {
  96. self.adjectWindowSize(size, true)
  97. }
  98. private func adjectWindowSize(_ size: NSSize, _ animated: Bool) {
  99. if (animated) {
  100. self.contentWidthConst.animator().constant = size.width
  101. self.contentHeightConst.animator().constant = size.height-120
  102. } else {
  103. self.contentWidthConst.constant = size.width
  104. self.contentHeightConst.constant = size.height-120
  105. }
  106. }
  107. private func showContent(_ index: Int) {
  108. for view in (self.contentBox.contentView?.subviews)! {
  109. view.removeFromSuperview()
  110. }
  111. self.currentItemIndex = index
  112. if (index == 1) {
  113. self.adjectWindowSize(NSSize(width: 512, height: 504), false)
  114. let controller = KMPreferenceGeneralController()
  115. self.contentBox.contentView?.addSubview(controller.view)
  116. controller.view.frame = self.contentBox.contentView!.bounds
  117. controller.view.autoresizingMask = [.width, .height]
  118. self.currentController = controller
  119. } else if (index == 2) {
  120. self.adjectWindowSize(NSSize(width: 512, height: 570), false)
  121. let controller = KMPreferenceDisplayController()
  122. self.contentBox.contentView?.addSubview(controller.view)
  123. controller.view.frame = self.contentBox.contentView!.bounds
  124. controller.view.autoresizingMask = [.width, .height]
  125. self.currentController = controller
  126. } else if (index == 3) {
  127. self.adjectWindowSize(NSSize(width: 512, height: 532), false)
  128. let controller = KMPreferenceMarkupController()
  129. self.contentBox.contentView?.addSubview(controller.view)
  130. controller.view.frame = self.contentBox.contentView!.bounds
  131. controller.view.autoresizingMask = [.width, .height]
  132. self.currentController = controller
  133. }
  134. }
  135. }