Skip to content

Fix dead macOS menu-bar icon (+ clearer right-click menu)#4789

Closed
bgoncal wants to merge 4 commits into
mainfrom
fix/macos-menu-bar-show-window
Closed

Fix dead macOS menu-bar icon (+ clearer right-click menu)#4789
bgoncal wants to merge 4 commits into
mainfrom
fix/macos-menu-bar-show-window

Conversation

@bgoncal

@bgoncal bgoncal commented Jun 19, 2026

Copy link
Copy Markdown
Member

Summary

The macOS menu-bar icon was completely unresponsive — neither left- nor right-click did anything — even though the icon rendered and the app's custom menus worked. Combines and supersedes #4787 and #4788.

Root cause (the real fix) — MacBridgeStatusItem

NSStatusBar/NSStatusItem are main-thread-only. The item was built in MacBridgeStatusItem.init, which runs inside AppEnvironment's lazy initialiser — created on the first touch of Current anywhere. Under the SwiftUI app lifecycle that first touch can be a background thread (e.g. an early Current.Log call), so the button's target/action were wired off-main and never hooked into the main-thread event machinery. The image (set later from buildMenu, which runs on main) still drew, but every click was silently dropped — the icon looked inert.

Fix: create + wire the status item lazily in ensureStatusItem(), first reached via configure(using:) on the main-thread buildMenu path. No synchronous main-thread hop inside the Current initialiser, so no deadlock risk. Plus a fallback to the button window's currentEvent so a configured item never drops a click if NSApp.currentEvent is momentarily nil.

Window activation, once clicks work — MacBridgeStatusItem callback

The status-item primary action toggled on NSApp.isActive, which in menu-bar (.accessory) mode can be true with no visible window — so the click would hide instead of show. isActive now also requires a visible standard window, and activate() brings an existing hidden/ordered-out window back via makeKeyAndOrderFront(_:).

