|
| 1 | +import Cocoa |
| 2 | +import ApplicationServices |
| 3 | + |
| 4 | +class AppDelegate: NSObject, NSApplicationDelegate { |
| 5 | + private var statusItem: NSStatusItem? |
| 6 | + private let monitor = FocusMonitor() |
| 7 | + private let indicator = IndicatorPanel() |
| 8 | + private var settingsWC: SettingsWindowController? |
| 9 | + private var permissionWC: PermissionWindowController? |
| 10 | + private var enabledItem: NSMenuItem! |
| 11 | + private var loginItem: NSMenuItem! |
| 12 | + private var permissionPollTimer: Timer? |
| 13 | + private var wasTrusted = false |
| 14 | + |
| 15 | + private var launchAgentPath: String { |
| 16 | + NSHomeDirectory() + "/Library/LaunchAgents/top.cuiko.termims.plist" |
| 17 | + } |
| 18 | + |
| 19 | + func applicationDidFinishLaunching(_ n: Notification) { |
| 20 | + Log.debug("=== TermIMS started ===") |
| 21 | + installEditMenu() |
| 22 | + if AXIsProcessTrusted() { |
| 23 | + wasTrusted = true |
| 24 | + startApp() |
| 25 | + } else { |
| 26 | + showPermissionWindow() |
| 27 | + } |
| 28 | + startPermissionPolling() |
| 29 | + } |
| 30 | + |
| 31 | + /// LSUIElement apps don't get a main menu by default, which means |
| 32 | + /// Cmd+C/V/X/A in any text field are dispatched into the void. Wire up |
| 33 | + /// a minimal Edit menu so the standard shortcuts reach the field editor. |
| 34 | + private func installEditMenu() { |
| 35 | + let main = NSMenu() |
| 36 | + let editItem = NSMenuItem() |
| 37 | + main.addItem(editItem) |
| 38 | + |
| 39 | + let edit = NSMenu(title: "Edit") |
| 40 | + edit.addItem(withTitle: "Undo", action: Selector(("undo:")), keyEquivalent: "z") |
| 41 | + let redo = NSMenuItem(title: "Redo", action: Selector(("redo:")), keyEquivalent: "z") |
| 42 | + redo.keyEquivalentModifierMask = [.command, .shift] |
| 43 | + edit.addItem(redo) |
| 44 | + edit.addItem(.separator()) |
| 45 | + edit.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x") |
| 46 | + edit.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c") |
| 47 | + edit.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v") |
| 48 | + edit.addItem(withTitle: "Delete", action: #selector(NSText.delete(_:)), keyEquivalent: "") |
| 49 | + edit.addItem(withTitle: "Select All", action: #selector(NSText.selectAll(_:)), keyEquivalent: "a") |
| 50 | + |
| 51 | + editItem.submenu = edit |
| 52 | + NSApp.mainMenu = main |
| 53 | + } |
| 54 | + |
| 55 | + private func startPermissionPolling() { |
| 56 | + permissionPollTimer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { [weak self] _ in |
| 57 | + guard let self else { return } |
| 58 | + let trusted = AXIsProcessTrusted() |
| 59 | + if self.wasTrusted && !trusted { |
| 60 | + self.wasTrusted = false |
| 61 | + self.monitor.stop() |
| 62 | + self.rebuildMenu() |
| 63 | + } else if !self.wasTrusted && trusted { |
| 64 | + self.wasTrusted = true |
| 65 | + self.startApp() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private func showPermissionWindow() { |
| 71 | + permissionWC = PermissionWindowController() |
| 72 | + permissionWC?.onGranted = { [weak self] in |
| 73 | + self?.permissionWC = nil |
| 74 | + self?.startApp() |
| 75 | + } |
| 76 | + permissionWC?.showWindow(nil) |
| 77 | + permissionWC?.startPolling() |
| 78 | + NSApp.activate(ignoringOtherApps: true) |
| 79 | + } |
| 80 | + |
| 81 | + private var appStarted = false |
| 82 | + private func startApp() { |
| 83 | + guard !appStarted else { rebuildMenu(); monitor.reload(); return } |
| 84 | + appStarted = true |
| 85 | + applyMenuBarVisibility() |
| 86 | + NotificationCenter.default.addObserver(self, selector: #selector(rebuildMenu), |
| 87 | + name: .rulesDidChange, object: nil) |
| 88 | + NotificationCenter.default.addObserver(self, selector: #selector(imDidSwitch), |
| 89 | + name: .imDidSwitch, object: nil) |
| 90 | + monitor.start() |
| 91 | + } |
| 92 | + |
| 93 | + func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool { |
| 94 | + if !AXIsProcessTrusted() { |
| 95 | + showPermissionWindow() |
| 96 | + } else { |
| 97 | + showSettings() |
| 98 | + } |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + @objc private func imDidSwitch() { |
| 103 | + guard RuleStore.shared.indicatorEnabled, |
| 104 | + let name = currentInputSourceName() else { return } |
| 105 | + indicator.show(text: name, position: RuleStore.shared.indicatorPosition) |
| 106 | + } |
| 107 | + |
| 108 | + private func applyMenuBarVisibility() { |
| 109 | + if RuleStore.shared.hideMenuBarIcon { |
| 110 | + if let item = statusItem { |
| 111 | + NSStatusBar.system.removeStatusItem(item) |
| 112 | + statusItem = nil |
| 113 | + } |
| 114 | + } else { |
| 115 | + if statusItem == nil { |
| 116 | + statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) |
| 117 | + if let btn = statusItem?.button { |
| 118 | + btn.image = NSImage(systemSymbolName: "keyboard", accessibilityDescription: "TermIMS") |
| 119 | + } |
| 120 | + } |
| 121 | + rebuildMenu() |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @objc private func rebuildMenu() { |
| 126 | + if RuleStore.shared.hideMenuBarIcon { applyMenuBarVisibility(); return } |
| 127 | + guard let statusItem else { applyMenuBarVisibility(); return } |
| 128 | + |
| 129 | + let menu = NSMenu() |
| 130 | + let store = RuleStore.shared |
| 131 | + let trusted = AXIsProcessTrusted() |
| 132 | + |
| 133 | + if !trusted { |
| 134 | + let warn = NSMenuItem(title: "Accessibility Permission Required", action: #selector(showPermissionPrompt), keyEquivalent: "") |
| 135 | + warn.target = self |
| 136 | + warn.image = NSImage(systemSymbolName: "exclamationmark.triangle", accessibilityDescription: nil) |
| 137 | + menu.addItem(warn) |
| 138 | + menu.addItem(.separator()) |
| 139 | + } |
| 140 | + |
| 141 | + enabledItem = NSMenuItem(title: "Enabled", action: #selector(toggleEnabled), keyEquivalent: "") |
| 142 | + enabledItem.target = self; enabledItem.state = monitor.enabled ? .on : .off |
| 143 | + menu.addItem(enabledItem) |
| 144 | + menu.addItem(.separator()) |
| 145 | + |
| 146 | + let defName = store.defaultSourceName ?? "None" |
| 147 | + let defItem = NSMenuItem(title: "Default: \(defName)", action: nil, keyEquivalent: "") |
| 148 | + defItem.isEnabled = false; menu.addItem(defItem) |
| 149 | + |
| 150 | + let activeAppRules = store.rules.filter(\.enabled) |
| 151 | + if !activeAppRules.isEmpty { |
| 152 | + menu.addItem(.separator()) |
| 153 | + let hdr = NSMenuItem(title: "App Rules", action: nil, keyEquivalent: "") |
| 154 | + hdr.isEnabled = false; menu.addItem(hdr) |
| 155 | + for rule in activeAppRules { |
| 156 | + let item = NSMenuItem(title: " \(rule.appName) \u{2192} \(rule.inputSourceName)", action: nil, keyEquivalent: "") |
| 157 | + item.isEnabled = false; menu.addItem(item) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + let activeTermRules = store.terminalRules.filter(\.enabled) |
| 162 | + if !activeTermRules.isEmpty { |
| 163 | + menu.addItem(.separator()) |
| 164 | + let hdr = NSMenuItem(title: "Terminal Rules", action: nil, keyEquivalent: "") |
| 165 | + hdr.isEnabled = false; menu.addItem(hdr) |
| 166 | + for rule in activeTermRules { |
| 167 | + let typeStr = rule.matchType == .title ? "title" : "proc" |
| 168 | + let item = NSMenuItem(title: " \(typeStr):\(rule.pattern) \u{2192} \(rule.inputSourceName)", action: nil, keyEquivalent: "") |
| 169 | + item.isEnabled = false; menu.addItem(item) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + menu.addItem(.separator()) |
| 174 | + let settings = NSMenuItem(title: "Settings\u{2026}", action: #selector(showSettings), keyEquivalent: ",") |
| 175 | + settings.target = self; menu.addItem(settings) |
| 176 | + |
| 177 | + menu.addItem(.separator()) |
| 178 | + loginItem = NSMenuItem(title: "Launch at Login", action: #selector(toggleLogin), keyEquivalent: "") |
| 179 | + loginItem.target = self |
| 180 | + loginItem.state = FileManager.default.fileExists(atPath: launchAgentPath) ? .on : .off |
| 181 | + menu.addItem(loginItem) |
| 182 | + |
| 183 | + menu.addItem(.separator()) |
| 184 | + let quit = NSMenuItem(title: "Quit TermIMS", action: #selector(quitApp), keyEquivalent: "q") |
| 185 | + quit.target = self; menu.addItem(quit) |
| 186 | + |
| 187 | + statusItem.menu = menu |
| 188 | + } |
| 189 | + |
| 190 | + @objc private func toggleEnabled() { |
| 191 | + monitor.enabled.toggle(); enabledItem.state = monitor.enabled ? .on : .off |
| 192 | + } |
| 193 | + @objc private func showPermissionPrompt() { |
| 194 | + showPermissionWindow() |
| 195 | + } |
| 196 | + @objc private func showSettings() { |
| 197 | + if !AXIsProcessTrusted() { showPermissionWindow(); return } |
| 198 | + if settingsWC == nil { settingsWC = SettingsWindowController() } |
| 199 | + settingsWC?.showWindow(nil); NSApp.activate(ignoringOtherApps: true) |
| 200 | + } |
| 201 | + @objc private func toggleLogin() { |
| 202 | + let fm = FileManager.default |
| 203 | + if fm.fileExists(atPath: launchAgentPath) { |
| 204 | + try? fm.removeItem(atPath: launchAgentPath) |
| 205 | + } else { |
| 206 | + try? fm.createDirectory(atPath: NSHomeDirectory() + "/Library/LaunchAgents", withIntermediateDirectories: true) |
| 207 | + let plist: NSDictionary = [ |
| 208 | + "Label": "top.cuiko.termims", |
| 209 | + "ProgramArguments": [Bundle.main.executablePath ?? ""], |
| 210 | + "RunAtLoad": true, |
| 211 | + ] |
| 212 | + plist.write(toFile: launchAgentPath, atomically: true) |
| 213 | + } |
| 214 | + loginItem.state = fm.fileExists(atPath: launchAgentPath) ? .on : .off |
| 215 | + } |
| 216 | + @objc private func quitApp() { monitor.stop(); NSApp.terminate(nil) } |
| 217 | +} |
0 commit comments