forked from nikitabobko/AeroSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayMenuModel.swift
More file actions
160 lines (148 loc) · 5.54 KB
/
TrayMenuModel.swift
File metadata and controls
160 lines (148 loc) · 5.54 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
import AppKit
import Common
public final class TrayMenuModel: ObservableObject {
@MainActor public static let shared = TrayMenuModel()
private init() {}
@Published var trayText: String = ""
@Published var trayItems: [TrayItem] = []
/// Is "layouting" enabled
@Published var isEnabled: Bool = true
@Published var workspaces: [WorkspaceViewModel] = []
@Published var experimentalUISettings: ExperimentalUISettings = ExperimentalUISettings()
@Published var sponsorshipMessage: String = sponsorshipPrompts.randomElement().orDie()
}
@MainActor func updateTrayText() {
let sortedMonitors = sortedMonitors
let focus = focus
TrayMenuModel.shared.trayText = (activeMode?.takeIf { $0 != mainModeId }?.first.map { "[\($0.uppercased())] " } ?? "") +
sortedMonitors
.map {
let hasFullscreenWindows = $0.activeWorkspace.allLeafWindowsRecursive.contains { $0.isFullscreen }
let activeWorkspaceName = hasFullscreenWindows ? "(\($0.activeWorkspace.name))" : $0.activeWorkspace.name
return ($0.activeWorkspace == focus.workspace && sortedMonitors.count > 1 ? "*" : "") + activeWorkspaceName
}
.joined(separator: " │ ")
TrayMenuModel.shared.workspaces = Workspace.all.map {
let apps = $0.allLeafWindowsRecursive.map { $0.app.name?.takeIf { !$0.isEmpty } }.filterNotNil().toSet()
let dash = " - "
let suffix = switch true {
case !apps.isEmpty: dash + apps.sorted().joinTruncating(separator: ", ", length: 25)
case $0.isVisible: dash + $0.workspaceMonitor.name
default: ""
}
let hasFullscreenWindows = $0.allLeafWindowsRecursive.contains { $0.isFullscreen }
let appViewModels: [AppViewModel]
if TrayMenuModel.shared.experimentalUISettings.displayStyle == .i3OrderedWithAppIcons {
let focusedWindowId = focus.windowOrNil?.windowId
appViewModels = $0.allLeafWindowsRecursive.map { window in
AppViewModel(
windowId: window.windowId,
name: window.app.name ?? "Unknown",
icon: resolveAppIcon(for: window),
isFocused: window.windowId == focusedWindowId,
)
}
} else {
appViewModels = []
}
return WorkspaceViewModel(
name: $0.name,
suffix: suffix,
isFocused: focus.workspace == $0,
isEffectivelyEmpty: $0.isEffectivelyEmpty,
isVisible: $0.isVisible,
hasFullscreenWindows: hasFullscreenWindows,
apps: appViewModels,
)
}
var items = sortedMonitors.map {
let hasFullscreenWindows = $0.activeWorkspace.allLeafWindowsRecursive.contains { $0.isFullscreen }
return TrayItem(
type: .workspace,
name: $0.activeWorkspace.name,
isActive: $0.activeWorkspace == focus.workspace,
hasFullscreenWindows: hasFullscreenWindows,
)
}
let mode = activeMode?.takeIf { $0 != mainModeId }?.first.map {
TrayItem(type: .mode, name: $0.uppercased(), isActive: true, hasFullscreenWindows: false)
}
if let mode {
items.insert(mode, at: 0)
}
TrayMenuModel.shared.trayItems = items
}
struct WorkspaceViewModel: Hashable {
let name: String
let suffix: String
let isFocused: Bool
let isEffectivelyEmpty: Bool
let isVisible: Bool
let hasFullscreenWindows: Bool
let apps: [AppViewModel]
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
struct AppViewModel: Identifiable, Equatable {
let windowId: UInt32
let name: String
let icon: NSImage
let isFocused: Bool
var id: UInt32 { windowId }
static func == (lhs: AppViewModel, rhs: AppViewModel) -> Bool {
lhs.windowId == rhs.windowId && lhs.isFocused == rhs.isFocused
}
}
enum TrayItemType: String, Hashable {
case mode
case workspace
}
private let validLetters = "A" ... "Z"
struct TrayItem: Hashable, Identifiable {
let type: TrayItemType
let name: String
let isActive: Bool
let hasFullscreenWindows: Bool
var systemImageName: String? {
// System image type is only valid for numbers 0 to 50 and single capital char workspace name
if let number = Int(name) {
guard number >= 0 && number <= 50 else { return nil }
} else if name.count == 1 {
guard validLetters.contains(name) else { return nil }
} else {
return nil
}
let lowercasedName = name.lowercased()
switch type {
case .mode:
return "\(lowercasedName).circle"
case .workspace:
if isActive {
return "\(lowercasedName).square.fill"
} else {
return "\(lowercasedName).square"
}
}
}
var id: String {
return type.rawValue + name
}
}
@MainActor
private func resolveAppIcon(for window: Window) -> NSImage {
if let macApp = window.app as? MacApp {
if let icon = macApp.nsApp.icon {
return icon
}
if let bundlePath = macApp.bundlePath {
return NSWorkspace.shared.icon(forFile: bundlePath)
}
if let bundleId = macApp.rawAppBundleId,
let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleId)
{
return NSWorkspace.shared.icon(forFile: url.path)
}
}
return NSImage(named: NSImage.applicationIconName) ?? NSImage()
}