Skip to content

Commit 4133ae1

Browse files
kenehongCopilot
andauthored
Fix tray menu toggle flicker
* Fix tray menu toggle flicker Add a tray-menu-specific ToggleSwitch style that preserves the WinUI control shape while removing the default switch reposition animation that flickers on menu open. Wire the tray connection and permissions toggles through the shared helper so their state updates remain centralized. Co-authored-by: Copilot <[email protected]> * Fix tray menu toggle style lookup Use the tray menu window instance to resolve the local ToggleSwitch style after scoping it to TrayMenuWindow resources. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: kenehong <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 16399ed commit 4133ae1

4 files changed

Lines changed: 175 additions & 30 deletions

File tree

‎src/OpenClaw.Tray.WinUI/App.xaml.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,10 +2437,7 @@ private void SyncConnectionToggle(ConnectionStatus status)
24372437
_suspendConnectionToggleEvent = true;
24382438
try
24392439
{
2440-
if (toggle.IsOn != shouldBeOn)
2441-
toggle.IsOn = shouldBeOn;
2442-
2443-
toggle.IsEnabled = canToggle;
2440+
TrayMenuWindow.SetMenuToggleSwitchState(toggle, shouldBeOn, canToggle);
24442441
ToolTipService.SetToolTip(toggle,
24452442
shouldBeOn ? "Connected - toggle off to disconnect"
24462443
: status == ConnectionStatus.Connecting ? "Connecting..."

‎src/OpenClaw.Tray.WinUI/Services/TrayMenuStateBuilder.cs‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,11 @@ internal void Build(TrayMenuWindow menu)
9494
Grid.SetColumn(brandRow, 0);
9595
brandGrid.Children.Add(brandRow);
9696

97-
var connectionToggle = new ToggleSwitch
98-
{
99-
IsOn = isConnected,
100-
OnContent = string.Empty,
101-
OffContent = string.Empty,
102-
VerticalAlignment = VerticalAlignment.Center,
103-
MinWidth = 0,
104-
Width = 40,
105-
HorizontalAlignment = HorizontalAlignment.Right,
106-
Margin = new Thickness(0),
107-
IsEnabled = _snapshot.CurrentStatus == ConnectionStatus.Connected
108-
|| _snapshot.CurrentStatus == ConnectionStatus.Disconnected
109-
|| _snapshot.CurrentStatus == ConnectionStatus.Error
110-
};
111-
AutomationProperties.SetName(connectionToggle, "Gateway connection");
97+
var canToggleConnection = _snapshot.CurrentStatus == ConnectionStatus.Connected
98+
|| _snapshot.CurrentStatus == ConnectionStatus.Disconnected
99+
|| _snapshot.CurrentStatus == ConnectionStatus.Error;
100+
var connectionToggle = menu.CreateMenuToggleSwitch(isConnected, "Gateway connection", canToggleConnection);
101+
connectionToggle.Margin = new Thickness(0);
112102
ToolTipService.SetToolTip(connectionToggle,
113103
isConnected ? "Connected - toggle off to disconnect" : "Disconnected - toggle on to connect");
114104
connectionToggle.Toggled += (s, ev) =>

‎src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
x:Class="OpenClawTray.Windows.TrayMenuWindow"
55
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:primitives="using:Microsoft.UI.Xaml.Controls.Primitives"
78
xmlns:winex="using:WinUIEx"
89
Title="OpenClaw Menu"
910
Width="320"
@@ -12,6 +13,147 @@
1213
<!-- Acrylic backdrop applied in code-behind via BackdropHelper -->
1314

1415
<Grid x:Name="RootGrid" Background="Transparent">
16+
<Grid.Resources>
17+
<Style x:Key="TrayMenuInstantToggleSwitchStyle" TargetType="ToggleSwitch">
18+
<Setter Property="Foreground" Value="{ThemeResource ToggleSwitchContentForeground}" />
19+
<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
20+
<Setter Property="FocusVisualMargin" Value="-7,-3,-7,-3" />
21+
<Setter Property="CornerRadius" Value="{StaticResource ControlCornerRadius}" />
22+
<Setter Property="Template">
23+
<Setter.Value>
24+
<ControlTemplate TargetType="ToggleSwitch">
25+
<Grid Background="{TemplateBinding Background}"
26+
BorderBrush="{TemplateBinding BorderBrush}"
27+
BorderThickness="{TemplateBinding BorderThickness}"
28+
CornerRadius="{TemplateBinding CornerRadius}">
29+
<VisualStateManager.VisualStateGroups>
30+
<VisualStateGroup x:Name="CommonStates">
31+
<VisualState x:Name="Normal">
32+
<VisualState.Setters>
33+
<Setter Target="OuterBorder.Fill" Value="{ThemeResource ToggleSwitchFillOff}" />
34+
<Setter Target="OuterBorder.Stroke" Value="{ThemeResource ToggleSwitchStrokeOff}" />
35+
<Setter Target="SwitchKnobBounds.Fill" Value="{ThemeResource ToggleSwitchFillOn}" />
36+
<Setter Target="SwitchKnobBounds.Stroke" Value="{ThemeResource ToggleSwitchStrokeOn}" />
37+
<Setter Target="SwitchKnobOn.Background" Value="{ThemeResource ToggleSwitchKnobFillOn}" />
38+
<Setter Target="SwitchKnobOff.Fill" Value="{ThemeResource ToggleSwitchKnobFillOff}" />
39+
</VisualState.Setters>
40+
</VisualState>
41+
<VisualState x:Name="PointerOver">
42+
<VisualState.Setters>
43+
<Setter Target="OuterBorder.Fill" Value="{ThemeResource ToggleSwitchFillOffPointerOver}" />
44+
<Setter Target="OuterBorder.Stroke" Value="{ThemeResource ToggleSwitchStrokeOffPointerOver}" />
45+
<Setter Target="SwitchKnobBounds.Fill" Value="{ThemeResource ToggleSwitchFillOnPointerOver}" />
46+
<Setter Target="SwitchKnobBounds.Stroke" Value="{ThemeResource ToggleSwitchStrokeOnPointerOver}" />
47+
<Setter Target="SwitchKnobOn.Background" Value="{ThemeResource ToggleSwitchKnobFillOnPointerOver}" />
48+
<Setter Target="SwitchKnobOff.Fill" Value="{ThemeResource ToggleSwitchKnobFillOffPointerOver}" />
49+
</VisualState.Setters>
50+
</VisualState>
51+
<VisualState x:Name="Pressed">
52+
<VisualState.Setters>
53+
<Setter Target="OuterBorder.Fill" Value="{ThemeResource ToggleSwitchFillOffPressed}" />
54+
<Setter Target="OuterBorder.Stroke" Value="{ThemeResource ToggleSwitchStrokeOffPressed}" />
55+
<Setter Target="SwitchKnobBounds.Fill" Value="{ThemeResource ToggleSwitchFillOnPressed}" />
56+
<Setter Target="SwitchKnobBounds.Stroke" Value="{ThemeResource ToggleSwitchStrokeOnPressed}" />
57+
<Setter Target="SwitchKnobOn.Background" Value="{ThemeResource ToggleSwitchKnobFillOnPressed}" />
58+
<Setter Target="SwitchKnobOff.Fill" Value="{ThemeResource ToggleSwitchKnobFillOffPressed}" />
59+
</VisualState.Setters>
60+
</VisualState>
61+
<VisualState x:Name="Disabled">
62+
<VisualState.Setters>
63+
<Setter Target="OuterBorder.Fill" Value="{ThemeResource ToggleSwitchFillOffDisabled}" />
64+
<Setter Target="OuterBorder.Stroke" Value="{ThemeResource ToggleSwitchStrokeOffDisabled}" />
65+
<Setter Target="SwitchKnobBounds.Fill" Value="{ThemeResource ToggleSwitchFillOnDisabled}" />
66+
<Setter Target="SwitchKnobBounds.Stroke" Value="{ThemeResource ToggleSwitchStrokeOnDisabled}" />
67+
<Setter Target="SwitchKnobOn.Background" Value="{ThemeResource ToggleSwitchKnobFillOnDisabled}" />
68+
<Setter Target="SwitchKnobOff.Fill" Value="{ThemeResource ToggleSwitchKnobFillOffDisabled}" />
69+
</VisualState.Setters>
70+
</VisualState>
71+
</VisualStateGroup>
72+
73+
<VisualStateGroup x:Name="ToggleStates">
74+
<VisualState x:Name="Off">
75+
<VisualState.Setters>
76+
<Setter Target="KnobTranslateTransform.X" Value="0" />
77+
<Setter Target="OuterBorder.Opacity" Value="1" />
78+
<Setter Target="SwitchKnobBounds.Opacity" Value="0" />
79+
<Setter Target="SwitchKnobOn.Opacity" Value="0" />
80+
<Setter Target="SwitchKnobOff.Opacity" Value="1" />
81+
</VisualState.Setters>
82+
</VisualState>
83+
<VisualState x:Name="On">
84+
<VisualState.Setters>
85+
<Setter Target="KnobTranslateTransform.X" Value="20" />
86+
<Setter Target="OuterBorder.Opacity" Value="0" />
87+
<Setter Target="SwitchKnobBounds.Opacity" Value="1" />
88+
<Setter Target="SwitchKnobOn.Opacity" Value="1" />
89+
<Setter Target="SwitchKnobOff.Opacity" Value="0" />
90+
</VisualState.Setters>
91+
</VisualState>
92+
</VisualStateGroup>
93+
</VisualStateManager.VisualStateGroups>
94+
95+
<Grid Width="40"
96+
Height="20"
97+
Control.IsTemplateFocusTarget="True">
98+
<Rectangle x:Name="OuterBorder"
99+
Width="40"
100+
Height="20"
101+
RadiusX="10"
102+
RadiusY="10"
103+
Fill="{ThemeResource ToggleSwitchFillOff}"
104+
Stroke="{ThemeResource ToggleSwitchStrokeOff}"
105+
StrokeThickness="{ThemeResource ToggleSwitchOuterBorderStrokeThickness}" />
106+
<Rectangle x:Name="SwitchKnobBounds"
107+
Width="40"
108+
Height="20"
109+
RadiusX="10"
110+
RadiusY="10"
111+
Fill="{ThemeResource ToggleSwitchFillOn}"
112+
Stroke="{ThemeResource ToggleSwitchStrokeOn}"
113+
StrokeThickness="{ThemeResource ToggleSwitchOnStrokeThickness}"
114+
Opacity="0" />
115+
<Grid x:Name="SwitchKnob"
116+
Width="20"
117+
Height="20"
118+
HorizontalAlignment="Left">
119+
<Border x:Name="SwitchKnobOn"
120+
Width="12"
121+
Height="12"
122+
Margin="0,0,1,0"
123+
HorizontalAlignment="Center"
124+
Background="{ThemeResource ToggleSwitchKnobFillOn}"
125+
BorderBrush="{ThemeResource ToggleSwitchKnobStrokeOn}"
126+
BackgroundSizing="OuterBorderEdge"
127+
CornerRadius="7"
128+
Opacity="0" />
129+
<Rectangle x:Name="SwitchKnobOff"
130+
Width="12"
131+
Height="12"
132+
Margin="-1,0,0,0"
133+
HorizontalAlignment="Center"
134+
Fill="{ThemeResource ToggleSwitchKnobFillOff}"
135+
RadiusX="7"
136+
RadiusY="7" />
137+
<Grid.RenderTransform>
138+
<TranslateTransform x:Name="KnobTranslateTransform" />
139+
</Grid.RenderTransform>
140+
</Grid>
141+
<primitives:Thumb x:Name="SwitchThumb"
142+
AutomationProperties.AccessibilityView="Raw">
143+
<primitives:Thumb.Template>
144+
<ControlTemplate TargetType="primitives:Thumb">
145+
<Rectangle Fill="Transparent" />
146+
</ControlTemplate>
147+
</primitives:Thumb.Template>
148+
</primitives:Thumb>
149+
</Grid>
150+
</Grid>
151+
</ControlTemplate>
152+
</Setter.Value>
153+
</Setter>
154+
</Style>
155+
</Grid.Resources>
156+
15157
<Border Background="Transparent"
16158
BorderBrush="{ThemeResource SurfaceStrokeColorDefaultBrush}"
17159
BorderThickness="1"

‎src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@ private enum MonitorDpiType
147147
private Brush SecondaryTextBrush =>
148148
_secondaryTextBrush ??= (Brush)Application.Current.Resources["TextFillColorSecondaryBrush"];
149149

150+
internal ToggleSwitch CreateMenuToggleSwitch(bool isOn, string automationName, bool isEnabled = true)
151+
{
152+
var toggle = new ToggleSwitch
153+
{
154+
IsOn = isOn,
155+
OnContent = string.Empty,
156+
OffContent = string.Empty,
157+
MinWidth = 0,
158+
Width = 40,
159+
HorizontalAlignment = HorizontalAlignment.Right,
160+
VerticalAlignment = VerticalAlignment.Center,
161+
IsEnabled = isEnabled,
162+
Style = (Style)RootGrid.Resources["TrayMenuInstantToggleSwitchStyle"]
163+
};
164+
AutomationProperties.SetName(toggle, automationName);
165+
return toggle;
166+
}
167+
168+
internal static void SetMenuToggleSwitchState(ToggleSwitch toggle, bool isOn, bool isEnabled)
169+
{
170+
toggle.IsEnabled = isEnabled;
171+
if (toggle.IsOn != isOn)
172+
toggle.IsOn = isOn;
173+
}
174+
150175
public TrayMenuWindow() : this(ownerMenu: null)
151176
{
152177
}
@@ -378,7 +403,6 @@ public void AddMenuItem(string text, IconElement? icon, string action, bool isEn
378403
{
379404
button.Background = s_transparentBrush;
380405
};
381-
382406
MenuPanel.Children.Add(button);
383407
_itemCount++;
384408
}
@@ -598,15 +622,7 @@ public void AddToggleItem(string title, string? icon, string? description, bool
598622
AutomationProperties.SetAccessibilityView(stateLabel, AccessibilityView.Raw);
599623
trailing.Children.Add(stateLabel);
600624

601-
var toggle = new ToggleSwitch
602-
{
603-
IsOn = isOn,
604-
OnContent = null,
605-
OffContent = null,
606-
MinWidth = 0,
607-
VerticalAlignment = VerticalAlignment.Center
608-
};
609-
AutomationProperties.SetName(toggle, title);
625+
var toggle = CreateMenuToggleSwitch(isOn, title);
610626
toggle.Toggled += (s, e) =>
611627
{
612628
stateLabel.Text = toggle.IsOn ? "On" : "Off";
@@ -1312,7 +1328,7 @@ public TrayMenuFlyoutItem(string text, string? icon = null, string? action = nul
13121328
public bool IsToggle { get; set; }
13131329
public bool IsOn { get; set; }
13141330

1315-
// Optional secondary description text shown below the title (for ToggleSwitch rows).
1331+
// Optional secondary description text shown below the title (for toggle rows).
13161332
public string? Description { get; set; }
13171333

13181334
// Free-form content: when set, the renderer drops the row in via

0 commit comments

Comments
 (0)