forked from dnSpy/ICSharpCode.TreeView
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClickHandler.cs
42 lines (37 loc) · 1.01 KB
/
ClickHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Timers;
using System.Windows;
namespace ICSharpCode.TreeView {
public class ClickHandler<T> {
readonly int delay;
Timer timer;
int click;
Action<T> action;
T context;
public ClickHandler(int delay = 300) => this.delay = delay;
public void UpdateContext(T context) => this.context = context;
void RunAction() => Application.Current.Dispatcher.BeginInvoke((Action)(() => {
timer?.Stop();
timer = null;
action?.Invoke(context);
action = null;
}));
public void MouseDown(T context) {
this.context = context;
click = timer == null ? 1 : click + 1;
if (click == 1) {
timer = new Timer { Interval = delay };
action = null;
timer.Elapsed += (sender, e) => { RunAction(); };
timer?.Start();
}
}
public void MouseUp(Action<T> singleClickAction, Action<T> doubleClickAction) {
action = click == 1 ? singleClickAction : doubleClickAction;
if (timer == null)
action(context);
if (timer != null && click == 2)
RunAction();
}
}
}