-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCBETagControl.xaml.cs
143 lines (124 loc) · 5.02 KB
/
CBETagControl.xaml.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Size = System.Windows.Size;
namespace CodeBlockEndTag
{
/// <summary>
/// Interaction logic for CBETagControl.xaml
/// </summary>
public partial class CBETagControl : UserControl
{
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CBETagControl), new PropertyMetadata("Unknown"));
public object TextColor
{
get => (object)GetValue(TextColorProperty);
set => SetValue(TextColorProperty, value);
}
public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register(
"TextColor", typeof(object), typeof(CBETagControl), new PropertyMetadata(Colors.Black));
public ImageMoniker IconMoniker
{
get => (ImageMoniker)GetValue(IconMonikerProperty);
set => SetValue(IconMonikerProperty, value);
}
public static readonly DependencyProperty IconMonikerProperty =
DependencyProperty.Register("IconMoniker", typeof(ImageMoniker), typeof(CBETagControl), new PropertyMetadata(KnownMonikers.QuestionMark));
public int DisplayMode
{
get => (int)GetValue(DisplayModeProperty);
set => SetValue(DisplayModeProperty, value);
}
public static readonly DependencyProperty DisplayModeProperty =
DependencyProperty.Register("DisplayMode", typeof(int), typeof(CBETagControl), new PropertyMetadata(0));
public double LineHeight
{
get => (double)GetValue(LineHeightProperty);
set => SetValue(LineHeightProperty, value);
}
public static readonly DependencyProperty LineHeightProperty =
DependencyProperty.Register("LineHeight", typeof(double), typeof(CBETagControl), new PropertyMetadata(9d));
internal CBAdornmentData AdornmentData { get; set; }
internal delegate void TagClickHandler(CBAdornmentData adornment, bool jumpToHead);
internal event TagClickHandler TagClicked;
public CBETagControl()
{
DataContext = this;
InitializeComponent();
}
protected override Size MeasureOverride(Size constraint)
{
var paddingRight = LineHeight / 2;
if (DisplayMode == 2)
{
// No label
return new Size(LineHeight + 4 + paddingRight, LineHeight);
}
TextBlock tb = btnTag.Template.FindName("txtTag", btnTag) as TextBlock;
return new Size(8 + LineHeight + (tb?.ActualWidth ?? 0) + paddingRight, LineHeight);
}
private DispatcherTimer buttonSingleClickTimeout;
private bool buttonModifiersPressed;
private void ButtonSingleClick(object sender, EventArgs e)
{
buttonSingleClickTimeout.Stop();
ButtonClicked(1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
buttonModifiersPressed = CheckModifiers();
if (buttonSingleClickTimeout == null)
{
buttonSingleClickTimeout =
new DispatcherTimer(
TimeSpan.FromSeconds(0.25),
DispatcherPriority.Background,
ButtonSingleClick,
Dispatcher.CurrentDispatcher);
}
buttonSingleClickTimeout.Start();
}
private void Button_DoubleClick(object sender, MouseButtonEventArgs e)
{
buttonSingleClickTimeout.Stop();
e.Handled = true;
ButtonClicked(2);
}
private void ButtonClicked(int clickCount)
{
int neededClickCount = CBETagPackage.CBEClickMode switch
{
(int)CBEOptionPage.ClickMode.SingleClick or (int)CBEOptionPage.ClickMode.CtrlClick => 1,
(int)CBEOptionPage.ClickMode.DoubleClick => 2,
_ => 0,
};
if (AdornmentData != null)
{
bool jumpToHead = (clickCount >= neededClickCount) && buttonModifiersPressed;
TagClicked?.Invoke(AdornmentData, jumpToHead);
}
}
private bool CheckModifiers()
{
return CBETagPackage.CBEClickMode != (int)CBEOptionPage.ClickMode.CtrlClick
|| Keyboard.IsKeyDown(Key.LeftCtrl)
|| Keyboard.IsKeyDown(Key.RightCtrl);
}
private void TxtTag_OnInitialized(object sender, EventArgs e)
{
this.InvalidateMeasure();
TextBlock tb = btnTag.Template.FindName("txtTag", btnTag) as TextBlock;
}
}
}