123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- //
- // SettingsGeneralView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/9/26.
- //
- import Cocoa
- import AppKit
- import KMComponentLibrary
- class SettingsGeneralView: BaseXibView {
-
- @IBOutlet weak var appearanceLabel: NSTextField!
-
- @IBOutlet weak var authorLabel: NSTextField!
- @IBOutlet weak var authorInputView: ComponentInput!
-
- @IBOutlet weak var defaultSettingLabel: NSTextField!
- @IBOutlet weak var defaultPDFViewerCheckbox: ComponentCheckBox!
- @IBOutlet weak var reopenFileCheckBox: ComponentCheckBox!
- @IBOutlet weak var autosaveCheckBox: ComponentCheckBox!
- @IBOutlet weak var autosaveInputNumberView: ComponentInputNumber!
- @IBOutlet weak var autosaveLabel: NSTextField!
- @IBOutlet weak var defaultViewerBoxWidthConst: NSLayoutConstraint!
- @IBOutlet weak var reopenBoxWidthConst: NSLayoutConstraint!
- @IBOutlet weak var autosaveBoxWidthConst: NSLayoutConstraint!
-
- @IBOutlet weak var filelistLabel: NSTextField!
- @IBOutlet weak var filelistInputNumberView: ComponentInputNumber!
- @IBOutlet weak var filelistSubLabel: NSTextField!
-
- @IBOutlet weak var languageLabel: NSTextField!
- @IBOutlet weak var languageSelectView: ComponentSelect!
-
- @IBOutlet weak var sepDividerView: ComponentDivider!
-
- @IBOutlet weak var resetWarningLabel: NSTextField!
- @IBOutlet weak var resetWarningButton: ComponentButton!
- @IBOutlet weak var resetWarningBtnWidthConst: NSLayoutConstraint!
-
- @IBOutlet weak var savePasswordLabel: NSTextField!
- @IBOutlet weak var savePWAskRadio: ComponentRadio!
- @IBOutlet weak var savePWAlwaysRadio: ComponentRadio!
- @IBOutlet weak var savePWNeverRadio: ComponentRadio!
- @IBOutlet weak var savePWAskRadioWidthConst: NSLayoutConstraint!
- @IBOutlet weak var savePWAlwaysRadioWidthConst: NSLayoutConstraint!
-
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
-
- self.setUp()
-
- self.reloadData()
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- }
-
- func setUp() {
-
- var titleLabelColor: NSColor = NSColor.labelColor
- if let value = ComponentLibrary.shared.getComponentValueFromKey("colorText/2") {
- let currentValue = value as! [String : Any]
- titleLabelColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- }
- var titleLabelFont: NSFont = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
-
- //Appearance
- self.appearanceLabel.stringValue = KMLocalizedString("Appearance", comment: "")
- self.appearanceLabel.textColor = titleLabelColor
- self.appearanceLabel.font = titleLabelFont
-
- //Author
- self.authorLabel.stringValue = KMLocalizedString("Author", comment: "")
- self.authorLabel.textColor = titleLabelColor
- self.authorLabel.font = titleLabelFont
-
- self.authorInputView.properties = ComponentInputProperty(size: .s,
- state: .normal,
- isError: false,
- showPrefix: false,
- showSuffix: false,
- showClear: true,
- isDisabled: false,
- placeholder: KMLocalizedString("Author", comment: ""),
- text: SettingsManager.sharedInstance.author,
- alignment: .left)
- authorInputView.delegate = self
-
- // Default Settings
- defaultSettingLabel.stringValue = KMLocalizedString("Default Settings", comment: "")
- defaultSettingLabel.textColor = titleLabelColor
- defaultSettingLabel.font = titleLabelFont
-
- defaultPDFViewerCheckbox.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Set PDF Reader Pro as the default PDF Viewer", comment: ""),
- checkboxType: SettingsManager.sharedInstance.defaultPDFViewer ? .selected : .normal)
- reopenFileCheckBox.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Reopen last open files at startup", comment: ""),
- checkboxType: SettingsManager.sharedInstance.reopenFile ? .selected : .normal)
-
- autosaveCheckBox.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Automatically save the document every", comment: ""),
- checkboxType: SettingsManager.sharedInstance.autoSaveFile ? .selected : .normal)
-
- autosaveInputNumberView.properties = ComponentInputNumberProperty(alignment: .left,
- size: .s,
- state: .normal,
- isError: false,
- showErrorInfo: false,
- isDisabled: SettingsManager.sharedInstance.autoSaveFile == false,
- showPrefix: false,
- showSuffix: false,
- minSize: 5,
- maxSize: 99,
- text: SettingsManager.sharedInstance.autoSaveMinutes)
- autosaveInputNumberView.delegate = self
- autosaveLabel.stringValue = KMLocalizedString("minutes(5-99)", comment: "")
- defaultViewerBoxWidthConst.constant = defaultPDFViewerCheckbox.properties.propertyInfo.viewWidth
- reopenBoxWidthConst.constant = reopenFileCheckBox.properties.propertyInfo.viewWidth
- autosaveBoxWidthConst.constant = autosaveCheckBox.properties.propertyInfo.viewWidth
-
- filelistLabel.stringValue = KMLocalizedString("Documents in recently used list:", comment: "")
- filelistInputNumberView.properties = ComponentInputNumberProperty(alignment: .left,
- size: .s,
- state: .normal,
- isError: false,
- showErrorInfo: false,
- isDisabled: false,
- showPrefix: false,
- showSuffix: false,
- minSize: 10,
- maxSize: 50,
- text: String(SettingsManager.sharedInstance.fileListCount))
- filelistInputNumberView.delegate = self
- filelistSubLabel.stringValue = "(10-50)"
-
- defaultPDFViewerCheckbox.setTarget(self, action: #selector(defaultSettingAction(_:)))
- reopenFileCheckBox.setTarget(self, action: #selector(defaultSettingAction(_:)))
- autosaveCheckBox.setTarget(self, action: #selector(defaultSettingAction(_:)))
-
-
- // Language
- languageLabel.stringValue = KMLocalizedString("Language", comment: "")
- languageLabel.textColor = titleLabelColor
- languageLabel.font = titleLabelFont
-
- languageSelectView.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- isDisabled: false,
- isError: false,
- leftIcon: false,
- placeholder: nil,
- errorText: nil,
- creatable: false,
- text: "English")
- if true {
- var languageArr = ["English", "Chinese_CN", "Chinese_TW", "Dutch", "French", "Spanish", "German", "Russian", "Italian", "Japanese"]
- var menuItemArr: [ComponentMenuitemProperty] = []
- for language in languageArr {
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: language)
- menuItemArr.append(itemProperty)
- }
- languageSelectView.updateMenuItemsArr(menuItemArr)
- }
- languageSelectView.delegate = self
-
- sepDividerView.properties = ComponentDividerProperty(type: .horizontal, dash: false)
-
- resetWarningLabel.stringValue = KMLocalizedString("Reset All Warnings:", comment: "")
- if let value = ComponentLibrary.shared.getComponentValueFromKey("comp-form/colorText-label") {
- let currentValue = value as! [String : Any]
- resetWarningLabel.textColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- } else {
- resetWarningLabel.textColor = NSColor.labelColor
- }
-
- resetWarningButton.properties = ComponentButtonProperty(type: .default_tertiary,
- size: .s,
- state: .normal,
- isDisable: false,
- onlyIcon: false,
- showLeftIcon: false,
- showRightIcon: false,
- buttonText: KMLocalizedString("Reset", comment: ""))
- resetWarningBtnWidthConst.constant = resetWarningButton.properties.propertyInfo.viewWidth
-
-
- //
- savePasswordLabel.stringValue = KMLocalizedString("Save the password in Keychain:", comment: "")
- savePasswordLabel.textColor = resetWarningLabel.textColor
-
- savePWAskRadio.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Ask", comment: ""),
- checkboxType: .normal)
- savePWAlwaysRadio.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Always", comment: ""),
- checkboxType: .normal)
- savePWNeverRadio.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: KMLocalizedString("Never", comment: ""),
- checkboxType: .normal)
- savePWAskRadioWidthConst.constant = savePWAskRadio.properties.propertyInfo.viewWidth
- savePWAlwaysRadioWidthConst.constant = savePWAlwaysRadio.properties.propertyInfo.viewWidth
-
-
- savePWAskRadio.setTarget(self, action: #selector(savePWClicked(_:)))
- savePWAlwaysRadio.setTarget(self, action: #selector(savePWClicked(_:)))
- savePWNeverRadio.setTarget(self, action: #selector(savePWClicked(_:)))
-
-
- }
-
- func reloadData() {
-
- //Default Settings
- defaultPDFViewerCheckbox.properties.checkboxType = SettingsManager.sharedInstance.defaultPDFViewer ? .selected : .normal
- defaultPDFViewerCheckbox.reloadData()
-
- reopenFileCheckBox.properties.checkboxType = SettingsManager.sharedInstance.reopenFile ? .selected : .normal
- reopenFileCheckBox.reloadData()
-
- autosaveCheckBox.properties.checkboxType = SettingsManager.sharedInstance.autoSaveFile ? .selected : .normal
- autosaveCheckBox.reloadData()
-
- autosaveInputNumberView.properties.isDisabled = SettingsManager.sharedInstance.autoSaveFile == false
- autosaveInputNumberView.properties.text = SettingsManager.sharedInstance.autoSaveMinutes
- autosaveInputNumberView.reloadData()
-
- filelistInputNumberView.properties.text = String(SettingsManager.sharedInstance.fileListCount)
- filelistInputNumberView.reloadData()
-
- //
-
-
- //
-
- languageSelectView.selectItemAtIndex(SettingsManager.sharedInstance.language.rawValue)
-
- //
-
-
- //
- savePWAskRadio.properties.checkboxType = .normal
- savePWAlwaysRadio.properties.checkboxType = .normal
- savePWNeverRadio.properties.checkboxType = .normal
- if SettingsManager.sharedInstance.keychainType == .ask {
- savePWAskRadio.properties.checkboxType = .selected
- } else if SettingsManager.sharedInstance.keychainType == .always {
- savePWAlwaysRadio.properties.checkboxType = .selected
- } else if SettingsManager.sharedInstance.keychainType == .never {
- savePWNeverRadio.properties.checkboxType = .selected
- }
- savePWAskRadio.reloadData()
- savePWAlwaysRadio.reloadData()
- savePWNeverRadio.reloadData()
-
- }
-
- //MARK: - Action
-
-
- @objc func defaultSettingAction(_ sender: NSView) {
- if sender == defaultPDFViewerCheckbox {
- SettingsManager.sharedInstance.defaultPDFViewer = defaultPDFViewerCheckbox.properties.checkboxType == .selected
- } else if sender == reopenFileCheckBox {
- SettingsManager.sharedInstance.reopenFile = reopenFileCheckBox.properties.checkboxType == .selected
- } else if sender == autosaveCheckBox {
- SettingsManager.sharedInstance.autoSaveFile = autosaveCheckBox.properties.checkboxType == .selected
- }
- self.reloadData()
-
- }
-
- @objc func savePWClicked(_ sender: NSView) {
- if sender == savePWAskRadio {
- SettingsManager.sharedInstance.keychainType = .ask
- } else if sender == savePWAlwaysRadio {
- SettingsManager.sharedInstance.keychainType = .always
- } else if sender == savePWNeverRadio {
- SettingsManager.sharedInstance.keychainType = .never
- }
- self.reloadData()
-
- }
-
- //MARK: - Mouse Event
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- self.window?.makeFirstResponder(nil)
-
- }
- }
- extension SettingsGeneralView: ComponentInputDelegate {
- func componentInputDidEndEditing(inputView: ComponentInput) {
- if inputView == authorInputView {
- SettingsManager.sharedInstance.author = inputView.properties.text
-
- }
- }
- }
- extension SettingsGeneralView: ComponentInputNumberDelegate {
- func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
- if inputNumber == autosaveInputNumberView {
- SettingsManager.sharedInstance.autoSaveMinutes = autosaveInputNumberView.properties.text ?? "5"
- } else if inputNumber == filelistInputNumberView {
- if let text: String = filelistInputNumberView.properties.text {
- SettingsManager.sharedInstance.fileListCount = Int(text)!
- }
- }
- self.reloadData()
- }
- }
- extension SettingsGeneralView: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
-
- let selectIndex: NSInteger = view?.indexOfSelect() ?? 0
- if let item = menuItemProperty {
- if selectIndex >= 0 {
- SettingsManager.sharedInstance.language = settingsLanguage(rawValue: selectIndex)!
- UserDefaults.standard.setValue("didChanged", forKey: settingsLanguageDidChangedKey)
- }
- }
- }
- }
|