-
Notifications
You must be signed in to change notification settings - Fork 5
/
ViewCreationListener.cs
143 lines (121 loc) · 4.66 KB
/
ViewCreationListener.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 System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
using System.Diagnostics;
using System;
namespace GradientSelection
{
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("any")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class ViewCreationListener : IWpfTextViewCreationListener
{
[Import]
IEditorFormatMapService formatMapService = null;
/// <summary>
/// When a text view is created, update its format map.
/// </summary>
public void TextViewCreated(IWpfTextView textView)
{
textView.Properties.GetOrCreateSingletonProperty(() =>
new FormatMapWatcher(textView, formatMapService.GetEditorFormatMap(textView)));
}
}
internal sealed class FormatMapWatcher
{
bool inUpdate = false;
IEditorFormatMap formatMap;
ITextView view;
static Brush gradientBrush;
static Pen gradientBorder;
static FormatMapWatcher()
{
Color highlight = SystemColors.HighlightColor;
Color darkColor = Color.FromArgb(96, highlight.R, highlight.G, highlight.B);
Color lightColor = Color.FromArgb(48, highlight.R, highlight.G, highlight.B);
gradientBrush = new LinearGradientBrush(new GradientStopCollection() { new GradientStop(darkColor, 0.0),
new GradientStop(lightColor, 0.5),
new GradientStop(darkColor, 1.0) },
90.0);
gradientBorder = new Pen(SystemColors.HighlightBrush, 1) { LineJoin = PenLineJoin.Round };
gradientBrush.Freeze();
gradientBorder.Freeze();
}
public FormatMapWatcher(ITextView view, IEditorFormatMap formatMap)
{
this.formatMap = formatMap;
this.view = view;
this.SetAppropriateBrush();
formatMap.FormatMappingChanged += (sender, args) => SetAppropriateBrush();
// Track the rich client option changing, to set and clear the gradient brush
view.Options.OptionChanged += (sender, args) =>
{
if (args.OptionId == DefaultWpfViewOptions.EnableSimpleGraphicsId.Name)
{
SetAppropriateBrush();
}
};
}
void SetAppropriateBrush()
{
if (inUpdate)
return;
try
{
inUpdate = true;
if (!view.Options.GetOptionValue(DefaultWpfViewOptions.EnableSimpleGraphicsId))
{
SetGradientBrush();
}
else
{
ClearGradientBrush();
}
}
finally
{
inUpdate = false;
}
}
void SetGradientBrush()
{
// Change the selected text properties to use a gradient brush and an outline pen
var properties = formatMap.GetProperties("Selected Text");
bool modified = false;
if (properties[EditorFormatDefinition.BackgroundBrushId] != gradientBrush)
{
modified = true;
properties[EditorFormatDefinition.BackgroundBrushId] = gradientBrush;
}
if (properties["BackgroundPen"] != gradientBorder)
{
modified = true;
properties["BackgroundPen"] = gradientBorder;
}
if (modified)
formatMap.SetProperties("Selected Text", properties);
}
void ClearGradientBrush()
{
// Clear out the gradient brush and outline pen
var properties = formatMap.GetProperties("Selected Text");
bool modified = false;
if (properties[EditorFormatDefinition.BackgroundBrushId] == gradientBrush)
{
modified = true;
properties.Remove(EditorFormatDefinition.BackgroundBrushId);
}
if (properties["BackgroundPen"] == gradientBorder)
{
modified = true;
properties.Remove("BackgroundPen");
}
if (modified)
formatMap.SetProperties("Selected Text", properties);
}
}
}