// // KMPreferenceDisplayController.swift // PDF Master // // Created by tangchao on 2023/2/3. // import Cocoa /// 偏好设置 Display控制器 class KMPreferenceDisplayController: NSViewController { @IBOutlet weak var defaultViewAndZoomLabel: NSTextField! @IBOutlet weak var defaultViewAndZoomBox: NSBox! @IBOutlet weak var pageIndicatorLabel: NSTextField! @IBOutlet weak var zoomSacleLabel: NSTextField! @IBOutlet weak var pageIndicatorComboBox: NSComboBox! @IBOutlet weak var zoomScaleComboBox: NSComboBox! @IBOutlet weak var leftSidePanelLabel: NSTextField! @IBOutlet weak var leftSidePanelBox: NSBox! @IBOutlet weak var defaultDisplayLabel: NSTextField! @IBOutlet weak var putAwayRadio: NSButton! @IBOutlet weak var rememberLastSelectionRadio: NSButton! @IBOutlet weak var showOutlineCheck: NSButton! @IBOutlet weak var leftSidePanel2Label: NSTextField! @IBOutlet weak var leftSidePanel2Box: NSBox! @IBOutlet weak var displayLabel: NSTextField! @IBOutlet weak var autoExpandRadio: NSButton! @IBOutlet weak var manualExpandRadio: NSButton! @IBOutlet weak var colorLabel: NSTextField! @IBOutlet weak var colorBox: NSBox! @IBOutlet weak var backgroundColorLabel: NSTextField! @IBOutlet weak var colorNormalLabel: NSTextField! @IBOutlet weak var colorNormalButton: NSButton! @IBOutlet weak var colorFullScreenLabel: NSTextField! @IBOutlet weak var colorFullScreenButton: NSButton! @IBOutlet weak var hightlightedFormsLabel: NSButton! @IBOutlet weak var fieldHighlightLabel: NSTextField! @IBOutlet weak var fieldHighlightButton: NSButton! @IBOutlet weak var requiredFieldHighlightLabel: NSTextField! @IBOutlet weak var requiredFieldHighlightButton: NSButton! @IBOutlet weak var pageIndicator2Label: NSTextField! @IBOutlet weak var highlightedLinksCheck: NSButton! @IBOutlet weak var autoDisplayRadio: NSButton! @IBOutlet weak var alwaysShowRadio: NSButton! @IBOutlet weak var neverShowRadio: NSButton! var pageDisplayTypes: [String] { get { return [NSLocalizedString("Single Page", comment: ""), NSLocalizedString("Single Page Continuous", comment: ""), NSLocalizedString("Two Pages", comment: ""), NSLocalizedString("Two Pages Continuous", comment: ""), NSLocalizedString("Book Mode", comment: ""), NSLocalizedString("Book Continuous", comment: "")] } } var viewZoomScaleTypes: [String] { get { return [NSLocalizedString("Zoom To Width", comment: ""), NSLocalizedString("Zoom To Fit", comment: ""), NSLocalizedString("Zoom To Actual Size", comment: "")] } } var currentColorButton: NSButton? convenience init() { self.init(nibName: "KMPreferenceDisplayController", bundle: nil) } override func viewDidLoad() { super.viewDidLoad() self.defaultViewAndZoomLabel.stringValue = NSLocalizedString("Default View and Zoom", comment: "") self.pageIndicatorLabel.stringValue = NSLocalizedString("Page display:", comment: "") self.pageIndicatorComboBox.removeAllItems() self.pageIndicatorComboBox.addItems(withObjectValues: self.pageDisplayTypes) self.zoomSacleLabel.stringValue = NSLocalizedString("Zoom scale:", comment: "") self.zoomScaleComboBox.removeAllItems() self.zoomScaleComboBox.addItems(withObjectValues: self.viewZoomScaleTypes) self.leftSidePanelLabel.stringValue = NSLocalizedString("Left Side Panel", comment: "") self.defaultDisplayLabel.stringValue = NSLocalizedString("Default display:", comment: "") self.putAwayRadio.title = NSLocalizedString("Hide when opening a file", comment: "") self.rememberLastSelectionRadio.title = NSLocalizedString("Remember the last selection when launching.", comment: "") self.showOutlineCheck.title = NSLocalizedString("Show outline list by default when outline is available.", comment: "") self.leftSidePanel2Label.stringValue = NSLocalizedString("Right Side Panel", comment: "") self.displayLabel.stringValue = NSLocalizedString("Default display:", comment: "") self.autoExpandRadio.title = NSLocalizedString("Auto expand", comment: "") self.manualExpandRadio.title = NSLocalizedString("Manually expand", comment: "") self.colorLabel.stringValue = NSLocalizedString("Color", comment: "") self.backgroundColorLabel.stringValue = NSLocalizedString("Background color:", comment: "") self.colorNormalLabel.stringValue = NSLocalizedString("Normal", comment: "") self.colorFullScreenLabel.stringValue = NSLocalizedString("Full Screen", comment: "") self.hightlightedFormsLabel.title = NSLocalizedString("Highlight forms:", comment: "") self.fieldHighlightLabel.stringValue = NSLocalizedString("Highlight field", comment: "") self.requiredFieldHighlightLabel.stringValue = NSLocalizedString("Highlight required field", comment: "") self.pageIndicator2Label.stringValue = NSLocalizedString("Page indicator:", comment: "") self.autoDisplayRadio.title = NSLocalizedString("Auto display", comment: "") self.alwaysShowRadio.title = NSLocalizedString("Always show", comment: "") self.neverShowRadio.title = NSLocalizedString("Never show", comment: "") self.highlightedLinksCheck.title = NSLocalizedString("Highlight links", comment: "") for comboBox in [self.pageIndicatorComboBox, self.zoomScaleComboBox] { comboBox?.delegate = self } for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck, self.autoExpandRadio, self.manualExpandRadio, self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] { radio?.target = self radio?.action = #selector(radioAction) } for check in [self.hightlightedFormsLabel,self.highlightedLinksCheck] { check?.target = self check?.action = #selector(checkAction) } for color in [self.colorNormalButton, self.colorFullScreenButton, self.fieldHighlightButton, self.requiredFieldHighlightButton] { color?.title = "" color?.wantsLayer = true color?.layer?.cornerRadius = 10 color?.layer?.borderWidth = 1 color?.layer?.borderColor = NSColor(hex: "#0000001A").cgColor color?.target = self color?.action = #selector(colorButtonAction) } self.initUIProperty() self.initData() } private func initUIProperty() { self.defaultViewAndZoomBox.borderColor = NSColor(white: 0, alpha: 0.05) self.defaultViewAndZoomBox.fillColor = NSColor(white: 0, alpha: 0.05) self.leftSidePanelBox.borderColor = NSColor(white: 0, alpha: 0.05) self.leftSidePanelBox.fillColor = NSColor(white: 0, alpha: 0.05) self.leftSidePanel2Box.borderColor = NSColor(white: 0, alpha: 0.05) self.leftSidePanel2Box.fillColor = NSColor(white: 0, alpha: 0.05) self.colorBox.borderColor = NSColor(white: 0, alpha: 0.05) self.colorBox.fillColor = NSColor(white: 0, alpha: 0.05) self.defaultViewAndZoomLabel.textColor = NSColor(hex: "#252629") self.pageIndicatorLabel.textColor = NSColor(hex: "#000000D9") self.zoomSacleLabel.textColor = NSColor(hex: "#000000D9") self.leftSidePanelLabel.textColor = NSColor(hex: "#252629") self.defaultDisplayLabel.textColor = NSColor(hex: "#000000D9") self.putAwayRadio.setTitleColor(NSColor(hex: "#000000D9")) self.rememberLastSelectionRadio.setTitleColor(NSColor(hex: "#000000D9")) self.showOutlineCheck.setTitleColor(NSColor(hex: "#000000D9")) self.leftSidePanel2Label.textColor = NSColor(hex: "#252629") self.displayLabel.textColor = NSColor(hex: "#000000D9") self.autoExpandRadio.setTitleColor(NSColor(hex: "#000000D9")) self.manualExpandRadio.setTitleColor(NSColor(hex: "#000000D9")) self.colorLabel.textColor = NSColor(hex: "#252629") self.backgroundColorLabel.textColor = NSColor(hex: "#000000D9") self.colorNormalLabel.textColor = NSColor(hex: "#000000D9") self.colorFullScreenLabel.textColor = NSColor(hex: "#000000D9") self.hightlightedFormsLabel.setTitleColor(NSColor(hex: "#000000D9")) self.fieldHighlightLabel.textColor = NSColor(hex: "#000000D9") self.requiredFieldHighlightLabel.textColor = NSColor(hex: "#000000D9") self.pageIndicator2Label.textColor = NSColor(hex: "#000000D9") self.autoDisplayRadio.setTitleColor(NSColor(hex: "#000000D9")) self.alwaysShowRadio.setTitleColor(NSColor(hex: "#000000D9")) self.neverShowRadio.setTitleColor(NSColor(hex: "#000000D9")) self.highlightedLinksCheck.setTitleColor(NSColor(hex: "#000000D9")) } private func initData() { let value = KMPreferenceManager.shared.viewPageDisplayType if (value.rawValue < self.pageDisplayTypes.count) { self.pageIndicatorComboBox.stringValue = self.pageDisplayTypes[value.rawValue] } let value2 = KMPreferenceManager.shared.viewZoomScaleType if (value2.rawValue < self.viewZoomScaleTypes.count) { self.zoomScaleComboBox.stringValue = self.viewZoomScaleTypes[value2.rawValue] } let leftSideDisplayType = KMPreferenceManager.shared.leftSideDisplayType if (leftSideDisplayType == .closeWhenOpenFile) { self.selectLeftSideRadio(self.putAwayRadio) } else if (leftSideDisplayType == .openAppSaveLastSelect) { self.selectLeftSideRadio(self.rememberLastSelectionRadio) } else if (leftSideDisplayType == .showOutlineIfHas) { self.selectLeftSideRadio(self.showOutlineCheck) } if (KMPreferenceManager.shared.propertyPanelExpandType == .auto) { self.selectDisplayRadio(self.autoExpandRadio) } else { self.selectDisplayRadio(self.manualExpandRadio) } if (KMPreferenceManager.shared.highlightForms) { self.hightlightedFormsLabel.state = .on } else { self.hightlightedFormsLabel.state = .off } let value3 = KMPreferenceManager.shared.pageIndicatorType if (value3 == .automatic) { self.selectPageIndicatorRadio(self.autoDisplayRadio) } else if (value3 == .always) { self.selectPageIndicatorRadio(self.alwaysShowRadio) } else if (value3 == .never) { self.selectPageIndicatorRadio(self.neverShowRadio) } if (KMPreferenceManager.shared.highlightLinks) { self.highlightedLinksCheck.state = .on } else { self.highlightedLinksCheck.state = .off } self.colorNormalButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor.cgColor self.colorFullScreenButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor.cgColor self.fieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayFieldHighlightingColor.cgColor self.requiredFieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayRequiredFieldHighlightingColor.cgColor } @objc private func radioAction(sender: NSButton) { if (self.putAwayRadio.isEqual(sender)) { self.selectLeftSideRadio(sender) KMPreferenceManager.shared.leftSideDisplayType = .closeWhenOpenFile } else if (self.rememberLastSelectionRadio.isEqual(sender)) { self.selectLeftSideRadio(sender) KMPreferenceManager.shared.leftSideDisplayType = .openAppSaveLastSelect } else if (self.showOutlineCheck.isEqual(sender)) { self.selectLeftSideRadio(sender) KMPreferenceManager.shared.leftSideDisplayType = .showOutlineIfHas } else if (self.autoExpandRadio.isEqual(sender)) { self.selectDisplayRadio(sender) KMPreferenceManager.shared.propertyPanelExpandType = .auto } else if (self.manualExpandRadio.isEqual(sender)) { self.selectDisplayRadio(sender) KMPreferenceManager.shared.propertyPanelExpandType = .manual } else if (self.autoDisplayRadio.isEqual(sender)) { self.selectPageIndicatorRadio(sender) KMPreferenceManager.shared.pageIndicatorType = .automatic } else if (self.alwaysShowRadio.isEqual(sender)) { self.selectPageIndicatorRadio(sender) KMPreferenceManager.shared.pageIndicatorType = .always } else if (self.neverShowRadio.isEqual(sender)) { self.selectPageIndicatorRadio(sender) KMPreferenceManager.shared.pageIndicatorType = .never } } @objc private func checkAction(sender: NSButton) { if (self.hightlightedFormsLabel.isEqual(sender)) { KMPreferenceManager.shared.highlightForms = sender.state == .on } else if (self.highlightedLinksCheck.isEqual(sender)) { KMPreferenceManager.shared.highlightLinks = sender.state == .on } } @objc private func colorButtonAction(sender: NSButton) { self.currentColorButton = sender var color: NSColor? let cgColor = sender.layer?.backgroundColor if (cgColor != nil) { color = NSColor(cgColor: cgColor!) } let colorPanel = NSColorPanel.shared if (color != nil) { colorPanel.color = color! } colorPanel.setTarget(self) colorPanel.setAction(#selector(colorPanelAction)) colorPanel.orderFront(nil) } @objc private func colorPanelAction(sender: NSColorPanel) { if (self.currentColorButton == nil) { return } let color = sender.color self.currentColorButton?.layer?.backgroundColor = color.cgColor if (self.colorNormalButton.isEqual(self.currentColorButton)) { KMPreferenceManager.shared.displayBackgroundNormalColor = color } else if (self.colorFullScreenButton.isEqual(self.currentColorButton)) { KMPreferenceManager.shared.displayBackgroundFullScreenColor = color } else if (self.fieldHighlightButton.isEqual(self.currentColorButton)) { KMPreferenceManager.shared.displayFieldHighlightingColor = color } else if (self.requiredFieldHighlightButton.isEqual(self.currentColorButton)) { KMPreferenceManager.shared.displayRequiredFieldHighlightingColor = color } } private func selectLeftSideRadio(_ sender: NSButton) { for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } private func selectDisplayRadio(_ sender: NSButton) { for radio in [self.autoExpandRadio, self.manualExpandRadio] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } private func selectPageIndicatorRadio(_ sender: NSButton) { for radio in [self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } } extension KMPreferenceDisplayController: NSComboBoxDelegate { func comboBoxSelectionDidChange(_ notification: Notification) { if (self.pageIndicatorComboBox.isEqual(to: notification.object)) { var index: Int = 0 if (self.pageIndicatorComboBox.indexOfSelectedItem >= 0) { index = self.pageIndicatorComboBox.indexOfSelectedItem } KMPreferenceManager.shared.viewPageDisplayType = KMPDFDisplayType(rawValue: index)! } else if (self.zoomScaleComboBox.isEqual(notification.object)) { var index: Int = 0 if (self.zoomScaleComboBox.indexOfSelectedItem >= 0) { index = self.zoomScaleComboBox.indexOfSelectedItem } KMPreferenceManager.shared.viewZoomScaleType = KMPDFZoomType(rawValue: index)! } } } extension KMPreferenceDisplayController: KMPreferenceDealDataChangeProtocol { func refreshUI() { DispatchQueue.main.async { self.initData() } } }