From 91f7e26bdb864cce17877429b174e8473d75fffd Mon Sep 17 00:00:00 2001 From: Barbara Kudiess Date: Thu, 19 Nov 2026 15:02:23 -0800 Subject: [PATCH 1/2] fix: reduce typing lag in long chats Skip cross-block Markdown selection geometry until Reactor can keep idle messages off the layout path. Reuse unchanged message content, localized strings, and animation resources, and avoid unrelated rail and title-row refreshes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docs/ARCHITECTURE.md | 2 +- docs/CHAT_VISUAL_DESIGN.md | 22 +- docs/GATEWAY_FIXTURE_TESTING.md | 26 - .../Controls/SetupStepTimeline.xaml.cs | 2 +- .../Chat/ChatHistoryNavigation.cs | 24 +- .../Chat/ChatMarkdownMessageView.cs | 107 -- .../Chat/ChatMarkdownPresentation.cs | 183 +-- .../Chat/ChatRunProgressPresentation.cs | 4 + .../Chat/ChatToolActivityPresentation.cs | 18 +- .../Chat/NativeWorkingClaw.cs | 14 +- .../Chat/ReactorChatSubagents.cs | 3 +- .../Chat/ReactorChatTimeline.cs | 59 +- .../Chat/ReactorConversationPanelHost.cs | 9 +- .../Chat/ReactorItemsViewScrollController.cs | 3 +- .../Chat/ReactorRunProgress.cs | 8 +- .../Helpers/LocalizationHelper.cs | 39 +- .../Strings/en-us/Resources.resw | 1 - .../Strings/fr-fr/Resources.resw | 1 - .../Strings/nl-nl/Resources.resw | 1 - .../Strings/pt-br/Resources.resw | 1 - .../Strings/zh-cn/Resources.resw | 1 - .../Strings/zh-tw/Resources.resw | 1 - .../Windows/DesktopCompanionWindow.cs | 4 +- .../Windows/WorkspaceTitleRow.cs | 61 +- .../Gateway/GatewayScenario.cs | 17 - .../ChatStreamingParityTests.cs | 16 + .../ChatToolActivityPresentationTests.cs | 20 + .../ReactorChatMarkdownContractTests.cs | 57 +- .../GatewayFixtureMarkdownSelectionTests.cs | 1064 ----------------- .../LocalizationHelperTests.cs | 39 + .../ReactorChatLayoutProofTests.cs | 23 +- .../ReactorMarkdownListProofTests.cs | 27 +- .../ReactorMarkdownTableProofTests.cs | 8 +- .../ReactorMarkdownWebParityProofTests.cs | 27 +- 34 files changed, 317 insertions(+), 1575 deletions(-) delete mode 100644 src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownMessageView.cs delete mode 100644 tests/OpenClaw.Tray.UITests/GatewayFixtureMarkdownSelectionTests.cs create mode 100644 tests/OpenClaw.Tray.UITests/LocalizationHelperTests.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b7efb55c7b..2c0eedd130 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -1262,7 +1262,7 @@ leading and trailing pipe. Columns, in order: | migration-consent-locking | authoritative | InnoMigrationConsentStore | protected consent publication and inspection while source is running | MigrationOperationLock + InnoMigrationConsentStore | consent uses a shared preparation lease and a separate exclusive writer lock | consent remains compatible with Inno runtime readers but cannot overlap exclusive Store mutation; finalization removes the writer lock before deleting the completion receipt | InnoMigrationConsentStoreTests.RuntimeReadLease_AllowsExplicitGrantAndInspection | behavioral | - | | chat-markdown-presentation | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | paragraph and heading typography, literal code-frame presentation and Copy | ChatMarkdownPresentation | timeline keeps sanitization, parser flags and inert links/images; Reactor owns default list wrapping | presentation uses public MarkdownOptions callbacks; code content is never truncated or parsed as markup | ReactorChatLayoutProofTests.CodeBlock_PreservesLiteralContentAndCopiesExactText | behavioral | - | | chat-markdown-disclosures | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | allowlisted Markdown disclosure parsing and native expansion state | ChatMarkdownDisclosures + ChatMarkdownPresentation | timeline sanitizes before parsing; disclosure bodies use the existing safe Markdown renderer | only balanced, allowlisted block wrappers become native Expanders; unsupported wrappers remain inert Markdown; a reader-toggled header is the timeline's scroll anchor, so details grow downward inside tall messages | ChatMarkdownDisclosureTests.CommonWrappers_KeepBodyMarkdownAndSurroundingOrder | behavioral | - | -| chat-markdown-selection-integration | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | per-message selection opt-in, custom disclosure/code surfaces and localized copy-result feedback | ChatMarkdownMessageView + ChatMarkdownPresentation | Microsoft.UI.Reactor.Markdown owns selection document, geometry, input, scrolling, clipboard write and UI Automation | one Scope shares options across all Markdown fragments; collapsed bodies and chrome are excluded; OnCopyResult never writes clipboard | ReactorChatMarkdownContractTests.MarkdownSelection_UsesOneReactorScopeAndFeedbackOnlyCopyResults | source-shape | when native message-boundary and copy-result acceptance fully replaces the integration source guard | +| chat-markdown-selection-integration | closed | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | Reactor MarkdownTextSelection cross-block scope, ChatMarkdownMessageView and selection copy-result feedback | native per-block text selection in ReactorChatTimeline + ChatMarkdownPresentation | text, code, table and list blocks stay individually selectable; Copy buttons keep their own feedback | chat Markdown does not mount Reactor preview.18 selection hosts, which rescan native text geometry on every LayoutUpdated and slowed typing and streaming | ReactorChatMarkdownContractTests.ChatMarkdown_DoesNotUseReactorCrossBlockSelection | source-shape | when a Reactor package with demand-driven selection geometry is adopted and proven against typing and streaming latency | | reactor-markdown-list-layout | closed | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | preview.12 ListItem layout replacement and StackElement shape assumption | Microsoft.UI.Reactor.Advanced default Markdown list layout (preview.16, upstream #1197) | timeline keeps sanitizer, parser safety and block presentation callbacks | ordered/unordered, nested and loose list content wraps in an Auto/Star Grid without an application-side ListItem override | ReactorChatMarkdownContractTests.AssistantMessages_UseSanitizedGitHubFlavoredMarkdown | source-shape | when upstream Markdown list layout is replaced | | chat-model-picker-presentation | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatComposer.cs, then custom Button rows in ChatModelPicker | catalog search, provider groups, native row interaction states and model metadata presentation | NativeChatModelPicker through a generated Reactor wrapper | ChatModelPicker owns flyout lifetime; composer controller owns model mutation and authorization | native single-selection navigation never writes a model until explicit activation; filtering and resizing preserve raw provider-qualified identities | ReactorChatLayoutProofTests.ModelMenu_UsesNativeSelectionStatesAndPreservesNavigationDuringResize | behavioral | - | | chat-model-picker-button-rows-closed | closed | src/OpenClaw.Tray.WinUI/Chat/ChatModelPicker.cs | hand-built Button rows, custom selected fill/checkmark and search-box styling | NativeChatModelPicker | declarative trigger and flyout lifetime only | native AutoSuggestBox/ListView own search and row interaction visuals; no duplicate hand-styled selection path | ReactorChatLayoutProofTests.ModelMenu_UsesNativeSelectionStatesAndPreservesNavigationDuringResize | behavioral | - | diff --git a/docs/CHAT_VISUAL_DESIGN.md b/docs/CHAT_VISUAL_DESIGN.md index 9f23cf98c4..4b09077ba2 100644 --- a/docs/CHAT_VISUAL_DESIGN.md +++ b/docs/CHAT_VISUAL_DESIGN.md @@ -1361,21 +1361,13 @@ same resources and bounded layouts. Markdown sanitization, disabled HTML, inert images, list measurement, timeline keys and scroll-follow remain with their existing owners. -Assistant Markdown opts into one Reactor text-selection scope per message, -spanning paragraphs, headings, list content, tables, code and expanded -disclosures. Reactor owns pointer and keyboard selection, geometry, scrolling -and the UI Automation text provider. Neighboring message rows, code toolbar -labels and buttons, and collapsed disclosure bodies are outside the selected -document. OpenClaw registers the summary once and supplies disclosure visibility; -it does not infer selectable text from Reactor's layout tree. - -Selection copy has one clipboard writer in Reactor. Its `OnCopyResult` callback -only updates native localized Copied or Copy failed feedback outside the -selection scope. Feedback resets after two seconds; content replacement and -unmount fence stale callbacks and timers. The separate whole-message and code -Copy buttons retain their existing clipboard and feedback paths. Native -acceptance for cross-block selection and recycling is required in addition to -the mounted typography and layout checks. +Assistant Markdown text, code and table blocks are individually selectable with +native WinUI text selection. Cross-block selection through Reactor's +`MarkdownTextSelection` is not used: its preview.18 host rescans native text +geometry on every layout pass, which slowed typing and streaming. Re-enable it +only with a Reactor package that tracks selection geometry on demand, proven +against typing and streaming latency. The whole-message and code Copy buttons +keep their own clipboard and feedback paths. Assistant Markdown accepts balanced, block-level `
` wrappers with a nonempty `` as an OpenClaw extension. The summary may follow the diff --git a/docs/GATEWAY_FIXTURE_TESTING.md b/docs/GATEWAY_FIXTURE_TESTING.md index 56ad63976b..8ecb83cef9 100644 --- a/docs/GATEWAY_FIXTURE_TESTING.md +++ b/docs/GATEWAY_FIXTURE_TESTING.md @@ -343,32 +343,6 @@ as well. That option enables only its own child and still rejects failed, skipped, and zero-test runs. The ordinary UI filter continues to exclude those keyboard-only cases; `-NativeInputProof` does not select them. -`MarkdownSelection_CopiesAcrossBlocksAndExcludesCollapsedAndNeighboringContent` -uses synthetic history in the shipped app to select and copy across paragraphs, -lists, code, a table and a native collapsed details block. It checks exact -normalized clipboard text, excludes adjacent messages and collapsed body text, -switches to a second conversation and back to verify that copying from the -focused message cannot leak another conversation's text, and exercises edge -scrolling in the production chat ItemsView. Returning to the same message may -retain its own selection. The disclosure is scrolled into view before its -expanded body is checked. This case uses native pointer, keyboard and clipboard -input, so run it only on an authorized, exclusive interactive desktop: - -```powershell -$env:OPENCLAW_RUN_GATEWAY_FIXTURE_UI = '1' -$env:OPENCLAW_GATEWAY_FIXTURE_APP = (Resolve-Path $app).Path -$env:OPENCLAW_GATEWAY_FIXTURE_ARTIFACTS = Join-Path $env:TEMP 'openclaw-markdown-selection' -$env:OPENCLAW_GATEWAY_FIXTURE_SCREENSHOTS = '1' -dotnet test .\tests\OpenClaw.Tray.UITests\OpenClaw.Tray.UITests.csproj ` - -p:Platform=ARM64 -r win-arm64 ` - --filter 'FullyQualifiedName~MarkdownSelection_CopiesAcrossBlocksAndExcludesCollapsedAndNeighboringContent' ` - --logger 'console;verbosity=normal' -``` - -The test captures only its owned app window. The fixture Gateway and profile are -synthetic and disposable; this is not live Gateway, provider, or installed-app -proof. - ### Saved Gateway ownership and released profiles `GatewayManagement_UsesSavedOwnershipWithoutSetupState` renders native-only, diff --git a/src/OpenClaw.SetupEngine.UI/Controls/SetupStepTimeline.xaml.cs b/src/OpenClaw.SetupEngine.UI/Controls/SetupStepTimeline.xaml.cs index 2d75a4d8a6..5b36158bcf 100644 --- a/src/OpenClaw.SetupEngine.UI/Controls/SetupStepTimeline.xaml.cs +++ b/src/OpenClaw.SetupEngine.UI/Controls/SetupStepTimeline.xaml.cs @@ -58,8 +58,8 @@ public SetupStepTimeline() row.PointerExited += Row_PointerExited; row.PointerCanceled += Row_PointerExited; row.PointerCaptureLost += Row_PointerExited; + row.SizeChanged += (_, _) => PositionConnectors(); } - Rows.LayoutUpdated += (_, _) => PositionConnectors(); Loaded += OnLoaded; Unloaded += OnUnloaded; } diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatHistoryNavigation.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatHistoryNavigation.cs index 66fa4aa74d..15b773a942 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatHistoryNavigation.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ChatHistoryNavigation.cs @@ -16,6 +16,7 @@ internal sealed record ChatHistoryAnchor(int RowIndex, string Key, string Sender internal sealed class ChatHistoryNavigation(ItemsView itemsView, Action navigate) : IDisposable { private AnnotatedScrollBar? _bar; + private Grid? _labelsGrid; private IReadOnlyList _anchors = []; private IReadOnlyList _ticks = []; private int _navigationVersion; @@ -70,6 +71,7 @@ public void SetScrollBar(AnnotatedScrollBar? bar) _bar.Labels.Clear(); } _bar = bar; + _labelsGrid = null; _presenterCount = -1; _firstPresenter = null; _hoveredKey = null; @@ -218,6 +220,7 @@ private void OnPointerExited(object sender, PointerRoutedEventArgs args) private void OnUnloaded(object sender, RoutedEventArgs args) { CancelVisualUpdate(); + _labelsGrid = null; _presenterCount = -1; _firstPresenter = null; } @@ -229,7 +232,7 @@ private void OnLayoutUpdated(object? sender, object args) || (_bar?.XamlRoot?.RasterizationScale ?? 1) != _scale) Refresh(); else if (labels is not null && (labels.Children.Count != _presenterCount - || !ReferenceEquals(labels.Children.FirstOrDefault(), _firstPresenter))) + || (_presenterCount > 0 && !ReferenceEquals(labels.Children[0], _firstPresenter)))) QueueVisualUpdate(); } @@ -254,7 +257,8 @@ private void CancelVisualUpdate() _visualUpdateQueued = false; } - private Grid? GetLabelsGrid() => GetTemplatePart("PART_LabelsGrid"); + private Grid? GetLabelsGrid() => _labelsGrid is { IsLoaded: true } labels + ? labels : _labelsGrid = GetTemplatePart("PART_LabelsGrid"); private T? GetTemplatePart(string name) where T : FrameworkElement => _bar is not null && VisualTreeHelper.GetChildrenCount(_bar) > 0 @@ -266,7 +270,12 @@ private void CollectVisibleRows() _visibleRows.Clear(); if (itemsView.ScrollView is not { Content: ItemsRepeater repeater } scroll) return; - for (var index = 0; index < VisualTreeHelper.GetChildrenCount(repeater); index++) + // The repeater is the scroller's content and each realized container is its + // direct child. Reuse its scroll-space transform for all row layout positions. + var transform = repeater.TransformToVisual(scroll); + var viewportHeight = scroll.ViewportHeight; + var count = VisualTreeHelper.GetChildrenCount(repeater); + for (var index = 0; index < count; index++) { if (VisualTreeHelper.GetChild(repeater, index) is not FrameworkElement { IsLoaded: true, ActualHeight: > 0 } element) @@ -274,9 +283,10 @@ private void CollectVisibleRows() var rowIndex = repeater.GetElementIndex(element); if (rowIndex < 0) continue; - var bounds = element.TransformToVisual(scroll).TransformBounds( - new global::Windows.Foundation.Rect(0, 0, element.ActualWidth, element.ActualHeight)); - if (bounds.Bottom > 0 && bounds.Top < scroll.ViewportHeight) + var offset = element.ActualOffset; + var bounds = transform.TransformBounds( + new global::Windows.Foundation.Rect(offset.X, offset.Y, element.ActualWidth, element.ActualHeight)); + if (bounds.Bottom > 0 && bounds.Top < viewportHeight) _visibleRows.Add(rowIndex); } } @@ -327,7 +337,7 @@ private void UpdateVisuals() distance == 0 ? "Highlighted" : _visibleRows.Contains(anchor.RowIndex) ? "Visible" : "Quiet", false); } _presenterCount = ready ? labels.Children.Count : -1; - _firstPresenter = ready ? labels.Children.FirstOrDefault() : null; + _firstPresenter = ready && _presenterCount > 0 ? labels.Children[0] : null; } private void OnScrolling(AnnotatedScrollBar sender, AnnotatedScrollBarScrollingEventArgs args) diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownMessageView.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownMessageView.cs deleted file mode 100644 index ea908d4fc4..0000000000 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownMessageView.cs +++ /dev/null @@ -1,107 +0,0 @@ -using Microsoft.UI.Dispatching; -using Microsoft.UI.Reactor; -using Microsoft.UI.Reactor.Core; -using Microsoft.UI.Reactor.Markdown; -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Automation.Peers; -using OpenClawTray.Services; -using Windows.Foundation; -using static Microsoft.UI.Reactor.Factories; - -namespace OpenClawTray.Chat; - -internal sealed record ChatMarkdownMessageProps( - string Text, Func? TryCopyCode, bool CompactSecondaryReading); - -/// -/// Mount through ReactorChatTimeline.BuildSafeMarkdown. Owns one message selection scope -/// and localized result feedback; Reactor alone writes selected text to the clipboard. -/// -internal sealed class ChatMarkdownMessageView : Component -{ - private readonly record struct CopyFeedback(long ContentVersion, bool Success, long Sequence); - - public override Element Render() - { - var props = Props; - var contentVersion = UseRef(0L); - var mounted = UseRef(true); - var sequence = UseRef(0L); - var announced = UseRef(0L); - var selection = UseRef<(string Text, MarkdownTextSelectionOptions Options)?>(null); - var (feedback, setFeedback) = UseState(null); - UseEffect((Func)(() => - { - mounted.Current = true; - return () => mounted.Current = false; - }), Array.Empty()); - - if (selection.Current is not { } existing || existing.Text != props.Text) - { - var version = ++contentVersion.Current; - selection.Current = (props.Text, new MarkdownTextSelectionOptions - { - OnCopyResult = (_, success) => - { - if (!mounted.Current || contentVersion.Current != version) - return; - if (!success) - Logger.Warn("Markdown selection clipboard copy failed."); - setFeedback(new(version, success, ++sequence.Current)); - }, - }); - } - - UseEffect((Func)(() => - { - if (feedback is not { } pending || pending.ContentVersion != contentVersion.Current) - return () => { }; - var timer = DispatcherQueue.GetForCurrentThread().CreateTimer(); - timer.Interval = ChatCopyButton.DefaultResetAfter; - timer.IsRepeating = false; - TypedEventHandler tick = (_, _) => - { - if (mounted.Current && sequence.Current == pending.Sequence - && contentVersion.Current == pending.ContentVersion) - setFeedback(null); - }; - timer.Tick += tick; - timer.Start(); - return () => { timer.Stop(); timer.Tick -= tick; }; - }), contentVersion.Current, feedback?.Sequence); - - var result = feedback.GetValueOrDefault(); - var active = feedback.HasValue && result.ContentVersion == contentVersion.Current; - var label = active - ? result.Success - ? ChatMarkdownPresentation.Localized("Chat_Copy_Copied", "Copied") - : ChatMarkdownPresentation.Localized("Chat_Copy_Failed", "Copy failed") - : string.Empty; - var options = selection.Current.Value.Options; - return VStack(4, - MarkdownTextSelection.Scope(options, - ReactorChatTimeline.BuildMessageMarkdown( - props.Text, props.TryCopyCode, options, props.CompactSecondaryReading)) - .AutomationName(ChatMarkdownPresentation.Localized("Chat_Markdown_SelectionName", "Message text")), - TextBlock(label) - .Foreground(Theme.Ref(active && result.Success - ? "ChatCopySuccessBrush" : "SystemFillColorCriticalBrush")) - .IsVisible(active) - .AutomationId("ChatMarkdownCopyStatus") - .AutomationName(label) - .LiveRegion(AutomationLiveSetting.Polite) - .Set(text => - { - if (!active || result.Sequence == announced.Current) - return; - announced.Current = result.Sequence; - text.DispatcherQueue.TryEnqueue(() => - { - if (mounted.Current && sequence.Current == result.Sequence - && contentVersion.Current == result.ContentVersion && text.IsLoaded) - FrameworkElementAutomationPeer.CreatePeerForElement(text) - ?.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged); - }); - })); - } -} diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs index e0b7cc1255..045fdbffb9 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs @@ -45,8 +45,7 @@ internal static Element WithInlineStyling(Element element, bool compactSecondary }); internal static Element Disclosures( - IReadOnlyList parts, Func renderMarkdown, - MarkdownTextSelectionOptions selectionOptions) + IReadOnlyList parts, Func renderMarkdown) { if (parts is [ChatMarkdownText markdown]) return renderMarkdown(markdown.Text); @@ -55,7 +54,7 @@ internal static Element Disclosures( ChatMarkdownText text => renderMarkdown(text.Text), ChatMarkdownDisclosure disclosure => Component( - new(disclosure, renderMarkdown, selectionOptions)).WithKey($"disclosure:{index}"), + new(disclosure, renderMarkdown)).WithKey($"disclosure:{index}"), _ => throw new InvalidOperationException("Unknown chat Markdown part."), }).ToArray()); } @@ -262,7 +261,7 @@ element is StackElement stack // Compact thinking text keeps its tighter pre-web rhythm. Spacing = compactSecondaryReading ? ChatVisuals.BlockGap : ChatVisuals.MessageBlockGap, Children = stack.Children - .Select((child, index) => ResetBlockEdges(child, index == 0 || compactSecondaryReading)) + .Select((child, index) => ResetBlockEdges(Table(child), index == 0 || compactSecondaryReading)) .ToArray(), } : element; @@ -271,24 +270,17 @@ element is StackElement stack private static Element ResetBlockEdges(Element element, bool dropTop) { var margin = element.Modifiers?.Margin ?? new Thickness(0); - var top = !dropTop && IsHeading(element) ? margin.Top : 0; + var top = !dropTop && s_headings.TryGetValue(element, out _) ? margin.Top : 0; return element.Margin(margin.Left, top, margin.Right, 0); } - // Text selection wraps each block after its hook and keeps only layout values on the wrapper, - // so headings also carry a neutral layout marker: MaxHeight at its native default. - private static bool IsHeading(Element element) => - s_headings.TryGetValue(element, out _) || - element.Modifiers?.Layout?.MaxHeight is double.PositiveInfinity; - private static readonly ConditionalWeakTable s_headings = new(); internal static Element Paragraph(Element element) => Format(element, ChatVisuals.BodySize, false); internal static Element Heading(int level, Element element) { - var heading = Format(element, level switch { 1 => 24, 2 => 20, _ => 16 }, true) - .MaxHeight(double.PositiveInfinity); + var heading = Format(element, level switch { 1 => 24, 2 => 20, _ => 16 }, true); s_headings.AddOrUpdate(heading, s_headings); return heading; } @@ -303,30 +295,13 @@ internal static Element List(Element[] items, bool compactSecondaryReading = fal /// internal static Element ListItem(Element row, bool compactSecondaryReading = false) { - if (row is not GridElement { Children: [var markerElement, var content] } grid) + if (row is not GridElement { Children: [TextBlockElement marker, var content] } grid) return row; - if (markerElement is not TextBlockElement marker) - { - // Text selection wraps the marker and content before this hook. Size the wrapper's - // column here and style the mounted marker before it first renders. In-place updates - // (streamed item text, a changed marker) do not reload the row, so every render also - // queues a restyle after its children reconcile. - return (grid with { Children = [markerElement.MinWidth(ChatVisuals.ListMarkerColumn), content] }) - .Set(control => QueueListRowStyling(control, compactSecondaryReading)) - .OnMountAdd(control => ((FrameworkElement)control).Loaded += - compactSecondaryReading ? OnCompactListRowLoaded : OnListRowLoaded) - .OnUnmountAdd(control => - { - ((FrameworkElement)control).Loaded -= OnCompactListRowLoaded; - ((FrameworkElement)control).Loaded -= OnListRowLoaded; - s_listRowStyling.Remove((Microsoft.UI.Xaml.Controls.Grid)control); - }); - } var column = (marker with { TextAlignment = TextAlignment.Right }) .FontFamily(ChatVisuals.BodyFontFamily) .MinWidth(ChatVisuals.ListMarkerColumn) .Set(text => StyleListMarker(text, compactSecondaryReading)); - if (!compactSecondaryReading && content is RichTextBlockElement rich && !IsHeading(content)) + if (!compactSecondaryReading && content is RichTextBlockElement rich && !s_headings.TryGetValue(content, out _)) { var prose = (Style)Application.Current.Resources["ChatProseRichTextStyle"]; content = rich.Set(text => text.Style = prose); @@ -334,50 +309,6 @@ internal static Element ListItem(Element row, bool compactSecondaryReading = fal return grid with { Children = [column, content] }; } - private static void OnListRowLoaded(object sender, RoutedEventArgs args) => - StyleListRow((Microsoft.UI.Xaml.Controls.Grid)sender, false); - - private static void OnCompactListRowLoaded(object sender, RoutedEventArgs args) => - StyleListRow((Microsoft.UI.Xaml.Controls.Grid)sender, true); - - // A fresh token per render; a queued pass from an older render, or for a row Reactor has - // since unmounted and pooled, no-ops. - private static readonly ConditionalWeakTable s_listRowStyling = new(); - - private static void QueueListRowStyling(Microsoft.UI.Xaml.Controls.Grid row, bool compactSecondaryReading) - { - var token = new object(); - s_listRowStyling.AddOrUpdate(row, token); - row.DispatcherQueue?.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () => - { - if (s_listRowStyling.TryGetValue(row, out var current) && ReferenceEquals(current, token)) - StyleListRow(row, compactSecondaryReading); - }); - } - - private static void StyleListRow(Microsoft.UI.Xaml.Controls.Grid row, bool compactSecondaryReading) - { - foreach (var child in row.Children.OfType()) - { - if (Microsoft.UI.Xaml.Controls.Grid.GetColumn(child) == 0) - { - if (FirstDescendant(child) is { } marker) - { - marker.TextAlignment = TextAlignment.Right; - marker.MinWidth = ChatVisuals.ListMarkerColumn; - marker.FontFamily = new FontFamily(ChatVisuals.BodyFontFamily); - StyleListMarker(marker, compactSecondaryReading); - } - } - else if (!compactSecondaryReading && FirstDescendant(child) is { Style: null } text && !IsCodeText(text)) - { - // Tight-list text bypasses the Paragraph hook; give it the prose tone before first render. - text.Style = (Style)Application.Current.Resources["ChatProseRichTextStyle"]; - text.ClearValue(Microsoft.UI.Xaml.Controls.RichTextBlock.ForegroundProperty); - } - } - } - private static void StyleListMarker(TextBlock text, bool compactSecondaryReading) { var marker = text.Text ?? string.Empty; @@ -392,19 +323,6 @@ private static void StyleListMarker(TextBlock text, bool compactSecondaryReading text.FontFamily = new FontFamily(ChatVisuals.TaskMarkerFontFamily); } - private static T? FirstDescendant(DependencyObject root) where T : DependencyObject - { - for (var index = 0; index < VisualTreeHelper.GetChildrenCount(root); index++) - { - var child = VisualTreeHelper.GetChild(root, index); - if (child is T match) - return match; - if (FirstDescendant(child) is { } nested) - return nested; - } - return null; - } - internal static Element ThematicBreak() => Border(Empty()) .Height(1) @@ -443,44 +361,35 @@ internal static Element BlockQuote(Element quote) } /// - /// Web-parity table: Reactor's per-row selection grids are flattened into one grid, so columns - /// stay aligned and copy order is unchanged, inside a rounded frame with 1px cell rules and a - /// tinted header row. Columns stay star-sized so long cells keep wrapping at narrow widths. + /// Web-parity table: Reactor's message-level table grid (Auto rows, Star columns, so long cells + /// keep wrapping at narrow widths) gets a rounded frame, 1px cell rules and a tinted header row. + /// Reactor's Table hook only receives rows with TextSelection enabled, so the built grid is + /// restyled instead; tables nested in lists or quotes keep Reactor's plain grid. /// - internal static Element Table(Element[] rows, MarkdownAlign[] alignments) + private static Element Table(Element block) { - var cellRows = rows.Select(row => (row as GridElement)?.Children.ToArray()).ToArray(); - if (cellRows.Length == 0 || cellRows.Any(cells => cells is null)) - return VStack(0, rows); - var columns = Math.Max(alignments.Length, cellRows.Max(cells => cells!.Length)); - if (columns == 0) - return VStack(0, rows); + if (block is not GridElement { Definition: { Rows.Length: > 0 } definition } grid || + definition.Columns.Any(column => column.Type != GridUnitType.Star)) + return block; + var columns = definition.Columns.Length; + var rows = definition.Rows.Length; var stroke = Theme.Ref("ChatStrokeBrush"); var inner = ChatVisuals.TableRadius - 1; - var children = new List + // GFM tables always start with exactly one header row. + Element header = Border(Empty()) + .Background(Theme.Ref("SubtleFillColorSecondaryBrush")) + .CornerRadius(inner, inner, 0, 0) + .AccessibilityView(Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw) + .Grid(row: 0, column: 0, columnSpan: columns); + var cells = grid.Children.Select(cell => { - // GFM tables always start with exactly one header row. - Border(Empty()) - .Background(Theme.Ref("SubtleFillColorSecondaryBrush")) - .CornerRadius(inner, inner, 0, 0) - .AccessibilityView(Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw) - .Grid(row: 0, column: 0, columnSpan: columns), - }; - for (var row = 0; row < cellRows.Length; row++) - { - var cells = cellRows[row]!; - for (var column = 0; column < cells.Length; column++) - { - children.Add(Border(cells[column]) - .BorderBrush(stroke) - .BorderThickness(0, 0, column == columns - 1 ? 0 : 1, row == cellRows.Length - 1 ? 0 : 1) - .Grid(row: row, column: column)); - } - } - return Border(Grid( - Enumerable.Repeat(GridSize.Star(), columns).ToArray(), - Enumerable.Repeat(GridSize.Auto, cellRows.Length).ToArray(), - children.ToArray())) + var at = cell.Attached?.GetValueOrDefault(typeof(GridAttached)) as GridAttached ?? new GridAttached(); + return (Element)Border(cell) + .BorderBrush(stroke) + .BorderThickness(0, 0, at.Column == columns - 1 ? 0 : 1, at.Row == rows - 1 ? 0 : 1) + .Grid(row: at.Row, column: at.Column); + }); + return Border(grid with { Children = [header, .. cells] }) .CornerRadius(ChatVisuals.TableRadius) .BorderThickness(1) .BorderBrush(stroke) @@ -506,21 +415,20 @@ private static Element Format(Element element, double size, bool heading) return element.Margin(0, 0, 0, 0); } - internal static Element CodeBlock(string code, string? language, Func? tryCopy = null, - MarkdownTextSelectionOptions? selectionOptions = null) + internal static Element CodeBlock(string code, string? language, Func? tryCopy = null) { var copyLabel = Localized("Chat_Code_Copy", "Copy code"); var label = string.IsNullOrWhiteSpace(language) ? Localized("Chat_Code_LanguageFallback", "code") : language.ToLowerInvariant(); - Element codeBody = (RichTextBlock(code) with { FontSize = ChatVisuals.CodeSize }) + var codeBody = (RichTextBlock(code) with { FontSize = ChatVisuals.CodeSize }) .Set(text => { text.Style = (Style)Application.Current.Resources["ChatCodeTextStyle"]; text.FontFamily = new FontFamily(ChatVisuals.CodeFontFamily); text.LineHeight = ChatVisuals.CodeLineHeight; text.LineStackingStrategy = LineStackingStrategy.MaxHeight; - text.IsTextSelectionEnabled = selectionOptions is null; + text.IsTextSelectionEnabled = true; text.TextWrapping = TextWrapping.NoWrap; ApplyCodeSyntax(text, code, language); }) @@ -538,8 +446,6 @@ internal static Element CodeBlock(string code, string? language, Func? TryCopyCode, bool CompactSecondaryReading); + +/// Mount through ReactorChatTimeline.BuildSafeMarkdown. +internal sealed class ChatMarkdownView : Component +{ + public override Element Render() => + ReactorChatTimeline.RenderSafeMarkdown(Props.Text, Props.TryCopyCode, Props.CompactSecondaryReading); +} + internal sealed record ChatMarkdownDisclosureProps( - ChatMarkdownDisclosure Disclosure, Func RenderMarkdown, - MarkdownTextSelectionOptions SelectionOptions); + ChatMarkdownDisclosure Disclosure, Func RenderMarkdown); internal sealed class ChatMarkdownDisclosureView : Component { @@ -691,17 +605,14 @@ public override Element Render() var (expanded, setExpanded) = UseState(disclosure.InitiallyExpanded); return (Expander( disclosure.Summary, - MarkdownTextSelection.Visibility(expanded, - ChatMarkdownPresentation.Disclosures( - disclosure.Body, Props.RenderMarkdown, Props.SelectionOptions)) + ChatMarkdownPresentation.Disclosures(disclosure.Body, Props.RenderMarkdown) .Margin(ChatVisuals.ToolRowTitleInset, 4, 0, 8), expanded, setExpanded) with { HeaderContent = ToolCallCardRenderer.BuildDisclosureHeader(expanded, - MarkdownTextSelection.Surface(disclosure.Summary, - TextBlock(disclosure.Summary).TextWrapping(TextWrapping.Wrap) - .FontFamily(ChatVisuals.BodyFontFamily))), + TextBlock(disclosure.Summary).TextWrapping(TextWrapping.Wrap) + .FontFamily(ChatVisuals.BodyFontFamily)), }) .Padding(0) .Background(Theme.Ref("SubtleFillColorTransparentBrush")) diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatRunProgressPresentation.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatRunProgressPresentation.cs index 5f6a2507ef..63e0f18ea7 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatRunProgressPresentation.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ChatRunProgressPresentation.cs @@ -18,6 +18,10 @@ internal static class ChatRunProgressProjection internal const string RetryLabel = "Retrying the model request"; internal const int StatusSwapMilliseconds = 220; + internal static bool NeedsClock(ChatRunProgressPresentation progress, DateTimeOffset now) => + progress.IsInProgress && (progress.RetryAt > now || + progress.StartedAt.HasValue && (progress.Waiting || !progress.EndedAt.HasValue)); + /// /// True while a status swap started at is still moving, so a newer /// phase continues it instead of restarting. No swap has started before the first phase change. diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatToolActivityPresentation.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatToolActivityPresentation.cs index b3e7848378..75d48b7b14 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatToolActivityPresentation.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ChatToolActivityPresentation.cs @@ -522,7 +522,8 @@ private static ChatToolActivityRow Standalone( private static string NormalizeToolName(string? toolName) { var name = (toolName ?? string.Empty).Trim().ToLowerInvariant(); - foreach (var prefix in new[] { "functions.", "tools.", "mcp." }) + ReadOnlySpan prefixes = ["functions.", "tools.", "mcp."]; + foreach (var prefix in prefixes) { if (name.StartsWith(prefix, StringComparison.Ordinal)) name = name[prefix.Length..]; @@ -530,11 +531,16 @@ private static string NormalizeToolName(string? toolName) return name.Replace('-', '_').Replace(' ', '_'); } - private static bool Matches(string name, params string[] aliases) => - aliases.Any(alias => - string.Equals(name, alias, StringComparison.Ordinal) - || name.EndsWith($".{alias}", StringComparison.Ordinal) - || name.EndsWith($"_{alias}", StringComparison.Ordinal)); + private static bool Matches(string name, params ReadOnlySpan aliases) + { + foreach (var alias in aliases) + { + if (name.EndsWith(alias, StringComparison.Ordinal) + && (name.Length == alias.Length || name[name.Length - alias.Length - 1] is '.' or '_')) + return true; + } + return false; + } private static string? ReadString(JsonObject? args, string propertyName) => args?.TryGetPropertyValue(propertyName, out var node) == true diff --git a/src/OpenClaw.Tray.WinUI/Chat/NativeWorkingClaw.cs b/src/OpenClaw.Tray.WinUI/Chat/NativeWorkingClaw.cs index fa7e8a7243..30bdfe5e5d 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/NativeWorkingClaw.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/NativeWorkingClaw.cs @@ -23,6 +23,7 @@ internal sealed class NativeWorkingClaw : Control private readonly CompositeTransform _flex = new() { CenterX = 9, CenterY = 9 }; private readonly ScaleTransform _flip = new() { CenterX = 9, CenterY = 9 }; private readonly ScaleTransform _sparkScale = new() { CenterX = 5.5, CenterY = 5.5 }; + private Matrix3DProjection? _projection; private readonly TextBlock _spark = new() { Text = "\u2726", FontSize = 11, Width = 11, Height = 14, Opacity = 0, @@ -132,13 +133,12 @@ private void Apply() var matrix = Matrix4x4.CreateTranslation(-9, -9, 0) * Matrix4x4.CreateRotationY((float)(sample.Body.RotationY * Math.PI / 180)) * perspective * Matrix4x4.CreateTranslation(9, 9, 0); - _body.Projection = new Matrix3DProjection - { - ProjectionMatrix = new Matrix3D(matrix.M11, matrix.M12, matrix.M13, matrix.M14, - matrix.M21, matrix.M22, matrix.M23, matrix.M24, - matrix.M31, matrix.M32, matrix.M33, matrix.M34, - matrix.M41, matrix.M42, matrix.M43, matrix.M44), - }; + var projection = _projection ??= new Matrix3DProjection(); + projection.ProjectionMatrix = new Matrix3D(matrix.M11, matrix.M12, matrix.M13, matrix.M14, + matrix.M21, matrix.M22, matrix.M23, matrix.M24, + matrix.M31, matrix.M32, matrix.M33, matrix.M34, + matrix.M41, matrix.M42, matrix.M43, matrix.M44); + _body.Projection = projection; } } } diff --git a/src/OpenClaw.Tray.WinUI/Chat/ReactorChatSubagents.cs b/src/OpenClaw.Tray.WinUI/Chat/ReactorChatSubagents.cs index ad954cf9ba..f0c243e7fe 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ReactorChatSubagents.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ReactorChatSubagents.cs @@ -36,7 +36,8 @@ public override Element Render() }), panels); var summary = ChatSubagentPresentation.Project(Props.Children, DateTimeOffset.UtcNow); // The tab body stays mounted behind other tabs; only refresh while it is the selected tab. - var ticking = summary.Running.Count > 0 && panels.Selected == ChatConversationPanelState.SubagentsSlot; + var ticking = panels.Presented && panels.Selected == ChatConversationPanelState.SubagentsSlot + && summary.Running.Any(row => row.Status != ChatSubagentStatus.Queued && row.Thread.CreatedAt.HasValue); UseEffect((Func)(() => { if (!ticking) return static () => { }; diff --git a/src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs b/src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs index 4d779babfd..2bb32403c9 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs @@ -978,25 +978,21 @@ private static Element BuildAssistantAvatarSlot(ReactorTimelineRow row) // URLs) are real WinUI hyperlinks in the Windows accent color, without an // underline, that open in the default browser. Reactor admits only http/https/mailto; any other // scheme renders as plain label text. Images and raw HTML stay inert. + // Realized rows refresh on every streamed delta; the component's props gate skips + // sanitizing, parsing and rebuilding messages whose text has not changed. internal static Element BuildSafeMarkdown( - string? text, Func? tryCopy = null, bool compactSecondaryReading = false) - => Component( - new(text ?? string.Empty, tryCopy, compactSecondaryReading)); + string? text, Func? tryCopy = null, bool compactSecondaryReading = false) => + Component(new(text, tryCopy, compactSecondaryReading)); - internal static Element BuildMessageMarkdown( - string text, Func? tryCopy, - MarkdownTextSelectionOptions selectionOptions, bool compactSecondaryReading) + internal static Element RenderSafeMarkdown(string? text, Func? tryCopy, bool compactSecondaryReading) { - var sanitized = ChatMarkdownSanitizer.Sanitize(text, preserveWebLinks: true); + var sanitized = ChatMarkdownSanitizer.Sanitize(text ?? string.Empty, preserveWebLinks: true); var parts = ChatMarkdownDisclosures.Parse(sanitized); return ChatMarkdownPresentation.Disclosures( - parts, - markdown => BuildInertMarkdown(markdown, tryCopy, selectionOptions, compactSecondaryReading), - selectionOptions); + parts, markdown => BuildInertMarkdown(markdown, tryCopy, compactSecondaryReading)); } - private static Element BuildInertMarkdown(string text, Func? tryCopy, - MarkdownTextSelectionOptions selectionOptions, bool compactSecondaryReading) + private static Element BuildInertMarkdown(string text, Func? tryCopy, bool compactSecondaryReading) { var options = new MarkdownOptions { @@ -1007,40 +1003,19 @@ private static Element BuildInertMarkdown(string text, Func? tryCo ListItem = row => ChatMarkdownPresentation.ListItem(row, compactSecondaryReading), ThematicBreak = ChatMarkdownPresentation.ThematicBreak, BlockQuote = ChatMarkdownPresentation.BlockQuote, - Table = ChatMarkdownPresentation.Table, TableHeaderBackground = null, TableCellPadding = new Thickness(ChatVisuals.TableCellHorizontalInset, ChatVisuals.TableCellVerticalInset, ChatVisuals.TableCellHorizontalInset, ChatVisuals.TableCellVerticalInset), - TextSelection = selectionOptions, - CodeBlock = (code, language) => ChatMarkdownPresentation.CodeBlock( - code, language, tryCopy: tryCopy, selectionOptions: selectionOptions), + CodeBlock = (code, language) => ChatMarkdownPresentation.CodeBlock(code, language, tryCopy: tryCopy), CodeFontFamily = ChatVisuals.CodeFontFamily, // GitHub dialect (tables, strikethrough, task lists, URL/www/email autolinks) // without HTML, plus the web chat's `breaks: true` single-newline line breaks. ParserFlags = MarkdownParserFlags.DialectGitHub | MarkdownParserFlags.NoHtml | MarkdownParserFlags.HardSoftBreaks, - Image = (alt, _) => - { - var label = string.IsNullOrWhiteSpace(alt) ? "[Image]" : $"[Image: {alt}]"; - return MarkdownTextSelection.Surface(label, Text( - label, - compactSecondaryReading ? ChatVisuals.ReasoningSize : ChatVisuals.BodySize, - FontWeights.Normal, - "ChatProseTextBrush") - .FontFamily(ChatVisuals.BodyFontFamily) - .IsTextSelectionEnabled(false)); - }, - HtmlBlock = raw => - { - var label = ChatMarkdownSanitizer.FlattenRawHtmlBlockToInertText(raw); - return MarkdownTextSelection.Surface(label, Text( - label, - compactSecondaryReading ? ChatVisuals.ReasoningSize : ChatVisuals.BodySize, - FontWeights.Normal, - "ChatProseTextBrush") - .FontFamily(ChatVisuals.BodyFontFamily) - .IsTextSelectionEnabled(false)); - }, + Image = (alt, _) => InertText( + string.IsNullOrWhiteSpace(alt) ? "[Image]" : $"[Image: {alt}]", compactSecondaryReading), + HtmlBlock = raw => InertText( + ChatMarkdownSanitizer.FlattenRawHtmlBlockToInertText(raw), compactSecondaryReading), }; return ChatMarkdownPresentation.WithInlineStyling( @@ -1049,6 +1024,14 @@ private static Element BuildInertMarkdown(string text, Func? tryCo compactSecondaryReading); } + private static Element InertText(string label, bool compactSecondaryReading) => + Text(label, + compactSecondaryReading ? ChatVisuals.ReasoningSize : ChatVisuals.BodySize, + FontWeights.Normal, + "ChatProseTextBrush") + .FontFamily(ChatVisuals.BodyFontFamily) + .IsTextSelectionEnabled(true); + private static Element BuildAssistantFooter( ReactorTimelineRow row, ChatTimelineItem entry, diff --git a/src/OpenClaw.Tray.WinUI/Chat/ReactorConversationPanelHost.cs b/src/OpenClaw.Tray.WinUI/Chat/ReactorConversationPanelHost.cs index 545524231d..a4937d56bc 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ReactorConversationPanelHost.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ReactorConversationPanelHost.cs @@ -47,7 +47,7 @@ public override Element Render() // Distance from the window's top edge, so a header that joins the caption row can reach it. var (topOffset, setTopOffset) = UseState(0d); var topOffsetRef = UseRef(0d); - var layoutWatch = UseRef?>(null); + var layoutWatch = UseRef?>(null); var focus = UseRef(null); var tabView = UseRef(null); var motion = UseRef(null).Current ??= new PanelWidthMotion(); @@ -399,8 +399,7 @@ void TogglePresented() var root = (FrameworkElement)control; layoutWatch.Current = (_, _) => { - // Only a host offered a title row needs its window offset (never the companion window). - if (state.TitleRowHeight <= 0 || !root.IsLoaded || root.XamlRoot is null) return; + if (!root.IsLoaded || root.XamlRoot is null) return; double y; try { y = root.TransformToVisual(null).TransformPoint(default).Y; } catch (ArgumentException) { return; } @@ -408,7 +407,7 @@ void TogglePresented() topOffsetRef.Current = y; setTopOffset(y); }; - root.LayoutUpdated += layoutWatch.Current; + root.EffectiveViewportChanged += layoutWatch.Current; }) .OnUnmount(control => { @@ -416,7 +415,7 @@ void TogglePresented() motion.Root = null; motion.Frame = null; ChatVisuals.StopObserving(control); - if (layoutWatch.Current is { } watch) ((FrameworkElement)control).LayoutUpdated -= watch; + if (layoutWatch.Current is { } watch) ((FrameworkElement)control).EffectiveViewportChanged -= watch; layoutWatch.Current = null; focus.Current?.Dispose(); focus.Current = null; diff --git a/src/OpenClaw.Tray.WinUI/Chat/ReactorItemsViewScrollController.cs b/src/OpenClaw.Tray.WinUI/Chat/ReactorItemsViewScrollController.cs index eead03bf8f..6ffeb34ee7 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ReactorItemsViewScrollController.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ReactorItemsViewScrollController.cs @@ -94,7 +94,8 @@ public UIElement Update( else { positioner.TailVisibilityChanged = newElement.TailVisibilityChanged; - positioner.HistoryNavigation.Update(newElement.HistoryAnchors); + if (!oldElement.HistoryAnchors.SequenceEqual(newElement.HistoryAnchors)) + positioner.HistoryNavigation.Update(newElement.HistoryAnchors); positioner.UpdateContentRevision(newElement.ContentRevision); if (!string.Equals(oldElement.InitialTailRequestKey, newElement.InitialTailRequestKey, StringComparison.Ordinal)) positioner.Request( diff --git a/src/OpenClaw.Tray.WinUI/Chat/ReactorRunProgress.cs b/src/OpenClaw.Tray.WinUI/Chat/ReactorRunProgress.cs index 02485dc603..58caf4f035 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ReactorRunProgress.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ReactorRunProgress.cs @@ -22,10 +22,12 @@ public override Element Render() var progress = Props.Progress; var entrance = UseRef(null); var reducedMotion = ChatMotion.IsReducedMotion(UseReducedMotion()); - var (now, setNow) = UseState(DateTimeOffset.UtcNow); + var (_, setNow) = UseState(DateTimeOffset.UtcNow); + var now = DateTimeOffset.UtcNow; + var ticking = ChatRunProgressProjection.NeedsClock(progress, now); UseEffect((Func)(() => { - if (!progress.IsInProgress) + if (!ticking) return static () => { }; var timer = DispatcherQueue.GetForCurrentThread().CreateTimer(); timer.Interval = TimeSpan.FromSeconds(1); @@ -33,7 +35,7 @@ public override Element Render() timer.Tick += tick; timer.Start(); return () => { timer.Stop(); timer.Tick -= tick; }; - }), progress.IsInProgress, progress.StartedAt); + }), ticking); var elapsed = ChatRunProgressProjection.Elapsed(progress.StartedAt, progress.Waiting ? now : progress.EndedAt ?? (progress.Active ? now : progress.StartedAt ?? now)); var label = progress.Label == ChatRunProgressProjection.RetryLabel diff --git a/src/OpenClaw.Tray.WinUI/Helpers/LocalizationHelper.cs b/src/OpenClaw.Tray.WinUI/Helpers/LocalizationHelper.cs index 5431d6d33f..0eb32ccfc0 100644 --- a/src/OpenClaw.Tray.WinUI/Helpers/LocalizationHelper.cs +++ b/src/OpenClaw.Tray.WinUI/Helpers/LocalizationHelper.cs @@ -11,43 +11,56 @@ public static class LocalizationHelper _languageOverride ?? System.Globalization.CultureInfo.CurrentUICulture.Name; private static ResourceManager? _resourceManager; - private static ResourceContext? _overrideContext; + private static ResourceContext? _context; private static string? _languageOverride; + private static readonly object s_resourceLock = new(); + private static readonly Dictionary s_strings = new(StringComparer.Ordinal); private static readonly ConcurrentDictionary s_loggedLookupFailures = new(); private const int MaxLoggedLookupFailures = 1024; private static int s_lookupFailureLimitLogged; /// /// Force a specific language for testing (e.g. "zh-CN"). - /// Must be called before any GetString calls. + /// Invalidates cached strings and resource qualifiers. /// public static void SetLanguageOverride(string language) { - _languageOverride = language; - _resourceManager = null; - _overrideContext = null; + lock (s_resourceLock) + { + _languageOverride = language; + _resourceManager = null; + _context = null; + s_strings.Clear(); + } } private static ResourceManager Manager => _resourceManager ??= new ResourceManager(); private static ResourceContext GetContext() { - if (_overrideContext != null) return _overrideContext; + if (_context != null) return _context; + _context = Manager.CreateResourceContext(); if (_languageOverride != null) + _context.QualifierValues["Language"] = _languageOverride; + return _context; + } + + public static string GetString(string resourceKey) + { + lock (s_resourceLock) { - _overrideContext = Manager.CreateResourceContext(); - _overrideContext.QualifierValues["Language"] = _languageOverride; - return _overrideContext; + if (s_strings.TryGetValue(resourceKey, out var cached)) + return cached; + return LookupString(resourceKey); } - return Manager.CreateResourceContext(); } - public static string GetString(string resourceKey) + private static string LookupString(string resourceKey) { var found = TryGetValueAsString(resourceKey, out var value, out var lookupFailure); if (found && !string.IsNullOrEmpty(value)) { - return value; + return s_strings[resourceKey] = value; } if (TryGetXamlPropertyResourcePath(resourceKey, out var propertyResourcePath)) @@ -55,7 +68,7 @@ public static string GetString(string resourceKey) var propertyFound = TryGetValueAsString(propertyResourcePath, out value, out var propertyLookupFailure); if (propertyFound && !string.IsNullOrEmpty(value)) { - return value; + return s_strings[resourceKey] = value; } if (!found) diff --git a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw index 6a05e92842..298839a540 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -6448,7 +6448,6 @@ Open Channels in the web dashboard. 1 queued message Copied Copy failed - Message text Effort Faster Smarter diff --git a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw index c6cd261329..d17be3c1b2 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -6376,7 +6376,6 @@ Ouvrir Canaux dans le tableau de bord web. 1 message en attente Copié Échec de la copie - Texte du message Niveau d'effort Plus rapide Plus approfondi diff --git a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw index 29799caed7..ad4441dc25 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -6377,7 +6377,6 @@ Open Kanalen in het webdashboard. 1 bericht in wachtrij Gekopieerd Kopiëren mislukt - Berichttekst Denkinspanning Sneller Grondiger diff --git a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw index 932c4cf399..579c2c857c 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -6429,7 +6429,6 @@ Abrir Canais no painel web. 1 mensagem na fila Copiado Falha ao copiar - Texto da mensagem Esforço Mais rápido Mais detalhado diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw index 900adc0cb5..58a1ea116b 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -6376,7 +6376,6 @@ 1 条排队消息 已复制 复制失败 - 消息文本 推理强度 更快 更深入 diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw index d708ad8fce..bdd5eb4848 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -6376,7 +6376,6 @@ 1 則佇列訊息 已複製 複製失敗 - 訊息文字 推理強度 更快 更深入 diff --git a/src/OpenClaw.Tray.WinUI/Windows/DesktopCompanionWindow.cs b/src/OpenClaw.Tray.WinUI/Windows/DesktopCompanionWindow.cs index 5ea9755255..cbffe756bd 100644 --- a/src/OpenClaw.Tray.WinUI/Windows/DesktopCompanionWindow.cs +++ b/src/OpenClaw.Tray.WinUI/Windows/DesktopCompanionWindow.cs @@ -67,7 +67,6 @@ internal DesktopCompanionWindow(Action openNotifications, Action openChat, Actio _frameMonitor = DesktopCompanionNative.CreateFrameMonitor(_hwnd); _bubbleTimer.Tick += OnBubbleTimeout; _host.Loaded += OnLoaded; - _host.LayoutUpdated += OnHostLayoutUpdated; _host.KeyDown += OnKeyDown; _bubbleHost.KeyDown += OnBubbleKeyDown; _bubbleHost.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(OnBubblePressed), true); @@ -132,6 +131,7 @@ private void OnAppWindowClosing(AppWindow sender, AppWindowClosingEventArgs args private void OnAppWindowChanged(AppWindow sender, AppWindowChangedEventArgs args) { + if (args.DidSizeChange) UpdateRegion(); if (args.DidPositionChange || args.DidSizeChange) RefreshBubblePlacement(); } @@ -254,7 +254,6 @@ private void RefreshBubblePlacement() _bubblePopup.HorizontalOffset = _nudge ? 4.01 : 4; } - private void OnHostLayoutUpdated(object? sender, object args) => UpdateRegion(); private void OnBubbleSizeChanged(object sender, SizeChangedEventArgs args) => UpdateBubblePopup(); private void OnBubbleTimeout(object? sender, object args) => Present(null); private void OnBubbleEntered(object sender, PointerRoutedEventArgs args) @@ -457,7 +456,6 @@ private void OnClosed(object sender, WindowEventArgs args) SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged; SystemEvents.UserPreferenceChanged -= OnUserPreferenceChanged; _host.Loaded -= OnLoaded; - _host.LayoutUpdated -= OnHostLayoutUpdated; _host.KeyDown -= OnKeyDown; _bubbleHost.KeyDown -= OnBubbleKeyDown; _bubbleHost.RemoveHandler(UIElement.PointerPressedEvent, new PointerEventHandler(OnBubblePressed)); diff --git a/src/OpenClaw.Tray.WinUI/Windows/WorkspaceTitleRow.cs b/src/OpenClaw.Tray.WinUI/Windows/WorkspaceTitleRow.cs index dae1568ace..6cfccdd585 100644 --- a/src/OpenClaw.Tray.WinUI/Windows/WorkspaceTitleRow.cs +++ b/src/OpenClaw.Tray.WinUI/Windows/WorkspaceTitleRow.cs @@ -32,7 +32,6 @@ internal sealed class WorkspaceTitleRow : IDisposable private readonly Action _invalidate; private FrameworkElement? _tabStrip; private FrameworkElement? _tabList; - private Rect? _tabBounds; private XamlRoot? _root; internal WorkspaceTitleRow(AppWindow window, TitleBar titleBar, FrameworkElement actions, @@ -103,16 +102,47 @@ private void WatchScale(XamlRoot root) private void Track(FrameworkElement? tabStrip) { if (ReferenceEquals(tabStrip, _tabStrip)) return; - if (_tabStrip is not null) _tabStrip.LayoutUpdated -= OnTabStripLayoutUpdated; + if (_tabStrip is not null) _tabStrip.LayoutUpdated -= FindTabListAfterLayout; + TrackTabList(null); _tabStrip = tabStrip; - _tabList = null; - if (tabStrip is not null) tabStrip.LayoutUpdated += OnTabStripLayoutUpdated; + if (tabStrip is null) return; + if (FindPart(tabStrip) is { } tabs) TrackTabList(tabs); + else tabStrip.LayoutUpdated += FindTabListAfterLayout; + } + + private void FindTabListAfterLayout(object? sender, object args) + { + if (_tabStrip is not { IsLoaded: true } strip || FindPart(strip) is not { } tabs) return; + strip.LayoutUpdated -= FindTabListAfterLayout; + TrackTabList(tabs); + _invalidate(); + } + + private void TrackTabList(FrameworkElement? tabs) + { + if (_tabList is not null) + { + _tabList.SizeChanged -= OnTabListSizeChanged; + _tabList.EffectiveViewportChanged -= OnTabListViewportChanged; + _tabList.Unloaded -= OnTabListUnloaded; + } + _tabList = tabs; + if (tabs is null) return; + tabs.SizeChanged += OnTabListSizeChanged; + tabs.EffectiveViewportChanged += OnTabListViewportChanged; + tabs.Unloaded += OnTabListUnloaded; + } + + private void OnTabListUnloaded(object sender, RoutedEventArgs args) + { + TrackTabList(null); + if (_tabStrip is not null) _tabStrip.LayoutUpdated += FindTabListAfterLayout; + _invalidate(); } private void PlaceTabsPassthrough() { - _tabBounds = TabBounds(); - if (_tabBounds is not { } bounds) + if (TabBounds() is not { } bounds) { _tabsPassthrough.Visibility = Visibility.Collapsed; return; @@ -127,9 +157,7 @@ private void PlaceTabsPassthrough() /// Bounds of the tab items in pass-through canvas coordinates, once the template part is ready. private Rect? TabBounds() { - if (_tabStrip is not { IsLoaded: true } strip) return null; - if (_tabList is not { IsLoaded: true }) _tabList = FindPart(strip); - if (_tabList is not { ActualWidth: > 0, ActualHeight: > 0 } tabs) return null; + if (_tabList is not { IsLoaded: true, ActualWidth: > 0, ActualHeight: > 0 } tabs) return null; try { var origin = tabs.TransformToVisual(_passthroughCanvas).TransformPoint(default); @@ -138,18 +166,9 @@ private void PlaceTabsPassthrough() catch (ArgumentException) { return null; } } - // Tabs move without resizing when the panel or sidebar slides, and the template part can load a - // pass after the strip; re-place whenever the bounds appear, change or disappear. - private void OnTabStripLayoutUpdated(object? sender, object e) - { - var bounds = TabBounds(); - if (bounds is null && _tabBounds is null) return; - if (bounds is { } next && _tabBounds is { } previous && - Math.Abs(next.X - previous.X) < 0.5 && Math.Abs(next.Y - previous.Y) < 0.5 && - Math.Abs(next.Width - previous.Width) < 0.5 && Math.Abs(next.Height - previous.Height) < 0.5) - return; - _invalidate(); - } + // The panel or sidebar can slide the tabs without resizing them. + private void OnTabListViewportChanged(FrameworkElement sender, EffectiveViewportChangedEventArgs args) => _invalidate(); + private void OnTabListSizeChanged(object sender, SizeChangedEventArgs args) => _invalidate(); private static FrameworkElement? FindPart(DependencyObject parent) { diff --git a/tests/OpenClaw.TestSupport/Gateway/GatewayScenario.cs b/tests/OpenClaw.TestSupport/Gateway/GatewayScenario.cs index 3371cd9728..3e4b51b40d 100644 --- a/tests/OpenClaw.TestSupport/Gateway/GatewayScenario.cs +++ b/tests/OpenClaw.TestSupport/Gateway/GatewayScenario.cs @@ -40,8 +40,6 @@ public sealed partial class GatewayScenario public const string VoiceSessionKey = "agent:main:fixture-voice"; public const string VoiceTitle = "Fixture: historical realtime voice"; public const string VoiceFinalSentinel = "FIXTURE VOICE FINAL: all historical voice messages loaded."; - public const string MarkdownSelectionSessionKey = "agent:main:fixture-markdown-selection"; - public const string MarkdownSelectionTitle = "Fixture: Markdown selection"; public const string OwnerProfileId = "fixture-owner"; private static readonly DateTimeOffset Epoch = new(2026, 8, 20, 12, 0, 0, TimeSpan.Zero); @@ -77,7 +75,6 @@ public sealed partial class GatewayScenario public string Name => _hubMethods is not null ? (_hubReadOnly ? SessionsHubReadOnlyName : SessionsHubName) : _systems is not null ? _systems.Name : _boards is not null ? DashboardsName : _inboxFeed is not null ? "inbox-feed" : _accessibilityBaseline ? "accessibility-baseline" : _contextUsage is not null ? ContextUsageFixture.Name : UsageAnalytics.RichProviders ? UsageAnalyticsFixture.Name : IsInteractive ? InteractiveName : _cronJobs is not null ? AutomationsName : _setup is not null ? "native-setup" : _allowChatSend ? "chat-send" : _allowSessionMutations ? "session-mutations" : _allowAgentCreation ? "agent-creation" - : ContainsSession(MarkdownSelectionSessionKey) ? "markdown-selection" : ContainsSession(VoiceSessionKey) ? "fractional-voice-history" : BrowseName; public int Version => _allowChatSend ? 2 : 1; public int ProtocolVersion => GatewayProtocolContract.CurrentVersion; @@ -458,20 +455,6 @@ public static GatewayScenario CreateBrowse(bool allowAgentCreation = false, bool publishedModelCatalog: publishedModelCatalog); } - /// Creates synthetic Markdown history for shipped-app text-selection proof. - public static GatewayScenario CreateMarkdownSelection(string markdownMessage) - { - ArgumentException.ThrowIfNullOrWhiteSpace(markdownMessage); - var browse = CreateBrowse(); - var selection = CreateSession(MarkdownSelectionSessionKey, MarkdownSelectionTitle, "main", "browse", 5, - [ - "FIXTURE SELECTION BEFORE: a separate message must not be copied.", - markdownMessage, - "FIXTURE SELECTION AFTER: a separate message must not be copied." - ]); - return new([.. browse._sessions, selection], browse._reads); - } - /// /// Creates a browse fixture with a profile whose avatar eligibility is resolved by the Gateway. /// It intentionally omits hasAvatar; when email is null, it also omits emails. diff --git a/tests/OpenClaw.Tray.Tests/ChatStreamingParityTests.cs b/tests/OpenClaw.Tray.Tests/ChatStreamingParityTests.cs index 6a31ec0f5e..9ef3be379c 100644 --- a/tests/OpenClaw.Tray.Tests/ChatStreamingParityTests.cs +++ b/tests/OpenClaw.Tray.Tests/ChatStreamingParityTests.cs @@ -576,6 +576,22 @@ public void StatusStreams_AreVisibleWithoutTerminatingTheTurn(string stream, str Assert.Equal(expected, ChatRunProgressProjection.Project(state)!.Label); } + [Fact] + public void RunClock_OnlyTicksForChangingElapsedTimeOrRetryCountdown() + { + var active = new ChatRunProgressPresentation(null, null, "Working", Active: true, Waiting: false); + Assert.False(ChatRunProgressProjection.NeedsClock(active, Start)); + Assert.True(ChatRunProgressProjection.NeedsClock(active with { StartedAt = Start }, Start)); + + var yielded = active with { Active = false, Waiting = true, StartedAt = Start, EndedAt = Start.AddSeconds(1) }; + Assert.True(ChatRunProgressProjection.NeedsClock(yielded, Start.AddSeconds(2))); + Assert.False(ChatRunProgressProjection.NeedsClock(yielded with { Waiting = false }, Start.AddSeconds(2))); + + var retrying = active with { Label = ChatRunProgressProjection.RetryLabel, RetryAt = Start.AddSeconds(1) }; + Assert.True(ChatRunProgressProjection.NeedsClock(retrying, Start)); + Assert.False(ChatRunProgressProjection.NeedsClock(retrying, Start.AddSeconds(1))); + } + [Fact] public void RetryStatus_CountsDownToSuppliedRetryAt_AndClearsWhenWorkResumes() { diff --git a/tests/OpenClaw.Tray.Tests/ChatToolActivityPresentationTests.cs b/tests/OpenClaw.Tray.Tests/ChatToolActivityPresentationTests.cs index af5a308445..552392cb3c 100644 --- a/tests/OpenClaw.Tray.Tests/ChatToolActivityPresentationTests.cs +++ b/tests/OpenClaw.Tray.Tests/ChatToolActivityPresentationTests.cs @@ -549,11 +549,31 @@ public void Project_StreamingAndTerminalUpdatesPreserveGroupIdentityAndExplicitE [InlineData("web_fetch", ChatToolActivityCategory.Fetch)] [InlineData("WebFetch", ChatToolActivityCategory.Fetch)] [InlineData("custom.calendar", ChatToolActivityCategory.Generic)] + [InlineData("vendor.read_file", ChatToolActivityCategory.Read)] + [InlineData("vendor_read_file", ChatToolActivityCategory.Read)] + [InlineData("vendorread_file", ChatToolActivityCategory.Generic)] + [InlineData("vendor.fetch", ChatToolActivityCategory.Fetch)] + [InlineData("vendorfetch", ChatToolActivityCategory.Generic)] public void Classify_CoversWebAndWindowsAliases( string name, ChatToolActivityCategory expected) => Assert.Equal(expected, ChatToolActivityPresentation.Classify(name)); + [Fact] + public void Classify_NormalizedNamesDoNotAllocatePerAlias() + { + const string name = "custom.calendar"; + for (var index = 0; index < 256; index++) + ChatToolActivityPresentation.Classify(name); + + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var index = 0; index < 128; index++) + ChatToolActivityPresentation.Classify(name); + var allocated = GC.GetAllocatedBytesForCurrentThread() - before; + + Assert.Equal(0, allocated); + } + [Fact] public void Classify_TextEditorUsesCommand() { diff --git a/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs b/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs index 4af419f6fb..a0fce58509 100644 --- a/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs +++ b/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs @@ -17,10 +17,13 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() timeline.Replace("\r\n", "\n", StringComparison.Ordinal), StringComparison.Ordinal); Assert.Contains( - "var sanitized = ChatMarkdownSanitizer.Sanitize(text, preserveWebLinks: true);", + "var sanitized = ChatMarkdownSanitizer.Sanitize(text ?? string.Empty, preserveWebLinks: true);", timeline, StringComparison.Ordinal); Assert.Contains("ChatMarkdownDisclosures.Parse(sanitized)", timeline, StringComparison.Ordinal); + // Streaming refreshes every realized row; unchanged messages must stay behind the props gate. + Assert.Contains("Component(new(text, tryCopy, compactSecondaryReading))", + timeline, StringComparison.Ordinal); Assert.Contains("Image = (alt, _) =>", timeline, StringComparison.Ordinal); // Web-parity links: Reactor's native hyperlink (http/https/mailto only), no inert override. Assert.DoesNotContain("LinkBuilder =", timeline, StringComparison.Ordinal); @@ -43,9 +46,9 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() Assert.DoesNotContain("BuildWrappingMarkdownListItem", timeline, StringComparison.Ordinal); Assert.Contains("Heading = ChatMarkdownPresentation.Heading", timeline, StringComparison.Ordinal); Assert.Contains("Paragraph = ChatMarkdownPresentation.Paragraph", timeline, StringComparison.Ordinal); - Assert.Contains("tryCopy: tryCopy, selectionOptions: selectionOptions)", timeline, StringComparison.Ordinal); + Assert.Contains("ChatMarkdownPresentation.CodeBlock(code, language, tryCopy: tryCopy)", timeline, StringComparison.Ordinal); // List items keep Reactor's Auto/Star wrapping row; the hook only places the marker. - Assert.Contains("row is not GridElement { Children: [var markerElement, var content] } grid", presentation, StringComparison.Ordinal); + Assert.Contains("row is not GridElement { Children: [TextBlockElement marker, var content] } grid", presentation, StringComparison.Ordinal); // Web-parity list rhythm and divider come from presentation hooks. Assert.Contains("UnorderedList = items => ChatMarkdownPresentation.List(items, compactSecondaryReading)", timeline, StringComparison.Ordinal); Assert.Contains("OrderedList = (_, items) => ChatMarkdownPresentation.List(items, compactSecondaryReading)", timeline, StringComparison.Ordinal); @@ -54,17 +57,16 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() Assert.Contains("BlockQuote = ChatMarkdownPresentation.BlockQuote", timeline, StringComparison.Ordinal); Assert.Contains("quote is not BorderElement { Child: { } content }", presentation, StringComparison.Ordinal); Assert.Contains("ThemeRef.Resolve(\"ChatMutedTextBrush\"", presentation, StringComparison.Ordinal); - // Web-parity tables: one flattened grid (aligned columns, unchanged copy order) in a rounded frame. - Assert.Contains("Table = ChatMarkdownPresentation.Table", timeline, StringComparison.Ordinal); + // Web-parity tables: Reactor's single table grid is restyled in a rounded frame; the + // Table hook is not used because it receives no rows without TextSelection. + Assert.DoesNotContain("Table = ChatMarkdownPresentation.Table", timeline, StringComparison.Ordinal); Assert.Contains("TableHeaderBackground = null", timeline, StringComparison.Ordinal); - Assert.Contains("rows.Select(row => (row as GridElement)?.Children.ToArray())", presentation, StringComparison.Ordinal); + Assert.Contains("ResetBlockEdges(Table(child)", presentation, StringComparison.Ordinal); // Fenced code keeps Reactor's single literal run (so reconcile never rebuilds it and // selection survives) and is colored by theme-resolved highlighters. Assert.Contains("ApplyCodeSyntax(text, code, language);", presentation, StringComparison.Ordinal); Assert.Contains("ClearCodeSyntax((RichTextBlock)control);", presentation, StringComparison.Ordinal); Assert.DoesNotContain("XamlReader", presentation, StringComparison.Ordinal); - Assert.Contains("TextSelection = selectionOptions", timeline, StringComparison.Ordinal); - Assert.Contains("MarkdownTextSelection.Surface(label, Text(", timeline, StringComparison.Ordinal); Assert.Contains( "BuildAccessibleAssistantText(entry.Text, metadata?.AssistantContent)", timeline, @@ -72,35 +74,16 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() } [Fact] - public void MarkdownSelection_UsesOneReactorScopeAndFeedbackOnlyCopyResults() + public void ChatMarkdown_DoesNotUseReactorCrossBlockSelection() { - var root = TestRepositoryPaths.GetRepositoryRoot(); - var message = File.ReadAllText(Path.Combine(root, "src", "OpenClaw.Tray.WinUI", - "Chat", "ChatMarkdownMessageView.cs")); - var presentation = File.ReadAllText(Path.Combine(root, "src", "OpenClaw.Tray.WinUI", - "Chat", "ChatMarkdownPresentation.cs")); - - var timeline = File.ReadAllText(Path.Combine(root, "src", "OpenClaw.Tray.WinUI", - "Chat", "ReactorChatTimeline.cs")); - Assert.Matches( - @"BuildSafeMarkdown\(\s*string\? text, Func\? tryCopy = null, " + - @"bool compactSecondaryReading = false\)\s*=>\s*" + - @"Component\(\s*new\(\s*" + - @"text \?\? string.Empty,\s*tryCopy,\s*compactSecondaryReading\)\);", - timeline); - Assert.Matches( - @"MarkdownTextSelection\.Scope\(options,\s*ReactorChatTimeline\.BuildMessageMarkdown\(" + - @"\s*props\.Text, props\.TryCopyCode, options, props\.CompactSecondaryReading\)\)", - message); - Assert.Equal(1, message.Split("MarkdownTextSelection.Scope(", StringSplitOptions.None).Length - 1); - Assert.Contains("OnCopyResult = (_, success) =>", message, StringComparison.Ordinal); - Assert.DoesNotContain("Clipboard.", message, StringComparison.Ordinal); - Assert.DoesNotContain("TryCopyText", message, StringComparison.Ordinal); - Assert.Contains("Chat_Copy_Copied", message, StringComparison.Ordinal); - Assert.Contains("Chat_Copy_Failed", message, StringComparison.Ordinal); - Assert.Contains("MarkdownTextSelection.Surface(code, codeBody)", presentation, StringComparison.Ordinal); - Assert.Contains("MarkdownTextSelection.Visibility(expanded,", presentation, StringComparison.Ordinal); - Assert.Contains("ToolCallCardRenderer.BuildDisclosureHeader(expanded,", presentation, StringComparison.Ordinal); - Assert.Contains("MarkdownTextSelection.Surface(disclosure.Summary,", presentation, StringComparison.Ordinal); + // Reactor preview.18's MarkdownTextSelection host rescans native text geometry on every + // LayoutUpdated, which made typing and streaming slow. Re-enable it only with a fixed package. + var tray = Path.Combine(TestRepositoryPaths.GetRepositoryRoot(), "src", "OpenClaw.Tray.WinUI"); + foreach (var file in Directory.EnumerateFiles(tray, "*.cs", SearchOption.AllDirectories)) + { + var source = File.ReadAllText(file); + Assert.False(source.Contains("MarkdownTextSelection", StringComparison.Ordinal), file); + Assert.False(source.Contains("TextSelection =", StringComparison.Ordinal), file); + } } } diff --git a/tests/OpenClaw.Tray.UITests/GatewayFixtureMarkdownSelectionTests.cs b/tests/OpenClaw.Tray.UITests/GatewayFixtureMarkdownSelectionTests.cs deleted file mode 100644 index 42a77dd34e..0000000000 --- a/tests/OpenClaw.Tray.UITests/GatewayFixtureMarkdownSelectionTests.cs +++ /dev/null @@ -1,1064 +0,0 @@ -// Shipped-app acceptance coverage for Markdown selection in the production chat timeline. -// Run with dotnet test .\tests\OpenClaw.Tray.UITests\OpenClaw.Tray.UITests.csproj -// --filter FullyQualifiedName~MarkdownSelection_. -using System.Globalization; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Security.Cryptography; -using System.Windows.Automation; -using System.Windows.Automation.Text; -using OpenClaw.GatewayFixtureHost; -using OpenClaw.TestSupport.Gateway; -using Windows.System; - -namespace OpenClaw.Tray.UITests; - -public sealed partial class GatewayFixtureUiTests -{ - private const string MarkdownSelectionOpening = "FIXTURE SELECTION opening paragraph."; - private const string MarkdownSelectionSecondParagraph = "FIXTURE SELECTION second paragraph."; - private const string MarkdownSelectionListFirst = "FIXTURE SELECTION list item one."; - private const string MarkdownSelectionListSecond = "FIXTURE SELECTION list item two."; - private const string MarkdownSelectionCodeFirst = "const selectionProof = \"fixture\";"; - private const string MarkdownSelectionCodeSecond = "console.log(selectionProof);"; - private const string MarkdownSelectionTableHeader = "Fixture selection table"; - private const string MarkdownSelectionTableCell = "FIXTURE SELECTION table cell."; - private const string MarkdownSelectionSummary = "FIXTURE SELECTION collapsed summary."; - private const string MarkdownSelectionCollapsedBody = "FIXTURE SELECTION hidden collapsed body."; - private const string MarkdownSelectionBeforeMessage = - "FIXTURE SELECTION BEFORE: a separate message must not be copied."; - private const int MarkdownSelectionFillerCount = 18; - private const uint UnicodeTextClipboardFormat = 13; - private static readonly TimeSpan ClipboardNoSelectionObservationWindow = TimeSpan.FromSeconds(1); - private static readonly string ExpectedMarkdownSelectionClipboard = string.Join("\n", - [ - "FIXTURE SELECTION opening paragraph.", - "FIXTURE SELECTION second paragraph.", - "\u2022 FIXTURE SELECTION list item one.", - "\u2022 FIXTURE SELECTION list item two.", - "const selectionProof = \"fixture\";", - "console.log(selectionProof);", - "FIXTURE SELECTION scroll filler 01: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 02: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 03: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 04: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 05: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 06: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 07: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 08: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 09: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 10: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 11: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 12: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 13: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 14: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 15: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 16: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 17: keep native timeline scrolling in the path.", - "FIXTURE SELECTION scroll filler 18: keep native timeline scrolling in the path.", - "Fixture selection table\tState", - "FIXTURE SELECTION table cell.\tvisible", - "FIXTURE SELECTION collapsed summary." - ]); - - private static readonly string MarkdownSelectionMessage = string.Join("\n\n", - [ - MarkdownSelectionOpening, - MarkdownSelectionSecondParagraph, - $"- {MarkdownSelectionListFirst}\n- {MarkdownSelectionListSecond}", - $"```csharp\n{MarkdownSelectionCodeFirst}\n{MarkdownSelectionCodeSecond}\n```", - .. Enumerable.Range(1, MarkdownSelectionFillerCount) - .Select(index => $"FIXTURE SELECTION scroll filler {index:D2}: keep native timeline scrolling in the path."), - $"| {MarkdownSelectionTableHeader} | State |\n| --- | --- |\n| {MarkdownSelectionTableCell} | visible |", - $"
\n{MarkdownSelectionSummary}\n\n{MarkdownSelectionCollapsedBody}\n
" - ]); - - [GatewayFixtureUiFact] - [Trait("Category", "GatewayFixture")] - [Trait("KeyboardProof", "Required")] - public async Task MarkdownSelection_CopiesAcrossBlocksAndExcludesCollapsedAndNeighboringContent() - { - await WithAppAsync(async run => - { - await run.InvokeAsync("app.navigate", new { page = "chat" }); - await SelectSessionAsync(run, GatewayScenario.MarkdownSelectionTitle, GatewayScenario.MarkdownSelectionSessionKey); - await WaitUiAsync(run, () => FindSelectionText(run, MarkdownSelectionOpening) is not null, - "synthetic Markdown selection history rendered"); - LogRuntimeIdentity(run, output.WriteLine); - - var window = FindHub(run); - var hwnd = new IntPtr(window.Current.NativeWindowHandle); - var timeline = FindTimeline(run) - ?? throw new InvalidOperationException("The production chat ItemsView was not mounted."); - var (scrollElement, timelineScroll) = FindTimelineScroll(timeline); - var scrollBounds = scrollElement.Current.BoundingRectangle; - output.WriteLine($"Timeline scroll owner {scrollElement.Current.ControlType.ProgrammaticName} " + - $"'{scrollElement.Current.Name}' at {scrollBounds}; initial vertical position " + - $"{timelineScroll.Current.VerticalScrollPercent}% / {timelineScroll.Current.VerticalViewSize}% view."); - - ClearClipboard(); - await DragSelectionToAsync(run, window, timeline, scrollElement, timelineScroll, output.WriteLine, - MarkdownSelectionOpening, MarkdownSelectionTableCell); - var tableCell = FindSelectionText(run, MarkdownSelectionTableCell) - ?? throw new InvalidOperationException("The selected Markdown table cell disappeared before copy."); - var selectionHost = FindMarkdownSelectionHost(tableCell); - var selectedOverlayText = selectionHost is null ? null : ReadMarkdownSelectionText(selectionHost); - output.WriteLine($"Pre-copy selection overlay text: " + - $"'{EscapeForLog(selectedOverlayText ?? "")}'."); - if (Environment.GetEnvironmentVariable("OPENCLAW_GATEWAY_FIXTURE_SCREENSHOTS") == "1") - OwnedWindowCapture.Save(run, window, "markdown-selection-table-cell-overlay.png", - tableCell, selectionHost ?? tableCell); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - var blocksCopied = NormalizeClipboardText(await WaitForClipboardTextAsync(run)); - AssertClipboardGolden(ExpectedSelectionThroughTableCell(), blocksCopied, - "Selection through first table cell", selectedOverlayText); - Assert.DoesNotContain("FIXTURE SELECTION BEFORE", blocksCopied, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION AFTER", blocksCopied, StringComparison.Ordinal); - - var summary = await WaitForSelectionExpanderAsync(run); - var expander = (ExpandCollapsePattern)summary.GetCurrentPattern(ExpandCollapsePattern.Pattern); - Assert.Equal(ExpandCollapseState.Collapsed, expander.Current.ExpandCollapseState); - var disclosureRange = await WaitForRangeScopedDisclosureAsync(run, timeline); - Assert.Equal(MarkdownSelectionSummary, NormalizeClipboardText(disclosureRange.RangeText)); - Assert.DoesNotContain(MarkdownSelectionCollapsedBody, disclosureRange.RangeText, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION BEFORE", disclosureRange.RangeText, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION AFTER", disclosureRange.RangeText, StringComparison.Ordinal); - Assert.Single(disclosureRange.Children); - Assert.True(Automation.Compare(summary, disclosureRange.Expander), - "The Markdown TextPattern selection range must expose its native disclosure Expander through GetChildren()."); - timelineScroll.SetScrollPercent(ScrollPattern.NoScroll, 100); - await WaitUiAsync(run, () => IsSelectionTextVisible(timeline, - FindSelectionText(run, MarkdownSelectionSummary)), - "disclosure summary is fully visible before expansion"); - var collapsedBody = FindSelectionText(run, MarkdownSelectionCollapsedBody); - Assert.False(IsSelectionTextVisible(timeline, collapsedBody), - "Collapsed details content must not be visible in the production timeline."); - expander.Expand(); - AutomationElement? expandedBody = null; - await WaitUiAsync(run, () => IsSelectionTextVisible(timeline, - expandedBody = FindSelectionText(run, MarkdownSelectionCollapsedBody)), - "native details disclosure expands its Markdown body"); - Assert.True(IsSelectionTextVisible(timeline, FindSelectionText(run, MarkdownSelectionCollapsedBody)), - "Expanded details content must be visible in the production timeline."); - if (Environment.GetEnvironmentVariable("OPENCLAW_GATEWAY_FIXTURE_SCREENSHOTS") == "1") - OwnedWindowCapture.Save(run, window, "markdown-selection-expanded.png", summary, expandedBody!); - expander.Collapse(); - await WaitUiAsync(run, () => !IsSelectionTextVisible(timeline, - FindSelectionText(run, MarkdownSelectionCollapsedBody)), - "native details disclosure collapses its Markdown body"); - Assert.False(IsSelectionTextVisible(timeline, FindSelectionText(run, MarkdownSelectionCollapsedBody)), - "Collapsed details content must not remain visible in the production timeline."); - - ClearClipboard(); - await DragSelectionToAsync(run, window, timeline, scrollElement, timelineScroll, output.WriteLine, - MarkdownSelectionOpening, MarkdownSelectionSummary); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - var fullVisibleMessageCopied = NormalizeClipboardText(await WaitForClipboardTextAsync(run)); - AssertClipboardGolden(ExpectedSelectionThroughSummary(), fullVisibleMessageCopied, - "Selection through disclosure summary"); - Assert.DoesNotContain(MarkdownSelectionCollapsedBody, fullVisibleMessageCopied, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION BEFORE", fullVisibleMessageCopied, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION AFTER", fullVisibleMessageCopied, StringComparison.Ordinal); - Assert.DoesNotContain("Copy code", fullVisibleMessageCopied, StringComparison.Ordinal); - Assert.DoesNotContain("csharp", fullVisibleMessageCopied, StringComparison.Ordinal); - - var codeToolbar = timeline.FindAll(TreeScope.Descendants, - new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)) - .Cast() - .SingleOrDefault(element => element.Current.AutomationId.StartsWith("ChatCopy_code_", - StringComparison.Ordinal)); - Assert.NotNull(codeToolbar); - - await SelectSessionAsync(run, GatewayScenario.MainTitle, GatewayScenario.MainSessionKey); - await WaitUiAsync(run, () => FindSelectionText(run, GatewayScenario.MainSentinel) is - { Current.IsOffscreen: false }, "separate session history rendered"); - ClearClipboard(); - await DragSelectionBetweenTextAsync(run, window, - GatewayScenario.MainSentinel, GatewayScenario.MainSentinel); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - Assert.Equal(GatewayScenario.MainSentinel, - NormalizeClipboardText(await WaitForClipboardTextAsync(run))); - - await SelectSessionAsync(run, GatewayScenario.MarkdownSelectionTitle, - GatewayScenario.MarkdownSelectionSessionKey); - AutomationElement? restoredAnchor = null; - await WaitUiAsync(run, () => (restoredAnchor = FindSelectionText(run, MarkdownSelectionOpening)) is not null, - "Markdown session restored after switching"); - var restoredSelectionHost = FindMarkdownSelectionHost(restoredAnchor!); - Assert.NotNull(restoredSelectionHost); - restoredSelectionHost.SetFocus(); - await WaitUiAsync(run, () => Automation.Compare(AutomationElement.FocusedElement, restoredSelectionHost), - "restored Markdown message owns keyboard copy"); - var restoredSelection = ReadMarkdownSelectionText(restoredSelectionHost); - // Returning to the same message may retain its selection, never another thread's text. - Assert.True(restoredSelection.Length == 0 || restoredSelection == ExpectedSelectionThroughSummary(), - $"Restored selection must be empty or belong to this message, not another conversation. " + - $"Actual: '{EscapeForLog(restoredSelection)}'."); - ClearClipboard(); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - string restoredCopy; - if (restoredSelection.Length == 0) - { - // test-timing-exception: negative probe that an empty selection leaves the clipboard empty - await Task.Delay(ClipboardNoSelectionObservationWindow); - restoredCopy = NormalizeClipboardText(ReadClipboardTextOrEmpty()); - } - else - { - restoredCopy = NormalizeClipboardText(await WaitForClipboardTextAsync(run)); - } - Assert.Equal(restoredSelection, restoredCopy); - Assert.DoesNotContain(GatewayScenario.MainSentinel, restoredCopy, StringComparison.Ordinal); - - AutomationElement? keyboardTimeline = null; - await WaitUiAsync(run, () => (keyboardTimeline = FindTimeline(run)) is not null, - "mounted chat timeline after session restoration and copy feedback"); - var (_, keyboardScroll) = FindTimelineScroll(keyboardTimeline!); - keyboardScroll.SetScrollPercent(ScrollPattern.NoScroll, 0); - AutomationElement? keyboardAnchor = null; - AutomationElement? keyboardTableEnd = null; - AutomationElement? keyboardEnd = null; - await WaitUiAsync(run, () => - (keyboardAnchor = FindSelectionText(keyboardTimeline, MarkdownSelectionOpening)) is not null - && (keyboardTableEnd = FindSelectionText(keyboardTimeline, MarkdownSelectionTableCell)) is not null - && (keyboardEnd = FindSelectionText(keyboardTimeline, MarkdownSelectionSummary)) is not null - && IsSelectionTextVisible(keyboardTimeline!, keyboardAnchor) - && keyboardTableEnd.Current.IsOffscreen && keyboardEnd.Current.IsOffscreen, - "visible keyboard anchor with table and disclosure fully clipped from the timeline viewport"); - Assert.NotNull(keyboardAnchor); - Assert.NotNull(keyboardTableEnd); - Assert.NotNull(keyboardEnd); - await NativeKeyboardProof.ActivateAsync(hwnd); - keyboardAnchor = await WaitForStableSelectionTextAsync(run, MarkdownSelectionOpening, - "keyboard anchor bounds stable before the caret click"); - // Sample the start offset only after the scroll reset has settled. - var initialKeyboardScroll = keyboardScroll.Current.VerticalScrollPercent; - using (var click = new NativeSelectionDrag(run, window, FirstGlyphLeftHalf(keyboardAnchor, - MarkdownSelectionOpening))) - { - } - NativeKeyboardProof.Press(hwnd, VirtualKey.Down, shift: true); - for (var attempt = 0; attempt < 8 && keyboardEnd.Current.IsOffscreen; attempt++) - { - var previousScroll = keyboardScroll.Current.VerticalScrollPercent; - NativeKeyboardProof.Press(hwnd, VirtualKey.PageDown); - await WaitUiAsync(run, () => keyboardScroll.Current.VerticalScrollPercent > previousScroll - || !keyboardEnd.Current.IsOffscreen, - "PageDown navigates the production timeline to the clipped Markdown endpoint"); - } - Assert.True(keyboardScroll.Current.VerticalScrollPercent > initialKeyboardScroll, - "Keyboard navigation must scroll the production timeline from its initial position."); - Assert.False(keyboardTableEnd.Current.IsOffscreen, - "Keyboard navigation must reach the fully clipped table block."); - Assert.False(keyboardEnd.Current.IsOffscreen, - "Keyboard navigation must reach the fully clipped disclosure summary."); - NativeKeyboardProof.Press(hwnd, VirtualKey.A, control: true); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - var keyboardCopied = NormalizeClipboardText(await WaitForClipboardTextAsync(run)); - AssertClipboardGolden(ExpectedMarkdownSelectionClipboard, keyboardCopied, - "Keyboard select-all"); - Assert.DoesNotContain(MarkdownSelectionCollapsedBody, keyboardCopied, StringComparison.Ordinal); - Assert.DoesNotContain("Copy code", keyboardCopied, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION BEFORE", keyboardCopied, StringComparison.Ordinal); - Assert.DoesNotContain("FIXTURE SELECTION AFTER", keyboardCopied, StringComparison.Ordinal); - - if (Environment.GetEnvironmentVariable("OPENCLAW_GATEWAY_FIXTURE_SCREENSHOTS") == "1") - { - AutomationElement? composer = null; - await WaitUiAsync(run, () => (composer = FindById(run, "ChatComposerInput")) is not null, - "composer available for the copied screenshot"); - OwnedWindowCapture.Save(run, window, "markdown-selection-copied.png", summary, composer!); - } - }, scenario: GatewayScenario.CreateMarkdownSelection(MarkdownSelectionMessage)); - } - - private static async Task DragSelectionToAsync( - GatewayFixtureRun run, - AutomationElement window, - AutomationElement timeline, - AutomationElement scrollElement, - ScrollPattern scroll, - Action writeLine, - string startText, - string endText) - { - scroll.SetScrollPercent(ScrollPattern.NoScroll, 0); - await WaitUiAsync(run, () => FindSelectionText(run, startText) is - { Current.IsOffscreen: false }, "selection anchor visible at timeline start"); - var admissionText = startText[..Math.Min(startText.Length, 7)]; - var hwnd = new IntPtr(window.Current.NativeWindowHandle); - await NativeKeyboardProof.ActivateAsync(hwnd); - - var admissionAnchor = await WaitForStableSelectionTextAsync(run, MarkdownSelectionBeforeMessage, - "independent selection-admission message is visible with stable bounds"); - await AssertInitialSelectionAdmissionAsync(run, window, hwnd, admissionAnchor, - admissionText, writeLine); - - scroll.SetScrollPercent(ScrollPattern.NoScroll, 0); - AutomationElement? end = null; - await WaitUiAsync(run, () => (end = FindSelectionText(run, endText)) is not null, - $"selection endpoint {endText} rendered"); - var anchor = await WaitForStableSelectionTextAsync(run, startText, - "selection anchor bounds stable after admission probe and scroll reset"); - var selectionHost = FindMarkdownSelectionHost(anchor); - var start = FirstGlyphLeftHalf(anchor, startText); - var admissionPoint = LastGlyphRightHalf(anchor, admissionText); - var firstGlyphBounds = GetGlyphBounds(anchor, startText, first: true); - var admissionGlyphBounds = GetGlyphBounds(anchor, admissionText, first: false); - var viewport = selectionHost?.Current.BoundingRectangle ?? timeline.Current.BoundingRectangle; - viewport.Intersect(timeline.Current.BoundingRectangle); - viewport.Intersect(scrollElement.Current.BoundingRectangle); - Assert.False(viewport.IsEmpty, "The selection surface must intersect the production ScrollView viewport."); - var dpi = GetDpiForWindow(hwnd); - Assert.True(dpi > 0, "The fixture window must expose its DPI for scale-aware edge input."); - // UIA reports the timeline without its bottom fade band, but the ScrollView (and Reactor's - // drag viewport) extends under that band. Cross the whole band, as a drag onto the composer would. - var outsideMargin = (int)Math.Ceiling((OpenClawTray.Chat.ChatVisuals.TranscriptFadeHeight + 8d) * dpi / 96d); - var edge = new System.Drawing.Point((int)(viewport.Left + viewport.Width / 2), - (int)Math.Ceiling(viewport.Bottom) + outsideMargin); - var windowBounds = window.Current.BoundingRectangle; - Assert.True(Contains(anchor.Current.BoundingRectangle, start), - $"The drag anchor {start} must be inside the first glyph's automation surface {anchor.Current.BoundingRectangle}."); - Assert.True(Contains(firstGlyphBounds, start), - $"The drag anchor {start} must be inside the first glyph {firstGlyphBounds}."); - Assert.True(Contains(anchor.Current.BoundingRectangle, admissionPoint), - $"The admission point {admissionPoint} must be inside the anchor automation surface {anchor.Current.BoundingRectangle}."); - Assert.True(Contains(admissionGlyphBounds, admissionPoint), - $"The admission point {admissionPoint} must be inside the last admission glyph {admissionGlyphBounds}."); - Assert.True(Contains(windowBounds, edge), - $"The autoscroll point {edge} must remain inside the fixture window {windowBounds}."); - Assert.False(Contains(viewport, edge), - $"The autoscroll point {edge} must cross the visible selection-host viewport {viewport}."); - writeLine($"Selection drag geometry after probe: host '{selectionHost?.Current.ClassName ?? "legacy text leaf"}' " + - $"bounds {selectionHost?.Current.BoundingRectangle.ToString() ?? "unavailable"}; first glyph {firstGlyphBounds}; " + - $"mouse-down {start}; in-host admission move {admissionPoint}; viewport {viewport}; " + - $"ScrollPattern owner {scrollElement.Current.BoundingRectangle}; DPI {dpi}; " + - $"outside-viewport point {edge}; fixture window {windowBounds}."); - - using var drag = new NativeSelectionDrag(run, window, start); - Assert.True(drag.IsButtonDown, "The native left button must remain down for the selection drag."); - if (selectionHost is not null) - await WaitForCollapsedFocusedSelectionAsync(run, selectionHost, - "production drag press focuses the Markdown selection Document with a collapsed range"); - drag.MoveTo(admissionPoint); - if (selectionHost is not null) - await WaitForSelectionPrefixAsync(run, selectionHost, admissionText, writeLine, - "production in-viewport move admits a non-empty opening-text selection"); - else - writeLine("Baseline message has no Markdown selection Document; independent prefix-copy probe verified native drag admission."); - for (var attempt = 0; attempt < 30 && !IsSelectionTextVisible(timeline, end!); attempt++) - { - var before = scroll.Current.VerticalScrollPercent; - drag.MoveTo(edge); - Assert.True(drag.IsButtonDown, "The native left button must remain down during edge autoscroll."); - await run.WaitForAsync(() => - { - try - { - var currentOffset = scroll.Current.VerticalScrollPercent; - var selectionText = selectionHost is null - ? null - : ReadMarkdownSelectionText(selectionHost); - var selectionProgress = selectionText is null - ? "" - : $"length={selectionText.Length}, end='{SelectionTailForLog(selectionText)}'"; - writeLine($"Edge poll toward '{endText}': selected {selectionProgress}; " + - $"vertical offset={currentOffset}% (before {before}%)."); - return Task.FromResult(currentOffset > before || IsSelectionTextVisible(timeline, end)); - } - catch (ElementNotAvailableException) { return Task.FromResult(false); } - }, $"production timeline edge-scrolls toward {endText}"); - } - - Assert.True(IsSelectionTextVisible(timeline, end!), - $"Edge scrolling did not fully reveal selection endpoint {endText}; " + - $"endpoint bounds {end!.Current.BoundingRectangle}, viewport {viewport}."); - var stopPoint = new System.Drawing.Point(edge.X, (int)Math.Ceiling(viewport.Bottom) - 1); - Assert.True(Contains(viewport, stopPoint), - $"The autoscroll stop point {stopPoint} must be inside the viewport {viewport}."); - drag.MoveTo(stopPoint); - // Autoscroll can apply one more tick after the pointer re-enters the viewport; target the - // endpoint glyph only once its bounds stop moving. - var finalEnd = await WaitForStableSelectionTextAsync(run, endText, - $"selection endpoint {endText} settles after autoscroll stops"); - Assert.True(IsSelectionTextVisible(timeline, finalEnd), - $"The selection endpoint moved outside the viewport while stopping autoscroll; " + - $"endpoint bounds {finalEnd.Current.BoundingRectangle}, viewport {viewport}."); - var finalGlyphBounds = GetGlyphBounds(finalEnd, endText, first: false); - var finalPoint = LastGlyphTrailingEdge(finalEnd, endText); - Assert.True(Contains(viewport, finalPoint), - $"The final selection point {finalPoint} must remain inside the viewport {viewport}."); - drag.MoveTo(finalPoint); - Assert.True(drag.IsButtonDown, "The native left button must remain down at the final selection endpoint."); - var actualPoint = drag.CurrentPosition; - var selectedBeforeRelease = selectionHost is null - ? "" - : EscapeForLog(ReadMarkdownSelectionText(selectionHost)); - writeLine($"Final selection endpoint '{endText}': glyph bounds {finalGlyphBounds}; " + - $"requested pointer {finalPoint}; actual pointer {actualPoint}; viewport {viewport}; " + - $"selection host text before release '{selectedBeforeRelease}'."); - } - - private static async Task DragSelectionBetweenTextAsync( - GatewayFixtureRun run, - AutomationElement window, - string startText, - string endText) - { - var start = await WaitForStableSelectionTextAsync(run, startText, - $"selection start {startText} visible with stable bounds"); - var end = await WaitForStableSelectionTextAsync(run, endText, - $"selection end {endText} visible with stable bounds"); - await NativeKeyboardProof.ActivateAsync(new IntPtr(window.Current.NativeWindowHandle)); - using (var drag = new NativeSelectionDrag(run, window, FirstGlyphLeftHalf(start, startText))) - drag.MoveTo(LastGlyphTrailingEdge(end, endText)); - var selectionHost = FindMarkdownSelectionHost(end) - ?? throw new InvalidOperationException("The selected message has no Markdown selection Document."); - await WaitUiAsync(run, () => Automation.Compare(AutomationElement.FocusedElement, selectionHost) - && ReadMarkdownSelectionText(selectionHost).Length > 0, - "native pointer selection and keyboard focus after release"); - } - - private static System.Drawing.Point FirstGlyphLeftHalf(AutomationElement element, string text) - { - var bounds = GetGlyphBounds(element, text, first: true); - return new((int)Math.Floor(bounds.Left + Math.Max(1, bounds.Width / 8)), - (int)Math.Round(bounds.Top + bounds.Height / 2)); - } - - private static System.Drawing.Point LastGlyphTrailingEdge(AutomationElement element, string text) - { - var bounds = GetGlyphBounds(element, text, first: false); - // A rounded UIA glyph box's last interior pixel can precede the trailing caret midpoint. - return new((int)Math.Ceiling(bounds.Right), - (int)Math.Round(bounds.Top + bounds.Height / 2)); - } - - private static System.Drawing.Point LastGlyphRightHalf(AutomationElement element, string text) - { - var bounds = GetGlyphBounds(element, text, first: false); - return new((int)Math.Floor(bounds.Left + bounds.Width * 7 / 8), - (int)Math.Round(bounds.Top + bounds.Height / 2)); - } - - private static bool Contains(System.Windows.Rect bounds, System.Drawing.Point point) => - !bounds.IsEmpty && bounds.Width > 0 && bounds.Height > 0 - && point.X >= bounds.Left && point.X < bounds.Right - && point.Y >= bounds.Top && point.Y < bounds.Bottom; - - private static async Task AssertInitialSelectionAdmissionAsync( - GatewayFixtureRun run, - AutomationElement window, - nint hwnd, - AutomationElement anchor, - string expectedText, - Action writeLine) - { - var mouseDown = FirstGlyphLeftHalf(anchor, expectedText); - var admissionMove = LastGlyphRightHalf(anchor, expectedText); - var glyphBounds = GetGlyphBounds(anchor, expectedText, first: true); - var selectionHost = FindMarkdownSelectionHost(anchor); - Assert.True(Contains(anchor.Current.BoundingRectangle, mouseDown), - $"The admission mouse-down {mouseDown} must be inside the independent message's " + - $"automation surface {anchor.Current.BoundingRectangle}."); - Assert.True(Contains(glyphBounds, mouseDown), - $"The admission mouse-down {mouseDown} must be inside its first glyph {glyphBounds}."); - Assert.True(Contains(anchor.Current.BoundingRectangle, admissionMove), - $"The admission move {admissionMove} must be inside the independent message's " + - $"automation surface {anchor.Current.BoundingRectangle}."); - using (var probe = new NativeSelectionDrag(run, window, mouseDown)) - { - Assert.True(probe.IsButtonDown, "The native left button must remain down during the admission probe."); - if (selectionHost is not null) - await WaitForCollapsedFocusedSelectionAsync(run, selectionHost, - "admission probe press focuses its Markdown selection Document with a collapsed range"); - probe.MoveTo(admissionMove); - Assert.True(probe.IsButtonDown, "The native left button must remain down during the admission probe."); - if (selectionHost is not null) - await WaitForSelectionPrefixAsync(run, selectionHost, expectedText, writeLine, - "admission probe selects a non-empty prefix in its independent Markdown message"); - writeLine($"Admission probe kept the native left button down from " + - $"{mouseDown} to {admissionMove} inside first-glyph bounds {glyphBounds} and " + - $"message bounds {anchor.Current.BoundingRectangle}; " + - $"selection Document {(selectionHost is null ? "unavailable on baseline" : "focused")}."); - } - if (selectionHost is null) - { - var legacySelection = ReadLegacyTextSelection(anchor); - if (legacySelection is not null) - { - Assert.Equal(expectedText, legacySelection); - writeLine($"Baseline RichTextBlock TextPattern selected exact admission prefix '{legacySelection}'."); - } - else - { - ClearClipboard(); - NativeKeyboardProof.Press(hwnd, VirtualKey.C, control: true); - var copied = NormalizeClipboardText(await WaitForClipboardTextAsync(run)); - Assert.Equal(expectedText, copied); - writeLine($"Baseline native Ctrl+C copied exact admission prefix '{copied}'."); - } - ClearClipboard(); - } - NativeKeyboardProof.Press(hwnd, VirtualKey.Escape); - if (selectionHost is not null) - await WaitForCollapsedFocusedSelectionAsync(run, selectionHost, - "Escape collapses the independent admission probe without leaving a stale selection"); - } - - private static async Task WaitForCollapsedFocusedSelectionAsync( - GatewayFixtureRun run, AutomationElement selectionHost, string description) - { - await WaitUiAsync(run, () => Automation.Compare(AutomationElement.FocusedElement, selectionHost) - && ReadMarkdownSelectionText(selectionHost).Length == 0, description); - Assert.True(Automation.Compare(AutomationElement.FocusedElement, selectionHost), - $"Expected the Markdown selection Document to remain focused, found " + - $"'{AutomationElement.FocusedElement.Current.ClassName}' / " + - $"'{AutomationElement.FocusedElement.Current.ControlType.ProgrammaticName}'."); - Assert.DoesNotContain(((TextPattern)selectionHost.GetCurrentPattern(TextPattern.Pattern)).GetSelection(), - range => !string.IsNullOrEmpty(range.GetText(-1))); - } - - private static async Task WaitForSelectionPrefixAsync( - GatewayFixtureRun run, AutomationElement selectionHost, string expectedText, - Action writeLine, string description) - { - string? selected = null; - await WaitUiAsync(run, () => - { - selected = ReadMarkdownSelectionText(selectionHost); - writeLine($"Selection admission poll: focused={Automation.Compare(AutomationElement.FocusedElement, selectionHost)}; " + - $"selected='{EscapeForLog(selected)}'."); - return string.Equals(selected, expectedText, StringComparison.Ordinal) - && ExpectedMarkdownSelectionClipboard.StartsWith(expectedText, StringComparison.Ordinal); - }, description); - Assert.Equal(expectedText, selected); - Assert.True(ExpectedMarkdownSelectionClipboard.StartsWith(expectedText, StringComparison.Ordinal), - $"The selected text '{EscapeForLog(expectedText)}' must be a prefix of the independent clipboard golden."); - } - - private static AutomationElement? FindMarkdownSelectionHost(AutomationElement leaf) - { - var walker = TreeWalker.ControlViewWalker; - for (AutomationElement? current = leaf; current is not null; current = walker.GetParent(current)) - if (current.Current.ClassName == "MarkdownTextSelection") - return current; - return null; - } - - private static string ReadMarkdownSelectionText(AutomationElement selectionHost) - { - var ranges = ((TextPattern)selectionHost.GetCurrentPattern(TextPattern.Pattern)).GetSelection(); - if (ranges.Length != 1) - throw new InvalidOperationException( - $"Markdown selection Document returned {ranges.Length} UIA selection ranges instead of one."); - return NormalizeClipboardText(ranges[0].GetText(-1)); - } - - private static string? ReadLegacyTextSelection(AutomationElement leaf) - { - if (!leaf.TryGetCurrentPattern(TextPattern.Pattern, out var rawPattern) - || rawPattern is not TextPattern textPattern) - return null; - var ranges = textPattern.GetSelection(); - return ranges.Length == 0 ? null : NormalizeClipboardText(string.Join("\n", - ranges.Select(range => range.GetText(-1)))); - } - - private static bool NearlyEqual(System.Windows.Rect left, System.Windows.Rect right) => - Math.Abs(left.Left - right.Left) <= 0.5 - && Math.Abs(left.Top - right.Top) <= 0.5 - && Math.Abs(left.Width - right.Width) <= 0.5 - && Math.Abs(left.Height - right.Height) <= 0.5; - - // Native input targets glyph geometry, so acquire the text inside the wait and return it only - // once two consecutive samples agree; scrolling or a re-render resets the sample. - private static async Task WaitForStableSelectionTextAsync( - GatewayFixtureRun run, string text, string description) - { - AutomationElement? element = null; - System.Windows.Rect? previousBounds = null; - await WaitUiAsync(run, () => - { - element = FindSelectionText(run, text); - if (element is null || element.Current.IsOffscreen) - { - previousBounds = null; - return false; - } - - var bounds = element.Current.BoundingRectangle; - var stable = previousBounds is { } previous && NearlyEqual(previous, bounds); - previousBounds = bounds; - return stable; - }, description, onProbeError: _ => previousBounds = null); - return element!; - } - - private static string EscapeForLog(string text) => - text.Replace("\r", "\\r", StringComparison.Ordinal) - .Replace("\n", "\\n", StringComparison.Ordinal) - .Replace("\t", "\\t", StringComparison.Ordinal); - - private static void AssertClipboardGolden( - string expected, string actual, string label, string? selectedOverlayText = null) - { - var normalizedExpected = NormalizeClipboardText(expected); - var clipboardMatchesGolden = string.Equals(normalizedExpected, actual, StringComparison.Ordinal); - var overlayMatchesClipboard = selectedOverlayText is null - || string.Equals(selectedOverlayText, actual, StringComparison.Ordinal); - Assert.True(clipboardMatchesGolden && overlayMatchesClipboard, - $"{label} clipboard mismatch ({normalizedExpected.Length} expected chars, {actual.Length} actual chars)." + - $"\nExpected: {EscapeForLog(normalizedExpected)}\nActual: {EscapeForLog(actual)}" + - (selectedOverlayText is null - ? string.Empty - : $"\nPre-copy UIA selection overlay: {EscapeForLog(selectedOverlayText)}" + - $"\nOverlay matches clipboard: {overlayMatchesClipboard}")); - } - - private static string SelectionTailForLog(string text) - { - var escaped = EscapeForLog(text); - const int MaximumTailLength = 80; - return escaped.Length <= MaximumTailLength ? escaped : $"...{escaped[^MaximumTailLength..]}"; - } - - private static System.Windows.Rect GetGlyphBounds(AutomationElement element, string text, bool first) - { - if (!element.TryGetCurrentPattern(TextPattern.Pattern, out var rawPattern) - || rawPattern is not TextPattern textPattern) - throw new InvalidOperationException($"UI Automation text '{text}' does not expose TextPattern."); - var documentRange = textPattern.DocumentRange; - var documentText = documentRange.GetText(-1); - var textOffset = documentText.IndexOf(text, StringComparison.Ordinal); - if (textOffset < 0) - throw new InvalidOperationException( - $"UI Automation TextPattern range did not contain '{text}'. Range text: {documentText}"); - var glyphStart = textOffset + (first ? 0 : text.Length - 1); - var prefixUnits = StringInfo.ParseCombiningCharacters(documentText[..glyphStart]).Length; - var suffixUnits = StringInfo.ParseCombiningCharacters(documentText[(glyphStart + 1)..]).Length; - var glyph = documentRange.Clone(); - glyph.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Character, prefixUnits); - glyph.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Character, -suffixUnits); - var glyphText = glyph.GetText(-1); - var expectedGlyph = first ? text[..1] : text[^1..]; - if (!string.Equals(glyphText, expectedGlyph, StringComparison.Ordinal)) - throw new InvalidOperationException( - $"UI Automation range selected '{glyphText}' instead of the { (first ? "first" : "last") } glyph '{expectedGlyph}' in '{text}'."); - var rectangles = glyph.GetBoundingRectangles(); - if (rectangles.Length == 0 || rectangles[0].IsEmpty) - { - var lineBounds = string.Join("; ", documentRange.GetBoundingRectangles() - .Select(bounds => bounds.ToString())); - throw new InvalidOperationException( - $"UI Automation did not expose a glyph rectangle for '{text}'. " + - $"Glyph text '{glyph.GetText(-1)}'; range text '{documentText}'; " + - $"offset {textOffset}, glyph start {glyphStart}, element bounds {element.Current.BoundingRectangle}; " + - $"document bounds: {lineBounds}"); - } - return rectangles[0]; - } - - private static string ExpectedSelectionThroughTableCell() - { - var secondColumnValue = ExpectedMarkdownSelectionClipboard.IndexOf("\tvisible", StringComparison.Ordinal); - if (secondColumnValue < 0) - throw new InvalidOperationException("The independent table golden is missing its tab-separated visible cell."); - return ExpectedMarkdownSelectionClipboard[..secondColumnValue]; - } - - private static string ExpectedSelectionThroughSummary() => ExpectedMarkdownSelectionClipboard; - - private static AutomationElement? FindSelectionText(GatewayFixtureRun run, string text) => - FindSelectionText(FindTimeline(run), text); - - private static AutomationElement? FindSelectionText(AutomationElement? timeline, string text) - { - if (timeline is null) return null; - return timeline.FindAll(TreeScope.Descendants, - new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text)) - .Cast() - .FirstOrDefault(element => element.Current.Name.Contains(text, StringComparison.Ordinal)); - } - - private static bool IsSelectionTextVisible(AutomationElement timeline, AutomationElement? element) - { - if (element is null || element.Current.IsOffscreen) return false; - var viewport = timeline.Current.BoundingRectangle; - var bounds = element.Current.BoundingRectangle; - return !bounds.IsEmpty && bounds.Width > 0 && bounds.Height > 0 - && bounds.Top >= viewport.Top - 1 && bounds.Bottom <= viewport.Bottom + 1 - && bounds.Right > viewport.Left && bounds.Left < viewport.Right; - } - - private static (AutomationElement Element, ScrollPattern Pattern) FindTimelineScroll(AutomationElement timeline) - { - if (timeline.TryGetCurrentPattern(ScrollPattern.Pattern, out var direct)) - return (timeline, (ScrollPattern)direct); - var scrollable = timeline.FindAll(TreeScope.Descendants, - new PropertyCondition(AutomationElement.IsScrollPatternAvailableProperty, true)) - .Cast() - .Select(element => (Element: element, Pattern: (ScrollPattern)element.GetCurrentPattern(ScrollPattern.Pattern))) - .Where(item => item.Pattern.Current.VerticallyScrollable) - .OrderByDescending(item => - { - var bounds = item.Element.Current.BoundingRectangle; - return bounds.Width * bounds.Height; - }) - .FirstOrDefault(); - return scrollable.Element is null - ? throw new InvalidOperationException("The production chat ItemsView has no accessible vertical scroll control.") - : scrollable; - } - - private static async Task WaitForSelectionExpanderAsync(GatewayFixtureRun run) - { - AutomationElement? expander = null; - await WaitUiAsync(run, () => (expander = FindInApp(run, new AndCondition( - new PropertyCondition(AutomationElement.NameProperty, MarkdownSelectionSummary), - new PropertyCondition(AutomationElement.IsExpandCollapsePatternAvailableProperty, true)))) is not null, - "native details summary expander"); - return expander!; - } - - private static async Task<(AutomationElement Expander, string RangeText, AutomationElement[] Children)> WaitForRangeScopedDisclosureAsync( - GatewayFixtureRun run, AutomationElement timeline) - { - (AutomationElement Expander, string RangeText, AutomationElement[] Children)? result = null; - await WaitUiAsync(run, () => (result = FindRangeScopedDisclosure(timeline)) is not null, - "summary-only Markdown TextPattern range exposes its native disclosure Expander"); - return result!.Value; - } - - private static (AutomationElement Expander, string RangeText, AutomationElement[] Children)? FindRangeScopedDisclosure( - AutomationElement timeline) - { - var textProviders = timeline.FindAll(TreeScope.Descendants, - new PropertyCondition(AutomationElement.IsTextPatternAvailableProperty, true)) - .Cast(); - foreach (var provider in textProviders) - { - var documentRange = ((TextPattern)provider.GetCurrentPattern(TextPattern.Pattern)).DocumentRange; - var documentText = documentRange.GetText(-1); - var summaryStart = documentText.IndexOf(MarkdownSelectionSummary, StringComparison.Ordinal); - if (summaryStart < 0 || !documentText.Contains(MarkdownSelectionOpening, StringComparison.Ordinal)) - continue; - - var range = documentRange.Clone(); - var afterSummary = summaryStart + MarkdownSelectionSummary.Length; - var prefixUnits = StringInfo.ParseCombiningCharacters(documentText[..summaryStart]).Length; - var trailingUnits = StringInfo.ParseCombiningCharacters(documentText[afterSummary..]).Length; - range.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Character, prefixUnits); - range.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Character, -trailingUnits); - var rangeText = range.GetText(-1); - if (!string.Equals(NormalizeClipboardText(rangeText), MarkdownSelectionSummary, StringComparison.Ordinal)) - continue; - - var children = range.GetChildren(); - var expander = children.FirstOrDefault(child => - child.Current.Name == MarkdownSelectionSummary - && child.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out _)); - if (expander is not null) - return (expander, rangeText, children); - } - return null; - } - - private static void LogRuntimeIdentity(GatewayFixtureRun run, Action writeLine) - { - var configuredApp = Environment.GetEnvironmentVariable("OPENCLAW_GATEWAY_FIXTURE_APP") - ?? throw new InvalidOperationException("The fixture app path was not explicitly configured."); - var expectedAppPath = Path.GetFullPath(configuredApp); - using var process = Process.GetProcessById(run.AppProcessId); - var loadedAppPath = process.MainModule?.FileName - ?? throw new InvalidOperationException("The fixture process did not expose its loaded apphost path."); - if (!string.Equals(expectedAppPath, Path.GetFullPath(loadedAppPath), StringComparison.OrdinalIgnoreCase)) - throw new InvalidOperationException( - $"Fixture app path mismatch: configured '{expectedAppPath}', loaded '{loadedAppPath}'."); - writeLine($"Fixture apphost: {loadedAppPath}; SHA256={HashFile(loadedAppPath)}"); - - var modules = process.Modules.Cast().ToArray(); - foreach (var moduleName in new[] { "OpenClaw.Tray.WinUI.dll", "Reactor.dll", "Reactor.Advanced.dll" }) - { - var module = modules.SingleOrDefault(candidate => - string.Equals(candidate.ModuleName, moduleName, StringComparison.OrdinalIgnoreCase)) - ?? throw new InvalidOperationException( - $"The fixture process did not expose loaded module '{moduleName}'."); - writeLine($"Loaded module {module.ModuleName}: {module.FileName}; SHA256={HashFile(module.FileName)}"); - } - } - - private static string HashFile(string path) => - Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))); - - private static async Task WaitForClipboardTextAsync(GatewayFixtureRun run) - { - string? text = null; - var lastClipboardError = 0; - try - { - await run.WaitForAsync(() => - { - if (!TryReadClipboardText(out text, out lastClipboardError)) - return Task.FromResult(false); - return Task.FromResult(!string.IsNullOrEmpty(text)); - }, "copied Markdown selection"); - } - catch (TimeoutException ex) - { - throw new InvalidOperationException( - $"Clipboard text was not available after the copy action (last Win32 error {lastClipboardError}).", ex); - } - return text!; - } - - private static string NormalizeClipboardText(string text) => - text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Trim(); - - private static string ReadClipboardTextOrEmpty() - { - if (!TryReadClipboardText(out var text, out var error) && error != 0) - throw new InvalidOperationException($"Could not inspect the isolated desktop clipboard (Win32 error {error})."); - return NormalizeClipboardText(text ?? string.Empty); - } - - private static void ClearClipboard() - { - if (!OpenClipboard(IntPtr.Zero)) - throw new InvalidOperationException($"Could not open the isolated desktop clipboard (Win32 error {Marshal.GetLastWin32Error()})."); - try - { - if (!EmptyClipboard()) - throw new InvalidOperationException($"Could not clear the isolated desktop clipboard (Win32 error {Marshal.GetLastWin32Error()})."); - } - finally { CloseClipboard(); } - } - - private static bool TryReadClipboardText(out string? text, out int error) - { - text = null; - error = 0; - if (!OpenClipboard(IntPtr.Zero)) - { - error = Marshal.GetLastWin32Error(); - return false; - } - try - { - if (!IsClipboardFormatAvailable(UnicodeTextClipboardFormat)) - return false; - var handle = GetClipboardData(UnicodeTextClipboardFormat); - if (handle == IntPtr.Zero) - { - error = Marshal.GetLastWin32Error(); - return false; - } - var pointer = GlobalLock(handle); - if (pointer == IntPtr.Zero) - { - error = Marshal.GetLastWin32Error(); - return false; - } - try - { - text = Marshal.PtrToStringUni(pointer); - return text is not null; - } - finally { GlobalUnlock(handle); } - } - finally { CloseClipboard(); } - } - - private sealed class NativeSelectionDrag : IDisposable - { - private const uint MouseInput = 0; - private const uint MouseMove = 0x0001; - private const uint LeftButtonDown = 0x0002; - private const uint LeftButtonUp = 0x0004; - private const uint MouseAbsolute = 0x8000; - private const uint MouseVirtualDesk = 0x4000; - private const int VirtualScreenLeft = 76; - private const int VirtualScreenTop = 77; - private const int VirtualScreenWidth = 78; - private const int VirtualScreenHeight = 79; - private readonly GatewayFixtureRun _run; - private readonly nint _window; - private readonly nint _previousDpiContext; - private bool _buttonDown; - - public NativeSelectionDrag(GatewayFixtureRun run, AutomationElement window, System.Drawing.Point start) - { - if (Environment.GetEnvironmentVariable("OPENCLAW_RUN_GATEWAY_FIXTURE_UI") != "1") - throw new InvalidOperationException("Native selection input requires the Gateway fixture UI opt-in."); - _run = run; - _window = new IntPtr(window.Current.NativeWindowHandle); - if (window.Current.ProcessId != run.AppProcessId || GetForegroundWindow() != _window) - throw new InvalidOperationException("Native selection input requires the foreground fixture-owned window."); - _previousDpiContext = SetThreadDpiAwarenessContext(new IntPtr(-4)); - if (_previousDpiContext == 0) - throw new InvalidOperationException("Could not establish physical-screen coordinates for native selection input."); - - try - { - MoveTo(start); - SendMouse(LeftButtonDown); - _buttonDown = true; - } - catch - { - SetThreadDpiAwarenessContext(_previousDpiContext); - throw; - } - } - - public bool IsButtonDown => _buttonDown && (GetAsyncKeyState(0x01) & 0x8000) != 0; - public System.Drawing.Point CurrentPosition - { - get - { - if (!GetCursorPos(out var actual)) - throw new InvalidOperationException( - $"Could not inspect the native pointer position (Win32 error {Marshal.GetLastWin32Error()})."); - return actual; - } - } - - public void MoveTo(System.Drawing.Point point) - { - _run.EnsureRunning(); - if (GetForegroundWindow() != _window) - throw new InvalidOperationException("The fixture window lost foreground ownership during native selection."); - if (_buttonDown) - { - SendAbsoluteMouseMove(point); - } - else if (!SetCursorPos(point.X, point.Y)) - { - throw new InvalidOperationException( - $"Could not move the pointer to selection start ({point.X}, {point.Y}) (Win32 error {Marshal.GetLastWin32Error()})."); - } - - var actual = new System.Drawing.Point(); - if (!GetCursorPos(out actual)) - throw new InvalidOperationException($"Could not inspect the native pointer position (Win32 error {Marshal.GetLastWin32Error()})."); - if (Math.Abs(actual.X - point.X) > 1 || Math.Abs(actual.Y - point.Y) > 1) - throw new InvalidOperationException( - $"Native pointer reached ({actual.X}, {actual.Y}), not requested point ({point.X}, {point.Y})."); - var root = GetAncestor(WindowFromPoint(actual), 2); - if (root != _window) - throw new InvalidOperationException( - $"Native selection point ({actual.X}, {actual.Y}) is outside the fixture-owned window."); - } - - public void Dispose() - { - try - { - if (_buttonDown) - { - SendMouse(LeftButtonUp); - _buttonDown = false; - } - } - finally { SetThreadDpiAwarenessContext(_previousDpiContext); } - } - - private void SendMouse(uint flags) - { - var input = new NativeInput - { - Type = MouseInput, - Data = new NativeInputUnion { Mouse = new NativeMouseInput { Flags = flags } } - }; - if (SendInput(1, [input], Marshal.SizeOf()) != 1) - throw new InvalidOperationException($"Native selection input failed with Win32 error {Marshal.GetLastWin32Error()}."); - } - - private static void SendAbsoluteMouseMove(System.Drawing.Point point) - { - var left = GetSystemMetrics(VirtualScreenLeft); - var top = GetSystemMetrics(VirtualScreenTop); - var width = GetSystemMetrics(VirtualScreenWidth); - var height = GetSystemMetrics(VirtualScreenHeight); - if (width <= 1 || height <= 1) - throw new InvalidOperationException("The virtual desktop does not expose usable pointer bounds."); - var x = (int)Math.Round((point.X - left) * 65535d / (width - 1), MidpointRounding.AwayFromZero); - var y = (int)Math.Round((point.Y - top) * 65535d / (height - 1), MidpointRounding.AwayFromZero); - var input = new NativeInput - { - Type = MouseInput, - Data = new NativeInputUnion - { - Mouse = new NativeMouseInput - { - X = Math.Clamp(x, 0, 65535), - Y = Math.Clamp(y, 0, 65535), - Flags = MouseMove | MouseAbsolute | MouseVirtualDesk, - } - } - }; - if (SendInput(1, [input], Marshal.SizeOf()) != 1) - throw new InvalidOperationException( - $"Native selection move failed with Win32 error {Marshal.GetLastWin32Error()}."); - } - - [StructLayout(LayoutKind.Sequential)] - private struct NativeInput - { - public uint Type; - public NativeInputUnion Data; - } - - [StructLayout(LayoutKind.Explicit)] - private struct NativeInputUnion - { - [FieldOffset(0)] public NativeMouseInput Mouse; - } - - [StructLayout(LayoutKind.Sequential)] - private struct NativeMouseInput - { - public int X; - public int Y; - public uint MouseData; - public uint Flags; - public uint Time; - public nuint ExtraInfo; - } - - [DllImport("user32.dll", SetLastError = true)] - private static extern uint SendInput(uint count, NativeInput[] inputs, int size); - [DllImport("user32.dll")] - private static extern bool SetCursorPos(int x, int y); - [DllImport("user32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool GetCursorPos(out System.Drawing.Point point); - [DllImport("user32.dll")] - private static extern int GetSystemMetrics(int index); - [DllImport("user32.dll")] - private static extern nint WindowFromPoint(System.Drawing.Point point); - [DllImport("user32.dll")] - private static extern nint GetAncestor(nint window, uint flags); - [DllImport("user32.dll")] - private static extern nint GetForegroundWindow(); - [DllImport("user32.dll")] - private static extern nint SetThreadDpiAwarenessContext(nint dpiContext); - [DllImport("user32.dll")] - private static extern short GetAsyncKeyState(int key); - } - - [DllImport("user32.dll", SetLastError = true)] - private static extern bool OpenClipboard(nint owner); - [DllImport("user32.dll", SetLastError = true)] - private static extern bool CloseClipboard(); - [DllImport("user32.dll", SetLastError = true)] - private static extern bool EmptyClipboard(); - [DllImport("user32.dll", SetLastError = true)] - private static extern bool IsClipboardFormatAvailable(uint format); - [DllImport("user32.dll", SetLastError = true)] - private static extern nint GetClipboardData(uint format); - [DllImport("kernel32.dll", SetLastError = true)] - private static extern nint GlobalLock(nint handle); - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool GlobalUnlock(nint handle); -} diff --git a/tests/OpenClaw.Tray.UITests/LocalizationHelperTests.cs b/tests/OpenClaw.Tray.UITests/LocalizationHelperTests.cs new file mode 100644 index 0000000000..bfb7aa6090 --- /dev/null +++ b/tests/OpenClaw.Tray.UITests/LocalizationHelperTests.cs @@ -0,0 +1,39 @@ +using OpenClawTray.Helpers; + +namespace OpenClaw.Tray.UITests; + +/// Exercises resource caching against the real Windows resource manager. +[Collection(UICollection.Name)] +public sealed class LocalizationHelperTests(UIThreadFixture ui) +{ + [Theory] + [InlineData("Chat_Activity_CommandOne", "{0} command", "{0} commande")] + [InlineData("SettingsSaveButton.Content", "Save", "Sauvegarder")] + public async Task CachedString_RefreshesOnLanguageOverride( + string key, string english, string french) + { + await ui.RunOnUIAsync(() => + { + var previousLanguage = LocalizationHelper.CurrentLanguage; + try + { + foreach (var (language, expected) in new[] + { ("en-us", english), ("fr-fr", french), ("en-us", english) }) + { + LocalizationHelper.SetLanguageOverride(language); + var first = LocalizationHelper.GetString(key); + Assert.Equal(expected, first); + Assert.Same(first, LocalizationHelper.GetString(key)); + } + + const string missing = "Missing_LocalizationCache_Regression"; + Assert.Equal(missing, LocalizationHelper.GetString(missing)); + Assert.Equal(missing, LocalizationHelper.GetString(missing)); + } + finally + { + LocalizationHelper.SetLanguageOverride(previousLanguage); + } + }); + } +} diff --git a/tests/OpenClaw.Tray.UITests/ReactorChatLayoutProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorChatLayoutProofTests.cs index 772845e906..767ca84abc 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorChatLayoutProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorChatLayoutProofTests.cs @@ -1827,11 +1827,11 @@ await ui.RunOnUIAsync(() => var assistantText = FindDescendants(host) .First(text => CollectText(text).Contains("Start with one outcome", StringComparison.Ordinal)); - Assert.False(assistantText.IsTextSelectionEnabled); + Assert.True(assistantText.IsTextSelectionEnabled); Assert.Equal(14, assistantText.FontSize); Assert.Equal("Segoe UI Variable Text", assistantText.FontFamily.Source); Assert.Equal(TextWrapping.Wrap, assistantText.TextWrapping); - var assistantSurface = AssistantMessageSurface(assistantText); + var assistantSurface = Ancestors(assistantText).OfType().First(); Assert.Null(assistantSurface.Background); Assert.Equal(new Thickness(0), assistantSurface.BorderThickness); Assert.Equal(new Thickness(0), assistantSurface.Padding); @@ -1860,7 +1860,7 @@ await ui.RunOnUIAsync(() => var input = FindControl(surface, "ChatComposerInput"); var composer = FindControl(surface, "ChatWritingSurface"); var paragraph = FindDescendants(host).First(text => CollectText(text).Contains("Start with one outcome", StringComparison.Ordinal)); - AssertCentered(surface, composer, AssistantMessageSurface(paragraph)); + AssertCentered(surface, composer, Ancestors(paragraph).OfType().First()); }); // Resize and rerender the same mounted tree. Do not recreate the footer @@ -1878,7 +1878,7 @@ await ui.RunOnUIAsync(() => AssertComposerBounds(surface); var composer = FindControl(surface, "ChatWritingSurface"); var paragraph = FindDescendants(host).First(text => CollectText(text).Contains("Start with one outcome", StringComparison.Ordinal)); - AssertCentered(surface, composer, AssistantMessageSurface(paragraph)); + AssertCentered(surface, composer, Ancestors(paragraph).OfType().First()); Assert.Same(originalInput, FindControl(surface, "ChatComposerInput")); Assert.Contains("A second line.", originalInput.Text, StringComparison.Ordinal); }); @@ -2060,7 +2060,7 @@ await ui.RunOnUIAsync(() => var input = FindControl(surface, "ChatComposerInput"); var composer = FindControl(surface, "ChatWritingSurface"); var paragraph = FindDescendants(host).First(text => CollectText(text).Contains("Start with one outcome", StringComparison.Ordinal)); - AssertCentered(surface, composer, AssistantMessageSurface(paragraph)); + AssertCentered(surface, composer, Ancestors(paragraph).OfType().First()); }); await CaptureAsync(surface, $"ReleasedParity-{width}-viewport"); }, "parity"); @@ -3494,7 +3494,7 @@ await ui.RunOnUIAsync(() => string? previousLayout = null; await ui.RunOnUIAsync(() => WaitForRenderedConditionAsync(() => { - // Selection wrappers add a layout layer; measure gaps only once block bounds stop moving. + // Measure gaps only once block bounds stop moving. var matches = FindDescendants(host).Where(panel => panel.Children.Count == 4 && panel.Spacing == ChatVisuals.MessageBlockGap).Take(2).ToArray(); stack = matches.Length == 1 ? matches[0] : null; @@ -4163,17 +4163,6 @@ private static void AssertComposerBounds(Border surface) Assert.True(Bounds(input, surface).Bottom <= controls.Min(control => Bounds(control, surface).Top)); } - private static Border AssistantMessageSurface(RichTextBlock paragraph) - { - // The unpadded styling Border around Reactor's blocks is the message's prose surface; - // it sits inside the message's Markdown selection host. - var surface = Ancestors(paragraph).OfType().First(); - Assert.Contains(Ancestors(surface).OfType(), control => - FrameworkElementAutomationPeer.CreatePeerForElement(control) - ?.GetPattern(PatternInterface.Text) is ITextProvider); - return surface; - } - private static IEnumerable Ancestors(DependencyObject element) { while (VisualTreeHelper.GetParent(element) is { } parent) diff --git a/tests/OpenClaw.Tray.UITests/ReactorMarkdownListProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorMarkdownListProofTests.cs index 00769c6ee1..a2ddb3f39b 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorMarkdownListProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorMarkdownListProofTests.cs @@ -1,7 +1,5 @@ using Microsoft.UI.Reactor.Hosting; using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Automation.Peers; -using Microsoft.UI.Xaml.Automation.Provider; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; using OpenClawTray.Chat; @@ -46,17 +44,14 @@ await _ui.RunOnUIAsync(() => await _ui.RunOnUIAsync(() => { _ui.Container.UpdateLayout(); - var row = Assert.Single(FindDescendants(root!), IsListRow); - var markerBlock = Assert.Single(FindDescendants(row.Children[0])); - var content = Assert.Single(FindDescendants(row.Children[1])); + var row = Assert.Single(FindLogical(root!), IsListRow); + var markerBlock = Assert.IsType(row.Children[0]); + var content = Assert.IsType(row.Children[1]); Assert.Equal(marker, markerBlock.Text); Assert.Equal(LongText, CollectText(content)); Assert.Equal(TextWrapping.Wrap, content.TextWrapping); - Assert.Single(FindDescendants(root!), control => - FrameworkElementAutomationPeer.CreatePeerForElement(control) - ?.GetPattern(PatternInterface.Text) is ITextProvider); - Assert.False(content.IsTextSelectionEnabled); - Assert.Equal(1, Grid.GetColumn((FrameworkElement)row.Children[1])); + Assert.True(content.IsTextSelectionEnabled); + Assert.Equal(1, Grid.GetColumn(content)); Assert.InRange(content.ActualWidth, 1, 239); Assert.True(content.ActualWidth + markerBlock.ActualWidth + row.ColumnSpacing <= 241); narrowHeight = content.ActualHeight; @@ -68,8 +63,8 @@ await _ui.RunOnUIAsync(() => await _ui.RunOnUIAsync(() => { _ui.Container.UpdateLayout(); - var row = Assert.Single(FindDescendants(root!), IsListRow); - var content = Assert.Single(FindDescendants(row.Children[1])); + var row = Assert.Single(FindLogical(root!), IsListRow); + var content = Assert.IsType(row.Children[1]); Assert.True(content.ActualHeight < narrowHeight); Assert.Equal(LongText, CollectText(content)); Assert.InRange(content.ActualWidth, 241, 600); @@ -111,15 +106,15 @@ await _ui.RunOnUIAsync(() => await _ui.RunOnUIAsync(() => { _ui.Container.UpdateLayout(); - var rows = FindDescendants(host!).Where(IsListRow).ToArray(); + var rows = FindLogical(host!).Where(IsListRow).ToArray(); Assert.Equal(3, rows.Length); Assert.All(rows, row => { - Assert.Equal("\u2022 ", Assert.Single(FindDescendants(row.Children[0])).Text); + Assert.Equal("\u2022 ", Assert.IsType(row.Children[0]).Text); Assert.Equal(1, Grid.GetColumn((FrameworkElement)row.Children[1])); Assert.InRange(((FrameworkElement)row.Children[1]).ActualWidth, 1, 279); }); - var texts = FindDescendants(host!).ToArray(); + var texts = FindLogical(host!).ToArray(); Assert.Contains(texts, text => CollectText(text) == "second paragraph"); Assert.Contains(texts, text => CollectText(text) == "nested item"); Assert.Contains(texts, text => CollectText(text) == "quoted content"); @@ -146,7 +141,7 @@ private static bool IsListRow(Grid grid) => grid.ColumnDefinitions[0].Width.IsAuto && grid.ColumnDefinitions[1].Width.IsStar && grid.Children.Count == 2 && - FindDescendants(grid.Children[0]).Any(); + grid.Children[0] is TextBlock; private static IEnumerable GetRuns(RichTextBlock text) => text.Blocks.OfType().SelectMany(paragraph => paragraph.Inlines.OfType()); diff --git a/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs index 99e2a9fe98..d4a53ade92 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs @@ -1,8 +1,6 @@ using Microsoft.UI.Reactor.Core; using Microsoft.UI.Reactor.Hosting; using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Automation.Peers; -using Microsoft.UI.Xaml.Automation.Provider; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; @@ -78,10 +76,8 @@ await _ui.RunOnUIAsync(() => var cells = table.Children.SelectMany(FindDescendants).ToArray(); Assert.Equal(12, cells.Length); - Assert.Single(FindDescendants(root!), control => - FrameworkElementAutomationPeer.CreatePeerForElement(control) - ?.GetPattern(PatternInterface.Text) is ITextProvider); - Assert.All(cells, cell => Assert.False(cell.IsTextSelectionEnabled)); + Assert.Equal(ChatVisuals.TableRadius, Assert.IsType(VisualTreeHelper.GetParent(table)).CornerRadius.TopLeft); + Assert.All(cells, cell => Assert.True(cell.IsTextSelectionEnabled)); Assert.All(cells, cell => Assert.Equal(TextWrapping.Wrap, cell.TextWrapping)); Assert.All(cells, cell => Assert.InRange(cell.ActualWidth, 1, 160)); Assert.True(GetRuns(cells[0]).All(run => run.FontWeight.Weight > 400)); diff --git a/tests/OpenClaw.Tray.UITests/ReactorMarkdownWebParityProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorMarkdownWebParityProofTests.cs index fe5f1ed101..51c926f7ee 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorMarkdownWebParityProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorMarkdownWebParityProofTests.cs @@ -2,8 +2,6 @@ // Run with --filter FullyQualifiedName~ReactorMarkdownWebParityProofTests. using Microsoft.UI.Reactor.Hosting; using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Automation.Peers; -using Microsoft.UI.Xaml.Automation.Provider; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; @@ -99,9 +97,7 @@ await ui.RunOnUIAsync(() => Assert.Same(Application.Current.Resources["ChatProseRichTextStyle"], prose.Style); var heading = Assert.Single(rich, text => Text(text) == "The design"); Assert.Same(Application.Current.Resources["ChatRichTextStyle"], heading.Style); - // Message selection wraps each block and carries the block's margin on the wrapper. - var headingBlock = Assert.IsAssignableFrom(VisualTreeHelper.GetParent(heading)); - Assert.Equal(ChatVisuals.HeadingTopInset, headingBlock.Margin.Top); + Assert.Equal(ChatVisuals.HeadingTopInset, heading.Margin.Top); var tightItem = Assert.Single(rich, text => Text(text) == "Backslashes are fine"); Assert.Same(Application.Current.Resources["ChatProseRichTextStyle"], tightItem.Style); @@ -125,8 +121,7 @@ await ui.RunOnUIAsync(() => Assert.Empty(diagram.TextHighlighters); var code = Assert.Single(rich, text => Text(text) == TypeScript); - // The message's selection provider replaces native per-block selection. - Assert.False(code.IsTextSelectionEnabled); + Assert.True(code.IsTextSelectionEnabled); Assert.Equal(ChatVisuals.CodeSize, code.FontSize); Assert.Single(Runs(code)); Assert.Equal(dark ? 0xFFFF8A80u : 0xFFB81B27u, Ink(code, "type")); @@ -150,23 +145,13 @@ await ui.RunOnUIAsync(() => // Re-rendering unchanged Markdown keeps Reactor's code paragraph, the user's // selection and the syntax colors. var paragraph = code.Blocks[0]; - var selection = Assert.Single(Ancestors(code).OfType(), control => - FrameworkElementAutomationPeer.CreatePeerForElement(control) - ?.GetPattern(PatternInterface.Text) is ITextProvider); - ITextProvider TextProvider() => (ITextProvider)FrameworkElementAutomationPeer - .CreatePeerForElement(selection)!.GetPattern(PatternInterface.Text); - // A range inside one block is harder to keep than the whole document: it must - // survive re-render at the same text offsets. - var codeRange = TextProvider().DocumentRange.FindText(TypeScript, false, false); - Assert.NotNull(codeRange); - codeRange.Select(); - var selected = Assert.Single(TextProvider().GetSelection()).GetText(-1); - Assert.Equal(TypeScript, selected); + code.SelectAll(); + Assert.Equal(TypeScript, code.SelectedText); var next = ReactorChatTimeline.BuildSafeMarkdown(Markdown); host!.Reconciler.Reconcile(element, next, root, static () => { }); element = next; Assert.Same(paragraph, code.Blocks[0]); - Assert.Equal(selected, Assert.Single(TextProvider().GetSelection()).GetText(-1)); + Assert.Equal(TypeScript, code.SelectedText); Assert.Equal(dark ? 0xFFFF8A80u : 0xFFB81B27u, Ink(code, "type")); // Switching theme re-resolves the syntax palette. @@ -305,7 +290,7 @@ await WaitForRenderedConditionAsync( } [Fact] - public async Task SelectableListMarker_RestylesWhenItsRowUpdatesInPlace() + public async Task ListMarker_RestylesWhenItsRowUpdatesInPlace() { await ui.ResetContainerAsync(); ReactorHostControl? host = null; From 956755612f7e2f050a0ac1289c15516bec713d91 Mon Sep 17 00:00:00 2001 From: Barbara Kudiess Date: Thu, 19 Nov 2026 20:51:10 -0800 Subject: [PATCH 2/2] fix: preserve nested markdown table presentation Retain list-column placement, add nested table proof, and stabilize the elapsed-clock UI test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docs/ARCHITECTURE.md | 2 +- docs/CHAT_VISUAL_DESIGN.md | 14 +-- .../Chat/ChatMarkdownPresentation.cs | 40 ++++++++- .../ReactorChatMarkdownContractTests.cs | 5 +- .../ReactorMarkdownTableProofTests.cs | 86 +++++++++++++++++++ .../ReactorToolActivityProofTests.cs | 47 ++++++++-- 6 files changed, 175 insertions(+), 19 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2c0eedd130..29a62c20e0 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -1262,7 +1262,7 @@ leading and trailing pipe. Columns, in order: | migration-consent-locking | authoritative | InnoMigrationConsentStore | protected consent publication and inspection while source is running | MigrationOperationLock + InnoMigrationConsentStore | consent uses a shared preparation lease and a separate exclusive writer lock | consent remains compatible with Inno runtime readers but cannot overlap exclusive Store mutation; finalization removes the writer lock before deleting the completion receipt | InnoMigrationConsentStoreTests.RuntimeReadLease_AllowsExplicitGrantAndInspection | behavioral | - | | chat-markdown-presentation | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | paragraph and heading typography, literal code-frame presentation and Copy | ChatMarkdownPresentation | timeline keeps sanitization, parser flags and inert links/images; Reactor owns default list wrapping | presentation uses public MarkdownOptions callbacks; code content is never truncated or parsed as markup | ReactorChatLayoutProofTests.CodeBlock_PreservesLiteralContentAndCopiesExactText | behavioral | - | | chat-markdown-disclosures | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | allowlisted Markdown disclosure parsing and native expansion state | ChatMarkdownDisclosures + ChatMarkdownPresentation | timeline sanitizes before parsing; disclosure bodies use the existing safe Markdown renderer | only balanced, allowlisted block wrappers become native Expanders; unsupported wrappers remain inert Markdown; a reader-toggled header is the timeline's scroll anchor, so details grow downward inside tall messages | ChatMarkdownDisclosureTests.CommonWrappers_KeepBodyMarkdownAndSurroundingOrder | behavioral | - | -| chat-markdown-selection-integration | closed | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | Reactor MarkdownTextSelection cross-block scope, ChatMarkdownMessageView and selection copy-result feedback | native per-block text selection in ReactorChatTimeline + ChatMarkdownPresentation | text, code, table and list blocks stay individually selectable; Copy buttons keep their own feedback | chat Markdown does not mount Reactor preview.18 selection hosts, which rescan native text geometry on every LayoutUpdated and slowed typing and streaming | ReactorChatMarkdownContractTests.ChatMarkdown_DoesNotUseReactorCrossBlockSelection | source-shape | when a Reactor package with demand-driven selection geometry is adopted and proven against typing and streaming latency | +| chat-markdown-selection-integration | closed | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | Reactor MarkdownTextSelection cross-block scope, ChatMarkdownMessageView and selection copy-result feedback | native per-block text selection in ReactorChatTimeline + ChatMarkdownPresentation | text, code, table and list blocks stay individually selectable; Copy buttons keep their own feedback | interim mitigation does not provide cross-block selection or click-out deselection parity; upstream fixes and matched long-chat latency/frame-time proof are pending | ReactorChatMarkdownContractTests.ChatMarkdown_DoesNotUseReactorCrossBlockSelection | source-shape | after upstream fixes are released and downstream selection parity and performance are verified | | reactor-markdown-list-layout | closed | src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs | preview.12 ListItem layout replacement and StackElement shape assumption | Microsoft.UI.Reactor.Advanced default Markdown list layout (preview.16, upstream #1197) | timeline keeps sanitizer, parser safety and block presentation callbacks | ordered/unordered, nested and loose list content wraps in an Auto/Star Grid without an application-side ListItem override | ReactorChatMarkdownContractTests.AssistantMessages_UseSanitizedGitHubFlavoredMarkdown | source-shape | when upstream Markdown list layout is replaced | | chat-model-picker-presentation | authoritative | src/OpenClaw.Tray.WinUI/Chat/ReactorChatComposer.cs, then custom Button rows in ChatModelPicker | catalog search, provider groups, native row interaction states and model metadata presentation | NativeChatModelPicker through a generated Reactor wrapper | ChatModelPicker owns flyout lifetime; composer controller owns model mutation and authorization | native single-selection navigation never writes a model until explicit activation; filtering and resizing preserve raw provider-qualified identities | ReactorChatLayoutProofTests.ModelMenu_UsesNativeSelectionStatesAndPreservesNavigationDuringResize | behavioral | - | | chat-model-picker-button-rows-closed | closed | src/OpenClaw.Tray.WinUI/Chat/ChatModelPicker.cs | hand-built Button rows, custom selected fill/checkmark and search-box styling | NativeChatModelPicker | declarative trigger and flyout lifetime only | native AutoSuggestBox/ListView own search and row interaction visuals; no duplicate hand-styled selection path | ReactorChatLayoutProofTests.ModelMenu_UsesNativeSelectionStatesAndPreservesNavigationDuringResize | behavioral | - | diff --git a/docs/CHAT_VISUAL_DESIGN.md b/docs/CHAT_VISUAL_DESIGN.md index 4b09077ba2..21efab1570 100644 --- a/docs/CHAT_VISUAL_DESIGN.md +++ b/docs/CHAT_VISUAL_DESIGN.md @@ -1362,12 +1362,14 @@ inert images, list measurement, timeline keys and scroll-follow remain with their existing owners. Assistant Markdown text, code and table blocks are individually selectable with -native WinUI text selection. Cross-block selection through Reactor's -`MarkdownTextSelection` is not used: its preview.18 host rescans native text -geometry on every layout pass, which slowed typing and streaming. Re-enable it -only with a Reactor package that tracks selection geometry on demand, proven -against typing and streaming latency. The whole-message and code Copy buttons -keep their own clipboard and feedback paths. +native WinUI text selection. Reactor preview.18's `MarkdownTextSelection` is +not used as an interim mitigation while upstream fixes and matched long-chat +typing and streaming latency/frame-time proof are pending. +This does not provide cross-block selection or click-out deselection parity. +Restore those behaviors in a separate downstream change only after the fixed +Reactor package is released and selection parity and performance are verified. +The whole-message and code Copy buttons keep their own clipboard and feedback +paths. Assistant Markdown accepts balanced, block-level `
` wrappers with a nonempty `` as an OpenClaw extension. The summary may follow the diff --git a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs b/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs index 045fdbffb9..d262c20292 100644 --- a/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs +++ b/src/OpenClaw.Tray.WinUI/Chat/ChatMarkdownPresentation.cs @@ -261,11 +261,44 @@ element is StackElement stack // Compact thinking text keeps its tighter pre-web rhythm. Spacing = compactSecondaryReading ? ChatVisuals.BlockGap : ChatVisuals.MessageBlockGap, Children = stack.Children - .Select((child, index) => ResetBlockEdges(Table(child), index == 0 || compactSecondaryReading)) + .Select((child, index) => ResetBlockEdges(StyleNestedTables(child), index == 0 || compactSecondaryReading)) .ToArray(), } : element; + private static Element StyleNestedTables(Element element) + { + switch (element) + { + case StackElement stack: + var stackChildren = StyleNestedTables(stack.Children); + return stackChildren is null ? stack : stack with { Children = stackChildren }; + case GridElement grid: + var gridChildren = StyleNestedTables(grid.Children); + return Table(gridChildren is null ? grid : grid with { Children = gridChildren }); + case BorderElement { Child: { } child } border: + var styledChild = StyleNestedTables(child); + return ReferenceEquals(child, styledChild) ? border : border with { Child = styledChild }; + default: + return element; + } + } + + private static Element[]? StyleNestedTables(IReadOnlyList children) + { + Element[]? styledChildren = null; + for (var index = 0; index < children.Count; index++) + { + var child = children[index]; + var styledChild = StyleNestedTables(child); + if (ReferenceEquals(child, styledChild)) + continue; + styledChildren ??= children.ToArray(); + styledChildren[index] = styledChild; + } + return styledChildren; + } + // The first and last blocks add no outer gap; only later headings keep their extra lead-in. private static Element ResetBlockEdges(Element element, bool dropTop) { @@ -364,7 +397,7 @@ internal static Element BlockQuote(Element quote) /// Web-parity table: Reactor's message-level table grid (Auto rows, Star columns, so long cells /// keep wrapping at narrow widths) gets a rounded frame, 1px cell rules and a tinted header row. /// Reactor's Table hook only receives rows with TextSelection enabled, so the built grid is - /// restyled instead; tables nested in lists or quotes keep Reactor's plain grid. + /// restyled instead; the recursive element pass applies this to tables nested in lists or quotes. /// private static Element Table(Element block) { @@ -389,11 +422,12 @@ private static Element Table(Element block) .BorderThickness(0, 0, at.Column == columns - 1 ? 0 : 1, at.Row == rows - 1 ? 0 : 1) .Grid(row: at.Row, column: at.Column); }); - return Border(grid with { Children = [header, .. cells] }) + var frame = (BorderElement)Border(grid with { Children = [header, .. cells] }) .CornerRadius(ChatVisuals.TableRadius) .BorderThickness(1) .BorderBrush(stroke) .Background(Theme.Ref("ChatCardBrush")); + return frame with { Attached = grid.Attached }; } private static Element Format(Element element, double size, bool heading) diff --git a/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs b/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs index a0fce58509..464e3648c4 100644 --- a/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs +++ b/tests/OpenClaw.Tray.Tests/ReactorChatMarkdownContractTests.cs @@ -61,7 +61,6 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() // Table hook is not used because it receives no rows without TextSelection. Assert.DoesNotContain("Table = ChatMarkdownPresentation.Table", timeline, StringComparison.Ordinal); Assert.Contains("TableHeaderBackground = null", timeline, StringComparison.Ordinal); - Assert.Contains("ResetBlockEdges(Table(child)", presentation, StringComparison.Ordinal); // Fenced code keeps Reactor's single literal run (so reconcile never rebuilds it and // selection survives) and is colored by theme-resolved highlighters. Assert.Contains("ApplyCodeSyntax(text, code, language);", presentation, StringComparison.Ordinal); @@ -76,8 +75,8 @@ public void AssistantMessages_UseSanitizedGitHubFlavoredMarkdown() [Fact] public void ChatMarkdown_DoesNotUseReactorCrossBlockSelection() { - // Reactor preview.18's MarkdownTextSelection host rescans native text geometry on every - // LayoutUpdated, which made typing and streaming slow. Re-enable it only with a fixed package. + // Keep preview.18 cross-block selection out of the interim path until upstream fixes and + // matched long-chat latency, frame-time and selection-parity proof are available. var tray = Path.Combine(TestRepositoryPaths.GetRepositoryRoot(), "src", "OpenClaw.Tray.WinUI"); foreach (var file in Directory.EnumerateFiles(tray, "*.cs", SearchOption.AllDirectories)) { diff --git a/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs index d4a53ade92..8b2f450288 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorMarkdownTableProofTests.cs @@ -105,6 +105,8 @@ await _ui.RunOnUIAsync(() => FindDescendants(root!), text => CollectText(text).Contains("literal | pipe", StringComparison.Ordinal)); Assert.Contains("---|---", CollectText(code), StringComparison.Ordinal); + Assert.Single(FindDescendants(root!), + border => border.CornerRadius.TopLeft == ChatVisuals.TableRadius); Assert.Equal(13, code.FontSize); Assert.Equal(18, code.LineHeight); Assert.Single( @@ -131,6 +133,90 @@ await _ui.RunOnUIAsync(() => } } + [Fact] + public async Task AssistantMarkdown_StylesTablesNestedInListsAndQuotes() + { + const string markdown = + "- List table\n\n" + + " Metric | Value\n" + + " ---|---\n" + + " nested-list | 1\n\n" + + "- Metric | Value\n" + + " ---|---\n" + + " table-only | This long cell value must wrap within the list content column.\n\n" + + "> Quote table\n>\n" + + "> Metric | Value\n" + + "> ---|---\n" + + "> nested-quote | 2\n"; + + await _ui.ResetContainerAsync(); + ReactorHostControl? host = null; + UIElement? root = null; + ChatThemeProofScope? themeScope = null; + try + { + await _ui.RunOnUIAsync(() => + { + themeScope = new ChatThemeProofScope("Light"); + host = new ReactorHostControl + { + Width = 320, + RequestedTheme = ElementTheme.Light, + }; + var element = ReactorChatTimeline.BuildSafeMarkdown(markdown); + root = host.Reconciler.Mount(element, static () => { }); + host.Content = root; + _ui.Container.Children.Add(host); + }); + await _ui.YieldToRenderAsync(); + + await _ui.RunOnUIAsync(() => + { + _ui.Container.UpdateLayout(); + var tables = FindDescendants(root!) + .Where(grid => grid.ColumnDefinitions.Count == 2 && grid.RowDefinitions.Count == 2) + .ToArray(); + + var grids = string.Join("; ", FindDescendants(root!).Select(grid => + $"{grid.RowDefinitions.Count}x{grid.ColumnDefinitions.Count}: " + + string.Join("|", grid.Children.SelectMany(FindDescendants).Select(CollectText)))); + Assert.True(tables.Length == 3, $"Expected 3 nested tables, found {tables.Length}. Grids: {grids}"); + var tableTexts = tables.Select(table => string.Join("|", table.Children + .SelectMany(FindDescendants) + .Select(CollectText))).ToArray(); + Assert.Contains(tableTexts, text => text.Contains("nested-list", StringComparison.Ordinal)); + Assert.Contains(tableTexts, text => text.Contains("nested-quote", StringComparison.Ordinal)); + var tableOnly = Assert.Single(tables, table => table.Children + .SelectMany(FindDescendants) + .Select(CollectText) + .Any(text => text.Contains("table-only", StringComparison.Ordinal))); + var tableOnlyFrame = Assert.IsType(VisualTreeHelper.GetParent(tableOnly)); + Assert.Equal(1, Grid.GetColumn(tableOnlyFrame)); + Assert.InRange(tableOnlyFrame.ActualWidth, 1, host!.Width); + var tableOnlyCells = tableOnly.Children.SelectMany(FindDescendants).ToArray(); + Assert.All(tableOnlyCells, cell => Assert.Equal(TextWrapping.Wrap, cell.TextWrapping)); + foreach (var table in tables) + { + var frame = Assert.IsType(VisualTreeHelper.GetParent(table)); + Assert.Equal(ChatVisuals.TableRadius, frame.CornerRadius.TopLeft); + } + }); + } + finally + { + if (host is not null) + { + await _ui.RunOnUIAsync(() => + { + _ui.Container.Children.Remove(host); + host.Content = null; + host.Dispose(); + themeScope?.Dispose(); + }); + } + } + } + private static string CollectText(RichTextBlock richTextBlock) { var text = new System.Text.StringBuilder(); diff --git a/tests/OpenClaw.Tray.UITests/ReactorToolActivityProofTests.cs b/tests/OpenClaw.Tray.UITests/ReactorToolActivityProofTests.cs index 97683c460f..883726aab9 100644 --- a/tests/OpenClaw.Tray.UITests/ReactorToolActivityProofTests.cs +++ b/tests/OpenClaw.Tray.UITests/ReactorToolActivityProofTests.cs @@ -1,3 +1,4 @@ +using System.Globalization; using Microsoft.UI.Reactor; using Microsoft.UI.Reactor.Core; using Microsoft.UI.Reactor.Animation; @@ -27,6 +28,25 @@ public sealed class ReactorToolActivityProofTests private const string ChevronRightGlyph = "\uE76C"; private const string ChevronDownGlyph = "\uE70D"; + private static int ReadElapsedSeconds(string text) + { + var separator = text.LastIndexOf('\u00b7'); + Assert.True(separator >= 0 && separator < text.Length - 1, $"Elapsed time is missing from '{text}'."); + var totalSeconds = 0; + foreach (var part in text[(separator + 1)..].Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries)) + { + var value = int.Parse(part.AsSpan(0, part.Length - 1), CultureInfo.InvariantCulture); + totalSeconds += part[^1] switch + { + 'h' => value * 3600, + 'm' => value * 60, + 's' => value, + _ => throw new InvalidOperationException($"Unexpected elapsed-time unit in '{part}'."), + }; + } + return totalSeconds; + } + private static int WorkDividerCount(DependencyObject root) => FindLogical(root).Count(border => border.Height == 1); @@ -576,8 +596,9 @@ await _ui.RunOnUIAsync(async () => { using var theme = new ChatThemeProofScope("Light"); using var host = new ReactorHostControl(); + var startedAt = DateTimeOffset.UtcNow.AddSeconds(-65); host.Mount(_ => Component(new(new( - DateTimeOffset.UtcNow.AddSeconds(-65), null, "Working", true, false, OutputTokens: 12345)))); + startedAt, null, "Working", true, false, OutputTokens: 12345)))); _ui.Container.Children.Add(host); try { @@ -592,11 +613,25 @@ await WaitForRenderedConditionAsync(() => FindLogical(host).Any(block Assert.StartsWith(" \u00b7 12,345 output tokens \u00b7 ", text.Text); Assert.Contains("1m", text.Text); Assert.IsNotType(text.Foreground); - var initial = text.Text; - await WaitForRenderedConditionAsync(() => text.Text != initial, "the running clock ticks"); - Assert.Equal(1, row.Opacity); - Assert.StartsWith(" \u00b7 12,345 output tokens \u00b7 ", text.Text); - Assert.IsNotType(text.Foreground); + var initialElapsedSeconds = ReadElapsedSeconds(text.Text); + await WaitForRenderedConditionAsync( + () => FindLogical(host).Any(block => + AutomationProperties.GetAutomationId(block) == "ChatRunProgressText" && + ReadElapsedSeconds(block.Text) > initialElapsedSeconds), + "the running clock advances", + () => string.Join(" | ", FindLogical(host) + .Where(block => AutomationProperties.GetAutomationId(block) == "ChatRunProgressText") + .Select(block => block.Text))); + var updatedText = Assert.Single(FindLogical(host), + block => AutomationProperties.GetAutomationId(block) == "ChatRunProgressText"); + var updatedRow = Assert.Single(FindLogical(host), + grid => AutomationProperties.GetAutomationId(grid) == "ChatRunProgress"); + Assert.Same(text, updatedText); + Assert.Same(row, updatedRow); + Assert.Equal(1, updatedRow.Opacity); + Assert.StartsWith(" \u00b7 12,345 output tokens \u00b7 ", updatedText.Text); + Assert.True(ReadElapsedSeconds(updatedText.Text) > initialElapsedSeconds); + Assert.IsNotType(updatedText.Foreground); } finally {