Explorar o código

fix: Tooltip 在文本区域长按左键时自动收回

龙腾猫跃 hai 4 meses
pai
achega
4f44549354
Modificáronse 1 ficheiros con 13 adicións e 0 borrados
  1. 13 0
      Wpf/Controls/ModernTooltipService.cs

+ 13 - 0
Wpf/Controls/ModernTooltipService.cs

@@ -111,15 +111,28 @@ public static class ModernTooltipService {
         if (sender is not FrameworkElement owner || !ReferenceEquals(owner, currentOwner)) return;
         if (sender is not FrameworkElement owner || !ReferenceEquals(owner, currentOwner)) return;
 
 
         lastMousePoint = e.GetPosition(owner);
         lastMousePoint = e.GetPosition(owner);
+        if (e.LeftButton == MouseButtonState.Pressed && !IsPointInside(owner, lastMousePoint)) {
+            Close(true);
+            return;
+        }
+
         if (GetFollowMouse(owner) && popup is { IsOpen: true }) UpdatePosition(owner, lastMousePoint);
         if (GetFollowMouse(owner) && popup is { IsOpen: true }) UpdatePosition(owner, lastMousePoint);
     }
     }
 
 
     private static void OnMouseLeave(object sender, MouseEventArgs e) {
     private static void OnMouseLeave(object sender, MouseEventArgs e) {
         if (sender is not FrameworkElement owner || !ReferenceEquals(owner, currentOwner)) return;
         if (sender is not FrameworkElement owner || !ReferenceEquals(owner, currentOwner)) return;
 
 
+        // 避免 Tooltip 在文本区域长按左键时自动收回
+        var point = e.GetPosition(owner);
+        if (e.LeftButton == MouseButtonState.Pressed && IsPointInside(owner, point)) return;
+
         Close(true);
         Close(true);
     }
     }
 
 
+    private static bool IsPointInside(FrameworkElement owner, Point point) {
+        return point.X >= 0 && point.Y >= 0 && point.X <= owner.ActualWidth && point.Y <= owner.ActualHeight;
+    }
+
     #endregion
     #endregion
 
 
     #region Tooltip 生命周期
     #region Tooltip 生命周期