Skip to content

Commit 47830b3

Browse files
committed
fix(tooltip): Keep the popup panel as parent while a ToolTip is open
Port of unoplatform#24592 (closes unoplatform#24557, unoplatform#24820) to servicing/6.8. Differences from the master version: - The owner DataContextChanged subscription is split into its own Subscribe/UnsubscribeOwnerDataContextChanged pair because 6.8 still gates the ActualThemeChanged subscription behind UNO_HAS_ENHANCED_LIFECYCLE; the DataContext forwarding is needed on every platform since the popup-panel parenting is unconditional. - The tests live in Tests/Windows_UI_Xaml_Controls/Given_ToolTip.cs (master moved the file to Microsoft/UI/Xaml/Controls).
1 parent 06d268c commit 47830b3

3 files changed

Lines changed: 287 additions & 11 deletions

File tree

‎src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_ToolTip.cs‎

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Uno.UI.RuntimeTests.Helpers;
77
using Microsoft.UI.Xaml.Controls;
88
using Microsoft.UI.Xaml.Media;
9+
using Microsoft.UI.Xaml.Media.Imaging;
910
using Microsoft.UI.Xaml.Controls.Primitives;
1011
using Microsoft.UI.Xaml.Data;
1112
using Microsoft.UI.Xaml;
@@ -643,6 +644,221 @@ public async Task When_ToolTip_Label_Uses_Owner_Subtree_Theme_Light_Under_Dark_A
643644
tooltip.IsOpen = false;
644645
#if HAS_UNO
645646
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
647+
#endif
648+
}
649+
}
650+
651+
[TestMethod]
652+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/24557")]
653+
public async Task When_Image_Source_Loads_While_Open()
654+
{
655+
var owner = new Button { Content = "owner" };
656+
var image = new Image { Width = 180, Height = 120, Stretch = Stretch.Uniform };
657+
var SUT = new ToolTip { Content = image };
658+
ToolTipService.SetToolTip(owner, SUT);
659+
660+
try
661+
{
662+
TestServices.WindowHelper.WindowContent = owner;
663+
await TestServices.WindowHelper.WaitForLoaded(owner);
664+
665+
SUT.IsOpen = true;
666+
await TestServices.WindowHelper.WaitForLoaded(image);
667+
668+
// The image gets its source only once the tooltip is showing, so the bitmap data arrives
669+
// while the image sits inside an already-laid-out popup.
670+
var opened = new TaskCompletionSource<bool>();
671+
image.ImageOpened += (_, _) => opened.TrySetResult(true);
672+
image.ImageFailed += (_, _) => opened.TrySetResult(false);
673+
image.Source = new BitmapImage(new Uri("ms-appx:///Assets/my500x200.jpg"));
674+
675+
await Task.WhenAny(opened.Task, Task.Delay(3000));
676+
677+
Assert.IsTrue(opened.Task.IsCompleted, "The image did not open while the tooltip was showing.");
678+
Assert.IsTrue(await opened.Task, "The image failed to load.");
679+
680+
await TestServices.WindowHelper.WaitForIdle();
681+
Assert.IsTrue(image.ActualWidth > 0 && image.ActualHeight > 0, "The image was not laid out with its bitmap.");
682+
683+
// Closing and reopening puts the tooltip back into the popup; a source assigned afterwards
684+
// must still get through.
685+
SUT.IsOpen = false;
686+
await TestServices.WindowHelper.WaitForIdle();
687+
SUT.IsOpen = true;
688+
await TestServices.WindowHelper.WaitForIdle();
689+
690+
var reopened = new TaskCompletionSource<bool>();
691+
image.ImageOpened += (_, _) => reopened.TrySetResult(true);
692+
image.ImageFailed += (_, _) => reopened.TrySetResult(false);
693+
image.Source = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png"));
694+
695+
await Task.WhenAny(reopened.Task, Task.Delay(3000));
696+
697+
Assert.IsTrue(reopened.Task.IsCompleted, "The image did not open while the reopened tooltip was showing.");
698+
Assert.IsTrue(await reopened.Task, "The image failed to load after reopening.");
699+
}
700+
finally
701+
{
702+
SUT.IsOpen = false;
703+
#if HAS_UNO
704+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
705+
#endif
706+
}
707+
}
708+
709+
[TestMethod]
710+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/24820")]
711+
public async Task When_Content_Realizes_On_Load_Then_Measured_On_First_Open()
712+
{
713+
// An ItemsControl only creates its containers once loaded, so it invalidates its measure
714+
// from inside the tooltip that has just opened.
715+
var items = new ItemsControl { ItemsSource = new[] { "first error", "second error" } };
716+
var owner = new Button { Content = "owner" };
717+
var SUT = new ToolTip { Content = items };
718+
ToolTipService.SetToolTip(owner, SUT);
719+
720+
try
721+
{
722+
TestServices.WindowHelper.WindowContent = owner;
723+
await TestServices.WindowHelper.WaitForLoaded(owner);
724+
725+
SUT.IsOpen = true;
726+
727+
await TestServices.WindowHelper.WaitFor(
728+
() => items.ActualWidth > 0 && items.ActualHeight > 0,
729+
timeoutMS: 3000,
730+
message: "The tooltip content was never measured on its first open.");
731+
}
732+
finally
733+
{
734+
SUT.IsOpen = false;
735+
#if HAS_UNO
736+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
737+
#endif
738+
}
739+
}
740+
741+
[TestMethod]
742+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/24557")]
743+
public async Task When_Closed_Then_DataContext_Kept()
744+
{
745+
var owner = new Button { Content = "owner" };
746+
var content = new TextBlock();
747+
content.SetBinding(TextBlock.TextProperty, new Binding());
748+
var SUT = new ToolTip { Content = content };
749+
ToolTipService.SetToolTip(owner, SUT);
750+
751+
try
752+
{
753+
TestServices.WindowHelper.WindowContent = owner;
754+
await TestServices.WindowHelper.WaitForLoaded(owner);
755+
owner.DataContext = "VM1";
756+
757+
SUT.IsOpen = true;
758+
await TestServices.WindowHelper.WaitForIdle();
759+
Assert.AreEqual("VM1", SUT.DataContext);
760+
Assert.AreEqual("VM1", content.Text);
761+
762+
SUT.IsOpen = false;
763+
await TestServices.WindowHelper.WaitForIdle();
764+
Assert.AreEqual("VM1", SUT.DataContext, "The DataContext was lost on close.");
765+
Assert.AreEqual("VM1", content.Text);
766+
767+
owner.DataContext = "VM2";
768+
await TestServices.WindowHelper.WaitForIdle();
769+
Assert.AreEqual("VM2", SUT.DataContext);
770+
771+
SUT.IsOpen = true;
772+
await TestServices.WindowHelper.WaitForIdle();
773+
owner.DataContext = "VM3";
774+
await TestServices.WindowHelper.WaitForIdle();
775+
Assert.AreEqual("VM3", SUT.DataContext);
776+
777+
SUT.IsOpen = false;
778+
await TestServices.WindowHelper.WaitForIdle();
779+
Assert.AreEqual("VM3", SUT.DataContext, "The DataContext was lost on the second close.");
780+
Assert.AreEqual("VM3", content.Text);
781+
}
782+
finally
783+
{
784+
SUT.IsOpen = false;
785+
#if HAS_UNO
786+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
787+
#endif
788+
}
789+
}
790+
791+
[TestMethod]
792+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/24557")]
793+
public async Task When_PlacementTarget_Then_DataContext_From_Owner()
794+
{
795+
var owner = new Button { Content = "owner", DataContext = "Owner" };
796+
var target = new Button { Content = "target", DataContext = "Target" };
797+
var SUT = new ToolTip { Content = "tooltip" };
798+
ToolTipService.SetPlacementTarget(owner, target);
799+
ToolTipService.SetToolTip(owner, SUT);
800+
801+
try
802+
{
803+
TestServices.WindowHelper.WindowContent = new StackPanel { Children = { owner, target } };
804+
await TestServices.WindowHelper.WaitForLoaded(owner);
805+
806+
SUT.IsOpen = true;
807+
await TestServices.WindowHelper.WaitForIdle();
808+
Assert.AreEqual("Owner", SUT.DataContext);
809+
810+
owner.DataContext = "Owner2";
811+
await TestServices.WindowHelper.WaitForIdle();
812+
Assert.AreEqual("Owner2", SUT.DataContext);
813+
814+
// The placement target only positions the tooltip.
815+
target.DataContext = "Target2";
816+
await TestServices.WindowHelper.WaitForIdle();
817+
Assert.AreEqual("Owner2", SUT.DataContext);
818+
819+
SUT.IsOpen = false;
820+
await TestServices.WindowHelper.WaitForIdle();
821+
Assert.AreEqual("Owner2", SUT.DataContext);
822+
}
823+
finally
824+
{
825+
SUT.IsOpen = false;
826+
#if HAS_UNO
827+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
828+
#endif
829+
}
830+
}
831+
832+
[TestMethod]
833+
public async Task When_Closed_Then_Parent_Cleared()
834+
{
835+
var owner = new Button { Content = "owner" };
836+
var SUT = new ToolTip { Content = "tooltip" };
837+
ToolTipService.SetToolTip(owner, SUT);
838+
839+
try
840+
{
841+
TestServices.WindowHelper.WindowContent = owner;
842+
await TestServices.WindowHelper.WaitForLoaded(owner);
843+
844+
for (var i = 0; i < 2; i++)
845+
{
846+
SUT.IsOpen = true;
847+
await TestServices.WindowHelper.WaitForIdle();
848+
Assert.IsInstanceOfType(SUT.Parent, typeof(Popup));
849+
Assert.IsNotNull(VisualTreeHelper.GetParent(SUT));
850+
851+
SUT.IsOpen = false;
852+
await TestServices.WindowHelper.WaitForIdle();
853+
Assert.IsNull(SUT.Parent);
854+
Assert.IsNull(VisualTreeHelper.GetParent(SUT));
855+
}
856+
}
857+
finally
858+
{
859+
SUT.IsOpen = false;
860+
#if HAS_UNO
861+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
646862
#endif
647863
}
648864
}

