Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 4d98c6d

Browse files
committed
Extract NSStatusItem hacks
1 parent 2a64746 commit 4d98c6d

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

Aware.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
0347D59B2BAF8CB200C5741E /* NotificationCenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0347D59A2BAF8CB200C5741E /* NotificationCenterTests.swift */; };
2626
0347D59D2BAF9A3F00C5741E /* SuspendingClock+Drift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0347D59C2BAF9A3F00C5741E /* SuspendingClock+Drift.swift */; };
2727
0347D59F2BAFB97300C5741E /* BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0347D59E2BAFB97300C5741E /* BackgroundTask.swift */; };
28+
035820952BDB11880099E707 /* View+NSStatusItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035820942BDB11880099E707 /* View+NSStatusItem.swift */; platformFilters = (macos, ); };
2829
036569CE2B9A40C8003D3DCA /* MenuBarTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */; platformFilters = (macos, ); };
2930
036DA9B52B7AF52E0066B4B2 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036DA9B42B7AF52E0066B4B2 /* App.swift */; };
3031
036EBD1B1C1408C200121D0B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1A1C1408C200121D0B /* Assets.xcassets */; };
@@ -68,6 +69,7 @@
6869
0347D59A2BAF8CB200C5741E /* NotificationCenterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationCenterTests.swift; sourceTree = "<group>"; };
6970
0347D59C2BAF9A3F00C5741E /* SuspendingClock+Drift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SuspendingClock+Drift.swift"; sourceTree = "<group>"; };
7071
0347D59E2BAFB97300C5741E /* BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundTask.swift; sourceTree = "<group>"; };
72+
035820942BDB11880099E707 /* View+NSStatusItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+NSStatusItem.swift"; sourceTree = "<group>"; };
7173
036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarTimelineView.swift; sourceTree = "<group>"; };
7274
036DA9B42B7AF52E0066B4B2 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
7375
036EBD151C1408C200121D0B /* Aware.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Aware.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -164,6 +166,7 @@
164166
036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */,
165167
0341CB692B9C2CC800CC0C96 /* NSEvent+AsyncStream.swift */,
166168
032E60AD2BD74BEF008BB2B4 /* SettingsView.swift */,
169+
035820942BDB11880099E707 /* View+NSStatusItem.swift */,
167170
);
168171
path = macOS;
169172
sourceTree = "<group>";
@@ -322,6 +325,7 @@
322325
036569CE2B9A40C8003D3DCA /* MenuBarTimelineView.swift in Sources */,
323326
0341CB6A2B9C2CC800CC0C96 /* NSEvent+AsyncStream.swift in Sources */,
324327
032E60AE2BD74BEF008BB2B4 /* SettingsView.swift in Sources */,
328+
035820952BDB11880099E707 /* View+NSStatusItem.swift in Sources */,
325329
03409BC42B897F7C00EF8EE9 /* ActivityMonitor.swift in Sources */,
326330
0394D1772B84630E00FE7020 /* TimerTextView.swift in Sources */,
327331
0394D1752B845FB400FE7020 /* TimerView.swift in Sources */,

Aware/macOS/MenuBar.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct MenuBar: Scene {
2727

2828
struct TimerMenuBarLabel: View {
2929
@State private var timerState = TimerState()
30-
@State private var statusBarButton: NSStatusBarButton?
30+
@State private var statusItem: NSStatusItem?
3131

3232
// User configurable idle time in seconds (defaults to 2 minutes)
3333
@AppStorage("userIdleSeconds") private var userIdleSeconds: Int = 120
@@ -65,27 +65,14 @@ struct TimerMenuBarLabel: View {
6565
}
6666
logger.log("Finished ActivityMonitor updates task: \(timerState, privacy: .public)")
6767
}
68-
.onAppear {
69-
statusBarButton = findStatusBarItem()?.button
70-
}
68+
.bindStatusItem($statusItem)
7169
.onChange(of: timerState.isIdle) { _, isIdle in
72-
assert(statusBarButton != nil)
73-
statusBarButton?.appearsDisabled = isIdle
70+
assert(statusItem?.button != nil, "missing statusItem button")
71+
statusItem?.button?.appearsDisabled = isIdle
7472
}
7573
}
7674
}
7775

78-
// Hack to get underlying NSStatusItem for MenuBarExtra
79-
// https://github.com/orchetect/MenuBarExtraAccess/blob/main/Sources/MenuBarExtraAccess/MenuBarExtraUtils.swift
80-
@MainActor
81-
private func findStatusBarItem() -> NSStatusItem? {
82-
for window in NSApp.windows where window.className == "NSStatusBarWindow" {
83-
return window.value(forKey: "statusItem") as? NSStatusItem
84-
}
85-
assertionFailure("couldn't find NSStatusBarWindow")
86-
return nil
87-
}
88-
8976
struct MenuBarContentView: View {
9077
var body: some View {
9178
SettingsLink()

Aware/macOS/View+NSStatusItem.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// View+NSStatusItem.swift
3+
// Aware
4+
//
5+
// Created by Joshua Peek on 4/25/24.
6+
//
7+
8+
#if canImport(AppKit)
9+
10+
import AppKit
11+
import SwiftUI
12+
13+
extension View {
14+
@MainActor
15+
func bindStatusItem(_ statusItem: Binding<NSStatusItem?>) -> some View {
16+
onAppear {
17+
let statusItems = NSApp.windows.filter { window in
18+
window.className == "NSStatusBarWindow"
19+
}.compactMap { window in
20+
window.value(forKey: "statusItem") as? NSStatusItem
21+
}
22+
23+
assert(!statusItems.isEmpty, "no NSStatusItems found")
24+
assert(statusItems.count == 1, "multiple NSStatusItems found")
25+
statusItem.wrappedValue = statusItems.first
26+
}
27+
}
28+
}
29+
30+
#endif

0 commit comments

Comments
 (0)