Replace positional diff with CollectionDifference-based shape matching#484
Open
wojo wants to merge 1 commit intoswiftbar:mainfrom
Open
Replace positional diff with CollectionDifference-based shape matching#484wojo wants to merge 1 commit intoswiftbar:mainfrom
wojo wants to merge 1 commit intoswiftbar:mainfrom
Conversation
The positional diff in diffMenuNodes() compares items by index. When items are inserted or removed mid-list, all subsequent items shift and get patched with wrong content — e.g. an image item inherits adjacent text as its attributedTitle, rendering ghost text beside the image. Replace with Swift's CollectionDifference using shape-based identity matching. Each MenuItemNode gets a ShapeFingerprint (separator, has image, font, title prefix, has children, fold) that captures its structural role independent of volatile content. An occurrence counter disambiguates duplicates. CollectionDifference.inferringMoves() correctly tracks items across position shifts. This preserves the incremental update benefit for all cases — items that shift position are patched in-place rather than rebuilt, keeping fold states, webviews, and menu interactivity intact. Fixes swiftbar#482
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Alternative fix for #482 — replaces the naive positional diff with Swift's
CollectionDifferenceusing shape-based identity matching. This correctly handles mid-list inserts and removes without misaligning item properties.See #483 for the simpler fallback approach (Option 1).
How it works
Each
MenuItemNodegets aShapeFingerprintthat captures its structural role:isSeparator— separators match other separatorshasImage— image-only lines are distinct from textfontName— monospace lines (e.g.font=Menlo) are distincttitleKey— stable prefix up to first:or first word ("Battery:", "Climate:", "Home")hasChildren/hasFold— structural distinctionsAn occurrence counter disambiguates duplicates (e.g. multiple separators).
CollectionDifference.inferringMoves()uses these tagged nodes to correctly track items across position shifts.Pros / Cons vs Option 1 (#483)
Option 1: Full rebuild on structural change (#483)
Pros:
Cons:
Option 2: CollectionDifference shape matching (this PR)
Pros:
Cons:
Files changed
MenuItemNode.swift— AddedShapeFingerprintstruct,shapeFingerprintcomputed property,extractTitleKey()helperMenuDiff.swift— ReplaceddiffMenuNodes()with CollectionDifference-based implementation, addedTaggedNodewrapperTest plan