// // KMSyncPreferences.swift // PDF Reader Pro // // Created by tangchao on 2023/11/6. // import Cocoa var TeXEditors: [[String : Any]] = [] let NAME_KEY = "name" var INITIALUSERDEFAULTS_KEY = "InitialUserDefaults" var TEXEDITORS_KEY = "TeXEditors" var COMMAND_KEY = "command" var ARGUMENTS_KEY = "arguments" class KMSyncPreferences: NSViewController { var customTeXEditor = false /* + (BOOL)getTeXEditorCommand:(NSString **)command arguments:(NSString **)arguments forPreset:(NSString *)name; */ @IBOutlet weak var checkButton: NSButton! @IBOutlet weak var checkTipLabel: NSTextField! @IBOutlet weak var pdfTexTitleLabel: NSTextField! @IBOutlet weak var presetLabel: NSTextField! @IBOutlet weak var commandLabel: NSTextField! @IBOutlet weak var argumentsLabel: NSTextField! @IBOutlet weak var presetButton: NSPopUpButton! @IBOutlet weak var commandTextField: NSTextField! @IBOutlet weak var argumentsTextField: NSTextField! var texEditorLabels: [NSTextField] = [] var texEditorControls: [NSControl] = [] override var nibName: NSNib.Name? { return "SyncPreferences" } override func loadView() { super.loadView() if let initialUserDefaultsURL: URL = Bundle.main.url(forResource: INITIALUSERDEFAULTS_KEY, withExtension: "plist") { TeXEditors = (NSDictionary(contentsOf: initialUserDefaultsURL)?.object(forKey: TEXEDITORS_KEY) as? [[String : Any]]) ?? [[:]] } self.texEditorLabels = [self.presetLabel, self.commandLabel, self.argumentsLabel] self.texEditorControls = [self.presetButton, self.commandTextField, self.argumentsTextField] _ = KMAutoSizeLabelFields(self.texEditorLabels, self.texEditorControls, true) let editorPreset = UserDefaults.standard.string(forKey: SKTeXEditorPresetKey) var i = TeXEditors.count var idx = -1 let texEditorPopUpButton = self.presetButton let ws = NSWorkspace.shared while i > 0 { i -= 1 let name = (TeXEditors[i][NAME_KEY] as? String) ?? "" let path = ws.fullPath(forApplication: name) let image = path != nil ? ws.icon(forFile: path!) : ws.icon(forFileType: NSFileTypeForHFSTypeCode(OSType(kGenericApplicationIcon))) texEditorPopUpButton?.insertItem(withTitle: name, at: 0) texEditorPopUpButton?.item(at: 0)?.setImageAndSize(image) if name == editorPreset { idx = i } } self.customTeXEditor = idx == -1 if (idx == -1) { if let data = texEditorPopUpButton?.lastItem { texEditorPopUpButton?.select(data) } } else { texEditorPopUpButton?.selectItem(at: idx) } } override var title: String? { set { super.title = newValue } get { return KMLocalizedString("Sync") } } override func viewDidLoad() { super.viewDidLoad() // SKAutoCheckFileUpdate // SKTeXEditorPresetKey // SKTeXEditorCommandKey // SKTeXEditorArgumentsKey // Scripting definition file name PDF Reader Pro Edition.sdef } /* + (BOOL)getTeXEditorCommand:(NSString **)command arguments:(NSString **)arguments forPreset:(NSString *)name { for (NSDictionary *editor in TeXEditors) { if ([[editor objectForKey:NAME_KEY] isEqualToString:name]) { if (command) *command = [editor objectForKey:COMMAND_KEY]; if (arguments) *arguments = [editor objectForKey:ARGUMENTS_KEY]; return YES; } } return NO; } */ static func getTeXEditorCommand(command: inout String?, arguments: inout String?, forPreset name: String) -> Bool { for editor in TeXEditors { if let editorName = editor[NAME_KEY] as? String, editorName == name { command = editor[COMMAND_KEY] as? String arguments = editor[ARGUMENTS_KEY] as? String return true } } return false } @IBAction func changeTeXEditorPreset(_ sender: AnyObject) { let sudc = NSUserDefaultsController.shared.defaults let idx = sender.indexOfSelectedItem() if (idx < sender.numberOfItems - 1) { let editor = TeXEditors[idx] sudc.setValue(sender.titleOfSelectedItem as Any?, forKey: SKTeXEditorPresetKey) sudc.setValue(editor[COMMAND_KEY], forKey: SKTeXEditorCommandKey) sudc.setValue(editor[ARGUMENTS_KEY], forKey: SKTeXEditorArgumentsKey) self.customTeXEditor = false } else { sudc.setValue("", forKey: SKTeXEditorPresetKey) self.customTeXEditor = true } } } extension KMSyncPreferences: KMPreferencePane { func defaultsDidRevert() { let editorPreset = UserDefaults.standard.string(forKey: SKTeXEditorPresetKey) let texEditorPopUpButton = self.presetButton if let data = editorPreset?.isEmpty, data { if let value = texEditorPopUpButton?.lastItem { texEditorPopUpButton?.select(value) } self.customTeXEditor = true } else { texEditorPopUpButton?.selectItem(withTitle: editorPreset ?? "") self.customTeXEditor = false } } }