‎src/Uno.UI/UI/Xaml/Controls/ToolTip/ToolTip.cs‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ protected override AutomationPeer OnCreateAutomationPeer()
3737

3838
private DependencyObject? _owner;
3939

40+
// The element the tooltip is set on. _owner is the placement target when there is one, which
41+
// only positions the tooltip; the DataContext comes from the container, as in WinUI.
42+
private FrameworkElement? _container;
43+
4044
internal Popup Popup
4145
{
4246
get
@@ -152,50 +156,105 @@ private void OnOpenChanged(bool isOpen)
152156
#if UNO_HAS_ENHANCED_LIFECYCLE
153157
SubscribeOwnerThemeChanged();
154158
#endif
159+
SubscribeOwnerDataContextChanged();
155160

156161
Opened?.Invoke(this, new RoutedEventArgs(this));
157162
GoToElementState("Opened", useTransitions: true);
158163

159-
if (_owner is FrameworkElement fe)
164+
if (GetDataContextOwner() is { } dataContextOwner)
160165
{
161166
// Propagate the DC once, the inheritance
162167
// will update the rest on DC changes.
163-
this.SetValue(DataContextProperty, fe.DataContext, DependencyPropertyValuePrecedences.Inheritance);
168+
this.SetValue(DataContextProperty, dataContextOwner.DataContext, DependencyPropertyValuePrecedences.Inheritance);
164169
}
165170
}
166171
else
167172
{
173+
DetachFromPopup();
168174
#if UNO_HAS_ENHANCED_LIFECYCLE
169175
UnsubscribeOwnerThemeChanged();
170176
#endif
177+
UnsubscribeOwnerDataContextChanged();
171178
Closed?.Invoke(this, new RoutedEventArgs(this));
172179
GoToElementState("Closed", useTransitions: true);
173180
}
174181
}
175182

