-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ScriptsSettingsPage.cs
371 lines (304 loc) · 10.9 KB
/
ScriptsSettingsPage.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using GitCommands;
using GitCommands.Utils;
using GitExtUtils.GitUI;
using GitExtUtils.GitUI.Theming;
using GitUI.ScriptsEngine;
using GitUIPluginInterfaces;
using ResourceManager;
namespace GitUI.CommandsDialogs.SettingsDialog.Pages
{
public partial class ScriptsSettingsPage : SettingsPageWithHeader
{
private readonly TranslationString _scriptSettingsPageHelpDisplayArgumentsHelp = new("Arguments help");
private readonly TranslationString _scriptSettingsPageHelpDisplayContent = new(@"Use {option} for normal replacement.
Use {{option}} for quoted replacement.
User Input:
{UserInput}
{UserFiles}
Working Dir:
{WorkingDir}
Repository:
{RepoName}
Selected Commits:
{sHashes}
Selected Branch:
{sTag}
{sBranch}
{sLocalBranch}
{sRemoteBranch}
{sRemoteBranchName} (without the remote's name)
{sRemote}
{sRemoteUrl}
{sRemotePathFromUrl}
{sHash}
{sMessage}
{sSubject}
{sAuthor}
{sCommitter}
{sAuthorDate}
{sCommitDate}
Current Branch:
{cTag}
{cBranch}
{cLocalBranch}
{cRemoteBranch}
{cRemoteBranchName} (without the remote's name)
{cHash}
{cMessage}
{cSubject}
{cAuthor}
{cCommitter}
{cAuthorDate}
{cCommitDate}
{cDefaultRemote}
{cDefaultRemoteUrl}
{cDefaultRemotePathFromUrl}");
private static readonly string[] WatchedProxyProperties = new string[]
{
nameof(ScriptInfoProxy.Name),
nameof(ScriptInfoProxy.Enabled),
nameof(ScriptInfoProxy.Icon),
nameof(ScriptInfoProxy.OnEvent),
nameof(ScriptInfoProxy.Command),
nameof(ScriptInfoProxy.Arguments),
};
private static readonly ImageList EmbeddedIcons = new()
{
ColorDepth = ColorDepth.Depth32Bit
};
private readonly BindingList<ScriptInfoProxy> _scripts = new();
private SimpleHelpDisplayDialog? _argumentsCheatSheet;
private bool _handlingCheck;
// settings maybe loaded before page is shown or after
// we need to track that so we load images before we bind the list
private bool _imagsLoaded;
public ScriptsSettingsPage(IServiceProvider serviceProvider)
: base(serviceProvider)
{
InitializeComponent();
// stop the localisation of the propertygrid
propertyGrid1.Text = string.Empty;
InitializeComplete();
foreach (ColumnHeader column in lvScripts.Columns)
{
column.Width = DpiUtil.Scale(column.Width);
}
tableLayoutPanel1.Dock = DockStyle.Fill;
var margin = propertyGrid1.Margin.Left;
propertyGrid1.Margin = new Padding(margin, margin, panelButtons.Width, margin);
propertyGrid1.SelectedGridItemChanged += (s, e) =>
{
if (WatchedProxyProperties.Contains(e.OldSelection?.PropertyDescriptor?.Name ?? ""))
{
BindScripts(_scripts, SelectedScript);
propertyGrid1.Focus();
}
};
}
private IScriptsManager ScriptsManager
{
get
{
if (!TryGetUICommands(out IGitUICommands commands))
{
throw new InvalidOperationException("IGitUICommands should have been assigned");
}
return commands.GetRequiredService<IScriptsManager>();
}
}
private ScriptInfoProxy? SelectedScript { get; set; }
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
if (Parent is null)
{
_argumentsCheatSheet?.Close();
}
}
public override void OnPageShown()
{
if (!EnvUtils.RunningOnWindows())
{
return;
}
System.Resources.ResourceManager rm = new("GitUI.Properties.Images", Assembly.GetExecutingAssembly());
// dummy request; for some strange reason the ResourceSets are not loaded until after the first object request... bug?
rm.GetObject("dummy");
using System.Resources.ResourceSet resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry icon in resourceSet.Cast<DictionaryEntry>().OrderBy(icon => icon.Key))
{
if (icon.Value is Bitmap bitmap)
{
EmbeddedIcons.Images.Add(icon.Key.ToString(), bitmap.AdaptLightness());
}
}
resourceSet.Close();
rm.ReleaseAllResources();
lvScripts.LargeImageList = lvScripts.SmallImageList = EmbeddedIcons;
_imagsLoaded = true;
BindScripts(_scripts, null);
}
protected override void SettingsToPage()
{
_scripts.Clear();
foreach (var script in ScriptsManager.GetScripts())
{
_scripts.Add(script);
}
if (_imagsLoaded)
{
BindScripts(_scripts, null);
}
base.SettingsToPage();
}
protected override void PageToSettings()
{
// TODO: this is an abomination, the whole script persistence must be scorched and rewritten
List<ScriptInfo> scripts = new(ScriptsManager.GetScripts());
scripts.Clear();
foreach (ScriptInfoProxy proxy in _scripts)
{
scripts.Add(proxy);
}
AppSettings.OwnScripts = ScriptsManager.SerializeIntoXml();
base.PageToSettings();
}
private void BindScripts(IList<ScriptInfoProxy> scripts, ScriptInfoProxy? selectedScript)
{
try
{
lvScripts.BeginUpdate();
lvScripts.SelectedIndexChanged -= lvScripts_SelectedIndexChanged;
lvScripts.ItemChecked -= lvScripts_ItemChecked;
lvScripts.Items.Clear();
if (scripts.Count < 1)
{
btnAdd.Focus();
return;
}
foreach (ScriptInfoProxy script in scripts)
{
var color = !script.Enabled ? SystemColors.GrayText : SystemColors.WindowText;
ListViewItem lvitem = new(script.Name)
{
ToolTipText = $"{script.Command} {script.Arguments}",
Tag = script,
ForeColor = color,
ImageKey = script.Icon,
Checked = script.Enabled
};
lvScripts.Items.Add(lvitem);
lvitem.SubItems.Add(script.OnEvent.ToString());
lvitem.SubItems.Add(string.Concat(script.Command, " ", script.Arguments));
}
lvScripts.SelectedIndexChanged += lvScripts_SelectedIndexChanged;
lvScripts.ItemChecked += lvScripts_ItemChecked;
lvScripts.SelectedIndices.Clear();
SelectedScript = selectedScript;
if (selectedScript is object)
{
ListViewItem lvi = lvScripts.Items.Cast<ListViewItem>().FirstOrDefault(x => x.Tag == selectedScript);
if (lvi is not null)
{
lvi.Selected = true;
lvScripts.FocusedItem = lvi;
}
}
if (lvScripts.FocusedItem is null)
{
lvScripts.Items[0].Selected = true;
lvScripts.FocusedItem = lvScripts.Items[0];
}
lvScripts.Select();
}
finally
{
lvScripts.EndUpdate();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ScriptInfoProxy script = _scripts.AddNew();
script.HotkeyCommandIdentifier = Math.Max(GitUI.ScriptsEngine.ScriptsManager.MinimumUserScriptID, _scripts.Max(s => s.HotkeyCommandIdentifier)) + 1;
script.Name = "<New Script>";
script.Enabled = true;
BindScripts(_scripts, script);
}
private void btnArgumentsHelp_Click(object sender, EventArgs e)
{
if (_argumentsCheatSheet?.Visible ?? false)
{
_argumentsCheatSheet.BringToFront();
return;
}
_argumentsCheatSheet = new SimpleHelpDisplayDialog
{
DialogTitle = _scriptSettingsPageHelpDisplayArgumentsHelp.Text,
ContentText = _scriptSettingsPageHelpDisplayContent.Text.Replace("\n", Environment.NewLine)
};
_argumentsCheatSheet.Show(this);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (SelectedScript is null)
{
return;
}
_scripts.Remove(SelectedScript);
BindScripts(_scripts, null);
}
private void btnMoveDown_Click(object sender, EventArgs e)
{
if (SelectedScript is null)
{
return;
}
ScriptInfoProxy script = SelectedScript;
int index = _scripts.IndexOf(script);
_scripts.Remove(script);
_scripts.Insert(Math.Min(index + 1, _scripts.Count), script);
BindScripts(_scripts, script);
}
private void btnMoveUp_Click(object sender, EventArgs e)
{
if (SelectedScript is null)
{
return;
}
ScriptInfoProxy script = SelectedScript;
int index = _scripts.IndexOf(script);
_scripts.Remove(script);
_scripts.Insert(Math.Max(index - 1, 0), script);
BindScripts(_scripts, script);
}
private void lvScripts_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (_handlingCheck)
{
return;
}
_handlingCheck = true;
e.Item.Selected = true;
e.Item.Checked = !e.Item.Checked;
_handlingCheck = false;
}
private void lvScripts_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvScripts.SelectedItems.Count < 1 || !(lvScripts.SelectedItems[0].Tag is ScriptInfoProxy script))
{
propertyGrid1.SelectedObject = null;
return;
}
SelectedScript = script;
script.SetImages(EmbeddedIcons);
propertyGrid1.SelectedObject = script;
int index = _scripts.IndexOf(script);
btnMoveUp.Enabled = index > 0;
btnMoveDown.Enabled = index < _scripts.Count - 1;
}
}
}