// // KMGeneralPreferences.swift // PDF Reader Pro // // Created by tangchao on 2023/11/6. // import Cocoa class KMGeneralPreferences: NSViewController { /* #define @"" #define AUTOMATICALLYCHECKSFORUPDATES_KEY @"automaticallyChecksForUpdates" #define UPDATECHECKINTERVAL_KEY @"updateCheckInterval" */ private let UPDATEINTERVAL_KEY = "updateInterval" private let SUScheduledCheckIntervalKey = "SUScheduledCheckInterval" @IBOutlet weak var authorLabel: NSTextField! @IBOutlet weak var authorTextField: NSTextField! @IBOutlet weak var openTabLabel: NSTextField! @IBOutlet weak var openTabButton: NSPopUpButton! @IBOutlet weak var showConvertMenuButton: NSButton! @IBOutlet weak var defaultOpenButton: NSButton! @IBOutlet weak var reopenLastOpenFilesButton: NSButton! @IBOutlet weak var autoSaveLbl: NSTextField! @IBOutlet weak var autoSaveInfoBtn: NSButton! @IBOutlet weak var autoSaveSlider: NSSlider! @IBOutlet weak var openFilesLabelField: NSTextField! @IBOutlet weak var openFilesMatrix: NSMatrix! @IBOutlet weak var openFileForDefaultButton: NSButtonCell! @IBOutlet weak var openFileForMaximButton: NSButtonCell! @IBOutlet weak var openFileForFitButton: NSButtonCell! @IBOutlet weak var showLeftSideButton: NSButton! @IBOutlet weak var editPDFPopWindowIsShowButton: NSButton! @IBOutlet weak var rememberSnapshotButton: NSButton! @IBOutlet weak var revertInitPDFSetLabel: NSTextField! @IBOutlet weak var normalButton: NSButton! @IBOutlet weak var fullScreenButton: NSButton! @IBOutlet weak var autoSaveLabel: NSTextField! @IBOutlet weak var autoSaveNotesBackupButton: NSButton! @IBOutlet weak var keepSnapshotOnTopButton: NSButton! @IBOutlet var savePwdKeychainLabel: NSTextField! @IBOutlet var savePasswordsMatrix: NSMatrix! @IBOutlet weak var savePwdForAlwaysButton: NSButtonCell! @IBOutlet weak var savePwdForNeverButton: NSButtonCell! @IBOutlet weak var savePwdForAskButton: NSButtonCell! @IBOutlet var updateIntervalPopUpButton: NSPopUpButton! @IBOutlet var updateIntervalLabelField: NSTextField! var revertPDFSettingsButtons: [NSButton] = [] var updateInterval: Float = 0 { didSet { } } deinit { KMPrint("KMGeneralPreferences deinit.") } override var nibName: NSNib.Name? { return "GeneralPreferences" } override func loadView() { super.loadView() } override func viewDidLoad() { super.viewDidLoad() self.authorLabel.stringValue = KMLocalizedString("Author:", nil) self.authorTextField.delegate = self self.openTabLabel.stringValue = String(format: "%@:", KMLocalizedString("Open a document in", nil)) for item in self.openTabButton.itemArray { item.title = KMLocalizedString(item.title, nil) } self.showConvertMenuButton.title = KMLocalizedString("Show PDF Reader Pro in menu bar", nil) self.defaultOpenButton.title = KMLocalizedString("Set PDF Reader Pro as the default PDF viewer", nil) self.reopenLastOpenFilesButton.title = KMLocalizedString("Reopen last open files at startup", nil) self.autoSaveInfoBtn.title = KMLocalizedString("Autosave Documents", nil) openFilesLabelField.stringValue = KMLocalizedString("Open files:", nil) openFileForDefaultButton.title = KMLocalizedString("Default", nil) openFileForMaximButton.title = KMLocalizedString("Maximized", nil) openFileForFitButton.title = KMLocalizedString("Fit", nil) showLeftSideButton.title = KMLocalizedString("Tick to show left sidebar when open", nil) self.editPDFPopWindowIsShowButton.title = NSLocalizedString("Show quick entrance while editing content", comment: "") self.editPDFPopWindowIsShowButton.target = self self.editPDFPopWindowIsShowButton.action = #selector(editPDFPopWindowShowAction) rememberSnapshotButton.title = KMLocalizedString("Remember snapshots", nil) revertInitPDFSetLabel.stringValue = KMLocalizedString("Revert initial PDF view settings for:", nil) normalButton.title = KMLocalizedString("Normal", nil) fullScreenButton.title = KMLocalizedString("Full Screen", nil) autoSaveLabel.stringValue = KMLocalizedString("To set the initial PDF view settings, choose \"Use Current View Settings as Default\" from the PDF menu.", nil) autoSaveNotesBackupButton.title = KMLocalizedString("Automatically save PDF Reader Pro Edition notes backups", nil) keepSnapshotOnTopButton.title = KMLocalizedString("Keep snapshot windows on top", nil) updateIntervalLabelField.stringValue = KMLocalizedString("Check for PDF Reader Pro Edition updates:", nil) savePwdKeychainLabel.stringValue = KMLocalizedString("Save passwords in Keychain:", nil) savePwdForAlwaysButton.title = KMLocalizedString("Always", nil) savePwdForNeverButton.title = KMLocalizedString("Never", nil) savePwdForAskButton.title = KMLocalizedString("Ask", nil) self.authorLabel.sizeToFit() var frame = self.authorTextField.frame frame.origin.x = NSMaxX(self.authorLabel.frame)+5 self.authorTextField.frame = frame self.openTabLabel.sizeToFit() frame = self.openTabButton.frame frame.origin.x = NSMaxX(self.openTabLabel.frame)+5 self.openTabButton.frame = frame self.openTabButton.isEnabled = true self.revertPDFSettingsButtons = [self.normalButton, self.fullScreenButton] KMAutoSizeButtons(self.revertPDFSettingsButtons, rightAlign: false) _ = KMAutoSizeLabelFields([openFilesLabelField], [openFilesMatrix], false) let dw = KMAutoSizeLabelFields([updateIntervalLabelField], [updateIntervalPopUpButton], false) self.openFilesMatrix.sizeToFit() self.savePasswordsMatrix.sizeToFit() KMShiftAndResizeView(self.view, dx: 0.0, dw: dw) #if VERSION_FREE if KMMemberInfo.shared.isMemberAllFunction == false { KMPreference.shared.openDocumentType = .newWindow } #endif self.initData() } func initData() { self.authorTextField.stringValue = KMPreferenceManager.shared.author if KMPreference.shared.openDocumentType == .newWindow{ self.openTabButton.selectItem(at: 1) }else{ self.openTabButton.selectItem(at: 0) } self.showConvertMenuButton.state = KMStatusBar.system().isShow ? .on : .off self.defaultOpenButton.state = KMPreference.shared.setDefaultPDFReader ? .on : .off self.reopenLastOpenFilesButton.state = KMPreference.shared.openLastUnclosedDocumentWhenAppStart ? .on : .off self.autoSaveInfoBtn.state = AutoSaveManager.manager.autoSaveEnabled ? .on : .off self.autoSaveSlider.isEnabled = AutoSaveManager.manager.autoSaveEnabled if (AutoSaveManager.manager.timeInterval > 1) { self.autoSaveSlider.floatValue = Float((AutoSaveManager.manager.timeInterval - 1) / 19) } else { self.autoSaveSlider.floatValue = 0 } self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager.manager.timeInterval, KMLocalizedString("minute(s)", nil)) self._selectOpenFileType(KMPreference.shared.openFileType) self.showLeftSideButton.state = KMPreference.shared.showLeftSideBar ? .on : .off self.editPDFPopWindowIsShowButton.state = KMPreference.shared.editPDFPopWindowIsShow ? .on : .off self.rememberSnapshotButton.state = KMPreference.shared.rememberSnapshot ? .on : .off self.autoSaveNotesBackupButton.state = KMPreference.shared.autoSaveNoteBackup ? .on : .off self.keepSnapshotOnTopButton.state = KMPreference.shared.keepSnapshotWindowToTop ? .on : .off self._selectSavePasswordType(KMPreference.shared.savePasswordType) self._synchronizeUpdateInterval() self._updateRevertButtons() } @IBAction func revertPDFViewSettings(_ sender: AnyObject?) { NSUserDefaultsController.shared.revertToInitialValue(forKey: KMDefaultPDFDisplaySettingsKey) // KMPreference.shared.revertInitPDFViewSettingType = .normal } @IBAction func revertFullScreenPDFViewSettings(_ sender: AnyObject?) { NSUserDefaultsController.shared.revertToInitialValue(forKey: KMDefaultFullScreenPDFDisplaySettingsKey) // KMPreference.shared.revertInitPDFViewSettingType = .fullScreen } @IBAction func defaultOpenButtonAction(_ sender: AnyObject?) { KMPreference.shared.setDefaultPDFReader = self.defaultOpenButton.state == .on } @IBAction func openTabButtonAction(_ sender: AnyObject?) { var tabbingMode = NSWindow.TabbingMode.automatic if (self.openTabButton.indexOfSelectedItem == 0) { #if VERSION_FREE if KMMemberInfo.shared.isMemberAllFunction == false { KMMemberInfo.shared.advancedFunctionUsage() self.openTabButton.selectItem(at: 1) return } #endif tabbingMode = .preferred KMPreference.shared.openDocumentType = .inSameWindow } else if (self.openTabButton.indexOfSelectedItem == 1) { tabbingMode = .disallowed KMPreference.shared.openDocumentType = .newWindow } for document in NSDocumentController.shared.documents { guard let _doc = document as? KMMainDocument else { continue } for windowController in _doc.windowControllers { // windowController.window?.tabbingMode = tabbingMode } } } @IBAction func showConvertMenuButtonAction(_ sender: AnyObject?) { if self.showConvertMenuButton.state == .on { KMStatusBar.system().isShow = true } else { KMStatusBar.system().isShow = false } KMPreference.shared.showInMenuBar = self.showConvertMenuButton.state == .on } @IBAction func reopenLastOpenFilesAtStartupAction(_ sender: NSButton) { KMPreference.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on } @IBAction func openFileForDefaultAction(_ sender: Any) { KMPreference.shared.openFileType = .default } @IBAction func openFileForMaximAction(_ sender: Any) { KMPreference.shared.openFileType = .maxim } @IBAction func openFileForFitAction(_ sender: Any) { KMPreference.shared.openFileType = .fit } @IBAction func showLeftSideAction(_ sender: Any) { KMPreference.shared.showLeftSideBar = self.showLeftSideButton.state == .on } @objc func editPDFPopWindowShowAction(_ sender: NSButton) { KMPreference.shared.editPDFPopWindowIsShow = sender.state == .on } @IBAction func rememberSnapshotAction(_ sender: Any) { KMPreference.shared.rememberSnapshot = self.rememberSnapshotButton.state == .on } @IBAction func autoSaveNoteBackupAction(_ sender: Any) { KMPreference.shared.autoSaveNoteBackup = self.autoSaveNotesBackupButton.state == .on } @IBAction func keepSnapshotOnTopAction(_ sender: Any) { KMPreference.shared.keepSnapshotWindowToTop = self.keepSnapshotOnTopButton.state == .on } @IBAction func autoSaveButtonAction(_ sender: AnyObject?) { AutoSaveManager.manager.autoSaveEnabled = self.autoSaveInfoBtn.state == .on self.autoSaveSlider.isEnabled = AutoSaveManager.manager.autoSaveEnabled } @IBAction func autoSaveSliderAction(_ sender: AnyObject?) { let value = 1 + 19 * self.autoSaveSlider.floatValue AutoSaveManager.manager.timeInterval = CGFloat(value) self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager.manager.timeInterval, KMLocalizedString("minute(s)", nil)) Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(autoSaveSliderFinish), object: nil) self.perform(#selector(autoSaveSliderFinish), with: nil, afterDelay: 1) } @objc func autoSaveSliderFinish() { NotificationCenter.default.post(name: NSNotification.Name(rawValue: KAutoSaveTimeValueChangedNoti), object: nil) } @IBAction func savePwdForAlwaysAction(_ sender: Any) { KMPreference.shared.savePasswordType = .always } @IBAction func savePwdForNeverAction(_ sender: Any) { KMPreference.shared.savePasswordType = .never } @IBAction func savePwdForAskAction(_ sender: Any) { KMPreference.shared.savePasswordType = .ask } /* #pragma mark Accessors - (void)setUpdateInterval:(NSInteger)interval { #if VERSION_DMG if (interval > 0) [[SUUpdater sharedUpdater] setUpdateCheckInterval:interval]; [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:interval > 0]; #endif updateInterval = interval; } */ override var title: String? { set { super.title = newValue } get { return KMLocalizedString("General", "Preference pane label") } } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) } } // MARK: - Private Methods extension KMGeneralPreferences { private func _synchronizeUpdateInterval() { // self.willChangeValue(forKey: UPDATEINTERVAL_KEY) #if VERSION_DMG if let updader = SUUpdater.shared() { self.updateInterval = updader.automaticallyChecksForUpdates ? Float(updader.updateCheckInterval) : 0 } else { self.updateInterval = 0 } #endif // self.didChangeValue(forKey: UPDATEINTERVAL_KEY) } private func _updateRevertButtons() { let initialValues = NSUserDefaultsController.shared.initialValues let sud = UserDefaults.standard if let data = sud.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) { self.normalButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultPDFDisplaySettingsKey]) == false } else { self.normalButton.isEnabled = true } if let data = sud.dictionary(forKey: KMDefaultFullScreenPDFDisplaySettingsKey) { self.fullScreenButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultFullScreenPDFDisplaySettingsKey]) == false } else { self.fullScreenButton.isEnabled = true } } private func _selectOpenFileType(_ type: KMPreferenceOpenFileType) { if type == .default { self.openFileForDefaultButton.state = .on self.openFileForMaximButton.state = .off self.openFileForFitButton.state = .off } else if type == .maxim { self.openFileForDefaultButton.state = .off self.openFileForMaximButton.state = .on self.openFileForFitButton.state = .off } else if type == .fit { self.openFileForDefaultButton.state = .off self.openFileForMaximButton.state = .off self.openFileForFitButton.state = .on } } private func _selectSavePasswordType(_ type: KMPreferenceSavePasswordType) { if type == .always { self.savePwdForAlwaysButton.state = .on self.savePwdForNeverButton.state = .off self.savePwdForAskButton.state = .off } else if type == .never { self.savePwdForAlwaysButton.state = .off self.savePwdForNeverButton.state = .on self.savePwdForAskButton.state = .off } else if type == .ask { self.savePwdForAlwaysButton.state = .off self.savePwdForNeverButton.state = .off self.savePwdForAskButton.state = .on } } } extension KMGeneralPreferences: KMPreferencePane { func defaultsDidRevert() { let interval = Bundle.main.object(forInfoDictionaryKey: SUScheduledCheckIntervalKey) #if VERSION_DMG let value = (interval as? NSNumber)?.doubleValue ?? 0 SUUpdater.shared().updateCheckInterval = value SUUpdater.shared().automaticallyChecksForUpdates = value > 0 #endif AutoSaveManager.manager.autoSaveEnabled = false AutoSaveManager.manager.timeInterval = 15 } func reloadData() { self.initData() } } extension KMGeneralPreferences: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { if self.authorTextField.isEqual(to: obj.object) { // 作者 KMPreference.shared.author = self.authorTextField.stringValue } } }