Skip to content

Commit 4712e22

Browse files
shanselmanCopilot
andauthored
fix(chat): stabilize composer accessibility state (#1135)
* fix(chat): stabilize composer accessibility state Co-authored-by: Copilot App <[email protected]> * test(a11y): prove composer UIA recovery Co-authored-by: Copilot App <[email protected]> --------- Co-authored-by: Copilot App <[email protected]>
1 parent c03d4da commit 4712e22

5 files changed

Lines changed: 392 additions & 24 deletions

File tree

‎src/OpenClaw.Tray.WinUI/Chat/OpenClawReactorChatRoot.cs‎

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,21 @@ void Send()
745745
: Localized("Chat_Composer_Tooltip_Send", "Send");
746746
var controlCornerRadius = new CornerRadius(4);
747747

748-
Element IconButton(string glyph, string automationName, Action onClick, bool enabled = true)
748+
Element IconButton(
749+
string glyph,
750+
string automationName,
751+
Action onClick,
752+
bool enabled = true,
753+
string? automationId = null)
749754
{
750755
return Button(
751756
TextBlock(glyph).Set(textBlock =>
752757
{
753758
textBlock.FontFamily = FluentIconCatalog.SymbolThemeFontFamily;
754759
textBlock.FontSize = 16;
760+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAccessibilityView(
761+
textBlock,
762+
Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw);
755763
}),
756764
onClick)
757765
.AutomationName(automationName)
@@ -773,11 +781,25 @@ Element IconButton(string glyph, string automationName, Action onClick, bool ena
773781
button.CornerRadius = controlCornerRadius;
774782
button.IsEnabled = enabled;
775783
button.BorderThickness = new Thickness(0);
784+
if (!string.IsNullOrWhiteSpace(automationId))
785+
{
786+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(
787+
button,
788+
automationId);
789+
}
790+
ComposerAutomationVisibility.Prepare(button);
776791
ToolTipService.SetToolTip(button, automationName);
777-
});
792+
})
793+
.OnUnmount(control => ComposerAutomationVisibility.Detach(
794+
(FrameworkElement)control));
778795
}
779796

780-
Element PickerButton(string label, string automationName, bool enabled, double maxLabelWidth)
797+
Element PickerButton(
798+
string label,
799+
string automationName,
800+
string automationId,
801+
bool enabled,
802+
double maxLabelWidth)
781803
{
782804
return Button(
783805
HStack(
@@ -794,6 +816,9 @@ Element PickerButton(string label, string automationName, bool enabled, double m
794816
textBlock.FontFamily = FluentIconCatalog.SymbolThemeFontFamily;
795817
textBlock.FontSize = 10;
796818
textBlock.Margin = new Thickness(2, 4, 0, 0);
819+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAccessibilityView(
820+
textBlock,
821+
Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw);
797822
})),
798823
() => { })
799824
.AutomationName(automationName)
@@ -814,7 +839,13 @@ Element PickerButton(string label, string automationName, bool enabled, double m
814839
button.CornerRadius = controlCornerRadius;
815840
button.IsEnabled = enabled;
816841
button.BorderThickness = new Thickness(0);
817-
});
842+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(
843+
button,
844+
automationId);
845+
ComposerAutomationVisibility.Prepare(button);
846+
})
847+
.OnUnmount(control => ComposerAutomationVisibility.Detach(
848+
(FrameworkElement)control));
818849
}
819850

