Sync TableView SelectedItems with the always-selected row 0 (#13230) - #13235
Merged
Conversation
SelectionMode.AlwaysSelected picks row 0 the moment ItemsSource is assigned to a populated collection, but that pick only reaches the internal selection model: the row is drawn highlighted and SelectedItem/SelectedIndex point at it, while the SelectedItems collection stays empty and no SelectionChanged is raised. Neither a layout pass nor a Selection.Clear()/Select(0) repairs it - only moving the selection to another row does. Subtitle Edit reads every selection through SelectedItems, so the grid showed the first row highlighted while the application saw nothing selected (#13230): - the edit box, Show and Duration went blank a few seconds after start-up, when the open-last-file restore reached SetRecentFileProperties -> UpdateVideoOffsetStatus -> SubtitleGridSelectionChanged, which read the empty SelectedItems and cleared SelectedSubtitle - a shift-selection starting at row 1 left row 1 out of the selection, so italic, copy, delete and friends silently skipped the first line Repair the collection from the selection model on every ItemsSource change, in MakeTableView so every grid in the application is covered rather than the main subtitle grid alone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Fixes #13230.
The bug
After opening a subtitle, the first row looks selected and its text shows - then a few seconds later the text box, Show and Duration go blank while row 1 stays highlighted. A shift-selection that visually starts at row 1 also leaves row 1 out, so italic/copy/delete skip the first line.
Root cause
MakeTableViewcreates every grid withSelectionMode.AlwaysSelected. In Avalonia 12.1.1 that mode selects row 0 as soon asItemsSourceis assigned to a populated collection, but the pick only reaches the internal selection model - the row is drawn highlighted andSelectedItem/SelectedIndexpoint at it, while theSelectedItemscollection stays empty and noSelectionChangedis raised. Verified headlessly:It does not heal on a layout pass, and neither
Selection.Clear()+Select(0)nor anItemsSource/SelectedIndexbounce repairs it - only moving the selection to a different row does, which is why clicking a row makes everything work again. WithoutAlwaysSelectedthe same sequence stays in sync, so the mode is the trigger.Subtitle Edit reads every selection through
SubtitleGridSelectedItems->SubtitleGrid.SelectedItems, so the whole application saw nothing selected:SelectedItembinding does push row 0 intoSelectedSubtitle, so the text shows at first. On the open-last-file-on-start path the restore chain ends inSetRecentFileProperties->UpdateVideoOffsetStatus->SubtitleGridSelectionChanged, which reads the emptySelectedItems, takes the "nothing selected" branch and setsSelectedSubtitle = null. That call runs after the awaited video open, which is the several-second delay - and it is why the repro needs a close-and-reopen rather than a plain File > Open.Selection.Count=5butSelectedItems=[line 2, line 3, line 4, line 5].The #13190 fix does not cover this: its guard is
!ReferenceEquals(SelectedSubtitle, itemToScroll), and the two-way binding has already setSelectedSubtitle, so it never fires - and if it did it would read the same emptySelectedItems.The fix
Repair
SelectedItemsfrom the selection model on everyItemsSourcechange, inMakeTableViewso every grid in the application is covered rather than the main subtitle grid alone. The repair is a no-op unlessSelectedItemsis empty while the selection model is not.Tests
Four regression tests in
tests/UI/Logic/TableViewSelectionSyncTests.cs, built on a grid configured like the main subtitle grid (multi-select, always-selected, two-waySelectedItem/SelectedIndexbindings) and filled the waySetSubtitlesfills it. Three of them fail without the fix; the fourth is the empty-grid guard case. Full UI suite: 1417 passing.🤖 Generated with Claude Code