KMSyncPreferences.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // KMSyncPreferences.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/6.
  6. //
  7. import Cocoa
  8. var TeXEditors: [[String : Any]] = []
  9. let NAME_KEY = "name"
  10. var INITIALUSERDEFAULTS_KEY = "InitialUserDefaults"
  11. var TEXEDITORS_KEY = "TeXEditors"
  12. var COMMAND_KEY = "command"
  13. var ARGUMENTS_KEY = "arguments"
  14. class KMSyncPreferences: NSViewController {
  15. var customTeXEditor = false
  16. /*
  17. + (BOOL)getTeXEditorCommand:(NSString **)command arguments:(NSString **)arguments forPreset:(NSString *)name;
  18. */
  19. @IBOutlet weak var checkButton: NSButton!
  20. @IBOutlet weak var checkTipLabel: NSTextField!
  21. @IBOutlet weak var pdfTexTitleLabel: NSTextField!
  22. @IBOutlet weak var presetLabel: NSTextField!
  23. @IBOutlet weak var commandLabel: NSTextField!
  24. @IBOutlet weak var argumentsLabel: NSTextField!
  25. @IBOutlet weak var presetButton: NSPopUpButton!
  26. @IBOutlet weak var commandTextField: NSTextField!
  27. @IBOutlet weak var argumentsTextField: NSTextField!
  28. var texEditorLabels: [NSTextField] = []
  29. var texEditorControls: [NSControl] = []
  30. override var nibName: NSNib.Name? {
  31. return "SyncPreferences"
  32. }
  33. override func loadView() {
  34. super.loadView()
  35. if let initialUserDefaultsURL: URL = Bundle.main.url(forResource: INITIALUSERDEFAULTS_KEY, withExtension: "plist") {
  36. TeXEditors = (NSDictionary(contentsOf: initialUserDefaultsURL)?.object(forKey: TEXEDITORS_KEY) as? [[String : Any]]) ?? [[:]]
  37. }
  38. self.texEditorLabels = [self.presetLabel, self.commandLabel, self.argumentsLabel]
  39. self.texEditorControls = [self.presetButton, self.commandTextField, self.argumentsTextField]
  40. _ = KMAutoSizeLabelFields(self.texEditorLabels, self.texEditorControls, true)
  41. let editorPreset = UserDefaults.standard.string(forKey: SKTeXEditorPresetKey)
  42. var i = TeXEditors.count
  43. var idx = -1
  44. let texEditorPopUpButton = self.presetButton
  45. let ws = NSWorkspace.shared
  46. while i > 0 {
  47. i -= 1
  48. let name = (TeXEditors[i][NAME_KEY] as? String) ?? ""
  49. let path = ws.fullPath(forApplication: name)
  50. let image = path != nil ? ws.icon(forFile: path!) : ws.icon(forFileType: NSFileTypeForHFSTypeCode(OSType(kGenericApplicationIcon)))
  51. texEditorPopUpButton?.insertItem(withTitle: name, at: 0)
  52. texEditorPopUpButton?.item(at: 0)?.setImageAndSize(image)
  53. if name == editorPreset {
  54. idx = i
  55. }
  56. }
  57. self.customTeXEditor = idx == -1
  58. if (idx == -1) {
  59. if let data = texEditorPopUpButton?.lastItem {
  60. texEditorPopUpButton?.select(data)
  61. }
  62. } else {
  63. texEditorPopUpButton?.selectItem(at: idx)
  64. }
  65. }
  66. override var title: String? {
  67. set {
  68. super.title = newValue
  69. }
  70. get {
  71. return KMLocalizedString("Sync", "Preference pane label")
  72. }
  73. }
  74. override func viewDidLoad() {
  75. super.viewDidLoad()
  76. // SKAutoCheckFileUpdate
  77. // SKTeXEditorPresetKey
  78. // SKTeXEditorCommandKey
  79. // SKTeXEditorArgumentsKey
  80. // Scripting definition file name PDF Reader Pro Edition.sdef
  81. }
  82. /*
  83. + (BOOL)getTeXEditorCommand:(NSString **)command arguments:(NSString **)arguments forPreset:(NSString *)name {
  84. for (NSDictionary *editor in TeXEditors) {
  85. if ([[editor objectForKey:NAME_KEY] isEqualToString:name]) {
  86. if (command) *command = [editor objectForKey:COMMAND_KEY];
  87. if (arguments) *arguments = [editor objectForKey:ARGUMENTS_KEY];
  88. return YES;
  89. }
  90. }
  91. return NO;
  92. }
  93. */
  94. static func getTeXEditorCommand(command: inout String?, arguments: inout String?, forPreset name: String) -> Bool {
  95. for editor in TeXEditors {
  96. if let editorName = editor[NAME_KEY] as? String, editorName == name {
  97. command = editor[COMMAND_KEY] as? String
  98. arguments = editor[ARGUMENTS_KEY] as? String
  99. return true
  100. }
  101. }
  102. return false
  103. }
  104. @IBAction func changeTeXEditorPreset(_ sender: AnyObject) {
  105. let sudc = NSUserDefaultsController.shared.defaults
  106. let idx = sender.indexOfSelectedItem()
  107. if (idx < sender.numberOfItems - 1) {
  108. let editor = TeXEditors[idx]
  109. sudc.setValue(sender.titleOfSelectedItem as Any?, forKey: SKTeXEditorPresetKey)
  110. sudc.setValue(editor[COMMAND_KEY], forKey: SKTeXEditorCommandKey)
  111. sudc.setValue(editor[ARGUMENTS_KEY], forKey: SKTeXEditorArgumentsKey)
  112. self.customTeXEditor = false
  113. } else {
  114. sudc.setValue("", forKey: SKTeXEditorPresetKey)
  115. self.customTeXEditor = true
  116. }
  117. }
  118. }
  119. extension KMSyncPreferences: KMPreferencePane {
  120. func defaultsDidRevert() {
  121. let editorPreset = UserDefaults.standard.string(forKey: SKTeXEditorPresetKey)
  122. let texEditorPopUpButton = self.presetButton
  123. if let data = editorPreset?.isEmpty, data {
  124. if let value = texEditorPopUpButton?.lastItem {
  125. texEditorPopUpButton?.select(value)
  126. }
  127. self.customTeXEditor = true
  128. } else {
  129. texEditorPopUpButton?.selectItem(withTitle: editorPreset ?? "")
  130. self.customTeXEditor = false
  131. }
  132. }
  133. }