Fix clipboard service binding for reused diagram views - #12
Merged
Conversation
The clipboard service on DiagramDocumentView was bound once per DiagramDocumentViewModel via a null-coalescing assignment (??=). When Dock recreated/reattached a tab's DiagramDocumentView control while reusing the same view model (for example creating two custom-view tabs in quick succession via the View Builder dialog), the clipboard service stayed anchored to the first, now-detached view instance. TopLevel.GetTopLevel then resolved null for that anchor, so SetTextAsync silently no-op'd instead of throwing - the correct snippet text reached the clipboard call, but never actually reached the OS clipboard, leaving whatever the last-successfully-bound tab copied. Fix: always rebind ClipboardService to the currently-attached view instance in OnDataContextChanged instead of only the first ever attached one. Also hardens AvaloniaClipboardService.SetTextAsync to marshal its work through Dispatcher.UIThread, since native OS clipboard access must happen on the UI thread and a caller further up an awaited chain could otherwise resume on a thread-pool thread. Adds a regression test that reproduces the original bug end-to-end through the real Dock-composed MainWindowView (confirmed to fail before the fix, returning null instead of the first tab's snippet). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.
This pull request addresses a bug where the "Copy as SysML" feature in the application would always return the snippet from the last-created custom view tab, regardless of which tab was active. The fix ensures that clipboard operations are always correctly anchored to the currently active view, even after tabs are detached and reattached. The changes also add a regression test to prevent this bug from recurring.
Clipboard Service Reliability
AvaloniaClipboardService) now always rebinds to the current view instance inDiagramDocumentView.OnDataContextChanged, ensuring that clipboard operations target the correct, currently attached visual anchor. This prevents clipboard writes from silently failing when a view is reattached after being detached.AvaloniaClipboardService.SetTextAsyncis now always marshaled onto the UI thread usingDispatcher.UIThread.InvokeAsync, guaranteeing correctness regardless of the calling thread's context. [1] [2] [3]Testing and Regression Prevention
DiagramContextMenu_CopyAsSysml_TwoCustomViewTabs_EachCopiesItsOwnSnippet, which verifies that each custom view tab copies its own SysML snippet, and that switching between tabs does not result in stale clipboard data. Utility methods for tab selection and clipboard reading were also added to support this test. [1] [2]