-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Events.swift
58 lines (50 loc) 路 1.55 KB
/
Events.swift
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
import SwiftUI
import KeyboardShortcuts
import LaunchAtLogin
extension AppState {
func setUpEvents() {
Defaults.publisher(.showInMenuBar)
.receive(on: DispatchQueue.main)
.sink { [self] in
// We only set the state if it's in Dock mode or menu bar mode showing the icon.
if !$0.newValue || ($0.newValue && !Defaults[.hideMenuBarIcon]) {
statusItem.isVisible = $0.newValue
}
SSApp.isDockIconVisible = !$0.newValue
NSApp.activate(ignoringOtherApps: true)
if !$0.newValue {
LaunchAtLogin.isEnabled = false
colorPanel.makeKeyAndOrderFront(nil)
}
}
.store(in: &cancellables)
Defaults.publisher(.stayOnTop)
.receive(on: DispatchQueue.main)
.sink { [weak self] in
self?.colorPanel.level = $0.newValue ? .floating : .normal
}
.store(in: &cancellables)
Defaults.publisher(.hideMenuBarIcon, options: [])
.receive(on: DispatchQueue.main)
.sink { [self] in
statusItem.isVisible = !$0.newValue
}
.store(in: &cancellables)
KeyboardShortcuts.onKeyUp(for: .pickColor) { [self] in
pickColor()
}
KeyboardShortcuts.onKeyUp(for: .toggleWindow) { [self] in
colorPanel.toggle()
}
// Workaround for the color picker window not becoming active after the settings window closes. (macOS 11.3)
NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)
.sink { [self] _ in
DispatchQueue.main.async { [self] in
if colorPanel.isVisible, SSApp.settingsWindow?.isVisible != true {
colorPanel.makeKeyAndOrderFront(nil)
}
}
}
.store(in: &cancellables)
}
}