SettingsWindowController.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // SettingsWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/9/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class SettingsWindowController: NSWindowController {
  10. @IBOutlet weak var typeContendBox: NSBox!
  11. @IBOutlet weak var generalView: ComponentNavBarItem!
  12. @IBOutlet weak var displayView: ComponentNavBarItem!
  13. @IBOutlet weak var typeDivider: ComponentDivider!
  14. @IBOutlet weak var infoContendBox: NSBox!
  15. @IBOutlet weak var scrollView: NSScrollView!
  16. @IBOutlet weak var resetContendBox: NSBox!
  17. @IBOutlet weak var resetButton: ComponentButton!
  18. @IBOutlet weak var resetAllButton: ComponentButton!
  19. @IBOutlet weak var resetButtonWidthConst: NSLayoutConstraint!
  20. @IBOutlet weak var resetAllButtonWidthConst: NSLayoutConstraint!
  21. static let shared = SettingsWindowController(windowNibName: "SettingsWindowController")
  22. var generalContendView: SettingsGeneralView = SettingsGeneralView(frame: CGRectMake(0, 0, 640, 692))
  23. var displayContendView: SettingsDisplayView = SettingsDisplayView(frame: CGRectMake(0, 0, 640, 624))
  24. var generalProperty: ComponentNavbarItemProperty = ComponentNavbarItemProperty(state: .normal,
  25. text: KMLocalizedString("General", comment: ""),
  26. iconImage: ComponentLibrary.shared.image(forResource: "test"))
  27. var displayProperty: ComponentNavbarItemProperty = ComponentNavbarItemProperty(state: .normal,
  28. text: KMLocalizedString("Page Display", comment: ""),
  29. iconImage: ComponentLibrary.shared.image(forResource: "test"))
  30. //MARK: - Func
  31. override func windowWillLoad() {
  32. super.windowWillLoad()
  33. SettingsManager.sharedInstance.config()
  34. }
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  38. self.window?.delegate = self
  39. self.setUpTypeView()
  40. self.settingTypeChoosed(generalView)
  41. self.setUpResetButton()
  42. NotificationCenter.default.addObserver(self, selector: #selector(windowdidResizeNotification(_:)), name: NSWindow.didResizeNotification, object: self.window)
  43. }
  44. func setUpTypeView() {
  45. if let value = ComponentLibrary.shared.getComponentValueFromKey("colorBg/layout-middle") {
  46. let currentValue = value as! [String : Any]
  47. typeContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  48. }
  49. generalView.properties = generalProperty
  50. generalView.setTarget(self, action: #selector(settingTypeChoosed(_:)))
  51. displayView.properties = displayProperty
  52. displayView.setTarget(self, action: #selector(settingTypeChoosed(_:)))
  53. typeDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
  54. }
  55. func setUpResetButton() {
  56. if let value = ComponentLibrary.shared.getComponentValueFromKey("colorBg/layout-low") {
  57. let currentValue = value as! [String : Any]
  58. infoContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  59. resetContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  60. }
  61. resetButton.properties = ComponentButtonProperty(type: .gray,
  62. size: .xxs,
  63. state: .normal,
  64. isDisable: false,
  65. onlyIcon: false,
  66. showLeftIcon: false,
  67. showRightIcon: false,
  68. buttonText: KMLocalizedString("Reset", comment: ""))
  69. resetAllButton.properties = ComponentButtonProperty(type: .gray,
  70. size: .xxs,
  71. state: .normal,
  72. isDisable: false,
  73. onlyIcon: false,
  74. showLeftIcon: false,
  75. showRightIcon: false,
  76. buttonText: KMLocalizedString("Reset All", comment: ""))
  77. resetButtonWidthConst.constant = resetButton.properties.propertyInfo.viewWidth
  78. resetAllButtonWidthConst.constant = resetAllButton.properties.propertyInfo.viewWidth
  79. resetButton.setTarget(self, action: #selector(resetButtonClicked(_:)))
  80. resetAllButton.setTarget(self, action: #selector(resetButtonClicked(_:)))
  81. }
  82. //MARK: - Action
  83. @objc func resetButtonClicked(_ sender: NSView) {
  84. let alert = NSAlert()
  85. alert.alertStyle = .critical
  86. alert.messageText = KMLocalizedString("Reset data?", comment: "")
  87. if sender == resetAllButton {
  88. alert.messageText = KMLocalizedString("Reset all data?", comment: "")
  89. }
  90. alert.addButton(withTitle: KMLocalizedString("Reset", comment: ""))
  91. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  92. let response = alert.runModal()
  93. if response.rawValue == 1000 {
  94. if sender == resetButton {
  95. if scrollView.documentView == generalContendView {
  96. SettingsManager.sharedInstance.resetGeneralData()
  97. generalContendView.reloadData()
  98. } else if scrollView.documentView == displayContendView {
  99. SettingsManager.sharedInstance.resetPageDisplayData()
  100. displayContendView.reloadData()
  101. }
  102. } else if sender == resetAllButton {
  103. SettingsManager.sharedInstance.resetDefaultData()
  104. generalContendView.reloadData()
  105. displayContendView.reloadData()
  106. }
  107. }
  108. }
  109. @objc func settingTypeChoosed(_ sender: ComponentNavBarItem) {
  110. if sender == generalView {
  111. generalProperty.state = .pressed
  112. displayProperty.state = .normal
  113. scrollView.documentView = generalContendView
  114. } else if sender == displayView {
  115. generalProperty.state = .normal
  116. displayProperty.state = .pressed
  117. scrollView.documentView = displayContendView
  118. }
  119. generalView.reloadData()
  120. displayView.reloadData()
  121. let rect = scrollView.documentView?.bounds ?? CGRectZero
  122. scrollView.documentView?.scroll(CGPoint(x: 0, y: rect.size.height))
  123. }
  124. //MARK: - Notification
  125. @objc func windowdidResizeNotification(_ notification: Notification) {
  126. generalContendView.frame = CGRectMake(0, 0, 540, max(692, scrollView.bounds.size.height))
  127. displayContendView.frame = CGRectMake(0, 0, 540, max(624, scrollView.bounds.size.height))
  128. }
  129. }
  130. //MARK: - Extension-NSWindowDelegate
  131. extension SettingsWindowController: NSWindowDelegate {
  132. func windowWillClose(_ notification: Notification) {
  133. SettingsManager.sharedInstance.saveData()
  134. }
  135. }