Skip to content

Commit 9c1b926

Browse files
committed
fix(ui): clean up native controls across render paths
1 parent e0da0de commit 9c1b926

2 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎src/OpenClawTray.FunctionalUI/FunctionalUI.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,13 @@ private UIElement ConfigureNativeElement(NativeElement element, string path)
10141014
RemoveRendererControlPath(path);
10151015

10161016
var control = element.GetControl();
1017+
foreach (var (existingPath, existingControl) in _nativeControls
1018+
.Where(pair => !string.Equals(pair.Key, path, StringComparison.Ordinal)
1019+
&& ReferenceEquals(pair.Value, control))
1020+
.ToArray())
1021+
{
1022+
RemoveNativeControlPath(existingPath);
1023+
}
10171024
if (_nativeControls.TryGetValue(path, out var previous) && !ReferenceEquals(previous, control))
10181025
RemoveNativeControlPath(path);
10191026
_nativeControls[path] = control;
@@ -1496,6 +1503,14 @@ private void PruneUnvisitedCachedSubtree(string prefix)
14961503
_contentFlyouts.Remove(path);
14971504
}
14981505

1506+
foreach (var (path, _) in _nativeControls
1507+
.Where(pair => IsPathAtOrBelow(pair.Key, prefix) && !_visitedNativePaths.Contains(pair.Key))
1508+
.OrderByDescending(pair => pair.Key.Length)
1509+
.ToArray())
1510+
{
1511+
RemoveNativeControlPath(path);
1512+
}
1513+
14991514
foreach (var (path, cachedControl) in _controls
15001515
.Where(pair => IsPathAtOrBelow(pair.Key, prefix) && !_visitedControlPaths.Contains(pair.Key))
15011516
.OrderByDescending(pair => pair.Key.Length)
@@ -1526,6 +1541,14 @@ private void RemoveCachedSubtree(string prefix)
15261541
_contentFlyouts.Remove(path);
15271542
}
15281543

1544+
foreach (var (path, _) in _nativeControls
1545+
.Where(pair => IsPathAtOrBelow(pair.Key, prefix))
1546+
.OrderByDescending(pair => pair.Key.Length)
1547+
.ToArray())
1548+
{
1549+
RemoveNativeControlPath(path);
1550+
}
1551+
15291552
foreach (var (path, cachedControl) in _controls
15301553
.Where(pair => IsPathAtOrBelow(pair.Key, prefix))
15311554
.OrderByDescending(pair => pair.Key.Length)

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

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.UI.Xaml;
22
using Microsoft.UI.Xaml.Automation;
33
using Microsoft.UI.Xaml.Controls;
4+
using Microsoft.UI.Xaml.Media;
45
using OpenClawTray.FunctionalUI;
56
using OpenClawTray.FunctionalUI.Hosting;
67
using static OpenClawTray.FunctionalUI.Factories;
@@ -132,6 +133,86 @@ await _ui.RunOnUIAsync(() =>
132133
});
133134
}
134135

