-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCodeNavMargin.cs
287 lines (241 loc) · 10.5 KB
/
CodeNavMargin.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#nullable enable
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using CodeNav.Helpers;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Text.Editor;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using CodeNav.Models;
using Microsoft.VisualStudio.Shell;
namespace CodeNav
{
public class CodeNavMargin : DockPanel, IWpfTextViewMargin
{
public const string MarginName = "CodeNav";
private bool _isDisposed;
public ICodeViewUserControl? _control;
public readonly IWpfTextView? _textView;
private readonly ColumnDefinition? _codeNavColumn;
private readonly Grid _codeNavGrid;
public readonly MarginSideEnum MarginSide;
public CodeNavMargin(IWpfTextViewHost textViewHost, MarginSideEnum side)
{
// Wire up references for the event handlers in RegisterEvents
MarginSide = side;
// Add the view/content to the margin area
if (side == MarginSideEnum.Top)
{
_codeNavGrid = CreateGridTop(textViewHost);
}
else
{
_codeNavGrid = CreateGrid(textViewHost);
_codeNavColumn = _codeNavGrid.ColumnDefinitions[(MarginSideEnum)General.Instance.MarginSide == MarginSideEnum.Left ? 0 : 2];
}
Children.Add(_codeNavGrid);
}
private Grid CreateGrid(IWpfTextViewHost textViewHost)
{
var marginWidth = General.Instance.ShowMargin ? General.Instance.Width : 0;
var leftColumnWidth = new GridLength(marginWidth, GridUnitType.Pixel);
if ((MarginSideEnum)General.Instance.MarginSide != MarginSideEnum.Left)
{
leftColumnWidth = new GridLength(0, GridUnitType.Star);
}
var rightColumnWidth = new GridLength(0, GridUnitType.Star);
if ((MarginSideEnum)General.Instance.MarginSide != MarginSideEnum.Left)
{
rightColumnWidth = new GridLength(marginWidth, GridUnitType.Pixel);
}
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = leftColumnWidth });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = rightColumnWidth });
grid.RowDefinitions.Add(new RowDefinition());
var splitter = new GridSplitter
{
Width = 5,
ResizeDirection = GridResizeDirection.Columns,
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
Background = ColorHelper.ToBrush(EnvironmentColors.EnvironmentBackgroundColorKey),
ToolTip = "What you can do with this bar:" + Environment.NewLine +
"- double-click it to toggle CodeNav visibility" + Environment.NewLine +
"- click and drag it to adjust CodeNav width"
};
VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged;
splitter.DragCompleted += DragCompleted;
splitter.MouseDoubleClick += Splitter_MouseDoubleClick;
grid.Children.Add(splitter);
var columnIndex = (MarginSideEnum)General.Instance.MarginSide == MarginSideEnum.Left ? 0 : 2;
_control = new CodeViewUserControl(grid.ColumnDefinitions[columnIndex]);
grid.Children.Add(_control as UIElement);
Grid.SetColumn(_control as UIElement, columnIndex);
Grid.SetColumn(splitter, 1);
Grid.SetColumn(textViewHost.HostControl, (MarginSideEnum)General.Instance.MarginSide == MarginSideEnum.Left ? 2 : 0);
var gridChild = grid.GetGridChildByType<CodeViewUserControl>();
if (gridChild != null)
{
if (General.Instance.BackgroundColor.IsNamedColor && General.Instance.BackgroundColor.Name.Equals("Transparent"))
{
gridChild.Background = Brushes.Transparent;
}
else
{
gridChild.Background = ColorHelper.ToBrush(General.Instance.BackgroundColor);
}
}
return grid;
}
private Grid CreateGridTop(IWpfTextViewHost textViewHost)
{
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition
{
Height = new GridLength(0, General.Instance.ShowMargin ? GridUnitType.Star : GridUnitType.Pixel)
});
grid.RowDefinitions.Add(new RowDefinition());
VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged;
_control = new CodeViewUserControlTop(grid.RowDefinitions[0]);
Grid.SetRow(_control as UIElement, 0);
Grid.SetRow(textViewHost.HostControl, 1);
// Apply custom background color to CodeNav grid child
(_control as CodeViewUserControlTop)!.Background =
General.Instance.BackgroundColor.IsNamedColor && General.Instance.BackgroundColor.Name.Equals("Transparent")
? Brushes.Transparent : ColorHelper.ToBrush(General.Instance.BackgroundColor);
return grid;
}
/// <summary>
/// Remove the margin
/// </summary>
public void Remove()
{
Children.Remove(_codeNavGrid);
}
#region Events
private void VSColorTheme_ThemeChanged(ThemeChangedEventArgs e)
{
if (_control is CodeViewUserControl)
{
((GridSplitter)_codeNavGrid.Children[0]).Background = ColorHelper.ToBrush(EnvironmentColors.EnvironmentBackgroundColorKey);
}
}
private void Splitter_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (_codeNavColumn == null)
{
return;
}
VisibilityHelper.SetMarginWidth(_codeNavColumn, _codeNavColumn.Width != new GridLength(0)).FireAndForget();
General.Instance.ShowMargin = !General.Instance.ShowMargin;
General.Instance.Save();
}
private void DragCompleted(object sender, DragCompletedEventArgs e)
{
if (!(_control is FrameworkElement controlElement))
{
return;
}
if (!double.IsNaN(controlElement.ActualWidth) && controlElement.ActualWidth != 0)
{
General.Instance.Width = controlElement.ActualWidth;
General.Instance.Save();
}
}
#endregion
#region IWpfTextViewMargin
/// <summary>
/// Gets the <see cref="FrameworkElement"/> that implements the visual representation of the margin.
/// </summary>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public FrameworkElement VisualElement
{
// Since this margin implements Canvas, this is the object which renders
// the margin.
get
{
ThrowIfDisposed();
return this;
}
}
#endregion
#region ITextViewMargin
/// <summary>
/// Gets the size of the margin.
/// </summary>
/// <remarks>
/// For a horizontal margin this is the height of the margin,
/// since the width will be determined by the <see cref="ITextView"/>.
/// For a vertical margin this is the width of the margin,
/// since the height will be determined by the <see cref="ITextView"/>.
/// </remarks>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public double MarginSize
{
get
{
ThrowIfDisposed();
// Since this is a vertical margin, its height will be bound to the height of the text view.
// Therefore, its size is its width.
return ActualWidth;
}
}
/// <summary>
/// Gets a value indicating whether the margin is enabled.
/// </summary>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public bool Enabled
{
get
{
ThrowIfDisposed();
// The margin should always be enabled
return true;
}
}
/// <summary>
/// Gets the <see cref="ITextViewMargin"/> with the given <paramref name="marginName"/> or null if no match is found
/// </summary>
/// <param name="marginName">The name of the <see cref="ITextViewMargin"/></param>
/// <returns>The <see cref="ITextViewMargin"/> named <paramref name="marginName"/>, or null if no match is found.</returns>
/// <remarks>
/// A margin returns itself if it is passed its own name. If the name does not match and it is a container margin, it
/// forwards the call to its children. Margin name comparisons are case-insensitive.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="marginName"/> is null.</exception>
public ITextViewMargin? GetTextViewMargin(string marginName)
{
return string.Equals(marginName, MarginName, StringComparison.OrdinalIgnoreCase) ? this : null;
}
/// <summary>
/// Disposes an instance of <see cref="CodeNavMargin"/> class.
/// </summary>
public void Dispose()
{
if (_isDisposed || _control == null)
{
return;
}
_control.CaretPositionChangedSubscription?.Dispose();
_control.TextContentChangedSubscription?.Dispose();
_control.UpdateWhileTypingSubscription?.Dispose();
_control.FileActionOccurredSubscription?.Dispose();
GC.SuppressFinalize(this);
_isDisposed = true;
}
/// <summary>
/// Checks and throws <see cref="ObjectDisposedException"/> if the object is disposed.
/// </summary>
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(MarginName);
}
}
#endregion
}
}