forked from VerifyTests/DiffEngine
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
149 lines (131 loc) · 4.23 KB
/
Program.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
using NotifyIcon = System.Windows.Forms.NotifyIcon;
static class Program
{
static async Task Main()
{
Logging.Init();
try
{
await Inner();
}
catch (Exception exception)
{
Log.Logger.Fatal(exception, "Failed at startup");
throw;
}
}
static async Task Inner()
{
var settings = await GetSettings();
if (settings == null)
{
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var tokenSource = new CancelSource();
var cancel = tokenSource.Token;
using var mutex = new Mutex(true, "DiffEngine", out var createdNew);
if (!createdNew)
{
Log.Information("Mutex already exists. Exiting.");
return;
}
using var icon = new NotifyIcon
{
Icon = Images.Default,
Visible = true,
Text = "DiffEngineTray"
};
await using var tracker = new Tracker(
active: () => icon.Icon = Images.Active,
inactive: () => icon.Icon = Images.Default);
using var task = StartServer(tracker, cancel);
using var keyRegister = new KeyRegister(icon.Handle());
ReBindKeys(settings, keyRegister, tracker);
var menuStrip = MenuBuilder.Build(
Application.Exit,
async () => await OptionsFormLauncher.Launch(keyRegister, tracker),
tracker);
icon.MouseClick += (_, args) =>
{
if (args.Button == MouseButtons.Left)
{
var position = Cursor.Position;
position.Offset(-menuStrip.Width, -menuStrip.Height);
menuStrip.Location = position;
ShowContextMenu(icon);
}
};
icon.ContextMenuStrip = menuStrip;
Application.Run();
await tokenSource.CancelAsync();
await task;
}
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ShowContextMenu")]
static extern void ShowContextMenu(NotifyIcon icon);
static void ReBindKeys(Settings settings, KeyRegister keyRegister, Tracker tracker)
{
var discardAllHotKey = settings.DiscardAllHotKey;
if (discardAllHotKey != null)
{
keyRegister.TryAddBinding(
KeyBindingIds.DiscardAll,
discardAllHotKey.Shift,
discardAllHotKey.Control,
discardAllHotKey.Alt,
discardAllHotKey.Key,
tracker.Clear);
}
var acceptAllHotKey = settings.AcceptAllHotKey;
if (acceptAllHotKey != null)
{
keyRegister.TryAddBinding(
KeyBindingIds.AcceptAll,
acceptAllHotKey.Shift,
acceptAllHotKey.Control,
acceptAllHotKey.Alt,
acceptAllHotKey.Key,
tracker.AcceptAll);
}
var acceptOpenHotKey = settings.AcceptOpenHotKey;
if (acceptOpenHotKey != null)
{
keyRegister.TryAddBinding(
KeyBindingIds.AcceptAll,
acceptOpenHotKey.Shift,
acceptOpenHotKey.Control,
acceptOpenHotKey.Alt,
acceptOpenHotKey.Key,
tracker.AcceptOpen);
}
}
static async Task<Settings?> GetSettings()
{
try
{
return await SettingsHelper.Read();
}
catch (Exception exception)
{
var message = $"Cannot start. Failed to read settings: {SettingsHelper.FilePath}";
Log.Fatal(exception, message);
IssueLauncher.LaunchForException(message, exception);
return null;
}
}
static Task StartServer(Tracker tracker, Cancel cancel) =>
PiperServer.Start(
payload =>
{
tracker.AddMove(
payload.Temp,
payload.Target,
payload.Exe,
payload.Arguments,
payload.CanKill,
payload.ProcessId);
},
payload => tracker.AddDelete(payload.File),
cancel);
}