-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCBETagControl.cs
More file actions
115 lines (97 loc) · 3.73 KB
/
Copy pathCBETagControl.cs
File metadata and controls
115 lines (97 loc) · 3.73 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace CodeBlockEndTag;
public class CBETagControl : ButtonBase
{
private static readonly Type typeofThis = typeof(CBETagControl);
static CBETagControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeofThis, new FrameworkPropertyMetadata(typeofThis));
}
public CBETagControl()
{
DataContext = this;
}
public double LineHeight
{
get { return (double)GetValue(LineHeightProperty); }
set { SetValue(LineHeightProperty, value); }
}
public static readonly DependencyProperty LineHeightProperty =
DependencyProperty.Register("LineHeight", typeof(double), typeofThis, new PropertyMetadata(16.0));
public object IconMoniker
{
get { return GetValue(IconMonikerProperty); }
set { SetValue(IconMonikerProperty, value); }
}
public static readonly DependencyProperty IconMonikerProperty =
DependencyProperty.Register("IconMoniker", typeof(object), typeofThis, new PropertyMetadata(null));
public int DisplayMode
{
get { return (int)GetValue(DisplayModeProperty); }
set { SetValue(DisplayModeProperty, value); }
}
public static readonly DependencyProperty DisplayModeProperty =
DependencyProperty.Register("DisplayMode", typeof(int), typeofThis, new PropertyMetadata(0));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeofThis, new PropertyMetadata(string.Empty));
public Brush TextColor
{
get { return (Brush)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
public static readonly DependencyProperty TextColorProperty =
DependencyProperty.Register("TextColor", typeof(Brush), typeofThis, new PropertyMetadata(Brushes.Black));
// Custom event for tag click
internal event Action<Model.CBAdornmentData, bool> TagClicked;
internal Model.CBAdornmentData? AdornmentData { get; set; }
private DispatcherTimer buttonSingleClickTimeout;
private bool buttonModifiersPressed;
private void ButtonSingleClick(object sender, EventArgs e)
{
buttonSingleClickTimeout?.Stop();
ButtonClicked(1);
}
protected override void OnClick()
{
buttonModifiersPressed = CheckModifiers();
buttonSingleClickTimeout ??= new DispatcherTimer(
TimeSpan.FromSeconds(0.25),
DispatcherPriority.Background,
ButtonSingleClick,
Dispatcher.CurrentDispatcher);
buttonSingleClickTimeout.Start();
}
protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
{
buttonSingleClickTimeout?.Stop();
e.Handled = true;
ButtonClicked(2);
}
private void ButtonClicked(int clickCount)
{
if (!AdornmentData.HasValue)
return;
int neededClickCount = CBETagPackage.CBEClickMode switch
{
(int)Model.ClickMode.SingleClick or (int)Model.ClickMode.CtrlClick => 1,
(int)Model.ClickMode.DoubleClick => 2,
_ => 0,
};
bool jumpToHead = (clickCount >= neededClickCount) && buttonModifiersPressed;
TagClicked?.Invoke(AdornmentData.Value, jumpToHead);
}
private bool CheckModifiers() =>
CBETagPackage.CBEClickMode != (int)Model.ClickMode.CtrlClick
|| Keyboard.IsKeyDown(Key.LeftCtrl)
|| Keyboard.IsKeyDown(Key.RightCtrl);
}