Skip to content

fix: update Linux tray icon in place on OS theme change#515

Open
IliyaBrook wants to merge 1 commit intoaaddrick:mainfrom
IliyaBrook:fix/tray-duplicate-icon-on-theme-change
Open

fix: update Linux tray icon in place on OS theme change#515
IliyaBrook wants to merge 1 commit intoaaddrick:mainfrom
IliyaBrook:fix/tray-duplicate-icon-on-theme-change

Conversation

@IliyaBrook
Copy link
Copy Markdown
Contributor

@IliyaBrook IliyaBrook commented Apr 24, 2026

What's in this PR

As asked in the #491 review:

  • patch_tray_inplace_update in scripts/patches/tray.sh — fast-path that updates the existing StatusNotifierItem in place via setImage + setContextMenu instead of destroy + recreate
  • scripts/patches/app-asar.sh wiring — calls the new patch between patch_tray_icon_selection and patch_menu_bar_default
  • docs/learnings/tray-rebuild-race.md — scoped-down learnings covering the race, the fix, and the trigger surface. The Appearance → Theme note you asked for is folded in: Colors / Plasma Style / Global Theme dropdowns all fire the same nativeTheme.on('updated') and all reproduce the duplicate icon.
  • CLAUDE.md learnings index entry

Addressing the review blocker from #491

  • Dynamic extraction of t and e — both literal minified locals called out as a blocker are now extracted alongside tray_var / menu_func / electron_var: path_var from the new Tray(nativeImage.createFromPath(X)) call, enabled_var via the same pattern as patch_menu_bar_default.
  • Idempotency guard re-keyed — now matches the distinctive ${tray_var}.setImage(${electron_var}.nativeImage.createFromPath(${path_var})) sequence using post-rename names, instead of the off-cursor (t)) marker that would've drifted silently.

Avoids a StatusNotifierItem re-registration race on KDE Plasma
where the old SNI remains registered when the new one appears,
resulting in two tray icons side by side until session logout.

`patch_tray_menu_handler` already bounds the race with a 250 ms
delay after `tray.destroy()`, but that's not enough on all setups
(reproduced on Fedora 43 KDE Plasma 6.6.4 + Wayland). Widening the
delay just moves the goalposts; the race is structural.

Fix: inject a fast-path before the existing destroy+recreate block
in the tray rebuild function. When the tray already exists and
isn't being disabled, update its icon and context menu in place
via `setImage` + `setContextMenu` — the existing StatusNotifierItem
stays registered, no DBus re-registration, no race. The slow path
(destroy + delay + re-create) is kept for the initial creation and
the tray-disable cases where it's unavoidable.

All five minified locals needed by the fast-path (tray function,
tray variable, electron module, menu function, icon path const,
menuBarEnabled flag) are extracted dynamically; the idempotency
guard re-keys off the post-rename `setImage(...)` sequence.

Triggered in KDE System Settings by any of Appearance → Colors /
Plasma Style / Global Theme, which all fire the same
`nativeTheme.on('updated')` signal.

Follow-up to aaddrick#491. The broader submenu work from that PR stays
parked on features/change-icon-color pending the scope discussion
in aaddrick#492; this PR ships only the duplicate-tray-icon fix that
@aaddrick asked to split out.

Co-Authored-By: Claude <[email protected]>
@IliyaBrook IliyaBrook requested a review from aaddrick as a code owner April 24, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant