11using Microsoft . UI . Xaml ;
22using Microsoft . UI . Xaml . Automation ;
33using Microsoft . UI . Xaml . Controls ;
4+ using Microsoft . UI . Xaml . Media ;
45using OpenClawTray . FunctionalUI ;
56using OpenClawTray . FunctionalUI . Hosting ;
67using 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