Repository navigation
fix: stabilize tray item key to prevent position jumps on property changes - #3386
Conversation
| const itemId = item?.id || ""; | ||
| return !hiddenTrayIds.includes(itemId.toLowerCase()); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Lets remove this function entirely and replace all of the call sites
|
/claude review and identify all of the call sites of the getTrayItemKey function |
| return id; | ||
| } | ||
| return `${id}::${tooltipTitle}`; | ||
| return item?.id || ""; |
There was a problem hiding this comment.
Keying on id alone reverts the fix from 67d3aa9 (system tray: use id+title as identifier, fixes #1542). SNI Id is per-application, not per-instance, so two instances of the same app (or two apps registering the same id) now collide on one key, which breaks the code below:
manualHiddenBarItems/visibleSortedTrayItems(L164, L187): hiding one item hides every item sharing that id, and the context-menu toggle (L1509) un/hides them all at once.hiddenBarItems(L189) matches by key membership, so if one duplicate lands inautoOverflowBarItemsthe other one renders both in the bar and in the overflow popup.moveTrayItemKeyInFullOrder(L260) usesindexOf, so dragging the second duplicate moves the first (or no-ops).
The Fcitx problem is that tooltipTitle is mutable, not that a disambiguator is wrong — dropping it trades one bug for another. Better to keep the key unique with something stable for the item's lifetime (e.g. the item's occurrence index within SystemTray.items when ids collide) instead of a property the app rewrites at runtime.
| return id; | ||
| } | ||
| return `${id}::${tooltipTitle}`; | ||
| return item?.id || ""; |
There was a problem hiding this comment.
This changes the persisted key format with no migration. SessionData.hiddenTrayIds and SessionData.trayItemOrder (quickshell/Common/SessionData.qml:191-192) store these keys verbatim in session.json, and existing users have entries in the id::tooltipTitle form. After this change isHiddenTrayId("myapp::Title") never matches "myapp", so on upgrade every hidden tray item reappears in the bar and the custom tray order resets, with the stale :: entries left in session.json forever.
Worth a one-time migration (strip everything from :: on load, dedupe) when the format changes.
Claude reviewFixes a real Fcitx bug, but by dropping the disambiguator entirely it regresses duplicate-id handling and silently resets persisted tray settings.
Call sites of
|
| Line | Context |
|---|---|
| 42 | definition |
| 154, 155 | sortByPreferredOrder - saved-order lookup for both sort operands |
| 163 | allSortedTrayItemKeys |
| 164 | visibleSortedTrayItems - hidden filter |
| 183 | mainBarItems model key |
| 187 | manualHiddenBarItems |
| 188 | hiddenBarItemKeys |
| 189 | hiddenBarItems |
| 247 | displayedInlineExpandedItems model key |
| 276 | promoteTrayItemToBar |
| 295 | isManualHiddenTrayItem |
| 299, 300 | isAutoOverflowTrayItem |
| 358 | finishPopupDrag |
| 1206 | overflow popup delegate itemKey |
| 1509 | context-menu visibility toggle |
Two more places build the same key inline and would need to stay in sync if the helper is inlined per @bbedward's request: quickshell/Common/TrayMenuManager.qml:38-42 (findTrayItem, still accepts both forms so IPC keeps working) and quickshell/DMSShellIPC.qml:2088-2090 (tray list still prints id::title).
Checked: full diff, every call site above, key persistence in SessionData/SessionSpec, and the IPC/TrayMenuManager key builders. Model: claude-opus-5.
eb042a2 to
8dd2a4c
Compare
|
The previous approach (using plain
Existing persisted data keeps matching on read — no migration needed. Verified locally against the live |
|
@bbedward Updated. |
|
/claude review |
| const id = key.includes("::") ? key.split("::")[0] : key; | ||
| if (root.trayItemCountForId(id) > 1) | ||
| return null; | ||
| return list.filter(saved => saved === id || saved.startsWith(id + "::"))[0] || null; |
There was a problem hiding this comment.
The ambiguity guard here only counts live items, not saved entries, so it misses the case where several stored keys share one id. resolveOrderIndex bails on that (L66-67) but this just takes [0].
Two instances of one app (the #1542 case), both hidden → hiddenTrayIds = ["foo::A", "foo::B"]. One instance exits, so trayItemCountForId("foo") === 1 and the fallback turns on for the survivor foo::B: the first unhide removes foo::B, then isTrayIdHidden prefix-matches the leftover foo::A and the item stays hidden — the user has to click unhide twice. Same leftover also makes a never-hidden survivor vanish from the bar.
Mirroring the order-lookup guard fixes it:
| return list.filter(saved => saved === id || saved.startsWith(id + "::"))[0] || null; | |
| const matches = list.filter(saved => saved === id || saved.startsWith(id + "::")); | |
| return matches.length === 1 ? matches[0] : null; |
Claude reviewMuch better than the previous revision —
Checked: all 4 new helpers, every |
|
/claude review |
| function isTrayIdHidden(key) { | ||
| if (SessionData.isHiddenTrayId(key)) | ||
| return true; | ||
| return !!findSavedTrayKey(key, SessionData.hiddenTrayIds); |
There was a problem hiding this comment.
The matches.length !== 1 guard added in 4377d30 closes the two-stale-entries case, but a single leftover id::* entry still leaks onto a different instance, because nothing ever removes stale entries.
Two instances of one app (the #1542 case), both hidden → hiddenTrayIds = ["foo::A", "foo::B"]:
- Instance A exits.
trayItemCountForId("foo") === 1, so the fallback is live for survivorfoo::B. - User unhides B → exact match,
showTrayId("foo::B")→ list is now["foo::A"]. isTrayIdHidden("foo::B")→ exact miss,matches === ["foo::A"], length 1 → hidden again. The user has to click unhide a second time.
Same leftover also hides an instance the user never hid: hiddenTrayIds = ["foo::A"], A exits, surviving foo::B prefix-matches and vanishes from the bar.
Fixing the guard alone can't cover this — the stale entry has to go away. Self-heal on resolve: when findSavedTrayKey returns a key that isn't the exact one, rewrite that entry to the current key (showTrayId(savedKey) + hideTrayId(key), or a single hiddenTrayIds splice) so at most one entry per id survives and the unhide in step 2 is final.
Claude reviewOrdering fallback looks right now; the stale-entry hole in the hidden-state fallback is narrower than last round but not closed.
Checked: |
|
Verified locally with simulated SNI instances: single-instance key stays stable under changing tooltips; duplicate ids get independent |
Revert the earlier simplistic key approach: keep getTrayItemKey() unchanged (id::tooltipTitle) so duplicate-id disambiguation (duxlabs-dev#1542) is preserved and persisted id::tooltipTitle entries keep matching. Instead make persisted-key lookups tolerant of dynamic tooltipTitle: resolveOrderIndex() in sortByPreferredOrder() does exact match first, falling back to prefix matching an id::* entry only when the id has a single instance. findSavedTrayKey()/isTrayIdHidden() replace direct SessionData.isHiddenTrayId() calls and showTrayId() now receives the actual saved key. Prefix fallback is gated by trayItemCountForId() to single-instance ids only, so multiple items sharing an id always match exactly and hiding/ordering one never affects the others.
|
/claude review |
| const tooltipTitle = item?.tooltipTitle || ""; | ||
| if (!tooltipTitle || tooltipTitle === id) { | ||
| if (!id) | ||
| return ""; |
There was a problem hiding this comment.
The key form depends on how many instances are live right now, so an item's key changes when a sibling appears or exits — the item itself didn't change, but its persisted order/hidden state stops matching.
- One instance of
foo; user drags it to position 1 →trayItemOrdercontains"foo", hide state"foo". - A second instance of
foostarts. Both items now key asfoo::0/foo::1, neither matches the saved"foo"→resolveOrderIndexmisses for both, so the original item jumps to the end of the bar (10000 + index), and if it was hidden it pops back into the bar. - The second instance exits → keys collapse back to
"foo"and the old state resurrects, while anyfoo::Nentries written in between are stale forever.
The occurrence index also shifts on exit: with foo::0/foo::1 hidden as foo::1, when instance 0 quits the survivor becomes "foo" (or with 3 instances, foo::2 becomes foo::1), so hidden/order state lands on the wrong item.
This is the same class of bug the PR is fixing — the disambiguator is derived from mutable global state instead of being stable for the item's lifetime. Assign the key once when an item first appears (keep a Map from item → key, pick the lowest free id::N among live keys, drop the entry when the item goes away) so an existing item's key never changes because of a sibling.
Claude reviewThe Fcitx case is fixed and the migration covers the format change; the remaining hole is that the duplicate-id disambiguator is still derived from live state.
Checked: |
ac49f19 to
00c9b02
Compare
|
/claude review |
Claude reviewNo new issues found. The head (00c9b02) hasn't changed since the last review, so the open inline comment at Checked: |
|
/claude review |
| count++; | ||
| } | ||
| return `${id}::${tooltipTitle}`; | ||
| const key = count === 0 ? id : `${id}::${count}`; |
There was a problem hiding this comment.
The suffix is the number of keyed siblings, not the lowest free suffix, so an item can get a key another live item already holds once an earlier instance exits.
- Instance A gets
foo, instance B getsfoo::1. - A exits and its map entry is dropped. B keeps
foo::1. - A new instance C starts. It has one keyed sibling (B), so
count === 1and C also getsfoo::1.
Now B and C share a key, which brings back the #1542 problems: hiding one hides both, hiddenBarItems shows one of them in both the bar and the overflow, and dragging uses indexOf on the key so it moves the wrong item.
Fix: collect the keys of live siblings and pick the first free one:
const taken = new Set();
for (const live of root.allTrayItems) {
if (live !== item && (live?.id || "") === id && root._trayKeyByItem.has(live))
taken.add(root._trayKeyByItem.get(live));
}
let key = id;
for (let n = 1; taken.has(key); n++)
key = `${id}::${n}`;
Claude reviewPinning the key per item fixes the flip when a sibling comes or goes, but choosing the suffix by counting siblings can still give two live items the same key.
Checked: |
|
/claude review |
| if (root._trayKeyByItem.has(item)) | ||
| return root._trayKeyByItem.get(item); | ||
| const last = root._trayLastIndexById.get(id) || 0; | ||
| const key = last === 0 ? id : `${id}::${last}`; |
There was a problem hiding this comment.
_trayLastIndexById only goes up and nothing ever resets it, so a single-instance app that restarts during the session gets a new key and loses its saved state:
nm-appletruns alone → keynm-applet, which the user has hidden or moved to position 1.- It exits and restarts (a crash, an update, or an app like Fcitx restarting). The new item isn't in
_trayKeyByItem,last === 1, so the key isnm-applet::1. resolveOrderIndexmisses, so it jumps to the end.isTrayIdHiddenmisses, so a hidden icon shows up again. If the user hides it again,nm-applet::1is saved, and after a shell restart the item keys asnm-appletagain, so the entry is stale.
This is the same "state lost when the item re-registers" bug this PR is fixing. The key should be the lowest suffix not used by a live sibling (see the earlier suggestion on this function). Then drop _trayLastIndexById.
Claude reviewThe per-id counter fixes duplicate keys between live siblings, but it never goes down, so a single app that restarts gets a new key and loses its saved order and hidden state.
Checked: |
|
/claude review |
Claude reviewNo issues found. Picking the lowest free Checked: |
…anges (#3386) * fix: keep tray order stable when tooltipTitle changes dynamically Revert the earlier simplistic key approach: keep getTrayItemKey() unchanged (id::tooltipTitle) so duplicate-id disambiguation (#1542) is preserved and persisted id::tooltipTitle entries keep matching. Instead make persisted-key lookups tolerant of dynamic tooltipTitle: resolveOrderIndex() in sortByPreferredOrder() does exact match first, falling back to prefix matching an id::* entry only when the id has a single instance. findSavedTrayKey()/isTrayIdHidden() replace direct SessionData.isHiddenTrayId() calls and showTrayId() now receives the actual saved key. Prefix fallback is gated by trayItemCountForId() to single-instance ids only, so multiple items sharing an id always match exactly and hiding/ordering one never affects the others. * fix: bail on ambiguous saved tray keys in fallback * tray: use occurrence index as stable key, add one-time session migration * tray: pin stable key per item instance so it never flips when siblings come and go * tray: assign unique per-id sequential keys to avoid collision after sibling exit * tray: reuse free key slots so a restarted singleton keeps its plain id (cherry picked from commit 60342b7)
Problem
Tray icons (especially Fcitx) jump positions when switching windows because
getTrayItemKey()usesid::tooltipTitleas the key. Fcitx5 updatestooltipTitleon input method changes, causing the key to change and fall back to end of list.Solution
Use only
idfor tray item key generation. TheStatusNotifierItemspecification guaranteesidis unique per application, makingtooltipTitleredundant for key purposes.Testing