820851
var attachmentRows = props.PendingAttachments
@@ -1122,6 +1153,7 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11221153
control.Resources["TextControlBorderBrush"] = transparent;
11231154
control.Resources["TextControlBorderBrushFocused"] = transparent;
11241155
control.Resources["TextControlBorderBrushPointerOver"] = transparent;
1156+
ComposerAutomationVisibility.Prepare(control);
11251157
})
11261158
.OnMount(control =>
11271159
{
@@ -1136,6 +1168,7 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11361168
var textBox = (TextBox)control;
11371169
textBox.Paste -= pasteHandler.Current;
11381170
textBox.ContextFlyout = null;
1171+
ComposerAutomationVisibility.Detach(textBox);
11391172
});
11401173
UseEffect((Func<Action>)(() =>
11411174
{
@@ -1150,6 +1183,7 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11501183
PickerButton(
11511184
props.CurrentThread.Title,
11521185
$"{Localized("Chat_Composer_Accessibility_Session", "Session")}: {props.CurrentThread.Title}",
1186+
"ChatComposerSessionPicker",
11531187
!props.MessageOptionsDisabled && props.AvailableChannels.Count > 1,
11541188
props.IsCompact ? 56 : 160),
11551189
props.AvailableChannels
@@ -1167,6 +1201,7 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11671201
PickerButton(
11681202
modelPickerLabel,
11691203
$"{Localized("Chat_Composer_Accessibility_Model", "Model")}: {modelPickerLabel}",
1204+
"ChatComposerModelPicker",
11701205
!props.MessageOptionsDisabled,
11711206
props.IsCompact ? 68 : 180),
11721207
modelNames
@@ -1187,6 +1222,7 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11871222
PickerButton(
11881223
ThinkingLevels[thinkingIndex],
11891224
$"{Localized("Chat_Composer_Accessibility_Reasoning", "Reasoning")}: {ThinkingLevels[thinkingIndex]}",
1225+
"ChatComposerReasoningPicker",
11901226
!props.MessageOptionsDisabled,
11911227
props.IsCompact ? 54 : 96),
11921228
ThinkingLevels
@@ -1201,7 +1237,8 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
12011237
"\uE723",
12021238
Localized("Chat_Composer_Tooltip_Attach", "Attach"),
12031239
() => props.OnAttachClick?.Invoke(),
1204-
props.OnAttachClick is not null);
1240+
props.OnAttachClick is not null,
1241+
"ChatComposerAttach");
12051242
var voiceButton = IconButton(
12061243
isRecording
12071244
? "\uE15B"
@@ -1219,25 +1256,35 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
12191256
else
12201257
StartVoiceRecording();
12211258
},
1222-
props.OnVoiceRequest is not null);
1259+
props.OnVoiceRequest is not null,
1260+
"ChatComposerVoice");
12231261
var speakerButton = IconButton(
12241262
props.IsSpeakerMuted ? "\uE74F" : "\uE767",
12251263
props.IsSpeakerMuted ? "Unmute" : "Mute",
1226-
props.OnSpeakerToggle);
1264+
props.OnSpeakerToggle,
1265+
automationId: "ChatComposerSpeakerToggle");
12271266
Element settingsButton = props.IsCompact || props.OnSettingsClick is null
12281267
? Empty()
12291268
: IconButton(
12301269
"\uE713",
12311270
Localized("Chat_Composer_Tooltip_Settings", "Settings"),
1232-
props.OnSettingsClick);
1271+
props.OnSettingsClick,
1272+
automationId: "ChatComposerSettings");
12331273

12341274
Element primaryAction = props.TurnActive
1235-
? IconButton("\uE71A", actionLabel, props.OnStop)
1275+
? IconButton(
1276+
"\uE71A",
1277+
actionLabel,
1278+
props.OnStop,
1279+
automationId: "ChatComposerPrimaryAction")
12361280
: Button(
12371281
TextBlock("\uE724").Set(textBlock =>
12381282
{
12391283
textBlock.FontFamily = FluentIconCatalog.SymbolThemeFontFamily;
12401284
textBlock.FontSize = 16;
1285+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAccessibilityView(
1286+
textBlock,
1287+
Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw);
12411288
}),
12421289
Send)
12431290
.AccentButton()
@@ -1250,12 +1297,18 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
12501297
button.MinHeight = 32;
12511298
button.Padding = new Thickness(0);
12521299
button.CornerRadius = controlCornerRadius;
1300+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(
1301+
button,
1302+
"ChatComposerPrimaryAction");
12531303
button.IsEnabled = props.ConnectionState == "connected"
12541304
&& !isSending
12551305
&& !slashDisplay.IsLoading
12561306
&& (!string.IsNullOrWhiteSpace(text) || props.PendingAttachments.Count > 0);
1307+
ComposerAutomationVisibility.Prepare(button);
12571308
ToolTipService.SetToolTip(button, actionLabel);
1258-
});
1309+
})
1310+
.OnUnmount(control => ComposerAutomationVisibility.Detach(
1311+
(FrameworkElement)control));
12591312

