123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // KMPreferenceWindowController.swift
- // PDF Master
- //
- // Created by tangchao on 2023/2/2.
- //
- import Cocoa
- protocol KMPreferenceDealDataChangeProtocol: NSObjectProtocol {
- func refreshUI()
- }
- class KMPreferenceWindowController: NSWindowController {
-
- static let shared = KMPreferenceWindowController(windowNibName: "KMPreferenceWindowController")
- @IBOutlet weak var titleBarBox: NSBox!
- @IBOutlet weak var contentBox: NSBox!
-
- @IBOutlet weak var bottomBarBox: NSBox!
- @IBOutlet weak var resetButton: NSButton!
- @IBOutlet weak var resetAllButton: NSButton!
-
- @IBOutlet weak var contentHeightConst: NSLayoutConstraint!
- @IBOutlet weak var contentWidthConst: NSLayoutConstraint!
-
- var currentController: KMPreferenceDealDataChangeProtocol?
- var currentItemIndex: Int = 0
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- override func loadWindow() {
- super.loadWindow()
-
- self.adjectWindowSize(NSSize(width: 512, height: 504), false)
- }
-
- override func showWindow(_ sender: Any?) {
- super.showWindow(sender)
- self.currentController?.refreshUI()
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- self.window?.title = NSLocalizedString("Preferences", comment: "")
- self.window?.titlebarAppearsTransparent = true
- self.window?.appearance = NSAppearance(named: .aqua)
- self.titleBarBox.fillColor = NSColor(white: 245/255.0, alpha: 1.0)
- self.window?.backgroundColor = NSColor(white: 245/255.0, alpha: 1.0)
- self.contentBox.fillColor = NSColor(white: 241/255.0, alpha: 1.0)
- self.bottomBarBox.fillColor = NSColor(white: 241/255.0, alpha: 1.0)
-
- let segmentControl = KMPreferenceSegementControl()
- segmentControl.frame = self.titleBarBox.contentView!.bounds
- segmentControl.autoresizingMask = [.width, .height]
- self.titleBarBox.contentView?.addSubview(segmentControl)
-
- segmentControl.target = self
- segmentControl.action = #selector(segmentControlSelectionDidChange)
-
- let titles = [NSLocalizedString("General", comment: ""),NSLocalizedString("Display", comment: ""), NSLocalizedString("Annotation", comment: "")]
- let selectedImages = ["KMImageNamePreferenceGeneralSelected", "KMImageNamePreferenceDisplaySelected", "KMImageNamePreferenceAnnotationSelected"]
- let unselectedImages = ["KMImageNamePreferenceGeneralUnSelected", "KMImageNamePreferenceDisplayUnSelected", "KMImageNamePreferenceAnnotationUnSelected"]
- var i: Int = 0
- for item in segmentControl.items {
- item.label.font = NSFont.systemFont(ofSize: 11)
- item.label.stringValue = titles[i]
- item.unSelectedTextColor = NSColor(red: 97/255.0, green: 100/255.0, blue: 105/255.0, alpha: 1.0)
- item.selectedTextColor = NSColor(red: 23/255.0, green: 112/255.0, blue: 244/255.0, alpha: 1.0)
- item.selectedImage = NSImage(named: selectedImages[i])
- item.unSelectedImage = NSImage(named: unselectedImages[i])
- i += 1
- }
- segmentControl.selectItem(segmentControl.items.first!.itemID)
- self.showContent(segmentControl.items.first!.itemID)
- resetButton.title = NSLocalizedString("Reset", comment: "")
- resetButton.target = self
- resetButton.action = #selector(resetAction)
-
- resetAllButton.title = NSLocalizedString("Reset All", comment: "")
- resetAllButton.target = self
- resetAllButton.action = #selector(resetAllAction)
-
- NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
- }
-
- @objc func resetAction() {
- if (self.currentItemIndex == 1) {
- KMPreferenceManager.shared.resetData(.general)
- } else if (self.currentItemIndex == 2) {
- KMPreferenceManager.shared.resetData(.display)
- } else if (self.currentItemIndex == 3) {
- KMPreferenceManager.shared.resetData(.markup)
- }
- }
-
- @objc func resetAllAction() {
- KMPreferenceManager.shared.resetAllData()
- }
-
- @objc func segmentControlSelectionDidChange(sender: KMPreferenceSegementControl) {
- if (self.currentItemIndex == sender.selectedIndex) {
- return
- }
-
- self.showContent(sender.selectedIndex)
- }
-
- @objc private func preferenceDidChangeNotification(sender: Notification) {
- if (self.currentController == nil) {
- return
- }
-
- self.currentController?.refreshUI()
- }
-
- private func adjectWindowSize(_ size: NSSize) {
- self.adjectWindowSize(size, true)
- }
-
- private func adjectWindowSize(_ size: NSSize, _ animated: Bool) {
- if (animated) {
- self.contentWidthConst.animator().constant = size.width
- self.contentHeightConst.animator().constant = size.height-120
- } else {
- self.contentWidthConst.constant = size.width
- self.contentHeightConst.constant = size.height-120
- }
- }
-
- private func showContent(_ index: Int) {
- for view in (self.contentBox.contentView?.subviews)! {
- view.removeFromSuperview()
- }
-
- self.currentItemIndex = index
-
- if (index == 1) {
- self.adjectWindowSize(NSSize(width: 512, height: 504), false)
-
- let controller = KMPreferenceGeneralController()
- self.contentBox.contentView?.addSubview(controller.view)
- controller.view.frame = self.contentBox.contentView!.bounds
- controller.view.autoresizingMask = [.width, .height]
- self.currentController = controller
- } else if (index == 2) {
- self.adjectWindowSize(NSSize(width: 512, height: 570), false)
-
- let controller = KMPreferenceDisplayController()
- self.contentBox.contentView?.addSubview(controller.view)
- controller.view.frame = self.contentBox.contentView!.bounds
- controller.view.autoresizingMask = [.width, .height]
- self.currentController = controller
- } else if (index == 3) {
- self.adjectWindowSize(NSSize(width: 512, height: 532), false)
-
- let controller = KMPreferenceMarkupController()
- self.contentBox.contentView?.addSubview(controller.view)
- controller.view.frame = self.contentBox.contentView!.bounds
- controller.view.autoresizingMask = [.width, .height]
- self.currentController = controller
- }
- }
-
- }
|