KMGeneralPreferences.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 IAPProductsManager.default().isAvailableAllFunction() == 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 IAPProductsManager.default().isAvailableAllFunction() == false {
  160. KMPreference.shared.openDocumentType = .newWindow
  161. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  162. self.openTabButton.selectItem(at: 1)
  163. return
  164. }
  165. #endif
  166. tabbingMode = .preferred
  167. KMPreference.shared.openDocumentType = .inSameWindow
  168. } else if (self.openTabButton.indexOfSelectedItem == 1) {
  169. tabbingMode = .disallowed
  170. KMPreference.shared.openDocumentType = .newWindow
  171. }
  172. for document in NSDocumentController.shared.documents {
  173. guard let _doc = document as? KMMainDocument else {
  174. continue
  175. }
  176. for windowController in _doc.windowControllers {
  177. // windowController.window?.tabbingMode = tabbingMode
  178. }
  179. }
  180. }
  181. @IBAction func showConvertMenuButtonAction(_ sender: AnyObject?) {
  182. if self.showConvertMenuButton.state == .on {
  183. KMStatusBar.system().isShow = true
  184. } else {
  185. KMStatusBar.system().isShow = false
  186. }
  187. KMPreference.shared.showInMenuBar = self.showConvertMenuButton.state == .on
  188. }
  189. @IBAction func reopenLastOpenFilesAtStartupAction(_ sender: NSButton) {
  190. KMPreference.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on
  191. }
  192. @IBAction func openFileForDefaultAction(_ sender: Any) {
  193. KMPreference.shared.openFileType = .default
  194. }
  195. @IBAction func openFileForMaximAction(_ sender: Any) {
  196. KMPreference.shared.openFileType = .maxim
  197. }
  198. @IBAction func openFileForFitAction(_ sender: Any) {
  199. KMPreference.shared.openFileType = .fit
  200. }
  201. @IBAction func showLeftSideAction(_ sender: Any) {
  202. KMPreference.shared.showLeftSideBar = self.showLeftSideButton.state == .on
  203. }
  204. @objc func editPDFPopWindowShowAction(_ sender: NSButton) {
  205. KMPreference.shared.editPDFPopWindowIsShow = sender.state == .on
  206. }
  207. @IBAction func rememberSnapshotAction(_ sender: Any) {
  208. KMPreference.shared.rememberSnapshot = self.rememberSnapshotButton.state == .on
  209. }
  210. @IBAction func autoSaveNoteBackupAction(_ sender: Any) {
  211. KMPreference.shared.autoSaveNoteBackup = self.autoSaveNotesBackupButton.state == .on
  212. }
  213. @IBAction func keepSnapshotOnTopAction(_ sender: Any) {
  214. KMPreference.shared.keepSnapshotWindowToTop = self.keepSnapshotOnTopButton.state == .on
  215. }
  216. @IBAction func autoSaveButtonAction(_ sender: AnyObject?) {
  217. AutoSaveManager.manager.autoSaveEnabled = self.autoSaveInfoBtn.state == .on
  218. self.autoSaveSlider.isEnabled = AutoSaveManager.manager.autoSaveEnabled
  219. }
  220. @IBAction func autoSaveSliderAction(_ sender: AnyObject?) {
  221. let value = 1 + 19 * self.autoSaveSlider.floatValue
  222. AutoSaveManager.manager.timeInterval = CGFloat(value)
  223. self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager.manager.timeInterval, KMLocalizedString("minute(s)", nil))
  224. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(autoSaveSliderFinish), object: nil)
  225. self.perform(#selector(autoSaveSliderFinish), with: nil, afterDelay: 1)
  226. }
  227. @objc func autoSaveSliderFinish() {
  228. NotificationCenter.default.post(name: NSNotification.Name(rawValue: KAutoSaveTimeValueChangedNoti), object: nil)
  229. }
  230. @IBAction func savePwdForAlwaysAction(_ sender: Any) {
  231. KMPreference.shared.savePasswordType = .always
  232. }
  233. @IBAction func savePwdForNeverAction(_ sender: Any) {
  234. KMPreference.shared.savePasswordType = .never
  235. }
  236. @IBAction func savePwdForAskAction(_ sender: Any) {
  237. KMPreference.shared.savePasswordType = .ask
  238. }
  239. /*
  240. #pragma mark Accessors
  241. - (void)setUpdateInterval:(NSInteger)interval {
  242. #if VERSION_DMG
  243. if (interval > 0)
  244. [[SUUpdater sharedUpdater] setUpdateCheckInterval:interval];
  245. [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:interval > 0];
  246. #endif
  247. updateInterval = interval;
  248. }
  249. */
  250. override var title: String? {
  251. set {
  252. super.title = newValue
  253. }
  254. get {
  255. return KMLocalizedString("General", "Preference pane label")
  256. }
  257. }
  258. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  259. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  260. }
  261. }
  262. // MARK: - Private Methods
  263. extension KMGeneralPreferences {
  264. private func _synchronizeUpdateInterval() {
  265. // self.willChangeValue(forKey: UPDATEINTERVAL_KEY)
  266. #if VERSION_DMG
  267. if let updader = SUUpdater.shared() {
  268. self.updateInterval = updader.automaticallyChecksForUpdates ? Float(updader.updateCheckInterval) : 0
  269. } else {
  270. self.updateInterval = 0
  271. }
  272. #endif
  273. // self.didChangeValue(forKey: UPDATEINTERVAL_KEY)
  274. }
  275. private func _updateRevertButtons() {
  276. let initialValues = NSUserDefaultsController.shared.initialValues
  277. let sud = UserDefaults.standard
  278. if let data = sud.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) {
  279. self.normalButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultPDFDisplaySettingsKey]) == false
  280. } else {
  281. self.normalButton.isEnabled = true
  282. }
  283. if let data = sud.dictionary(forKey: KMDefaultFullScreenPDFDisplaySettingsKey) {
  284. self.fullScreenButton.isEnabled = NSDictionary(dictionary: data).isEqual(initialValues?[KMDefaultFullScreenPDFDisplaySettingsKey]) == false
  285. } else {
  286. self.fullScreenButton.isEnabled = true
  287. }
  288. }
  289. private func _selectOpenFileType(_ type: KMPreferenceOpenFileType) {
  290. if type == .default {
  291. self.openFileForDefaultButton.state = .on
  292. self.openFileForMaximButton.state = .off
  293. self.openFileForFitButton.state = .off
  294. } else if type == .maxim {
  295. self.openFileForDefaultButton.state = .off
  296. self.openFileForMaximButton.state = .on
  297. self.openFileForFitButton.state = .off
  298. } else if type == .fit {
  299. self.openFileForDefaultButton.state = .off
  300. self.openFileForMaximButton.state = .off
  301. self.openFileForFitButton.state = .on
  302. }
  303. }
  304. private func _selectSavePasswordType(_ type: KMPreferenceSavePasswordType) {
  305. if type == .always {
  306. self.savePwdForAlwaysButton.state = .on
  307. self.savePwdForNeverButton.state = .off
  308. self.savePwdForAskButton.state = .off
  309. } else if type == .never {
  310. self.savePwdForAlwaysButton.state = .off
  311. self.savePwdForNeverButton.state = .on
  312. self.savePwdForAskButton.state = .off
  313. } else if type == .ask {
  314. self.savePwdForAlwaysButton.state = .off
  315. self.savePwdForNeverButton.state = .off
  316. self.savePwdForAskButton.state = .on
  317. }
  318. }
  319. }
  320. extension KMGeneralPreferences: KMPreferencePane {
  321. func defaultsDidRevert() {
  322. let interval = Bundle.main.object(forInfoDictionaryKey: SUScheduledCheckIntervalKey)
  323. #if VERSION_DMG
  324. let value = (interval as? NSNumber)?.doubleValue ?? 0
  325. SUUpdater.shared().updateCheckInterval = value
  326. SUUpdater.shared().automaticallyChecksForUpdates = value > 0
  327. #endif
  328. AutoSaveManager.manager.autoSaveEnabled = false
  329. AutoSaveManager.manager.timeInterval = 15
  330. }
  331. func reloadData() {
  332. self.initData()
  333. }
  334. }
  335. extension KMGeneralPreferences: NSTextFieldDelegate {
  336. func controlTextDidChange(_ obj: Notification) {
  337. if self.authorTextField.isEqual(to: obj.object) { // 作者
  338. KMPreference.shared.author = self.authorTextField.stringValue
  339. }
  340. }
  341. }