-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuBarView.swift
More file actions
316 lines (275 loc) · 10.3 KB
/
Copy pathMenuBarView.swift
File metadata and controls
316 lines (275 loc) · 10.3 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/// # MenuBarView — Menubar Popover
///
/// A compact 300px-wide popover displayed from the menubar extra. Provides
/// at-a-glance disk status and one-tap cleanup without opening the full window.
///
/// ## Layout Structure
///
/// ```
/// ┌──────────────────────────┐
/// │ [Gauge] Macintosh HD │
/// │ XX GB available │
/// ├──────────────────────────┤
/// │ Recoverable │ Categories │
/// ├──────────────────────────┤
/// │ Top 5 categories by size │
/// ├──────────────────────────┤
/// │ Scanned X min ago │
/// │ Docker Prune [Run] │
/// ├──────────────────────────┤
/// │ [Scan] [Quick Clean] [⊞] │
/// └──────────────────────────┘
/// ```
///
/// ## Gauge Colors
///
/// - Blue: < 85% used (normal)
/// - Orange: 85-95% used (warning)
/// - Red: > 95% used (critical)
///
/// ## Auto-Scan
///
/// Automatically triggers a scan when the popover opens if data is stale
/// (no previous results or older than the scan interval).
import SwiftUI
/// Compact menubar popover showing disk status and quick-clean options.
struct MenuBarView: View {
@EnvironmentObject var viewModel: CacheoutViewModel
@Environment(\.openWindow) private var openWindow
var body: some View {
VStack(spacing: 0) {
// Header with disk gauge
diskHeader
.padding(.horizontal, 14)
.padding(.top, 14)
.padding(.bottom, 10)
Divider()
// Quick stats or scan prompt
if viewModel.hasResults {
quickStats
.padding(.horizontal, 14)
.padding(.vertical, 10)
Divider()
// Top reclaimable categories (max 5)
topCategories
.padding(.vertical, 6)
Divider()
}
// Last scanned timestamp
if let lastScanned = viewModel.lastScanDate {
HStack {
Text("Scanned \(lastScanned, style: .relative) ago")
.font(.caption2)
.foregroundStyle(.tertiary)
Spacer()
}
.padding(.horizontal, 14)
.padding(.top, 6)
}
// Docker prune
dockerRow
.padding(.horizontal, 14)
.padding(.vertical, 6)
Divider()
// Action buttons
actionBar
.padding(.horizontal, 14)
.padding(.vertical, 10)
}
.frame(width: 300)
.task(id: viewModel.scanGeneration) {
// Refresh disk info every time the popover appears or scan completes
// scanGeneration increments on each scan, so this re-fires when data changes
}
.task {
// Auto-scan on popover open if stale (no results or >5 min old)
if viewModel.shouldAutoRescan {
await viewModel.scan()
}
}
}
// MARK: - Disk Header
private var diskHeader: some View {
HStack(spacing: 12) {
// Circular gauge
ZStack {
Circle()
.stroke(Color(.separatorColor), lineWidth: 6)
Circle()
.trim(from: 0, to: viewModel.diskInfo?.usedPercentage ?? 0)
.stroke(gaugeColor, style: StrokeStyle(lineWidth: 6, lineCap: .round))
.rotationEffect(.degrees(-90))
VStack(spacing: 0) {
Text("\(Int((viewModel.diskInfo?.usedPercentage ?? 0) * 100))")
.font(.system(size: 18, weight: .bold, design: .rounded))
Text("%")
.font(.system(size: 9, weight: .medium))
.foregroundStyle(.secondary)
}
}
.frame(width: 52, height: 52)
VStack(alignment: .leading, spacing: 3) {
Text("Macintosh HD")
.font(.headline)
if let disk = viewModel.diskInfo {
Text("\(disk.formattedFree) available")
.font(.subheadline)
.foregroundStyle(.secondary)
} else {
Text("Checking...")
.font(.subheadline)
.foregroundStyle(.tertiary)
}
}
Spacer()
}
}
// MARK: - Quick Stats
private var quickStats: some View {
HStack {
statPill(
label: "Recoverable",
value: ByteCountFormatter.sharedFile.string(fromByteCount: viewModel.totalRecoverable),
color: .orange
)
Spacer()
statPill(
label: "Categories",
// ⚡ Bolt: Use .lazy.filter to prevent intermediate array allocation for .count
value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)",
color: .blue
)
}
}
private func statPill(label: String, value: String, color: Color) -> some View {
VStack(spacing: 2) {
Text(value)
.font(.system(.title3, design: .rounded).bold())
.foregroundStyle(color)
Text(label)
.font(.caption2)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 6)
.background(color.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
.accessibilityElement(children: .combine)
}
// MARK: - Top Categories
private var topCategories: some View {
// ⚡ Bolt: Filter eagerly before sorting, as sorting forces full array materialization
let top = viewModel.scanResults
.filter { !$0.isEmpty }
.sorted { $0.sizeBytes > $1.sizeBytes }
.prefix(5)
return VStack(spacing: 0) {
ForEach(Array(top)) { result in
HStack(spacing: 8) {
Image(systemName: result.category.icon)
.font(.caption)
.foregroundStyle(riskColor(result.category.riskLevel))
.frame(width: 18)
Text(result.category.name)
.font(.caption)
.lineLimit(1)
Spacer()
Text(result.formattedSize)
.font(.caption.monospacedDigit())
.foregroundStyle(.secondary)
}
.padding(.horizontal, 14)
.padding(.vertical, 5)
}
}
}
// MARK: - Action Bar
private var actionBar: some View {
HStack(spacing: 8) {
// Rescan
Button {
Task { await viewModel.scan() }
} label: {
Label("Scan", systemImage: "arrow.clockwise")
.font(.caption.weight(.medium))
}
.buttonStyle(.bordered)
.disabled(viewModel.isScanning)
.help(viewModel.isScanning ? "Scan in progress" : "Scan for caches")
if viewModel.isScanning {
ProgressView()
.scaleEffect(0.6)
.frame(width: 16, height: 16)
}
Spacer()
// Smart Clean button
Button {
Task { await viewModel.smartClean() }
} label: {
Label(
viewModel.isCleaning ? "Cleaning..." : "Quick Clean",
systemImage: "sparkles"
)
.font(.caption.weight(.medium))
}
.buttonStyle(.borderedProminent)
.tint(Color(red: 0.85, green: 0.45, blue: 0.1)) // burnt orange — readable white text
.disabled(viewModel.totalRecoverable == 0 || viewModel.isCleaning)
.help(viewModel.isCleaning ? "Cleanup in progress" : (viewModel.totalRecoverable == 0 ? "Nothing to clean" : "Quick clean recoverable items"))
// Open main window
Button {
openWindow(id: "main")
} label: {
Image(systemName: "macwindow")
.font(.caption)
}
.buttonStyle(.bordered)
.help("Open full window")
.accessibilityLabel("Open full window")
}
}
// MARK: - Docker Row
private var dockerRow: some View {
HStack(spacing: 8) {
Image(systemName: "cube.transparent")
.font(.caption)
.foregroundStyle(.secondary)
.frame(width: 18)
Text("Docker Prune")
.font(.caption)
Spacer()
if viewModel.isDockerPruning {
ProgressView()
.scaleEffect(0.5)
.frame(width: 14, height: 14)
} else if let result = viewModel.lastDockerPruneResult {
Text(result.contains("reclaimed") ? "✓" : "—")
.font(.caption2)
.foregroundStyle(.secondary)
}
Button {
Task { await viewModel.dockerPrune() }
} label: {
Text("Run")
.font(.caption2.weight(.medium))
}
.buttonStyle(.bordered)
.controlSize(.mini)
.disabled(viewModel.isDockerPruning)
.help(viewModel.isDockerPruning ? "Pruning in progress" : "Run Docker prune")
}
}
// MARK: - Helpers
private var gaugeColor: Color {
guard let pct = viewModel.diskInfo?.usedPercentage else { return .blue }
if pct > 0.95 { return .red }
if pct > 0.85 { return .orange }
return .blue
}
private func riskColor(_ level: RiskLevel) -> Color {
switch level {
case .safe: return .green
case .review: return .yellow
case .caution: return .red
}
}
}