KMGeneralPreferences.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // KMGeneralPreferences.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/6.
  6. //
  7. import Cocoa
  8. class KMGeneralPreferences: NSViewController {
  9. /*
  10. #define @""
  11. #define AUTOMATICALLYCHECKSFORUPDATES_KEY @"automaticallyChecksForUpdates"
  12. #define UPDATECHECKINTERVAL_KEY @"updateCheckInterval"
  13. */
  14. private let UPDATEINTERVAL_KEY = "updateInterval"
  15. private let SUScheduledCheckIntervalKey = "SUScheduledCheckInterval"
  16. @IBOutlet weak var authorLabel: NSTextField!
  17. @IBOutlet weak var authorTextField: NSTextField!
  18. @IBOutlet weak var openTabLabel: NSTextField!
  19. @IBOutlet weak var openTabButton: NSPopUpButton!
  20. @IBOutlet weak var showConvertMenuButton: NSButton!
  21. @IBOutlet weak var defaultOpenButton: NSButton!
  22. @IBOutlet weak var reopenLastOpenFilesButton: NSButton!
  23. @IBOutlet weak var autoSaveLbl: NSTextField!
  24. @IBOutlet weak var autoSaveInfoBtn: NSButton!
  25. @IBOutlet weak var autoSaveSlider: NSSlider!
  26. @IBOutlet weak var openFilesLabelField: NSTextField!
  27. @IBOutlet weak var openFilesMatrix: NSMatrix!
  28. @IBOutlet weak var openFileForDefaultButton: NSButtonCell!
  29. @IBOutlet weak var openFileForMaximButton: NSButtonCell!
  30. @IBOutlet weak var openFileForFitButton: NSButtonCell!
  31. @IBOutlet weak var showLeftSideButton: NSButton!
  32. @IBOutlet weak var editPDFPopWindowIsShowButton: NSButton!
  33. @IBOutlet weak var rememberSnapshotButton: NSButton!
  34. @IBOutlet weak var revertInitPDFSetLabel: NSTextField!
  35. @IBOutlet weak var normalButton: NSButton!
  36. @IBOutlet weak var fullScreenButton: NSButton!
  37. @IBOutlet weak var autoSaveLabel: NSTextField!
  38. @IBOutlet weak var autoSaveNotesBackupButton: NSButton!
  39. @IBOutlet weak var keepSnapshotOnTopButton: NSButton!
  40. @IBOutlet var savePwdKeychainLabel: NSTextField!
  41. @IBOutlet var savePasswordsMatrix: NSMatrix!
  42. @IBOutlet weak var savePwdForAlwaysButton: NSButtonCell!
  43. @IBOutlet weak var savePwdForNeverButton: NSButtonCell!
  44. @IBOutlet weak var savePwdForAskButton: NSButtonCell!
  45. @IBOutlet var updateIntervalPopUpButton: NSPopUpButton!
  46. @IBOutlet var updateIntervalLabelField: NSTextField!
  47. var revertPDFSettingsButtons: [NSButton] = []
  48. var updateInterval: Float = 0 {
  49. didSet {
  50. }
  51. }
  52. deinit {
  53. KMPrint("KMGeneralPreferences deinit.")
  54. }
  55. override var nibName: NSNib.Name? {
  56. return "GeneralPreferences"
  57. }
  58. override func loadView() {
  59. super.loadView()
  60. }
  61. override func viewDidLoad() {
  62. super.viewDidLoad()
  63. self.authorLabel.stringValue = KMLocalizedString("Author:", nil)
  64. self.authorTextField.delegate = self
  65. self.openTabLabel.stringValue = String(format: "%@:", KMLocalizedString("Open a document in", nil))
  66. for item in self.openTabButton.itemArray {
  67. item.title = KMLocalizedString(item.title, nil)
  68. }
  69. self.showConvertMenuButton.title = KMLocalizedString("Show PDF Reader Pro in menu bar", nil)
  70. self.defaultOpenButton.title = KMLocalizedString("Set PDF Reader Pro as the default PDF viewer", nil)
  71. self.reopenLastOpenFilesButton.title = KMLocalizedString("Reopen last open files at startup", nil)
  72. self.autoSaveInfoBtn.title = KMLocalizedString("Autosave Documents", nil)
  73. openFilesLabelField.stringValue = KMLocalizedString("Open files:", nil)
  74. openFileForDefaultButton.title = KMLocalizedString("Default", nil)
  75. openFileForMaximButton.title = KMLocalizedString("Maximized", nil)
  76. openFileForFitButton.title = KMLocalizedString("Fit", nil)
  77. showLeftSideButton.title = KMLocalizedString("Tick to show left sidebar when open", nil)
  78. self.editPDFPopWindowIsShowButton.title = NSLocalizedString("Show quick entrance while editing content", comment: "")
  79. self.editPDFPopWindowIsShowButton.target = self
  80. self.editPDFPopWindowIsShowButton.action = #selector(editPDFPopWindowShowAction)
  81. rememberSnapshotButton.title = KMLocalizedString("Remember snapshots", nil)
  82. revertInitPDFSetLabel.stringValue = KMLocalizedString("Revert initial PDF view settings for:", nil)
  83. normalButton.title = KMLocalizedString("Normal", nil)
  84. fullScreenButton.title = KMLocalizedString("Full Screen", nil)
  85. autoSaveLabel.stringValue = KMLocalizedString("To set the initial PDF view settings, choose \"Use Current View Settings as Default\" from the PDF menu.", nil)
  86. autoSaveNotesBackupButton.title = KMLocalizedString("Automatically save PDF Reader Pro Edition notes backups", nil)
  87. keepSnapshotOnTopButton.title = KMLocalizedString("Keep snapshot windows on top", nil)
  88. updateIntervalLabelField.stringValue = KMLocalizedString("Check for PDF Reader Pro Edition updates:", nil)
  89. savePwdKeychainLabel.stringValue = KMLocalizedString("Save passwords in Keychain:", nil)
  90. savePwdForAlwaysButton.title = KMLocalizedString("Always", nil)
  91. savePwdForNeverButton.title = KMLocalizedString("Never", nil)
  92. savePwdForAskButton.title = KMLocalizedString("Ask", nil)
  93. self.authorLabel.sizeToFit()
  94. var frame = self.authorTextField.frame
  95. frame.origin.x = NSMaxX(self.authorLabel.frame)+5
  96. self.authorTextField.frame = frame
  97. self.openTabLabel.sizeToFit()
  98. frame = self.openTabButton.frame
  99. frame.origin.x = NSMaxX(self.openTabLabel.frame)+5
  100. self.openTabButton.frame = frame
  101. self.openTabButton.isEnabled = true
  102. self.revertPDFSettingsButtons = [self.normalButton, self.fullScreenButton]
  103. KMAutoSizeButtons(self.revertPDFSettingsButtons, rightAlign: false)
  104. _ = KMAutoSizeLabelFields([openFilesLabelField], [openFilesMatrix], false)
  105. let dw = KMAutoSizeLabelFields([updateIntervalLabelField], [updateIntervalPopUpButton], false)
  106. self.openFilesMatrix.sizeToFit()
  107. self.savePasswordsMatrix.sizeToFit()
  108. KMShiftAndResizeView(self.view, dx: 0.0, dw: dw)
  109. #if VERSION_FREE
  110. if KMMemberInfo.shared.isMemberAllFunction == false {
  111. KMPreference.shared.openDocumentType = .newWindow
  112. }
  113. #endif
  114. self.initData()
  115. }
  116. func initData() {
  117. self.authorTextField.stringValue = KMPreferenceManager.shared.author
  118. if KMPreference.shared.openDocumentType == .newWindow{
  119. self.openTabButton.selectItem(at: 1)
  120. }else{
  121. self.openTabButton.selectItem(at: 0)
  122. }
  123. self.showConvertMenuButton.state = KMStatusBar.system().isShow ? .on : .off
  124. self.defaultOpenButton.state = KMPreference.shared.setDefaultPDFReader ? .on : .off
  125. self.reopenLastOpenFilesButton.state = KMPreference.shared.openLastUnclosedDocumentWhenAppStart ? .on : .off
  126. self.autoSaveInfoBtn.state = AutoSaveManager.manager.autoSaveEnabled ? .on : .off
  127. self.autoSaveSlider.isEnabled = AutoSaveManager.manager.autoSaveEnabled
  128. if (AutoSaveManager.manager.timeInterval > 1) {
  129. self.autoSaveSlider.floatValue = Float((AutoSaveManager.manager.timeInterval - 1) / 19)
  130. } else {
  131. self.autoSaveSlider.floatValue = 0
  132. }
  133. self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager.manager.timeInterval, KMLocalizedString("minute(s)", nil))
  134. self._selectOpenFileType(KMPreference.shared.openFileType)
  135. self.showLeftSideButton.state = KMPreference.shared.showLeftSideBar ? .on : .off
  136. self.editPDFPopWindowIsShowButton.state = KMPreference.shared.editPDFPopWindowIsShow ? .on : .off
  137. self.rememberSnapshotButton.state = KMPreference.shared.rememberSnapshot ? .on : .off
  138. self.autoSaveNotesBackupButton.state = KMPreference.shared.autoSaveNoteBackup ? .on : .off
  139. self.keepSnapshotOnTopButton.state = KMPreference.shared.keepSnapshotWindowToTop ? .on : .off
  140. self._selectSavePasswordType(KMPreference.shared.savePasswordType)
  141. self._synchronizeUpdateInterval()
  142. self._updateRevertButtons()
  143. }
  144. @IBAction func revertPDFViewSettings(_ sender: AnyObject?) {
  145. NSUserDefaultsController.shared.revertToInitialValue(forKey: KMDefaultPDFDisplaySettingsKey)
  146. // KMPreference.shared.revertInitPDFViewSettingType = .normal
  147. }
  148. @IBAction func revertFullScreenPDFViewSettings(_ sender: AnyObject?) {
  149. NSUserDefaultsController.shared.revertToInitialValue(forKey: KMDefaultFullScreenPDFDisplaySettingsKey)
  150. // KMPreference.shared.revertInitPDFViewSettingType = .fullScreen
  151. }
  152. @IBAction func defaultOpenButtonAction(_ sender: AnyObject?) {
  153. KMPreference.shared.setDefaultPDFReader = self.defaultOpenButton.state == .on
  154. }
  155. @IBAction func openTabButtonAction(_ sender: AnyObject?) {
  156. var tabbingMode = NSWindow.TabbingMode.automatic
  157. if (self.openTabButton.indexOfSelectedItem == 0) {
  158. #if VERSION_FREE
  159. if KMMemberInfo.shared.isMemberAllFunction == false {
  160. KMMemberInfo.shared.advancedFunctionUsage()
  161. self.openTabButton.selectItem(at: 1)
  162. return
  163. }
  164. #endif
  165. tabbingMode = .preferred
  166. KMPreference.shared.openDocumentType = .inSameWindow
  167. } else if (self.openTabButton.indexOfSelectedItem == 1) {
  168. tabbingMode = .disallowed
  169. KMPreference.shared.openDocumentType = .newWindow
  170. }
  171. for document in NSDocumentController.shared.documents {
  172. guard let _doc = document as? KMMainDocument else {
  173. continue
  174. }
  175. for windowController in _doc.windowControllers {
  176. // windowController.window?.tabbingMode = tabbingMode
  177. }
  178. }
  179. }
  180. @IBAction func showConvertMenuButtonAction(_ sender: AnyObject?) {
  181. if self.showConvertMenuButton.state == .on {
  182. KMStatusBar.system().isShow = true
  183. } else {
  184. KMStatusBar.system().isShow = false
  185. }
  186. KMPreference.shared.showInMenuBar = self.showConvertMenuButton.state == .on
  187. }
  188. @IBAction func reopenLastOpenFilesAtStartupAction(_ sender: NSButton) {
  189. KMPreference.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on
  190. }
  191. @IBAction func openFileForDefaultAction(_ sender: Any) {
  192. KMPreference.shared.openFileType = .default
  193. }
  194. @IBAction func openFileForMaximAction(_ sender: Any) {
  195. KMPreference.shared.openFileType = .maxim
  196. }
  197. @IBAction func openFileForFitAction(_ sender: Any) {
  198. KMPreference.shared.openFileType = .fit
  199. }
  200. @IBAction func showLeftSideAction(_ sender: Any) {
  201. KMPreference.shared.showLeftSideBar = self.showLeftSideButton.state == .on
  202. }
  203. @objc func editPDFPopWindowShowAction(_ sender: NSButton) {
  204. KMPreference.shared.editPDFPopWindowIsShow = sender.state == .on
  205. }
  206. @IBAction func rememberSnapshotAction(_ sender: Any) {
  207. KMPreference.shared.rememberSnapshot = self.rememberSnapshotButton.state == .on
  208. }
  209. @IBAction func autoSaveNoteBackupAction(_ sender: Any) {
  210. KMPreference.shared.autoSaveNoteBackup = self.autoSaveNotesBackupButton.state == .on
  211. }
  212. @IBAction func keepSnapshotOnTopAction(_ sender: Any) {
  213. KMPreference.shared.keepSnapshotWindowToTop = self.keepSnapshotOnTopButton.state == .on
  214. }
  215. @IBAction func autoSaveButtonAction(_ sender: AnyObject?) {
  216. AutoSaveManager.manager.autoSaveEnabled = self.autoSaveInfoBtn.state == .on
  217. self.autoSaveSlider.isEnabled = AutoSaveManager.manager.autoSaveEnabled
  218. }
  219. @IBAction func autoSaveSliderAction(_ sender: AnyObject?) {
  220. let value = 1 + 19 * self.autoSaveSlider.floatValue
  221. AutoSaveManager.manager.timeInterval = CGFloat(value)
  222. self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager.manager.timeInterval, KMLocalizedString("minute(s)", nil))
  223. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(autoSaveSliderFinish), object: nil)
  224. self.perform(#selector(autoSaveSliderFinish), with: nil, afterDelay: 1)
  225. }
  226. @objc func autoSaveSliderFinish() {
  227. NotificationCenter.default.post(name: NSNotification.Name(rawValue: KAutoSaveTimeValueChangedNoti), object: nil)
  228. }
  229. @IBAction func savePwdForAlwaysAction(_ sender: Any) {
  230. KMPreference.shared.savePasswordType = .always
  231. }
  232. @IBAction func savePwdForNeverAction(_ sender: Any) {
  233. KMPreference.shared.savePasswordType = .never
  234. }
  235. @IBAction func savePwdForAskAction(_ sender: Any) {
  236. KMPreference.shared.savePasswordType = .ask
  237. }
  238. /*
  239. #pragma mark Accessors
  240. - (void)setUpdateInterval:(NSInteger)interval {
  241. #if VERSION_DMG
  242. if (interval > 0)
  243. [[SUUpdater sharedUpdater] setUpdateCheckInterval:interval];
  244. [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:interval > 0];
  245. #endif
  246. updateInterval = interval;
  247. }
  248. */
  249. override var title: String? {
  250. set {
  251. super.title = newValue
  252. }
  253. get {
  254. return KMLocalizedString("General", "Preference pane label")
  255. }
  256. }
  257. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  258. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  259. }
  260. }
  261. // MARK: - Private Methods
  262. extension KMGeneralPreferences {
  263. private func _synchronizeUpdateInterval() {
  264. // self.willChangeValue(forKey: UPDATEINTERVAL_KEY)
  265. #if VERSION_DMG
  266. if let updader = SUUpdater.shared() {
  267. self.updateInterval = updader.automaticallyChecksForUpdates ? Float(updader.updateCheckInterval) : 0
  268. } else {
  269. self.updateInterval = 0
  270. }
  271. #endif
  272. // self.didChangeValue(forKey: UPDATEINTERVAL_KEY)
  273. }
  274. private func _updateRevertButtons() {
  275. let initialValues = NSUserDefaultsController.shared.initialValues
  276. let sud = UserDefaults.standard
  277. if let data = sud.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) {
  278. self.normalButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultPDFDisplaySettingsKey]) == false
  279. } else {
  280. self.normalButton.isEnabled = true
  281. }
  282. if let data = sud.dictionary(forKey: KMDefaultFullScreenPDFDisplaySettingsKey) {
  283. self.fullScreenButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultFullScreenPDFDisplaySettingsKey]) == false
  284. } else {
  285. self.fullScreenButton.isEnabled = true
  286. }
  287. }
  288. private func _selectOpenFileType(_ type: KMPreferenceOpenFileType) {
  289. if type == .default {
  290. self.openFileForDefaultButton.state = .on
  291. self.openFileForMaximButton.state = .off
  292. self.openFileForFitButton.state = .off
  293. } else if type == .maxim {
  294. self.openFileForDefaultButton.state = .off
  295. self.openFileForMaximButton.state = .on
  296. self.openFileForFitButton.state = .off
  297. } else if type == .fit {
  298. self.openFileForDefaultButton.state = .off
  299. self.openFileForMaximButton.state = .off
  300. self.openFileForFitButton.state = .on
  301. }
  302. }
  303. private func _selectSavePasswordType(_ type: KMPreferenceSavePasswordType) {
  304. if type == .always {
  305. self.savePwdForAlwaysButton.state = .on
  306. self.savePwdForNeverButton.state = .off
  307. self.savePwdForAskButton.state = .off
  308. } else if type == .never {
  309. self.savePwdForAlwaysButton.state = .off
  310. self.savePwdForNeverButton.state = .on
  311. self.savePwdForAskButton.state = .off
  312. } else if type == .ask {
  313. self.savePwdForAlwaysButton.state = .off
  314. self.savePwdForNeverButton.state = .off
  315. self.savePwdForAskButton.state = .on
  316. }
  317. }
  318. }
  319. extension KMGeneralPreferences: KMPreferencePane {
  320. func defaultsDidRevert() {
  321. let interval = Bundle.main.object(forInfoDictionaryKey: SUScheduledCheckIntervalKey)
  322. #if VERSION_DMG
  323. let value = (interval as? NSNumber)?.doubleValue ?? 0
  324. SUUpdater.shared().updateCheckInterval = value
  325. SUUpdater.shared().automaticallyChecksForUpdates = value > 0
  326. #endif
  327. AutoSaveManager.manager.autoSaveEnabled = false
  328. AutoSaveManager.manager.timeInterval = 15
  329. }
  330. func reloadData() {
  331. self.initData()
  332. }
  333. }
  334. extension KMGeneralPreferences: NSTextFieldDelegate {
  335. func controlTextDidChange(_ obj: Notification) {
  336. if self.authorTextField.isEqual(to: obj.object) { // 作者
  337. KMPreference.shared.author = self.authorTextField.stringValue
  338. }
  339. }
  340. }