136+
[Fact]
137+
public async Task NativeElement_RemainsAttachedWhenItsRenderPathChanges()
138+
{
139+
await _ui.ResetContainerAsync();
140+
141+
FunctionalHostControl? host = null;
142+
TextBlock? nativeText = null;
143+
var showPrefix = false;
144+
145+
await _ui.RunOnUIAsync(() =>
146+
{
147+
nativeText = new TextBlock { Text = "native" };
148+
host = new FunctionalHostControl { SuppressAutoDispose = true };
149+
_ui.Container.Children.Add(host);
150+
host.Mount(_ => showPrefix
151+
? VStack(0, TextBlock("prefix"), Native(() => nativeText!))
152+
: VStack(0, Native(() => nativeText!)));
153+
});
154+
155+
await DrainRenderQueueAsync();
156+
157+
await _ui.RunOnUIAsync(() =>
158+
{
159+
showPrefix = true;
160+
host!.Mount(_ => VStack(0, TextBlock("prefix"), Native(() => nativeText!)));
161+
});
162+
163+
await DrainRenderQueueAsync();
164+
165+
await _ui.RunOnUIAsync(() =>
166+
{
167+
var wrapper = Assert.IsType<Border>(host!.Content);
168+
var panel = Assert.IsType<StackPanel>(wrapper.Child);
169+
Assert.Equal(2, panel.Children.Count);
170+
Assert.Same(nativeText, panel.Children[1]);
171+
host.Dispose();
172+
});
173+
}
174+
175+
[Fact]
176+
public async Task VirtualStack_RecycleDetachesNativeElement()
177+
{
178+
await _ui.ResetContainerAsync();
179+
180+
FunctionalHostControl? host = null;
181+
TextBlock? nativeText = null;
182+
183+
await _ui.RunOnUIAsync(() =>
184+
{
185+
nativeText = new TextBlock { Text = "native" };
186+
host = new FunctionalHostControl
187+
{
188+
Width = 400,
189+
Height = 300,
190+
SuppressAutoDispose = true,
191+
};
192+
_ui.Container.Children.Add(host);
193+
host.Mount(_ => VirtualVStack(0, Native(() => nativeText!)));
194+
});
195+
196+
await DrainRenderQueueAsync();
197+
198+
await _ui.RunOnUIAsync(() =>
199+
{
200+
var wrapper = Assert.IsType<Border>(host!.Content);
201+
var repeater = Assert.IsType<ItemsRepeater>(wrapper.Child);
202+
var container = Assert.IsType<Border>(repeater.TryGetElement(0));
203+
Assert.Same(nativeText, container.Child);
204+
host.Mount(_ => VirtualVStack(0));
205+
});
206+
207+
await DrainRenderQueueAsync();
208+
209+
await _ui.RunOnUIAsync(() =>
210+
{
211+
Assert.Null(VisualTreeHelper.GetParent(nativeText));
212+
host!.Dispose();
213+
});
214+
}
215+
135216
[Fact]
136217
public async Task NativeElement_OnMountRunsAcrossSamePathOwnershipChanges()
137218
{
@@ -203,20 +284,23 @@ public async Task NativeElement_OnMountRunsWhenStablePathGetsNewNativeInstance()
203284
await _ui.ResetContainerAsync();
204285

205286
FunctionalHostControl? host = null;
206-
var firstNative = new TextBlock { Text = "first" };
207-
var secondNative = new TextBlock { Text = "second" };
208-
var currentNative = firstNative;
287+
TextBlock? firstNative = null;
288+
TextBlock? secondNative = null;
289+
TextBlock? currentNative = null;
209290
var mountCount = 0;
210291

211292
await _ui.RunOnUIAsync(() =>
212293
{
213294
TestApp.EnsureFluentBrushFallbacks(Application.Current.Resources);
295+
firstNative = new TextBlock { Text = "first" };
296+
secondNative = new TextBlock { Text = "second" };
297+
currentNative = firstNative;
214298
host = new FunctionalHostControl
215299
{
216300
SuppressAutoDispose = true,
217301
};
218302
_ui.Container.Children.Add(host);
219-
host.Mount(_ => Border(Native(() => currentNative).OnMount(_ => mountCount++)));
303+
host.Mount(_ => Border(Native(() => currentNative!).OnMount(_ => mountCount++)));
220304
});
221305

222306
await DrainRenderQueueAsync();
@@ -225,7 +309,7 @@ await _ui.RunOnUIAsync(() =>
225309
{
226310
Assert.Equal(1, mountCount);
227311
currentNative = secondNative;
228-
host!.Mount(_ => Border(Native(() => currentNative).OnMount(_ => mountCount++)));
312+
host!.Mount(_ => Border(Native(() => currentNative!).OnMount(_ => mountCount++)));
229313
});
230314

231315
await DrainRenderQueueAsync();

0 commit comments

Comments
 (0)