176183
private void AttachToPopup()
177184
{
178-
if (Parent == null)
185+
// The public Parent reports the popup once Popup.Child has been set, so the actual parent is
186+
// what tells whether the tooltip is free to be shown by the popup.
187+
var actualParent = this.GetParent();
188+
189+
if (actualParent is null || ReferenceEquals(actualParent, Popup.PopupPanel))
179190
{
180-
var previousParent = this.GetParent();
191+
// Captured before any parenting: joining the popup panel inherits the panel's own
192+
// DataContext, and the one the owner provides through ToolTipService.ToolTip must win.
193+
var ownerDataContext = DataContext;
181194

182-
Popup.Child = this;
195+
if (!ReferenceEquals(Popup.Child, this))
196+
{
197+
Popup.Child = this;
198+
}
183199

184-
// The tooltip is integrated into the PopupPanel, which automatically
185-
// propagates its own datacontext through inheritance. This ends up changing
186-
// the DataContext of the tooltip to a local value which cannot be overriden
187-
// through the tooltip's own actual parent (the control that owns the TooltipService).
188-
// Restoring the original parent ensures that an incorrect DataContext property will not flow through.
189-
this.SetParent(previousParent);
200+
// Layout invalidations raised by the tooltip content (an image whose bitmap arrives after the
201+
// tooltip opened, for instance) reach the layout root through the parent chain, so the popup
202+
// panel has to stay the parent while the tooltip is showing.
203+
this.SetParent(Popup.PopupPanel);
204+
this.SetValue(DataContextProperty, ownerDataContext, DependencyPropertyValuePrecedences.Inheritance);
190205
}
191206
else if (!ReferenceEquals(Parent, _popup))
192207
{
193208
this.Log().Warn($"This ToolTip is already in visual tree: won't be able to use it with TooltipService.");
194209
}
195210
}
196211