12601313
var leftToolbar = HStack(8, attachButton, sessionPicker, modelPicker, reasoningPicker)
12611314
.HAlign(HorizontalAlignment.Left)
@@ -1906,3 +1959,66 @@ private static string Localized(string key, string fallback)
19061959
: value;
19071960
}
19081961
}
1962+
1963+
internal static class ComposerAutomationVisibility
1964+
{
1965+
public static void Prepare(FrameworkElement control)
1966+
{
1967+
Detach(control);
1968+
if (HasUsableLayout(control))
1969+
{
1970+
ApplyReadyState(control);
1971+
return;
1972+
}
1973+
1974+
control.IsHitTestVisible = false;
1975+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAccessibilityView(
1976+
control,
1977+
Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Raw);
1978+
control.Loaded += OnLoaded;
1979+
control.SizeChanged += OnSizeChanged;
1980+
}
1981+
1982+
public static void Detach(FrameworkElement control)
1983+
{
1984+
control.Loaded -= OnLoaded;
1985+
control.SizeChanged -= OnSizeChanged;
1986+
}
1987+
1988+
private static void OnLoaded(object sender, RoutedEventArgs args) =>
1989+
TryEnableHitTesting(sender);
1990+
1991+
private static void OnSizeChanged(object sender, SizeChangedEventArgs args) =>
1992+
TryEnableHitTesting(sender);
1993+
1994+
private static void TryEnableHitTesting(object sender)
1995+
{
1996+
if (sender is not FrameworkElement control || !HasUsableLayout(control))
1997+
return;
1998+
1999+
ApplyReadyState(control);
2000+
}
2001+
2002+
private static void ApplyReadyState(FrameworkElement control)
2003+
{
2004+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAccessibilityView(
2005+
control,
2006+
Microsoft.UI.Xaml.Automation.Peers.AccessibilityView.Control);
2007+
control.IsHitTestVisible = true;
2008+
var peer = Microsoft.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer
2009+
.FromElement(control)
2010+
?? Microsoft.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer
2011+
.CreatePeerForElement(control);
2012+
peer?.RaisePropertyChangedEvent(
2013+
Microsoft.UI.Xaml.Automation.AutomationElementIdentifiers.IsOffscreenProperty,
2014+
true,
2015+
false);
2016+
Detach(control);
2017+
}
2018+
2019+
private static bool HasUsableLayout(FrameworkElement control) =>
2020+
control.IsLoaded
2021+
&& control.Visibility == Visibility.Visible
2022+
&& control.ActualWidth > 0
2023+
&& control.ActualHeight > 0;
2024+
}

‎tests/OpenClaw.Tray.Tests/ChatTimelinePresentationTests.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ public void ReactorComposer_OffsetsPickerChevronRightAndUp()
212212
Assert.Contains("textBlock.Margin = new Thickness(2, 4, 0, 0)", root);
213213
}
214214

