-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathApp.swift
172 lines (153 loc) · 6.4 KB
/
App.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
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
import Cocoa
import Darwin
import LetsMove
import ShortcutRecorder
let cgsMainConnectionId = CGSMainConnectionID()
class App: NSApplication, NSApplicationDelegate {
static let name = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
static let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
static let licence = Bundle.main.object(forInfoDictionaryKey: "NSHumanReadableCopyright") as! String
static let repository = "https://github.com/lwouis/alt-tab-macos"
static var app: App!
static let shortcutMonitor = LocalShortcutMonitor()
var statusItem: NSStatusItem?
var thumbnailsPanel: ThumbnailsPanel?
var preferencesWindow: PreferencesWindow?
var feedbackWindow: FeedbackWindow?
var uiWorkShouldBeDone = true
var isFirstSummon = true
var appIsBeingUsed = false
override init() {
super.init()
delegate = self
App.app = self
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
#if !DEBUG
PFMoveToApplicationsFolderIfNecessary()
#endif
SystemPermissions.ensureAccessibilityCheckboxIsChecked()
SystemPermissions.ensureScreenRecordingCheckboxIsChecked()
Preferences.migrateOldPreferences()
Preferences.registerDefaults()
statusItem = Menubar.make()
loadMainMenuXib()
thumbnailsPanel = ThumbnailsPanel()
Spaces.initialDiscovery()
Applications.initialDiscovery()
Keyboard.listenToGlobalEvents()
preferencesWindow = PreferencesWindow()
UpdatesTab.observeUserDefaults()
// TODO: undeterministic; events in the queue may still be processing; good enough for now
DispatchQueue.main.async { Windows.sortByLevel() }
}
// keyboard shortcuts are broken without a menu. We generated the default menu from XCode and load it
// see https://stackoverflow.com/a/3746058/2249756
private func loadMainMenuXib() {
var menuObjects: NSArray?
Bundle.main.loadNibNamed("MainMenu", owner: self, topLevelObjects: &menuObjects)
menu = menuObjects?.first(where: { $0 is NSMenu }) as? NSMenu
}
// we put application code here which should be executed on init() and Preferences change
func resetPreferencesDependentComponents() {
ThumbnailsView.recycledViews = ThumbnailsView.recycledViews.map { _ in ThumbnailView() }
thumbnailsPanel!.thumbnailsView.layer!.cornerRadius = Preferences.windowCornerRadius
}
func hideUi() {
debugPrint("hideUi")
thumbnailsPanel!.orderOut(nil)
appIsBeingUsed = false
isFirstSummon = true
}
func focusTarget() {
debugPrint("focusTarget")
focusSelectedWindow(Windows.focusedWindow())
}
@objc
func checkForUpdatesNow(_ sender: NSMenuItem) {
UpdatesTab.checkForUpdatesNow(sender)
}
@objc
func showPreferencesPanel() {
Screen.repositionPanel(preferencesWindow!, Screen.preferred(), .appleCentered)
preferencesWindow?.show()
}
@objc
func showFeedbackPanel() {
if feedbackWindow == nil {
feedbackWindow = FeedbackWindow()
}
Screen.repositionPanel(feedbackWindow!, Screen.preferred(), .appleCentered)
feedbackWindow?.show()
}
@objc
func showUi() {
uiWorkShouldBeDone = true
appIsBeingUsed = true
DispatchQueue.main.async { self.showUiOrCycleSelection(0) }
}
func cycleSelection(_ step: Int) {
Windows.cycleFocusedWindowIndex(step)
}
func focusSelectedWindow(_ window: Window?) {
hideUi()
guard !CGWindow.isMissionControlActive() else { return }
window?.focus()
}
func reopenUi() {
thumbnailsPanel!.orderOut(nil)
rebuildUi()
}
func refreshOpenUi(_ windowsToRefresh: [Window]? = nil) {
guard appIsBeingUsed else { return }
windowsToRefresh?.forEach { $0.refreshThumbnail() }
guard uiWorkShouldBeDone else { return }
// workaround: when Preferences > Mission Control > "Displays have separate Spaces" is unchecked,
// switching between displays doesn't trigger .activeSpaceDidChangeNotification; we get the latest manually
Spaces.refreshCurrentSpaceId()
guard uiWorkShouldBeDone else { return }
let currentScreen = Screen.preferred() // fix screen between steps since it could change (e.g. mouse moved to another screen)
thumbnailsPanel!.thumbnailsView.updateItems(currentScreen)
guard uiWorkShouldBeDone else { return }
thumbnailsPanel!.setFrame(thumbnailsPanel!.thumbnailsView.frame, display: false)
guard uiWorkShouldBeDone else { return }
Screen.repositionPanel(thumbnailsPanel!, currentScreen, .appleCentered)
}
func showUiOrCycleSelection(_ step: Int) {
debugPrint("showUiOrCycleSelection", step)
if isFirstSummon {
debugPrint("showUiOrCycleSelection: isFirstSummon")
isFirstSummon = false
if Windows.list.count == 0 || CGWindow.isMissionControlActive() { hideUi(); return }
// TODO: find a way to update isSingleSpace by listening to space creation, instead of on every trigger
Spaces.idsAndIndexes = Spaces.allIdsAndIndexes()
// TODO: find a way to update space index when windows are moved to another space, instead of on every trigger
Windows.updateSpaces()
let screen = Screen.preferred()
Windows.refreshWhichWindowsToShowTheUser(screen)
if Windows.list.first(where: { $0.shouldShowTheUser }) == nil { hideUi(); return }
Windows.updateFocusedWindowIndex(0)
Windows.cycleFocusedWindowIndex(step)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Preferences.windowDisplayDelay) {
self.rebuildUi()
}
} else {
cycleSelection(step)
}
}
func rebuildUi() {
guard uiWorkShouldBeDone else { return }
Windows.refreshAllThumbnails()
guard uiWorkShouldBeDone else { return }
refreshOpenUi()
guard uiWorkShouldBeDone else { return }
thumbnailsPanel!.show()
// guard uiWorkShouldBeDone else { return }
// DispatchQueue.main.async {
// Windows.refreshAllExistingThumbnails()
// }
}
}