-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
76 lines (63 loc) · 2.77 KB
/
AppDelegate.swift
File metadata and controls
76 lines (63 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Cocoa
import SwiftUI
import Carbon.HIToolbox
class AppDelegate: NSObject, NSApplicationDelegate {
private var statusBarController: StatusBarController?
private var clipboardWatcher: ClipboardWatcher?
private var historyWindowController: HistoryWindowController?
private var hotkeyManager: HotkeyManager?
let clipboardStore = ClipboardStore()
func applicationDidFinishLaunching(_ notification: Notification) {
// Hide dock icon - we're menu bar only
NSApp.setActivationPolicy(.accessory)
let defaults = UserDefaults.standard
if !defaults.bool(forKey: "hasLaunchedBefore") {
// Give it a tiny delay to ensure everything is loaded before registering SMAppService
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
SettingsManager.shared.toggleLaunchAtLogin(true)
defaults.set(true, forKey: "hasLaunchedBefore")
}
}
// Request Accessibility permissions for global hotkeys
let options = [kAXTrustedCheckOptionPrompt.takeRetainedValue(): true] as CFDictionary
AXIsProcessTrustedWithOptions(options)
// Initialize clipboard watcher
clipboardWatcher = ClipboardWatcher(store: clipboardStore)
clipboardWatcher?.startWatching()
// Initialize status bar
statusBarController = StatusBarController(
store: clipboardStore,
watcher: clipboardWatcher!,
onShowHistory: { [weak self] in
self?.showHistoryWindow()
}
)
// Initialize history window controller
historyWindowController = HistoryWindowController(store: clipboardStore)
// Setup global hotkey (Shift + Command + V)
hotkeyManager = HotkeyManager { [weak self] in
self?.toggleHistoryWindow()
}
hotkeyManager?.register()
NotificationCenter.default.addObserver(forName: .bufferHotkeyChanged, object: nil, queue: .main) { [weak self] _ in
self?.hotkeyManager?.reregister()
}
}
func applicationWillTerminate(_ notification: Notification) {
clipboardWatcher?.stopWatching()
hotkeyManager?.unregister()
}
private func toggleHistoryWindow() {
print("[AppDelegate] toggleHistoryWindow called")
if let window = historyWindowController?.window, window.isVisible {
print("[AppDelegate] Window is visible, closing...")
historyWindowController?.close()
} else {
print("[AppDelegate] Window is hidden, showing...")
showHistoryWindow()
}
}
private func showHistoryWindow() {
historyWindowController?.showWindow(nil)
}
}