forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDiffMergeToolProvider.cs
124 lines (112 loc) · 4.77 KB
/
CustomDiffMergeToolProvider.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GitCommands;
namespace GitUI
{
public class CustomDiffMergeToolProvider
{
/// <summary>
/// Time to wait before loading custom diff tools in FormBrowse
/// Avoid loading while git-log and git-diff run.
/// </summary>
private const int FormBrowseToolDelay = 8000;
/// <summary>
/// Clear the existing caches.
/// </summary>
/// <param name="isDiff">True if diff, false if merge.</param>
public async Task ClearAsync(bool isDiff)
{
if (isDiff)
{
await CustomDiffMergeToolCache.DiffToolCache.ClearAsync();
}
else
{
await CustomDiffMergeToolCache.MergeToolCache.ClearAsync();
}
}
/// <summary>
/// Load the available DiffMerge tools and apply to the menus.
/// </summary>
/// <param name="module">The Git module.</param>
/// <param name="menus">The menus to update.</param>
/// <param name="components">The calling Form components, to dispose correctly.</param>
/// <param name="isDiff">True if diff, false if merge.</param>
/// <param name="delay">The delay before starting the operation.</param>
public void LoadCustomDiffMergeTools(GitModule module, IList<CustomDiffMergeTool> menus, IContainer components, bool isDiff, int delay = FormBrowseToolDelay, CancellationToken cancellationToken = default)
{
InitMenus(menus);
if (isDiff && !AppSettings.ShowAvailableDiffTools)
{
return;
}
ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
// get the tools, possibly with a delay as requesting requires considerable time
// cache is shared
List<string> tools = (await (isDiff ? CustomDiffMergeToolCache.DiffToolCache : CustomDiffMergeToolCache.MergeToolCache)
.GetToolsAsync(module, delay, cancellationToken))
.ToList();
if (tools.Count <= 1)
{
// No need to show the menu
return;
}
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
foreach (var menu in menus)
{
menu.MenuItem.DropDown = new ContextMenuStrip(components);
foreach (var tool in tools)
{
ToolStripMenuItem item = new(tool) { Tag = tool };
item.Click += menu.Click;
menu.MenuItem.DropDown.Items.Add(item);
if (menu.MenuItem.DropDownItems.Count == 1)
{
item.Font = new Font(item.Font, FontStyle.Bold);
if (menu.MenuItem.ShortcutKeys != Keys.None)
{
item.ShortcutKeys = menu.MenuItem.ShortcutKeys;
}
else
{
item.ShortcutKeyDisplayString = menu.MenuItem.ShortcutKeyDisplayString;
}
}
}
if (isDiff)
{
// Allow disabling for difftools
menu.MenuItem.DropDown.Items.Add(new ToolStripSeparator());
ToolStripMenuItem disableItem = new()
{
Text = ResourceManager.TranslatedStrings.DisableMenuItem
};
disableItem.Click += (o, s) =>
{
// Disables the dropdown in the current form only
// RevDiff will not be affected from other forms, requires a restart
// To overcome this limitation other forms would require a reference to RevDiff
AppSettings.ShowAvailableDiffTools = false;
InitMenus(menus);
};
menu.MenuItem.DropDown.Items.Add(disableItem);
}
}
}).FileAndForget();
return;
static void InitMenus(IList<CustomDiffMergeTool> menus)
{
foreach (var menu in menus)
{
menu.MenuItem.DropDown = null;
}
}
}
}
}