212+
private void DetachFromPopup()
213+
{
214+
// Leave the popup panel's inheritance so the tooltip is a free-standing value of the
215+
// owner's ToolTipService.ToolTip property again, as it was before it opened.
216+
if (_popup is not null && ReferenceEquals(this.GetParent(), _popup.PopupPanel))
217+
{
218+
// Leaving the parent drops the inherited DataContext, and the owner only pushes its own
219+
// again when it changes: keep it, so the closed tooltip's bindings stay resolved.
220+
var dataContext = DataContext;
221+
222+
// As in WinUI's ToolTip::Close, the popup lets go of the tooltip, so Parent is null while closed.
223+
_popup.Child = null;
224+
this.SetParent(null);
225+
this.SetValue(DataContextProperty, dataContext, DependencyPropertyValuePrecedences.Inheritance);
226+
}
227+
}
228+
197229
public void SetAnchor(UIElement element) => _owner = element;
198230

231+
internal void SetContainer(FrameworkElement container) => _container = container;
232+
233+
private FrameworkElement? GetDataContextOwner() => _container ?? GetOwnerFrameworkElement();
234+
235+
private void SubscribeOwnerDataContextChanged()
236+
{
237+
if (GetDataContextOwner() is { } dataContextOwner)
238+
{
239+
dataContextOwner.DataContextChanged += OnOwnerDataContextChanged;
240+
}
241+
}
242+
243+
private void UnsubscribeOwnerDataContextChanged()
244+
{
245+
if (GetDataContextOwner() is { } dataContextOwner)
246+
{
247+
dataContextOwner.DataContextChanged -= OnOwnerDataContextChanged;
248+
}
249+
}
250+
251+
private void OnOwnerDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
252+
{
253+
// While showing, the tooltip is parented to the popup panel, so the owner's DataContext no
254+
// longer reaches it through ToolTipService.ToolTip; forward the changes explicitly.
255+
this.SetValue(DataContextProperty, args.NewValue, DependencyPropertyValuePrecedences.Inheritance);
256+
}
257+
199258
#if UNO_HAS_ENHANCED_LIFECYCLE
200259
private void SubscribeOwnerThemeChanged()
201260
{

‎src/Uno.UI/UI/Xaml/Controls/ToolTip/ToolTipService.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private static void RegisterToolTip(
4949

5050
toolTip.Placement = GetPlacement(toolTip);
5151
toolTip.SetAnchor(GetPlacementTarget(container) ?? container);
52+
toolTip.SetContainer(container);
5253

5354
if (isKeyboardAcceleratorToolTip)
5455
{

0 commit comments

Comments
 (0)