Skip to content

Commit aae751f

Browse files
authored
Fix macOS menu bar text not updating after connection loss (#4106)
1 parent 4f0b4ec commit aae751f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/App/AppDelegate.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5050
}
5151
}
5252

53+
private var shouldRefreshTitleSubscription = false
54+
5355
private var watchCommunicatorService: WatchCommunicatorService?
5456

5557
func application(
@@ -133,9 +135,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
133135

134136
#if targetEnvironment(macCatalyst)
135137
titleSubscription = manager.subscribeStatusItemTitle(
136-
existing: titleSubscription,
138+
existing: shouldRefreshTitleSubscription ? nil : titleSubscription,
137139
update: Current.macBridge.configureStatusItem(title:)
138140
)
141+
shouldRefreshTitleSubscription = false
139142
#endif
140143
}
141144
}
@@ -400,12 +403,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
400403
name: SettingsStore.menuRelatedSettingDidChange,
401404
object: nil
402405
)
406+
NotificationCenter.default.addObserver(
407+
self,
408+
selector: #selector(apiDidConnect(_:)),
409+
name: HomeAssistantAPI.didConnectNotification,
410+
object: nil
411+
)
403412
}
404413

405414
@objc private func menuRelatedSettingDidChange(_ note: Notification) {
406415
UIMenuSystem.main.setNeedsRebuild()
407416
}
408417

418+
@objc private func apiDidConnect(_ note: Notification) {
419+
// When API reconnects, rebuild the menu to refresh the status item title subscription
420+
// Force refresh by setting the flag that will cause the subscription to be recreated
421+
#if targetEnvironment(macCatalyst)
422+
shouldRefreshTitleSubscription = true
423+
#endif
424+
UIMenuSystem.main.setNeedsRebuild()
425+
}
426+
409427
private func setupUIApplicationShortcutItems() {
410428
if Current.isCatalyst {
411429
UIApplication.shared.shortcutItems = [.init(

Sources/App/Utilities/MenuManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class MenuManager {
8181
return existing
8282
}
8383

84+
// Cancel the old subscription before creating a new one
85+
existing?.cancel()
86+
8487
// if we know it's going to change, reset it for now so it doesn't show the old value
8588
update("")
8689

0 commit comments

Comments
 (0)