Repository navigation
Expand file tree
/
Copy pathCheckUsageApp.swift
More file actions
72 lines (66 loc) · 2.29 KB
/
Copy pathCheckUsageApp.swift
File metadata and controls
72 lines (66 loc) · 2.29 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
import AppKit
import SwiftUI
@main
struct CheckUsageApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
@StateObject private var model = AppModel.shared
@StateObject private var settings = SettingsStore.shared
var body: some Scene {
MenuBarExtra {
Button(model.panelVisible && model.settings.widgetEnabled ? L10n.t("hide_panel") : L10n.t("show_panel")) {
model.togglePanel()
}
Button(L10n.t("hotkey_toggle_detail")) {
PanelController.shared.toggleLastDetail()
}
Button(L10n.t("retry")) {
Task { await model.refresh() }
}
Divider()
Button(L10n.t("open_settings")) {
NotificationCenter.default.post(name: .checkUsageOpenSettings, object: nil)
}
Divider()
Button(L10n.t("quit")) {
NSApp.terminate(nil)
}
} label: {
MenuBarLabel(model: model, showPercent: settings.showMenuBarPercent)
}
.menuBarExtraStyle(.menu)
}
}
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.setActivationPolicy(.accessory)
AppModel.shared.start()
PanelController.shared.start()
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if AppModel.shared.settings.widgetEnabled {
AppModel.shared.panelVisible = true
PanelController.shared.applyWidgetVisibility()
} else {
PanelController.shared.toggleLastDetail()
}
return false
}
}
struct MenuBarLabel: View {
@ObservedObject var model: AppModel
var showPercent: Bool
var body: some View {
HStack(spacing: 4) {
if let snapshot = model.worstReady {
BrandIcon(id: snapshot.provider, size: 12)
} else {
Image(systemName: "circle.circle")
}
if showPercent, let snapshot = model.worstReady {
Text(String(format: "%.0f%%", snapshot.primaryPercent))
.monospacedDigit()
}
}
.accessibilityLabel(L10n.t("app_name"))
}
}