123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- //
- // SettingsManager.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/9/26.
- //
- import Cocoa
- @objc public enum appearanceMode: Int {
- case lightMode
- case darkMode
- case autoMode
- }
- @objc public enum keyChainPasswordType : Int {
- case ask = 0
- case always
- case never
- }
- @objc public enum pageLayoutType : Int {
- case singlePage = 0
- case singlePageContinue
- case twoPage
- case twoPageContinue
- case bookMode
- case bookModeContinue
- }
- @objc public enum leftSidePanelType : Int {
- case defaultOpen = 0
- case sameAsLastOpen
- case hideLeftSide
- case prioritizeOutline
- }
- @objc public enum defaultOpenType: Int {
- case thumbnail = 0
- case outline
- case bookmark
- case annotation
- }
- @objc public enum zoomInfoType : Int {
- case adaptationWidth = 0
- case adapPage
- case actualSize
- case percent_10
- case percent_25
- case percent_50
- case percent_75
- case percent_100
- case percent_150
- case percent_200
- case percent_400
- case percent_800
- }
- @objc public enum panelOpenInteractionType : Int {
- case automatic = 0
- case hidePropertyPanel
- }
- @objc public enum settingsLanguage: Int {
- case English = 0
- case Chinese_CN
- case Chinese_TW
- case Dutch
- case French
- case Spanish
- case German
- case Russian
- case Italian
- case Polish
- case Japanese
-
- }
- let APPAppearanceChangedNotificationName = NSNotification.Name("APPAppearanceChangedNotificationName")
- let APPLanguageChangedNotificationName = NSNotification.Name("APPLanguageChangedNotificationName")
- //General
- let settingsAppearanceKey = "settingsAppearanceKey"
- let settingsAuthorKey = "settingsAuthorKey"
- let settingsDefaultPDFViewerKey = "settingsDefaultPDFViewerKey"
- var settingsReopenFileKey = "settingsReopenFileKey"
- var settingsAutoSaveFileKey = "settingsAutoSaveFileKey"
- var settingsAutoSaveMinutesKey = "settingsAutoSaveMinutesKey"
- var settingsFileListCountKey = "settingsFileListCountKey"
- var settingsLanguageDidChangedKey = "settingsLanguageDidChangedKey" //记录是否有手动修改language信息
- var settingsLanguageTypeKey = "settingsLanguageTypeKey"
- var settingsKeychainTypeKey = "settingsKeychainTypeKey"
- //PageDisplay
- var settingsPageLayoutTypeKey = "settingsPageLayoutTypeKey"
- var settingsZoomTypeKey = "settingsZoomTypeKey"
- var settingsLeftSidePanelKey = "settingsLeftSidePanelKey"
- var settingsDefaultOpenTypeKey = "settingsDefaultOpenTypeKey"
- var settingsPanelOpenTypeKey = "settingsPanelOpenTypeKey"
- var settingsShowQuickActionBarKey = "settingsShowQuickActionBarKey"
- var settingsHighlightLinkKey = "settingsHighlightLinkKey"
- var settingsHighlightFormFieldKey = "settingsHighlightFormFieldKey"
- class SettingsManager: NSObject {
-
- static let sharedInstance = SettingsManager.init()
-
- //General
- private var _appearance: appearanceMode = .autoMode
- private var _author: String = ""
- private var _defaultPDFViewer: Bool = true
- private var _reopenFile: Bool = false
- private var _autoSaveFile: Bool = false
- private var _autoSaveMinutes: String = "15"
- private var _fileListCount: Int = 10
- private var _language: settingsLanguage = .English
- private var _keychainType: keyChainPasswordType = .ask
-
- //PageDisplay
- private var _layoutType: pageLayoutType = .singlePage
- private var _zoomType: zoomInfoType = .adaptationWidth
- private var _leftPanelType: leftSidePanelType = .defaultOpen
- private var _defaultOpen: defaultOpenType = .thumbnail
- private var _panelOpenType: panelOpenInteractionType = .automatic
- private var _showQuickActionBar: Bool = true
- private var _highlightLink: Bool = true
- private var _highlightFormField: Bool = true
-
- override init() {
- super.init()
-
- self.config()
- }
-
- func config() {
- if UserDefaults.standard.object(forKey: "savedSettingsKey") != nil {
- self.loadData()
- } else {
- self.resetDefaultData()
- UserDefaults.standard.setValue("didSaved", forKey: "savedSettingsKey")
- }
-
-
- // if appearance == .autoMode {
- //
- // } else if appearance == .lightMode {
- // NSApplication.shared.appearance = NSAppearance(named: .aqua)
- // } else if appearance == .darkMode {
- // NSApplication.shared.appearance = NSAppearance(named: .darkAqua)
- // }
-
- }
-
- private func loadData() {
- //General
- self.appearance = appearanceMode(rawValue: UserDefaults.standard.integer(forKey: settingsAppearanceKey)) ?? .autoMode
- self.author = UserDefaults.standard.string(forKey: settingsAuthorKey) ?? ""
- self.defaultPDFViewer = UserDefaults.standard.bool(forKey: settingsDefaultPDFViewerKey)
- self.reopenFile = UserDefaults.standard.bool(forKey: settingsReopenFileKey)
- self.autoSaveFile = UserDefaults.standard.bool(forKey: settingsAutoSaveFileKey)
- self.autoSaveMinutes = UserDefaults.standard.string(forKey: settingsAutoSaveMinutesKey) ?? ""
- self.fileListCount = UserDefaults.standard.integer(forKey: settingsFileListCountKey)
- if UserDefaults.standard.string(forKey: settingsLanguageDidChangedKey) != nil {
- self.language = settingsLanguage(rawValue: UserDefaults.standard.integer(forKey: settingsLanguageTypeKey)) ?? .English
- } else {
- self.setUpLanguageKey()
- }
- self.keychainType = keyChainPasswordType(rawValue: UserDefaults.standard.integer(forKey: settingsKeychainTypeKey)) ?? .ask
-
- //PageDisplay
- self.layoutType = pageLayoutType(rawValue: UserDefaults.standard.integer(forKey: settingsPageLayoutTypeKey)) ?? .singlePage
- self.zoomType = zoomInfoType(rawValue: UserDefaults.standard.integer(forKey: settingsZoomTypeKey)) ?? .adaptationWidth
- self.leftPanelType = leftSidePanelType(rawValue: UserDefaults.standard.integer(forKey: settingsLeftSidePanelKey)) ?? .defaultOpen
- self.defaultOpen = defaultOpenType(rawValue: UserDefaults.standard.integer(forKey: settingsDefaultOpenTypeKey)) ?? .thumbnail
- self.panelOpenType = panelOpenInteractionType(rawValue: UserDefaults.standard.integer(forKey: settingsPanelOpenTypeKey)) ?? .automatic
- self.showQuickActionBar = UserDefaults.standard.bool(forKey: settingsShowQuickActionBarKey)
- self.highlightLink = UserDefaults.standard.bool(forKey: settingsHighlightLinkKey)
- self.highlightFormField = UserDefaults.standard.bool(forKey: settingsHighlightFormFieldKey)
-
- }
-
- func saveData() {
- //General
- UserDefaults.standard.setValue(appearance.rawValue, forKey: settingsAppearanceKey)
- UserDefaults.standard.setValue(author, forKey: settingsAuthorKey)
- UserDefaults.standard.setValue(defaultPDFViewer, forKey: settingsDefaultPDFViewerKey)
- UserDefaults.standard.setValue(reopenFile, forKey: settingsReopenFileKey)
- UserDefaults.standard.setValue(autoSaveFile, forKey: settingsAutoSaveFileKey)
- UserDefaults.standard.setValue(autoSaveMinutes, forKey: settingsAutoSaveMinutesKey)
- UserDefaults.standard.setValue(fileListCount, forKey: settingsFileListCountKey)
- UserDefaults.standard.setValue(language.rawValue, forKey: settingsLanguageTypeKey)
- UserDefaults.standard.setValue(keychainType.rawValue, forKey: settingsKeychainTypeKey)
-
- //PageDisplay
- UserDefaults.standard.setValue(layoutType.rawValue, forKey: settingsPageLayoutTypeKey)
- UserDefaults.standard.setValue(zoomType.rawValue, forKey: settingsZoomTypeKey)
- UserDefaults.standard.setValue(leftPanelType.rawValue, forKey: settingsLeftSidePanelKey)
- UserDefaults.standard.setValue(panelOpenType.rawValue, forKey: settingsPanelOpenTypeKey)
- UserDefaults.standard.setValue(defaultOpen.rawValue, forKey: settingsDefaultOpenTypeKey)
- UserDefaults.standard.setValue(showQuickActionBar, forKey: settingsShowQuickActionBarKey)
- UserDefaults.standard.setValue(highlightLink, forKey: settingsHighlightLinkKey)
- UserDefaults.standard.setValue(highlightFormField, forKey: settingsHighlightFormFieldKey)
-
- }
-
- //MARK: - Setter
- var appearance: appearanceMode {
- get {
- return _appearance
- }
- set {
- _appearance = newValue
- }
- }
-
- var author: String {
- get {
- return _author
- }
- set {
- _author = newValue
- }
- }
-
- var defaultPDFViewer: Bool {
- get {
- return _defaultPDFViewer
- }
- set {
- _defaultPDFViewer = newValue
- }
- }
-
- var reopenFile: Bool {
- get {
- return _reopenFile
- }
- set {
- _reopenFile = newValue
- }
- }
-
- var autoSaveFile: Bool {
- get {
- return _autoSaveFile
- }
- set {
- _autoSaveFile = newValue
- }
- }
-
- var autoSaveMinutes: String {
- get {
- return _autoSaveMinutes
- }
- set {
- _autoSaveMinutes = newValue
- }
- }
-
- var fileListCount: Int {
- get {
- return _fileListCount
- }
- set {
- _fileListCount = newValue
- }
- }
-
- var language: settingsLanguage {
- get {
- return _language
- }
- set {
- _language = newValue
- }
- }
-
- var keychainType: keyChainPasswordType {
- get {
- return _keychainType
- }
- set {
- _keychainType = newValue
- }
- }
-
- var layoutType: pageLayoutType {
- get {
- return _layoutType
- }
- set {
- _layoutType = newValue
- }
- }
-
- var zoomType: zoomInfoType {
- get {
- return _zoomType
- }
- set {
- _zoomType = newValue
- }
- }
-
- var leftPanelType: leftSidePanelType {
- get {
- return _leftPanelType
- }
- set {
- _leftPanelType = newValue
- }
- }
-
- var defaultOpen: defaultOpenType {
- get {
- return _defaultOpen
- }
- set {
- _defaultOpen = newValue
- }
- }
-
- var panelOpenType: panelOpenInteractionType {
- get {
- return _panelOpenType
- }
- set {
- _panelOpenType = newValue
- }
- }
-
- var showQuickActionBar: Bool {
- get {
- return _showQuickActionBar
- }
- set {
- _showQuickActionBar = newValue
- }
- }
-
- var highlightLink: Bool {
- get {
- return _highlightLink
- }
- set {
- _highlightLink = newValue
- }
- }
-
- var highlightFormField: Bool {
- get {
- return _highlightFormField
- }
- set {
- _highlightFormField = newValue
- }
- }
-
- //MARK: - Reset
- func resetDefaultData() {
- self.resetGeneralData()
-
- self.resetPageDisplayData()
-
- self.saveData()
- }
-
- func resetGeneralData() {
- self.appearance = .autoMode
- self.author = NSFullUserName()
- self.defaultPDFViewer = true
- self.reopenFile = false
- self.autoSaveFile = false
- self.autoSaveMinutes = "15"
- self.fileListCount = 10
- self.keychainType = .ask
-
- self.setUpLanguageKey()
-
- }
-
- func resetPageDisplayData() {
- self.layoutType = .singlePage
- self.zoomType = .adaptationWidth
- self.leftPanelType = .defaultOpen
- self.defaultOpen = .thumbnail
- self.panelOpenType = .automatic
- self.showQuickActionBar = true
- self.highlightLink = true
- self.highlightFormField = true
-
- }
-
- func setUpLanguageKey() {
- let defaultLan = NSLocalizedString("LanguageMarkKey", comment: "")
- if defaultLan == "English" {
- self.language = .English
- } else if defaultLan == "Simplified" {
- self.language = .Chinese_CN
- } else if defaultLan == "Traditional" {
- self.language = .Chinese_TW
- }
- }
-
- }
- //MARK: - Method
-
- public func KMLocalizedString(_ key: String, comment: String? = nil) -> String {
-
- var bundlePath: String? = nil
-
- // if SettingsManager.sharedInstance.language == .English {
- // bundlePath = Bundle.main.path(forResource: "en", ofType: "lproj")
- // } else if SettingsManager.sharedInstance.language == .Chinese_CN {
- // bundlePath = Bundle.main.path(forResource: "zh-Hans", ofType: "lproj")
- // } else if SettingsManager.sharedInstance.language == .Chinese_TW {
- // bundlePath = Bundle.main.path(forResource: "zh-Hant", ofType: "lproj")
- // }
-
- if let bPath = bundlePath, let bundle = Bundle.init(path: bPath) {
- return NSLocalizedString(key, tableName: "Localizable", bundle: bundle, comment: comment ?? "")
- } else {
- return NSLocalizedString(key, comment: "")
- }
- }
-
-
|