-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
90 lines (72 loc) · 2.23 KB
/
AppDelegate.swift
File metadata and controls
90 lines (72 loc) · 2.23 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
//
// AppDelegate.swift
// XcodeProjects
//
// Created by Dima Kalachniuk on 06/03/2020.
// Copyright © 2020 com.dkcompany.xcodeprojects. All rights reserved.
//
import Cocoa
var statusItem: NSStatusItem?
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
private lazy var popover = NSPopover()
private var eventMonitor: EventMonitor?
private let preferences = Preferences()
private lazy var statusController: StatusViewController = {
StatusViewController(preferences: preferences)
}()
var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
updateButton()
popover.contentViewController = statusController
eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown]) { [weak self] event in
guard let self = self else { return }
guard self.popover.isShown else { return }
self.closePopover(sender: event)
}
}
}
// MARK: - UI
private extension AppDelegate {
func updateButton() {
guard let button = statusItem.button else {
return
}
button.image = NSImage(named: "terminal")
button.image?.size = NSSize(width: 15, height: 15)
button.action = #selector(togglePopover)
}
}
// MARK: - Actions
extension AppDelegate {
static func closePopover() {
if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
appDelegate.closePopover(sender: nil)
}
}
@objc
func togglePopover(_ sender: Any?) {
if popover.isShown {
closePopover(sender: sender)
} else {
showPopover(sender: sender)
}
}
func showPopover(sender: Any?) {
guard let button = statusItem.button else {
return
}
eventMonitor?.start()
popover.show(
relativeTo: button.bounds,
of: button,
preferredEdge: NSRectEdge.minY
)
NSApp.activate(ignoringOtherApps: true)
}
func closePopover(sender: Any?) {
popover.performClose(sender)
eventMonitor?.stop()
}
}