aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2019-04-09 01:47:48 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2019-04-09 01:47:48 +0300
commit080f1697e97e13461ec6df4d31c8924d01257a1b (patch)
treeb1fe0285de7bc9bc52e9e2195e66fe022bf8f5b3 /Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs
parent1608e69a417bc5e40a607c3958c4a60f19f66f1a (diff)
downloadTango-080f1697e97e13461ec6df4d31c8924d01257a1b.tar.gz
Tango-080f1697e97e13461ec6df4d31c8924d01257a1b.zip
MERGE
Diffstat (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs')
-rw-r--r--Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs134
1 files changed, 134 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs
new file mode 100644
index 000000000..857affcbb
--- /dev/null
+++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Rendering/MouseHoverLogic.cs
@@ -0,0 +1,134 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using System.Windows;
+using System.Windows.Input;
+using System.Windows.Threading;
+
+namespace Tango.Scripting.Editors.Rendering
+{
+ /// <summary>
+ /// Encapsulates and adds MouseHover support to UIElements.
+ /// </summary>
+ public class MouseHoverLogic : IDisposable
+ {
+ UIElement target;
+
+ DispatcherTimer mouseHoverTimer;
+ Point mouseHoverStartPoint;
+ MouseEventArgs mouseHoverLastEventArgs;
+ bool mouseHovering;
+
+ /// <summary>
+ /// Creates a new instance and attaches itself to the <paramref name="target" /> UIElement.
+ /// </summary>
+ public MouseHoverLogic(UIElement target)
+ {
+ if (target == null)
+ throw new ArgumentNullException("target");
+ this.target = target;
+ this.target.MouseLeave += MouseHoverLogicMouseLeave;
+ this.target.MouseMove += MouseHoverLogicMouseMove;
+ this.target.MouseEnter += MouseHoverLogicMouseEnter;
+ }
+
+ void MouseHoverLogicMouseMove(object sender, MouseEventArgs e)
+ {
+ Vector mouseMovement = mouseHoverStartPoint - e.GetPosition(this.target);
+ if (Math.Abs(mouseMovement.X) > SystemParameters.MouseHoverWidth
+ || Math.Abs(mouseMovement.Y) > SystemParameters.MouseHoverHeight)
+ {
+ StartHovering(e);
+ }
+ // do not set e.Handled - allow others to also handle MouseMove
+ }
+
+ void MouseHoverLogicMouseEnter(object sender, MouseEventArgs e)
+ {
+ StartHovering(e);
+ // do not set e.Handled - allow others to also handle MouseEnter
+ }
+
+ void StartHovering(MouseEventArgs e)
+ {
+ StopHovering();
+ mouseHoverStartPoint = e.GetPosition(this.target);
+ mouseHoverLastEventArgs = e;
+ mouseHoverTimer = new DispatcherTimer(SystemParameters.MouseHoverTime, DispatcherPriority.Background, OnMouseHoverTimerElapsed, this.target.Dispatcher);
+ mouseHoverTimer.Start();
+ }
+
+ void MouseHoverLogicMouseLeave(object sender, MouseEventArgs e)
+ {
+ StopHovering();
+ // do not set e.Handled - allow others to also handle MouseLeave
+ }
+
+ void StopHovering()
+ {
+ if (mouseHoverTimer != null) {
+ mouseHoverTimer.Stop();
+ mouseHoverTimer = null;
+ }
+ if (mouseHovering) {
+ mouseHovering = false;
+ OnMouseHoverStopped(mouseHoverLastEventArgs);
+ }
+ }
+
+ void OnMouseHoverTimerElapsed(object sender, EventArgs e)
+ {
+ mouseHoverTimer.Stop();
+ mouseHoverTimer = null;
+
+ mouseHovering = true;
+ OnMouseHover(mouseHoverLastEventArgs);
+ }
+
+ /// <summary>
+ /// Occurs when the mouse starts hovering over a certain location.
+ /// </summary>
+ public event EventHandler<MouseEventArgs> MouseHover;
+
+ /// <summary>
+ /// Raises the <see cref="MouseHover"/> event.
+ /// </summary>
+ protected virtual void OnMouseHover(MouseEventArgs e)
+ {
+ if (MouseHover != null) {
+ MouseHover(this, e);
+ }
+ }
+
+ /// <summary>
+ /// Occurs when the mouse stops hovering over a certain location.
+ /// </summary>
+ public event EventHandler<MouseEventArgs> MouseHoverStopped;
+
+ /// <summary>
+ /// Raises the <see cref="MouseHoverStopped"/> event.
+ /// </summary>
+ protected virtual void OnMouseHoverStopped(MouseEventArgs e)
+ {
+ if (MouseHoverStopped != null) {
+ MouseHoverStopped(this, e);
+ }
+ }
+
+ bool disposed;
+
+ /// <summary>
+ /// Removes the MouseHover support from the target UIElement.
+ /// </summary>
+ public void Dispose()
+ {
+ if (!disposed) {
+ this.target.MouseLeave -= MouseHoverLogicMouseLeave;
+ this.target.MouseMove -= MouseHoverLogicMouseMove;
+ this.target.MouseEnter -= MouseHoverLogicMouseEnter;
+ }
+ disposed = true;
+ }
+ }
+}