KMGeneralPreferences.swift 16 KB

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