-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
487 lines (403 loc) · 16.8 KB
/
Copy pathAppDelegate.swift
File metadata and controls
487 lines (403 loc) · 16.8 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import Cocoa
import Defaults
import KeyboardShortcuts
import LaunchAtLogin
import SwiftUI
#if TARGET_SPARKLE
import Sparkle
#endif
@main
// swiftlint:disable type_body_length
// swiftlint:disable file_length
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
var statusBarItem: NSStatusItem!
var statusBarMenu: NSMenu!
var pikaWindow: NSWindow!
var splashWindow: NSWindow!
var aboutWindow: NSWindow!
var preferencesWindow: NSWindow!
var eyedroppers: Eyedroppers!
var undoManager = UndoManager()
var pikaTouchBarController: PikaTouchBarController!
var splashTouchBarController: SplashTouchBarController!
var aboutTouchBarController: SplashTouchBarController!
let notificationCenter = NotificationCenter.default
func setupAppMode() {
var currentMode = Defaults[.appMode] == .regular
? NSApplication.ActivationPolicy.regular
: NSApplication.ActivationPolicy.accessory
NSApp.setActivationPolicy(currentMode)
Defaults.observe(.appMode) { change in
let newMode = change.newValue == .regular
? NSApplication.ActivationPolicy.regular
: NSApplication.ActivationPolicy.accessory
if newMode != currentMode {
currentMode = newMode
NSApp.setActivationPolicy(newMode)
NSApp.activate(ignoringOtherApps: true)
if change.newValue == .regular {
DispatchQueue.main.asyncAfter(deadline: .now()) {
NSApp.unhide(self)
if let window = NSApp.windows.first {
// Verify window can become key before making it key and moving it to front
if window.canBecomeKey {
window.makeKeyAndOrderFront(self)
}
window.setIsVisible(true)
}
}
}
self.statusBarItem.isVisible = Defaults[.hideMenuBarIcon] == false && change.newValue == .menubar
}
}.tieToLifetime(of: self)
}
func setupStatusBar() {
// Set up status bar and menu
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
if let button = statusBarItem.button {
button.image = NSImage(named: "StatusBarIcon")
button.action = #selector(statusBarClicked(sender:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
statusBarMenu = getStatusBarMenu()
statusBarItem.isVisible = Defaults[.hideMenuBarIcon] == false && Defaults[.appMode] == .menubar
Defaults.observe(.hideMenuBarIcon) { change in
self.statusBarItem.isVisible = change.newValue == false && Defaults[.appMode] == .menubar
}.tieToLifetime(of: self)
}
func applicationWillFinishLaunching(_: Notification) {
NSApp.setActivationPolicy(.prohibited)
let appleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self, andSelector: #selector(handleGetURLEvent(_:withReplyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
}
func applicationDidFinishLaunching(_: Notification) {
LaunchAtLogin.migrateIfNeeded()
#if TARGET_MAS
if let mainMenu = NSApp.mainMenu?.item(withTitle: PikaText.textAppName)?.submenu {
if let checkForUpdatesMenuItem = mainMenu.item(withTitle: "\(PikaText.textMenuUpdates)…") {
mainMenu.removeItem(checkForUpdatesMenuItem)
}
}
#endif
setupAppMode()
setupStatusBar()
// Set up eyedroppers
eyedroppers = Eyedroppers()
eyedroppers.foreground.undoManager = undoManager
eyedroppers.background.undoManager = undoManager
// Define content view
let contentView = ContentView()
.environmentObject(eyedroppers)
.frame(minWidth: 480,
idealWidth: 480,
maxWidth: 650,
minHeight: 230,
idealHeight: 230,
maxHeight: 400,
alignment: .center)
pikaWindow = PikaWindow.createPrimaryWindow()
pikaWindow.contentView = NSHostingView(rootView: contentView)
pikaTouchBarController = PikaTouchBarController(window: pikaWindow)
// Define global keyboard shortcuts
KeyboardShortcuts.onKeyUp(for: .togglePika) { [] in
if Defaults[.viewedSplash] {
NSApp.sendAction(#selector(AppDelegate.triggerPickForeground), to: nil, from: nil)
}
}
// Open splash window, or main
if !Defaults[.viewedSplash] {
openSplashWindow(nil)
NSApp.activate(ignoringOtherApps: true)
}
// Configure color space
if !NSColorSpace.availableColorSpaces(with: .rgb).contains(Defaults[.colorSpace]) {
Defaults[.colorSpace] = Defaults.Keys.colorSpace.defaultValue
}
if Defaults[.alwaysShowOnLaunch] {
showPika(true)
}
}
func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows: Bool) -> Bool {
if !hasVisibleWindows {
pikaWindow.makeKeyAndOrderFront(self)
}
return true
}
func applicationSupportsSecureRestorableState(_: NSApplication) -> Bool {
true
}
// swiftlint:disable cyclomatic_complexity
// swiftlint:disable function_body_length
@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor, withReplyEvent _: NSAppleEventDescriptor) {
if let urlString = event.forKeyword(AEKeyword(keyDirectObject))?.stringValue {
let url = URL(string: urlString)
guard url != nil, let scheme = url!.scheme, let action = url!.host else {
// some error
return
}
var list = url!.pathComponents.dropFirst()
let task = list.popFirst()
let colorFormat = list.popFirst()
if scheme.caseInsensitiveCompare("pika") == .orderedSame {
if colorFormat != nil {
if let format = ColorFormat.withLabel(colorFormat!) {
Defaults[.colorFormat] = format
}
}
if action == "format" {
if let format = ColorFormat.withLabel(task!) {
Defaults[.colorFormat] = format
}
}
if action == "pick" {
if task == "foreground" {
NSApp.sendAction(#selector(AppDelegate.triggerPickForeground(_:)), to: nil, from: nil)
}
if task == "background" {
NSApp.sendAction(#selector(AppDelegate.triggerPickBackground(_:)), to: nil, from: nil)
}
}
if action == "system" {
if task == "foreground" {
NSApp.sendAction(#selector(AppDelegate.triggerSystemPickerForeground(_:)), to: nil, from: nil)
}
if task == "background" {
NSApp.sendAction(#selector(AppDelegate.triggerSystemPickerBackground(_:)), to: nil, from: nil)
}
}
if action == "copy", task == "foreground" {
NSApp.sendAction(#selector(AppDelegate.triggerCopyForeground(_:)), to: nil, from: nil)
}
if action == "copy", task == "background" {
NSApp.sendAction(#selector(AppDelegate.triggerCopyBackground(_:)), to: nil, from: nil)
}
if action == "copy", task == "text" {
NSApp.sendAction(#selector(AppDelegate.triggerCopyText(_:)), to: nil, from: nil)
}
if action == "copy", task == "json" {
NSApp.sendAction(#selector(AppDelegate.triggerCopyData(_:)), to: nil, from: nil)
}
if action == "swap" {
NSApp.sendAction(#selector(AppDelegate.triggerSwap(_:)), to: nil, from: nil)
}
if action == "undo" {
NSApp.sendAction(#selector(AppDelegate.triggerUndo(_:)), to: nil, from: nil)
}
if action == "redo" {
NSApp.sendAction(#selector(AppDelegate.triggerRedo(_:)), to: nil, from: nil)
}
}
}
}
// swiftlint:enable function_body_length
// swiftlint:enable cyclomatic_complexity
func startMainWindow() {
if !pikaWindow.isVisible {
pikaWindow.fadeIn(nil)
}
Defaults[.viewedSplash] = true
}
func showMainWindow() {
pikaWindow.makeKeyAndOrderFront(nil)
}
func hideMainWindow() {
pikaWindow.orderOut(nil)
}
@objc func closeSplashWindow() {
splashWindow.fadeOut(sender: nil, duration: 0.25, closeSelector: .close, completionHandler: startMainWindow)
}
func getStatusBarMenu() -> NSMenu {
statusBarMenu = NSMenu(title: "Status Bar Menu")
statusBarMenu.delegate = self
statusBarMenu.addItem(
withTitle: PikaText.textMenuAbout,
action: #selector(openAboutWindow(_:)),
keyEquivalent: ""
)
statusBarMenu.addItem(
withTitle: "\(PikaText.textMenuUpdates)...",
action: #selector(checkForUpdates(_:)),
keyEquivalent: ""
)
statusBarMenu.addItem(
withTitle: PikaText.textMenuGitHubIssue,
action: #selector(openGitHubIssue(_:)),
keyEquivalent: ""
)
let preferences = NSMenuItem(
title: "\(PikaText.textMenuPreferences)...",
action: #selector(openPreferencesWindow(_:)),
keyEquivalent: ","
)
preferences.keyEquivalentModifierMask = NSEvent.ModifierFlags.command
statusBarMenu.addItem(preferences)
statusBarMenu.addItem(NSMenuItem.separator())
statusBarMenu.addItem(
withTitle: PikaText.textMenuQuit,
action: #selector(terminatePika(_:)),
keyEquivalent: ""
)
return statusBarMenu
}
@objc func statusBarClicked(sender _: NSStatusBarButton) {
let event = NSApp.currentEvent
if event != nil, event!.type == NSEvent.EventType.rightMouseUp || event!.modifierFlags.contains(.control) {
statusBarItem.menu = statusBarMenu
statusBarItem.button?.performClick(nil)
} else {
togglePopover(nil)
}
}
@objc func menuDidClose(_: NSMenu) {
statusBarItem.menu = nil
}
@objc func togglePopover(_: AnyObject?) {
if pikaWindow.isVisible {
hideMainWindow()
} else {
showMainWindow()
NSApp.activate(ignoringOtherApps: true)
}
}
@IBAction func openAboutWindow(_: Any?) {
if aboutWindow == nil {
let view = NSHostingView(rootView: AboutView().edgesIgnoringSafeArea(.all))
aboutWindow = PikaWindow.createSecondaryWindow(
title: "About",
size: NSRect(x: 0, y: 0, width: 750, height: 650),
styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView]
)
aboutWindow.contentView = view
}
aboutTouchBarController = SplashTouchBarController(window: aboutWindow)
aboutWindow.makeKeyAndOrderFront(nil)
}
@IBAction func openPreferencesWindow(_: Any?) {
if preferencesWindow == nil {
let view = NSHostingView(rootView: PreferencesView()
.edgesIgnoringSafeArea(.all)
.frame(minWidth: 750,
maxWidth: .infinity,
minHeight: 0,
maxHeight: 750,
alignment: .topLeading)
.fixedSize(horizontal: false, vertical: true)
.environmentObject(eyedroppers))
preferencesWindow = PikaWindow.createSecondaryWindow(
title: "Preferences",
size: NSRect(x: 0, y: 0, width: 750, height: 750),
styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView],
maxHeight: 750,
)
preferencesWindow.contentView = view
}
preferencesWindow.makeKeyAndOrderFront(nil)
preferencesWindow.makeFirstResponder(nil)
notificationCenter.post(name: .triggerPreferences, object: self)
}
@IBAction func openSplashWindow(_: Any?) {
splashWindow = PikaWindow.createSecondaryWindow(
title: "Splash",
size: NSRect(x: 0, y: 0, width: 650, height: 380),
styleMask: [.titled, .fullSizeContentView]
)
splashWindow.title = PikaText.textAppName
splashWindow.titleVisibility = .visible
splashTouchBarController = SplashTouchBarController(window: splashWindow)
splashWindow.contentView = NSHostingView(rootView: SplashView().edgesIgnoringSafeArea(.all))
splashWindow.fadeIn(nil)
}
@IBAction func triggerPickForeground(_: Any) {
notificationCenter.post(name: .triggerPickForeground, object: self)
}
@IBAction func triggerPickBackground(_: Any) {
notificationCenter.post(name: .triggerPickBackground, object: self)
}
@IBAction func triggerCopyForeground(_: Any) {
notificationCenter.post(name: .triggerCopyForeground, object: self)
}
@IBAction func triggerCopyBackground(_: Any) {
notificationCenter.post(name: .triggerCopyBackground, object: self)
}
@IBAction func triggerSystemPickerForeground(_: Any) {
notificationCenter.post(name: .triggerSystemPickerForeground, object: self)
}
@IBAction func triggerSystemPickerBackground(_: Any) {
notificationCenter.post(name: .triggerSystemPickerBackground, object: self)
}
@IBAction func triggerSwap(_: Any) {
notificationCenter.post(name: .triggerSwap, object: self)
}
@IBAction func triggerUndo(_: Any) {
notificationCenter.post(name: .triggerUndo, object: self)
undoManager.undo()
}
@IBAction func triggerRedo(_: Any) {
notificationCenter.post(name: .triggerRedo, object: self)
undoManager.redo()
}
@IBAction func triggerCopyText(_: Any) {
notificationCenter.post(name: .triggerCopyText, object: self)
}
@IBAction func triggerCopyData(_: Any) {
notificationCenter.post(name: .triggerCopyData, object: self)
}
@IBAction func triggerFormatHex(_: Any) {
notificationCenter.post(name: .triggerFormatHex, object: self)
}
@IBAction func triggerFormatRGB(_: Any) {
notificationCenter.post(name: .triggerFormatRGB, object: self)
}
@IBAction func triggerFormatHSB(_: Any) {
notificationCenter.post(name: .triggerFormatHSB, object: self)
}
@IBAction func triggerFormatHSL(_: Any) {
notificationCenter.post(name: .triggerFormatHSL, object: self)
}
@IBAction func triggerFormatOpenGL(_: Any) {
notificationCenter.post(name: .triggerFormatOpenGL, object: self)
}
@IBAction func triggerFormatLAB(_: Any) {
notificationCenter.post(name: .triggerFormatLAB, object: self)
}
@IBAction func triggerFormatOKLCH(_: Any) {
notificationCenter.post(name: .triggerFormatOKLCH, object: self)
}
@IBAction func hidePika(_: Any) {
hideMainWindow()
}
@IBAction func showPika(_: Any) {
if pikaWindow.isVisible {
pikaWindow.makeKeyAndOrderFront(self)
} else {
pikaWindow.fadeIn(sender: nil, duration: 0.2)
}
NSApp.activate(ignoringOtherApps: true)
}
#if TARGET_SPARKLE
@IBAction func updateFeedURL(_: Any) {
SUUpdater.shared().feedURL = URL(string: PikaConstants.url())
}
@IBAction func checkForUpdates(_: Any) {
SUUpdater.shared().feedURL = URL(string: PikaConstants.url())
SUUpdater.shared()?.checkForUpdates(self)
}
#endif
#if TARGET_MAS
@IBAction func checkForUpdates(_: Any) {}
#endif
@IBAction func openWebsite(_: Any) {
NSWorkspace.shared.open(URL(string: PikaConstants.pikaWebsiteURL)!)
}
@IBAction func openGitHubIssue(_: Any) {
NSWorkspace.shared.open(URL(string: PikaConstants.gitHubIssueURL)!)
}
@IBAction func terminatePika(_: Any) {
NSApplication.shared.terminate(self)
}
}
// swiftlint:enable type_body_length
// swiftlint:enable file_length