forked from nikitabobko/AeroSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuBarLabel.swift
More file actions
209 lines (194 loc) · 7.6 KB
/
MenuBarLabel.swift
File metadata and controls
209 lines (194 loc) · 7.6 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Common
import Foundation
import SwiftUI
@MainActor
struct MenuBarLabel: View {
@Environment(\.colorScheme) var menuColorScheme: ColorScheme
@EnvironmentObject var viewModel: TrayMenuModel
let color: Color?
let style: MenuBarStyle?
let hStackSpacing = CGFloat(6)
let itemSize = CGFloat(40)
let itemBorderSize = CGFloat(3)
let itemPadding = CGFloat(8)
let itemCornerRadius = CGFloat(6)
private var finalColor: Color {
return color ?? (menuColorScheme == .dark ? Color.white : Color.black)
}
init(style: MenuBarStyle? = nil, color: Color? = nil) {
self.style = style
self.color = color
}
var body: some View {
if #available(macOS 14, *) { // https://github.com/nikitabobko/AeroSpace/issues/1122
let renderer = ImageRenderer(content: menuBarContent)
if let cgImage = renderer.cgImage {
// Using scale: 1 results in a blurry image for unknown reasons
Image(cgImage, scale: 2, label: Text(viewModel.trayText))
} else {
// In case image can't be rendered fallback to plain text
Text(viewModel.trayText)
}
} else { // macOS 13 and lower
Text(viewModel.trayText)
}
}
var menuBarContent: some View {
return HStack(spacing: hStackSpacing) {
let style = style ?? viewModel.experimentalUISettings.displayStyle
switch style {
case .monospacedText: getText(for: .monospaced)
case .systemText: getText(for: .default)
case .squares: squares
case .i3:
squares
let workspaces = viewModel.workspaces.filter { !$0.isEffectivelyEmpty && !$0.isVisible }
if !workspaces.isEmpty {
otherWorkspaces(with: workspaces)
}
case .i3Ordered:
orderedWorkspacesView(showApps: false)
case .i3OrderedWithAppIcons:
orderedWorkspacesView(showApps: true)
}
}
}
private func getText(for design: Font.Design) -> some View {
Text(viewModel.trayText)
.font(.system(.largeTitle, design: design))
.foregroundStyle(finalColor)
}
private var squares: some View {
ForEach(viewModel.trayItems, id: \.id) { item in
itemView(for: item)
if item.type == .mode {
modeSeparator(with: .monospaced)
}
}
}
private func otherWorkspaces(with otherWorkspaces: [WorkspaceViewModel]) -> some View {
Group {
Text("|")
.font(.system(.largeTitle))
.foregroundStyle(finalColor)
.bold()
.padding(.bottom, 6)
ForEach(otherWorkspaces, id: \.name) { item in
itemView(for: TrayItem(type: .workspace, name: item.name, isActive: false, hasFullscreenWindows: item.hasFullscreenWindows))
}
}
.opacity(0.6)
}
private func modeSeparator(with design: Font.Design) -> some View {
Text(":")
.font(.system(.largeTitle, design: design))
.foregroundStyle(finalColor)
.bold()
}
@ViewBuilder
fileprivate func itemView(for item: TrayItem) -> some View {
let view = itemSubView(for: item)
if item.hasFullscreenWindows {
let strokeStyle = StrokeStyle(lineWidth: 2, lineCap: .square, lineJoin: .miter, miterLimit: 10, dash: [10, 5], dashPhase: 3)
view
.padding(4)
.overlay {
RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous)
.strokeBorder(finalColor, style: strokeStyle)
}
} else {
view
}
}
@ViewBuilder
fileprivate func itemSubView(for item: TrayItem) -> some View {
// If workspace name contains emojis we use the plain emoji in text to avoid visibility issues scaling the emoji to fit the squares
if item.name.containsEmoji() {
Text(item.name)
.font(.system(.largeTitle))
.foregroundStyle(finalColor)
.frame(height: itemSize)
} else {
if let imageName = item.systemImageName {
Image(systemName: imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.symbolRenderingMode(.monochrome)
.foregroundStyle(finalColor)
.frame(width: itemSize, height: itemSize)
} else {
let text = Text(item.name)
.font(.system(.largeTitle))
.bold()
.padding(.horizontal, itemBorderSize * 2)
.frame(height: itemSize)
if item.isActive {
ZStack {
text.background {
RoundedRectangle(cornerRadius: itemCornerRadius, style: .circular)
}
text.blendMode(.destinationOut)
}
.compositingGroup()
.foregroundStyle(finalColor)
.frame(height: itemSize)
} else {
text.background {
RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous)
.strokeBorder(lineWidth: itemBorderSize)
}
.foregroundStyle(finalColor)
.frame(height: itemSize)
}
}
}
}
private func orderedWorkspacesView(showApps: Bool) -> some View {
let modeItem = viewModel.trayItems.first { $0.type == .mode }
let orderedWorkspaces = viewModel.workspaces.filter { !$0.isEffectivelyEmpty || $0.isVisible }
return Group {
if let modeItem {
itemView(for: modeItem)
modeSeparator(with: .monospaced)
}
ForEach(orderedWorkspaces, id: \.name) { ws in
let trayItem = TrayItem(
type: .workspace,
name: ws.name,
isActive: ws.isFocused,
hasFullscreenWindows: ws.hasFullscreenWindows,
)
itemView(for: trayItem)
.opacity(ws.isVisible ? 1 : 0.5)
if showApps {
let limit = style != nil ? (ws.isFocused ? 1 : 0) : ws.apps.count
ForEach(ws.apps.prefix(limit)) { app in
appIconView(for: app)
.opacity(ws.isFocused && app.isFocused ? 1 : 0.5)
}
}
}
}
}
@ViewBuilder
private func appIconView(for app: AppViewModel) -> some View {
let icon = Image(nsImage: app.icon)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: itemSize, height: itemSize)
if app.isFocused {
icon.clipShape(RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous)
.strokeBorder(finalColor, lineWidth: 1)
}
} else {
icon
}
}
}
extension String {
fileprivate func containsEmoji() -> Bool {
unicodeScalars.contains { $0.properties.isEmoji && $0.properties.isEmojiPresentation }
}
}