-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatusBarLayoutTest.swift
260 lines (212 loc) · 9.74 KB
/
StatusBarLayoutTest.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import Cocoa
import SwiftUI
/*
This file contains a test harness for status bar layout testing.
It creates a window that shows all possible status bar layout combinations,
allowing us to verify the layout is correct before integrating into the app.
*/
@available(macOS 11.0, *)
class StatusBarLayoutTest: NSObject {
var window: NSWindow?
// Test all layout combinations
func runTest() {
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 600, height: 400),
styleMask: [.titled, .closable, .resizable],
backing: .buffered,
defer: false
)
window?.title = "Status Bar Layout Test"
let contentView = NSView()
window?.contentView = contentView
// Create different combinations to test
let testConfigs = [
// Test 1: No text, just pie
TestConfig(showMinutes: false, showSeconds: false, usePieChart: true, progress: 0.7),
// Test 2: No text, just bar
TestConfig(showMinutes: false, showSeconds: false, usePieChart: false, progress: 0.7),
// Test 3: Minutes only, pie
TestConfig(showMinutes: true, showSeconds: false, usePieChart: true, progress: 0.7),
// Test 4: Seconds only, pie
TestConfig(showMinutes: false, showSeconds: true, usePieChart: true, progress: 0.7),
// Test 5: Both minutes and seconds, pie
TestConfig(showMinutes: true, showSeconds: true, usePieChart: true, progress: 0.7),
// Test 6: Minutes only, bar
TestConfig(showMinutes: true, showSeconds: false, usePieChart: false, progress: 0.7),
// Test 7: Seconds only, bar
TestConfig(showMinutes: false, showSeconds: true, usePieChart: false, progress: 0.7),
// Test 8: Both minutes and seconds, bar
TestConfig(showMinutes: true, showSeconds: true, usePieChart: false, progress: 0.7)
]
// Create a vertical layout for all test cases
let outerStackView = NSStackView()
outerStackView.orientation = .vertical
outerStackView.alignment = .leading
outerStackView.spacing = 20
outerStackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(outerStackView)
NSLayoutConstraint.activate([
outerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
outerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
outerStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
outerStackView.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -20)
])
// Add each test configuration
for (index, config) in testConfigs.enumerated() {
let rowStack = NSStackView()
rowStack.orientation = .horizontal
rowStack.spacing = 10
// Add label describing the test case
let label = NSTextField(labelWithString: "Test \(index + 1): \(config.description)")
label.preferredMaxLayoutWidth = 200
label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
rowStack.addArrangedSubview(label)
// Add a visual separator
let separator = NSBox()
separator.boxType = .separator
rowStack.addArrangedSubview(separator)
// Create the actual status bar mockup
let mockupView = createStatusBarMockup(with: config)
rowStack.addArrangedSubview(mockupView)
outerStackView.addArrangedSubview(rowStack)
}
window?.center()
window?.makeKeyAndOrderFront(nil)
}
// Create a mockup of the status bar with the given configuration
private func createStatusBarMockup(with config: TestConfig) -> NSView {
// Create a container that mimics a status bar item
let container = NSView()
container.wantsLayer = true
container.layer?.backgroundColor = NSColor.darkGray.cgColor
container.layer?.cornerRadius = 4
// Set fixed height to match menu bar
let height: CGFloat = 22
NSLayoutConstraint.activate([
container.heightAnchor.constraint(equalToConstant: height)
])
// Width depends on content
if !config.showMinutes && !config.showSeconds {
NSLayoutConstraint.activate([
container.widthAnchor.constraint(equalToConstant: 22)
])
} else {
NSLayoutConstraint.activate([
container.widthAnchor.constraint(equalToConstant: 60)
])
}
// IMPLEMENTATION OF STATUS BAR LAYOUT GOES HERE
// This should match exactly what we plan to use in MenuBarApp.swift
let stackView = NSStackView()
stackView.orientation = .horizontal
stackView.spacing = 4
// Set spacing based on what text is shown
if config.showMinutes && config.showSeconds {
stackView.spacing = 4
} else if config.showMinutes || config.showSeconds {
stackView.spacing = 2
} else {
stackView.spacing = 0
}
// Add stack view to container
container.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
// THIS IS THE CRITICAL PART - Positioning
NSLayoutConstraint.activate([
// Center vertically
stackView.centerYAnchor.constraint(equalTo: container.centerYAnchor),
// Horizontal positioning
stackView.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 3),
stackView.trailingAnchor.constraint(lessThanOrEqualTo: container.trailingAnchor, constant: -3)
])
// --- CREATE CHART VIEW ---
let chartSize = config.usePieChart ? NSSize(width: 16, height: 16) : NSSize(width: 8, height: 16)
let chartView = NSView(frame: NSRect(origin: .zero, size: chartSize))
chartView.wantsLayer = true
if config.usePieChart {
// Create pie chart
let bgLayer = CAShapeLayer()
bgLayer.path = CGPath(ellipseIn: CGRect(origin: .zero, size: chartSize), transform: nil)
bgLayer.fillColor = NSColor.systemGray.cgColor
chartView.layer = CALayer()
chartView.layer?.addSublayer(bgLayer)
// Progress indicator
if config.progress > 0 {
let progressLayer = CAShapeLayer()
let angle = 2 * .pi * config.progress
let path = CGMutablePath()
path.move(to: CGPoint(x: chartSize.width/2, y: chartSize.height/2))
path.addLine(to: CGPoint(x: chartSize.width/2, y: 0))
path.addArc(center: CGPoint(x: chartSize.width/2, y: chartSize.height/2),
radius: chartSize.width/2,
startAngle: -.pi/2,
endAngle: angle - .pi/2,
clockwise: true)
path.closeSubpath()
progressLayer.path = path
progressLayer.fillColor = NSColor.systemRed.cgColor
chartView.layer?.addSublayer(progressLayer)
}
} else {
// Create bar chart
let bgLayer = CAShapeLayer()
bgLayer.path = CGPath(roundedRect: CGRect(origin: .zero, size: chartSize),
cornerWidth: 2, cornerHeight: 2, transform: nil)
bgLayer.fillColor = NSColor.systemGray.cgColor
chartView.layer = CALayer()
chartView.layer?.addSublayer(bgLayer)
// Progress indicator
if config.progress > 0 {
let progressHeight = max(1, chartSize.height * config.progress)
let progressRect = CGRect(x: 0, y: 0, width: chartSize.width, height: progressHeight)
let progressLayer = CAShapeLayer()
progressLayer.path = CGPath(roundedRect: progressRect,
cornerWidth: 2, cornerHeight: 2, transform: nil)
progressLayer.fillColor = NSColor.systemRed.cgColor
chartView.layer?.addSublayer(progressLayer)
}
}
// Add chart to stack view
stackView.addArrangedSubview(chartView)
// Add text if needed
if config.showMinutes || config.showSeconds {
let timeString: String
if config.showMinutes && config.showSeconds {
timeString = "25:00"
} else if config.showMinutes {
timeString = "25m"
} else {
timeString = "1500s"
}
let textField = NSTextField(labelWithString: timeString)
textField.font = NSFont.monospacedDigitSystemFont(ofSize: 12, weight: .medium)
textField.textColor = NSColor.white
textField.isBezeled = false
textField.drawsBackground = false
textField.isEditable = false
stackView.addArrangedSubview(textField)
}
return container
}
}
// Configuration for each test case
struct TestConfig {
let showMinutes: Bool
let showSeconds: Bool
let usePieChart: Bool
let progress: Double
var description: String {
var parts: [String] = []
if showMinutes && showSeconds {
parts.append("Minutes+Seconds")
} else if showMinutes {
parts.append("Minutes only")
} else if showSeconds {
parts.append("Seconds only")
} else {
parts.append("No text")
}
parts.append(usePieChart ? "Pie chart" : "Bar chart")
return parts.joined(separator: ", ")
}
}