Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Rendering/MouseHoverLogic.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 4.5 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Windows;
21using System.Windows.Input;
22using System.Windows.Threading;
23
24namespace ICSharpCode.AvalonEdit.Rendering
25{
26  /// <summary>
27  /// Encapsulates and adds MouseHover support to UIElements.
28  /// </summary>
29  public class MouseHoverLogic : IDisposable
30  {
31    UIElement target;
32   
33    DispatcherTimer mouseHoverTimer;
34    Point mouseHoverStartPoint;
35    MouseEventArgs mouseHoverLastEventArgs;
36    bool mouseHovering;
37   
38    /// <summary>
39    /// Creates a new instance and attaches itself to the <paramref name="target" /> UIElement.
40    /// </summary>
41    public MouseHoverLogic(UIElement target)
42    {
43      if (target == null)
44        throw new ArgumentNullException("target");
45      this.target = target;
46      this.target.MouseLeave += MouseHoverLogicMouseLeave;
47      this.target.MouseMove += MouseHoverLogicMouseMove;
48      this.target.MouseEnter += MouseHoverLogicMouseEnter;
49    }
50   
51    void MouseHoverLogicMouseMove(object sender, MouseEventArgs e)
52    {
53      Vector mouseMovement = mouseHoverStartPoint - e.GetPosition(this.target);
54      if (Math.Abs(mouseMovement.X) > SystemParameters.MouseHoverWidth
55          || Math.Abs(mouseMovement.Y) > SystemParameters.MouseHoverHeight)
56      {
57        StartHovering(e);
58      }
59      // do not set e.Handled - allow others to also handle MouseMove
60    }
61   
62    void MouseHoverLogicMouseEnter(object sender, MouseEventArgs e)
63    {
64      StartHovering(e);
65      // do not set e.Handled - allow others to also handle MouseEnter
66    }
67   
68    void StartHovering(MouseEventArgs e)
69    {
70      StopHovering();
71      mouseHoverStartPoint = e.GetPosition(this.target);
72      mouseHoverLastEventArgs = e;
73      mouseHoverTimer = new DispatcherTimer(SystemParameters.MouseHoverTime, DispatcherPriority.Background, OnMouseHoverTimerElapsed, this.target.Dispatcher);
74      mouseHoverTimer.Start();
75    }
76   
77    void MouseHoverLogicMouseLeave(object sender, MouseEventArgs e)
78    {
79      StopHovering();
80      // do not set e.Handled - allow others to also handle MouseLeave
81    }
82   
83    void StopHovering()
84    {
85      if (mouseHoverTimer != null) {
86        mouseHoverTimer.Stop();
87        mouseHoverTimer = null;
88      }
89      if (mouseHovering) {
90        mouseHovering = false;
91        OnMouseHoverStopped(mouseHoverLastEventArgs);
92      }
93    }
94   
95    void OnMouseHoverTimerElapsed(object sender, EventArgs e)
96    {
97      mouseHoverTimer.Stop();
98      mouseHoverTimer = null;
99     
100      mouseHovering = true;
101      OnMouseHover(mouseHoverLastEventArgs);
102    }
103   
104    /// <summary>
105    /// Occurs when the mouse starts hovering over a certain location.
106    /// </summary>
107    public event EventHandler<MouseEventArgs> MouseHover;
108   
109    /// <summary>
110    /// Raises the <see cref="MouseHover"/> event.
111    /// </summary>
112    protected virtual void OnMouseHover(MouseEventArgs e)
113    {
114      if (MouseHover != null) {
115        MouseHover(this, e);
116      }
117    }
118   
119    /// <summary>
120    /// Occurs when the mouse stops hovering over a certain location.
121    /// </summary>
122    public event EventHandler<MouseEventArgs> MouseHoverStopped;
123   
124    /// <summary>
125    /// Raises the <see cref="MouseHoverStopped"/> event.
126    /// </summary>
127    protected virtual void OnMouseHoverStopped(MouseEventArgs e)
128    {
129      if (MouseHoverStopped != null) {
130        MouseHoverStopped(this, e);
131      }
132    }
133   
134    bool disposed;
135   
136    /// <summary>
137    /// Removes the MouseHover support from the target UIElement.
138    /// </summary>
139    public void Dispose()
140    {
141      if (!disposed) {
142        this.target.MouseLeave -= MouseHoverLogicMouseLeave;
143        this.target.MouseMove -= MouseHoverLogicMouseMove;
144        this.target.MouseEnter -= MouseHoverLogicMouseEnter;
145      }
146      disposed = true;
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.