瀏覽代碼

fix: 修复tooltip弹窗在某些情况下不会正常消失的问题(Meloong-Git/PCL#8917,@Peacock-Que)

HerobrineXia 3 月之前
父節點
當前提交
41f44afd9f
共有 1 個文件被更改,包括 14 次插入 和 2 次删除
  1. 14 2
      Wpf/Controls/ModernTooltipService.cs

+ 14 - 2
Wpf/Controls/ModernTooltipService.cs

@@ -201,8 +201,8 @@ public static class ModernTooltipService {
 
         if (owner is not null) {
             BeginShow(owner, Mouse.GetPosition(owner));
-        } else if (currentOwner is { IsEnabled: false }) {
-            Close(true); // 禁用的控件不会触发 MouseLeave,需要由其他元素收到的 MouseMove 主动关闭
+        } else if (currentOwner is not null) {
+            Close(true); // 弹窗遮挡等情况可能不会触发原拥有者的 MouseLeave,需要由其他元素收到的 MouseMove 主动关闭
         }
     }
 
@@ -220,6 +220,14 @@ public static class ModernTooltipService {
         var owner = FindOwnerInAncestors(Mouse.DirectlyOver as DependencyObject);
         if (owner is not null) return owner;
 
+        // 重新从窗口根元素命中测试,避免弹窗出现后 Mouse.DirectlyOver 尚未刷新,继续命中已被遮挡的旧元素。
+        if (PresentationSource.FromVisual(reference)?.RootVisual is UIElement root) {
+            var rootHit = root.InputHitTest(Mouse.GetPosition(root)) as DependencyObject;
+            owner = FindOwnerInAncestors(rootHit);
+            if (owner is not null) return owner;
+            if (rootHit is not null && !IsSameTreeBranch(rootHit, reference)) return null;
+        }
+
         return reference.InputHitTest(Mouse.GetPosition(reference)) is DependencyObject hit
             ? FindOwnerInAncestors(hit)
             : null;
@@ -307,6 +315,10 @@ public static class ModernTooltipService {
 
     private static void Show(FrameworkElement owner, Point point) {
         if (!ReferenceEquals(owner, currentOwner) || !CanShowModernTooltip(owner) || !TryGetToolTip(owner, out var content, out var sourceToolTip)) return;
+        if (!ReferenceEquals(owner, FindCurrentTooltipOwner(owner))) {
+            Close(false);
+            return;
+        }
 
         if (popup is null) {
             popupScale = new ScaleTransform(closedScale, closedScale);