Skip to content

Commit 3cfe072

Browse files
committed
fix(tooltip): Let go of a ToolTip in the service when it closes, however it closed (port)
Port of 9d414ec (stacked on unoplatform#24592). As in WinUI's ToolTip::OnIsOpenChanged, the ToolTip tells the service when it has closed, and the service drops it if it is still the current one, so a tooltip closed by a path the service does not drive (its popup closed with the other popups, or the app setting IsOpen) is not kept. 6.8 differences: the call sits after UnsubscribeOwnerDataContextChanged in ToolTip.OnOpenChanged (6.8's shape of unoplatform#24592), and the tests live in Tests/Windows_UI_Xaml_Controls. This branch also carries the unoplatform#25102 port, so the owner is already collected once it unloads; the service-state test is the one that fails without this change here.
1 parent b522bd8 commit 3cfe072

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,5 +912,85 @@ public async Task When_Closed_Then_Parent_Cleared()
912912
#endif
913913
}
914914
}
915+
916+
#if HAS_UNO
917+
#if !HAS_INPUT_INJECTOR
918+
[Ignore("InputInjector is not supported on this platform.")]
919+
#endif
920+
[TestMethod]
921+
public async Task When_Popup_Closed_Externally_Then_Service_Releases_ToolTip()
922+
{
923+
// A tooltip closed by a path the service does not drive (here its popup, closed with every other popup) must not
924+
// stay the service's current tooltip: WinUI lets go of it in the tooltip's own close (ToolTip::OnIsOpenChanged).
925+
var root = new ContentControl();
926+
TestServices.WindowHelper.WindowContent = root;
927+
await TestServices.WindowHelper.WaitForIdle();
928+
929+
var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
930+
using var mouse = injector.GetMouse();
931+
932+
try
933+
{
934+
await HoverToolTipOwnerThenClosePopup(root, mouse);
935+
936+
Assert.IsNull(ToolTipService.CurrentToolTipForTesting, "The service must not keep a tooltip that has closed.");
937+
}
938+
finally
939+
{
940+
mouse.MoveTo(new Windows.Foundation.Point(0, 0));
941+
TestServices.WindowHelper.WindowContent = null;
942+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
943+
}
944+
}
945+
946+
#if !HAS_INPUT_INJECTOR
947+
[Ignore("InputInjector is not supported on this platform.")]
948+
#endif
949+
[TestMethod]
950+
public async Task When_Popup_Closed_Externally_Then_Owner_Collected()
951+
{
952+
// Once its tooltip has closed, however it closed, the owner is not kept alive through the service.
953+
var root = new ContentControl();
954+
TestServices.WindowHelper.WindowContent = root;
955+
await TestServices.WindowHelper.WaitForIdle();
956+
957+
var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
958+
using var mouse = injector.GetMouse();
959+
960+
try
961+
{
962+
var ownerRef = await HoverToolTipOwnerThenClosePopup(root, mouse);
963+
964+
root.Content = null;
965+
await TestServices.WindowHelper.WaitForIdle();
966+
967+
Assert.IsTrue(await TestHelper.TryWaitUntilCollected(ownerRef), "The owner of a tooltip closed with its popup was not collected.");
968+
}
969+
finally
970+
{
971+
mouse.MoveTo(new Windows.Foundation.Point(0, 0));
972+
TestServices.WindowHelper.WindowContent = null;
973+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
974+
}
975+
}
976+
977+
private static async Task<WeakReference> HoverToolTipOwnerThenClosePopup(ContentControl root, Mouse mouse)
978+
{
979+
var owner = new Button { Content = "Hover me", Width = 100, Height = 50 };
980+
var toolTip = new ToolTip { Content = "ToolTip content" };
981+
ToolTipService.SetToolTip(owner, toolTip);
982+
root.Content = owner;
983+
await TestServices.WindowHelper.WaitForLoaded(owner);
984+
985+
mouse.MoveTo(owner.GetAbsoluteBoundsRect().GetCenter());
986+
await UITestHelper.WaitFor(() => toolTip.IsOpen, timeoutMS: 5000, message: "The automatic tooltip did not open on hover.");
987+
988+
// The pointer stays over the owner, so nothing the service listens to closes the tooltip.
989+
VisualTreeHelper.CloseAllPopups(TestServices.WindowHelper.XamlRoot);
990+
await UITestHelper.WaitFor(() => !toolTip.IsOpen, timeoutMS: 5000, message: "Closing the popup did not close the tooltip.");
991+
992+
return new WeakReference(owner);
993+
}
994+
#endif
915995
}
916996
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ private void OnOpenChanged(bool isOpen)
177177
UnsubscribeOwnerDataContextChanged();
178178
Closed?.Invoke(this, new RoutedEventArgs(this));
179179
GoToElementState("Closed", useTransitions: true);
180+
181+
if (!isOpen)
182+
{
183+
// As in WinUI's ToolTip::OnIsOpenChanged, the service lets go of a tooltip once it has closed, whichever
184+
// path closed it (a disabled tooltip asked to open also lands here, and stays the service's to close).
185+
ToolTipService.OnToolTipClosed(this);
186+
}
180187
}
181188
}
182189

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,23 @@ private static void OnPointerPressed(object sender, PointerRoutedEventArgs e)
324324
}
325325
}
326326
}
327+
328+
/// <summary>
329+
/// Called by a <see cref="ToolTip"/> when it closes, whichever path closed it: the service, the app, or its popup closing.
330+
/// </summary>
331+
internal static void OnToolTipClosed(ToolTip toolTip)
332+
{
333+
// WinUI clears ToolTipServiceMetadata::m_tpCurrentToolTip in the ToolTip's own IsOpen change rather than in the
334+
// service's close paths (ToolTip_Partial.cpp, ToolTip::OnIsOpenChanged), so a tooltip closed by a path the service
335+
// does not drive is not kept as the current one, and with it its owner.
336+
if (ReferenceEquals(m_CurrentToolTip, toolTip))
337+
{
338+
m_CurrentToolTip = null;
339+
}
340+
}
341+
342+
/// <summary>
343+
/// The tooltip the service is opening or showing, if any; for tests.
344+
/// </summary>
345+
internal static ToolTip CurrentToolTipForTesting => m_CurrentToolTip;
327346
}

0 commit comments

Comments
 (0)