215+
[Fact]
216+
public void ReactorComposer_GatesClickableControlsUntilLayoutIsUsable()
217+
{
218+
var root = File.ReadAllText(Path.Combine(
219+
TestRepositoryPaths.GetRepositoryRoot(),
220+
"src",
221+
"OpenClaw.Tray.WinUI",
222+
"Chat",
223+
"OpenClawReactorChatRoot.cs"));
224+
225+
Assert.Contains("internal static class ComposerAutomationVisibility", root);
226+
Assert.Contains("control.IsHitTestVisible = false;", root);
227+
Assert.Contains("control.IsLoaded", root);
228+
Assert.Contains("control.ActualWidth > 0", root);
229+
Assert.Contains("control.ActualHeight > 0", root);
230+
Assert.Contains("AccessibilityView.Raw", root);
231+
Assert.Contains("AccessibilityView.Control", root);
232+
Assert.True(
233+
root.Split("AccessibilityView.Raw", StringSplitOptions.None).Length - 1 >= 4);
234+
Assert.Contains(".AutomationId(\"ChatComposerInput\")", root);
235+
Assert.Contains("AutomationProperties.SetAutomationId(", root);
236+
Assert.Contains("RaisePropertyChangedEvent(", root);
237+
Assert.Contains("AutomationElementIdentifiers.IsOffscreenProperty", root);
238+
Assert.Equal(
239+
4,
240+
root.Split(
241+
"ComposerAutomationVisibility.Prepare(",
242+
StringSplitOptions.None).Length - 1);
243+
Assert.Contains("\"ChatComposerAttach\"", root);
244+
Assert.Contains("\"ChatComposerSpeakerToggle\"", root);
245+
Assert.Contains("\"ChatComposerSessionPicker\"", root);
246+
Assert.Contains("\"ChatComposerModelPicker\"", root);
247+
Assert.Contains("\"ChatComposerReasoningPicker\"", root);
248+
Assert.Contains("\"ChatComposerVoice\"", root);
249+
Assert.Contains("\"ChatComposerSettings\"", root);
250+
Assert.Contains("\"ChatComposerPrimaryAction\"", root);
251+
}
252+
215253
[Fact]
216254
public void ReactorComposer_BoundsAndAnnouncesQueuedMessages()
217255
{

‎tests/OpenClaw.Tray.UITests/AccessibilityScanTests.cs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.Threading.Tasks;
4+
using System.Windows.Automation;
35
using Axe.Windows.Core.Enums;
46
using Xunit;
57

@@ -71,4 +73,54 @@ public async Task Page_PassesAccessibilityScan(
7173
exclusions,
7274
context: pageName);
7375
}
76+
77+
[Fact]
78+
[Trait("Category", "Accessibility")]
79+
public async Task ChatComposerControls_ExposeOnscreenLayoutThroughUia()
80+
{
81+
await _app.NavigateAsync("chat", "ChatComposerInput");
82+
var hub = AutomationElement.FromHandle(_app.HubWindowHandle);
83+
foreach (var automationId in new[]
84+
{
85+
"ChatComposerInput",
86+
"ChatComposerAttach",
87+
"ChatComposerSpeakerToggle",
88+
})
89+
{
90+
var element = await WaitForOnscreenLayoutAsync(hub, automationId);
91+
Assert.False(element.Current.IsOffscreen);
92+
Assert.True(element.Current.BoundingRectangle.Width > 0);
93+
Assert.True(element.Current.BoundingRectangle.Height > 0);
94+
}
95+
}
96+
97+
private static async Task<AutomationElement> WaitForOnscreenLayoutAsync(
98+
AutomationElement hub,
99+
string automationId)
100+
{
101+
var timeout = Stopwatch.StartNew();
102+
while (timeout.Elapsed < TimeSpan.FromSeconds(5))
103+
{
104+
var element = hub.FindFirst(
105+
TreeScope.Descendants,
106+
new PropertyCondition(
107+
AutomationElement.AutomationIdProperty,
108+
automationId));
109+
if (element is not null)
110+
{
111+
var bounds = element.Current.BoundingRectangle;
112+
if (!element.Current.IsOffscreen
113+
&& bounds.Width > 0
114+
&& bounds.Height > 0)
115+
{
116+
return element;
117+
}
118+
}
119+
120+
await Task.Delay(50);
121+
}
122+
123+
throw new TimeoutException(
124+
$"Composer control '{automationId}' did not expose onscreen nonzero UIA bounds.");
125+
}
74126
}

0 commit comments

Comments
 (0)