Route a new webView scene to its WindowGroup — HAApp (was #4787)

The primary WindowGroup was the only scene missing handlesExternalEvents(matching:); added so a freshly-created .webView scene (when the window was fully closed) binds to it.

Clearer right-click menu — MenuManager (was #4788)

Replaced the ambiguous "Toggle Home Assistant" with explicit Open Home Assistant and Open Settings… (keeps ⌘,); removed the duplicate "Preferences…" status-item entry.

Screenshots

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

Regression from the SwiftUI app-lifecycle migration (#4748). New localized strings menu.status_item.open / menu.status_item.open_settings added (non-English locales sync from Lokalise). Needs verification on a real Mac — the worktree can't build the Catalyst target.

bgoncal and others added 3 commits June 19, 2026 18:11
The primary webView WindowGroup was the only scene missing
handlesExternalEvents(matching:), so a scene activation request carrying
targetContentIdentifier "ha.webview" (issued when tapping the menu-bar
item with no open window) had no WindowGroup to bind to and no window
appeared. Match the identifier here, consistent with the other groups.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The status-item right-click menu's first action was an ambiguous
"Toggle Home Assistant". Replace it with two explicit actions —
"Open Home Assistant" (activates the main window) and "Open Settings…"
(activates the Settings window, keeping the ⌘, shortcut) — so a
right-click clearly offers opening the app or its settings. The
left-click show/hide toggle behaviour is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The real reason tapping the menu-bar icon did nothing: the status-item
primary action toggled on `NSApp.isActive`, but in menu-bar (.accessory)
mode the app can be "active" with no visible window, so the click took
the hide branch and no window ever appeared — the icon looked inert.

- `isActive` now also requires a visible standard window, so a click
  shows the app whenever no window is on screen.
- `activate()` brings an existing (hidden/ordered-out) window back to the
  front via makeKeyAndOrderFront, which `NSApp.activate` alone doesn't do
  reliably in accessory mode. New windows are still created by the scene
  activation request, which the WindowGroup matcher (this branch) routes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

⚠️ Unused L10n strings detected

Found 21 unused localization strings in the codebase.

Click to see details
Parsing Strings.swift...
Found 1656 L10n strings

Reading all Swift source code...
Read 5713310 characters of Swift code

Checking for unused strings...
Checked 100/1656 strings...
Checked 200/1656 strings...
Checked 300/1656 strings...
Checked 400/1656 strings...
Checked 500/1656 strings...
Checked 600/1656 strings...
Checked 700/1656 strings...
Checked 800/1656 strings...
Checked 900/1656 strings...
Checked 1000/1656 strings...
Checked 1100/1656 strings...
Checked 1200/1656 strings...
Checked 1300/1656 strings...
Checked 1400/1656 strings...
Checked 1500/1656 strings...
Checked 1600/1656 strings...

================================================================================
UNUSED STRINGS REPORT
================================================================================

Found 21 unused strings:


ACTIONSCONFIGURATOR:
  - L10n.ActionsConfigurator.Action.createAutomation
    Key: actions_configurator.action.create_automation
    Line: 132
  - L10n.ActionsConfigurator.VisualSection.sceneDefined
    Key: actions_configurator.visual_section.scene_defined
    Line: 172
  - L10n.ActionsConfigurator.VisualSection.serverDefined
    Key: actions_configurator.visual_section.server_defined
    Line: 178

APPINTENTS:
  - L10n.AppIntents.WidgetAction.actionsParameterConfiguration
    Key: app_intents.widget_action.actions_parameter_configuration
    Line: 606

CAMERA:
  - L10n.Camera.serverNotFound
    Key: camera.server_not_found
    Line: 764
  - L10n.Camera.snapshotFailed
    Key: camera.snapshot_failed
    Line: 766

CAMERALIST:
  - L10n.CameraList.noArea
    Key: camera_list.no_area
    Line: 771
  - L10n.CameraList.searchPlaceholder
    Key: camera_list.search_placeholder
    Line: 773

CAMERAS:
  - L10n.Cameras.dragToReorder
    Key: cameras.drag_to_reorder
    Line: 829

SETTINGSDETAILS:
  - L10n.SettingsDetails.Actions.ActionsSynced.footerNoActions
    Key: settings_details.actions.actions_synced.footer_no_actions
    Line: 3842
  - L10n.SettingsDetails.Actions.Scenes.customizeAction
    Key: settings_details.actions.scenes.customize_action
    Line: 3860

URLHANDLER:
  - L10n.UrlHandler.Error.actionNotFound
    Key: url_handler.error.action_not_found
    Line: 4679

WIDGETS:
  - L10n.Widgets.Controls.Automations.placeholderTitle
    Key: widgets.controls.automations.placeholder_title
    Line: 5731
  - L10n.Widgets.Controls.Button.placeholderTitle
    Key: widgets.controls.button.placeholder_title
    Line: 5739
  - L10n.Widgets.Controls.Cover.placeholderTitle
    Key: widgets.controls.cover.placeholder_title
    Line: 5749
  - L10n.Widgets.Controls.Fan.placeholderTitle
    Key: widgets.controls.fan.placeholder_title
    Line: 5759
  - L10n.Widgets.Controls.Light.placeholderTitle
    Key: widgets.controls.light.placeholder_title
    Line: 5769
  - L10n.Widgets.Controls.Scenes.placeholderTitle
    Key: widgets.controls.scenes.placeholder_title
    Line: 5923
  - L10n.Widgets.Controls.Scripts.placeholderTitle
    Key: widgets.controls.scripts.placeholder_title
    Line: 5935
  - L10n.Widgets.Controls.Switch.placeholderTitle
    Key: widgets.controls.switch.placeholder_title
    Line: 5943

ROOT:
  - L10n.noArea
    Key: no_area
    Line: 36

================================================================================
Total unused: 21
================================================================================

To clean up these strings, manually remove them from the Localizable.strings files
and regenerate Strings.swift using SwiftGen.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes macOS (Catalyst) menu-bar status-item behavior when the app runs with .accessory activation policy, where clicking the status-item icon could fail to surface any window. It does so by improving “active” detection and window surfacing for the status-item click path, ensuring the main WindowGroup can be targeted by scene activation, and clarifying the right-click status-item menu options.

Changes:

  • Make status-item “active” detection depend on having a visible, standard (.normal) window; and explicitly bring an existing standard window to front on activation (MacBridgeStatusItem).
  • Add .handlesExternalEvents(matching:) to the primary webView WindowGroup so activateAnyScene(for: .webView) can create/route a new scene when needed (HAApp).
  • Replace the ambiguous status-item menu entry with explicit “Open Home Assistant” and “Open Settings…” and add new localized strings (MenuManager, Localizable.strings, Strings.swift).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Sources/Shared/Resources/Swiftgen/Strings.swift Adds SwiftGen accessors for the new status-item menu strings.
Sources/MacBridge/MacBridgeStatusItem.swift Updates status-item activation logic to reliably surface a standard window in menu-bar (.accessory) mode.
Sources/App/Utilities/MenuManager.swift Adjusts the status-item right-click menu to explicit “Open” actions and removes the duplicate preferences entry.
Sources/App/Resources/en.lproj/Localizable.strings Introduces the new localized strings for the status-item menu labels.
Sources/App/HAApp.swift Routes externally-activated ha.webview scenes to the primary WindowGroup via handlesExternalEvents.

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@e46f174). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4789   +/-   ##
=======================================
  Coverage        ?   44.69%           
=======================================
  Files           ?      275           
  Lines           ?    16835           
  Branches        ?        0           
=======================================
  Hits            ?     7524           
  Misses          ?     9311           
  Partials        ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Root cause of the dead menu-bar icon (both left- and right-click did
nothing even though the icon rendered and the custom menus worked):
NSStatusBar/NSStatusItem are main-thread-only, but the item was built in
MacBridgeStatusItem.init, which runs inside AppEnvironment's lazy
initialiser — created on the first touch of `Current` anywhere. Under
the SwiftUI app lifecycle that first touch can be a background thread
(e.g. an early Current.Log call), so the button's target/action were
wired off-main and never hooked into the main-thread event machinery:
the image (set later from buildMenu, on main) still drew, but clicks
were silently dropped.

Defer creation+wiring to ensureStatusItem(), first reached via
configure(using:) on the main-thread buildMenu path — no synchronous
main-thread hop during the Current initialiser, so no deadlock risk.
Also fall back to the button window's currentEvent in statusItemTapped
so a configured item never drops a click if NSApp.currentEvent is nil.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@bgoncal bgoncal changed the title Fix macOS menu-bar icon: reliably show the main window + clearer right-click menu Fix dead macOS menu-bar icon (+ clearer right-click menu) Jun 19, 2026
@bgoncal bgoncal closed this Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants