-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
85 lines (70 loc) · 2.8 KB
/
AppDelegate.swift
File metadata and controls
85 lines (70 loc) · 2.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
import Cocoa
import IOBluetooth
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
var popover: NSPopover!
var crusher: CrusherConnection!
var rightClickMenu: NSMenu!
func applicationDidFinishLaunching(_ notification: Notification) {
// Create the status bar item
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem.button {
button.image = NSImage(systemSymbolName: "headphones", accessibilityDescription: "Crusher Control")
button.action = #selector(handleClick)
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
button.target = self
}
// Create right-click menu
rightClickMenu = NSMenu()
rightClickMenu.addItem(NSMenuItem(title: "Check for Updates...", action: #selector(checkForUpdates), keyEquivalent: ""))
rightClickMenu.addItem(NSMenuItem.separator())
rightClickMenu.addItem(NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q"))
// Create the popover
popover = NSPopover()
popover.contentSize = NSSize(width: 280, height: 360)
popover.behavior = .transient
popover.animates = true
// Create the view controller
let viewController = PopoverViewController()
popover.contentViewController = viewController
// Initialize Bluetooth connection
crusher = CrusherConnection.shared
viewController.crusher = crusher
// Try to connect on launch
crusher.connect()
// Check for updates silently on launch
UpdateChecker.checkForUpdates(silent: true)
}
@objc func handleClick(_ sender: AnyObject?) {
guard let event = NSApp.currentEvent else { return }
if event.type == .rightMouseUp {
// Right-click: show menu
if let button = statusItem.button {
rightClickMenu.popUp(positioning: nil, at: NSPoint(x: 0, y: button.bounds.height + 5), in: button)
}
} else {
// Left-click: toggle popover
togglePopover(sender)
}
}
func togglePopover(_ sender: AnyObject?) {
if let button = statusItem.button {
if popover.isShown {
popover.performClose(sender)
} else {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
NSApp.activate(ignoringOtherApps: true)
}
}
}
@objc func checkForUpdates() {
UpdateChecker.checkForUpdates(silent: false)
}
@objc func quitApp() {
crusher.disconnect()
NSApp.terminate(nil)
}
func applicationWillTerminate(_ notification: Notification) {
crusher.disconnect()
}
}