123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // SettingsWindowController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/9/26.
- //
- import Cocoa
- import KMComponentLibrary
- class SettingsWindowController: NSWindowController {
- @IBOutlet weak var typeContendBox: NSBox!
- @IBOutlet weak var generalView: ComponentNavBarItem!
- @IBOutlet weak var displayView: ComponentNavBarItem!
- @IBOutlet weak var typeDivider: ComponentDivider!
-
- @IBOutlet weak var infoContendBox: NSBox!
- @IBOutlet weak var scrollView: NSScrollView!
- @IBOutlet weak var resetContendBox: NSBox!
- @IBOutlet weak var resetButton: ComponentButton!
- @IBOutlet weak var resetAllButton: ComponentButton!
- @IBOutlet weak var resetButtonWidthConst: NSLayoutConstraint!
- @IBOutlet weak var resetAllButtonWidthConst: NSLayoutConstraint!
-
- static let shared = SettingsWindowController(windowNibName: "SettingsWindowController")
-
- var generalContendView: SettingsGeneralView = SettingsGeneralView(frame: CGRectMake(0, 0, 640, 692))
- var displayContendView: SettingsDisplayView = SettingsDisplayView(frame: CGRectMake(0, 0, 640, 624))
-
- var generalProperty: ComponentNavbarItemProperty = ComponentNavbarItemProperty(state: .normal,
- text: KMLocalizedString("General", comment: ""),
- iconImage: ComponentLibrary.shared.image(forResource: "test"))
-
- var displayProperty: ComponentNavbarItemProperty = ComponentNavbarItemProperty(state: .normal,
- text: KMLocalizedString("Page Display", comment: ""),
- iconImage: ComponentLibrary.shared.image(forResource: "test"))
-
- //MARK: - Func
- override func windowWillLoad() {
- super.windowWillLoad()
-
- SettingsManager.sharedInstance.config()
-
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- self.window?.delegate = self
-
- self.setUpTypeView()
-
- self.settingTypeChoosed(generalView)
- self.setUpResetButton()
-
- NotificationCenter.default.addObserver(self, selector: #selector(windowdidResizeNotification(_:)), name: NSWindow.didResizeNotification, object: self.window)
- }
-
- func setUpTypeView() {
-
- if let value = ComponentLibrary.shared.getComponentValueFromKey("colorBg/layout-middle") {
- let currentValue = value as! [String : Any]
- typeContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- }
-
- generalView.properties = generalProperty
- generalView.setTarget(self, action: #selector(settingTypeChoosed(_:)))
- displayView.properties = displayProperty
- displayView.setTarget(self, action: #selector(settingTypeChoosed(_:)))
-
- typeDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
- }
-
- func setUpResetButton() {
-
- if let value = ComponentLibrary.shared.getComponentValueFromKey("colorBg/layout-low") {
- let currentValue = value as! [String : Any]
- infoContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- resetContendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- }
-
- resetButton.properties = ComponentButtonProperty(type: .gray,
- size: .xxs,
- state: .normal,
- isDisable: false,
- onlyIcon: false,
- showLeftIcon: false,
- showRightIcon: false,
- buttonText: KMLocalizedString("Reset", comment: ""))
-
- resetAllButton.properties = ComponentButtonProperty(type: .gray,
- size: .xxs,
- state: .normal,
- isDisable: false,
- onlyIcon: false,
- showLeftIcon: false,
- showRightIcon: false,
- buttonText: KMLocalizedString("Reset All", comment: ""))
- resetButtonWidthConst.constant = resetButton.properties.propertyInfo.viewWidth
- resetAllButtonWidthConst.constant = resetAllButton.properties.propertyInfo.viewWidth
-
- resetButton.setTarget(self, action: #selector(resetButtonClicked(_:)))
- resetAllButton.setTarget(self, action: #selector(resetButtonClicked(_:)))
- }
-
-
-
- //MARK: - Action
- @objc func resetButtonClicked(_ sender: NSView) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("Reset data?", comment: "")
- if sender == resetAllButton {
- alert.messageText = KMLocalizedString("Reset all data?", comment: "")
- }
- alert.addButton(withTitle: KMLocalizedString("Reset", comment: ""))
- alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
- let response = alert.runModal()
- if response.rawValue == 1000 {
- if sender == resetButton {
- if scrollView.documentView == generalContendView {
- SettingsManager.sharedInstance.resetGeneralData()
- generalContendView.reloadData()
-
- } else if scrollView.documentView == displayContendView {
- SettingsManager.sharedInstance.resetPageDisplayData()
- displayContendView.reloadData()
-
- }
- } else if sender == resetAllButton {
- SettingsManager.sharedInstance.resetDefaultData()
-
- generalContendView.reloadData()
- displayContendView.reloadData()
- }
-
- }
- }
-
- @objc func settingTypeChoosed(_ sender: ComponentNavBarItem) {
- if sender == generalView {
- generalProperty.state = .pressed
- displayProperty.state = .normal
-
- scrollView.documentView = generalContendView
-
- } else if sender == displayView {
- generalProperty.state = .normal
- displayProperty.state = .pressed
-
- scrollView.documentView = displayContendView
- }
- generalView.reloadData()
- displayView.reloadData()
-
- let rect = scrollView.documentView?.bounds ?? CGRectZero
- scrollView.documentView?.scroll(CGPoint(x: 0, y: rect.size.height))
-
- }
-
- //MARK: - Notification
- @objc func windowdidResizeNotification(_ notification: Notification) {
-
- generalContendView.frame = CGRectMake(0, 0, 540, max(692, scrollView.bounds.size.height))
-
- displayContendView.frame = CGRectMake(0, 0, 540, max(624, scrollView.bounds.size.height))
-
- }
-
-
- }
- //MARK: - Extension-NSWindowDelegate
- extension SettingsWindowController: NSWindowDelegate {
- func windowWillClose(_ notification: Notification) {
- SettingsManager.sharedInstance.saveData()
- }
- }
|