-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrayIconContext.cs
236 lines (200 loc) · 9.58 KB
/
TrayIconContext.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MacroDeckWebView.Properties;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
namespace MacroDeckWebView
{
class TrayIconContext : ApplicationContext
{
private System.ComponentModel.IContainer components; // a list of components to dispose when the context is disposed
private NotifyIcon _notifyIcon; // the icon that sits in the system tray
private MacroDeckWebViewForm _exampleForm;
public TrayIconContext()
{
InitializeContext();
}
private void InitializeContext()
{
components = new System.ComponentModel.Container();
_notifyIcon = new NotifyIcon(components)
{
ContextMenuStrip = new ContextMenuStrip(),
Icon = Icon.FromHandle(Resources.ICON.GetHicon()),
Text = Resources.NOTIFY_TOOLTIP + " v" + Resources.VERSION,
Visible = true
};
// Menus
_notifyIcon.ContextMenuStrip.Items.Add(ToolStripMenuItemWithHandler(Resources.NOTIFY_MENU_1, showLogsItem_Click));
_notifyIcon.ContextMenuStrip.Items.Add(ToolStripMenuItemWithHandler(Resources.NOTIFY_MENU_3, editWebTrayConfig_Click));
_notifyIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator());
_notifyIcon.ContextMenuStrip.Items.Add(ToolStripMenuItemWithHandler(Resources.NOTIFY_MENU_2, openMacroDeck_Click));
_notifyIcon.ContextMenuStrip.Items.Add(ToolStripMenuItemWithHandler(Resources.NOTIFY_EXIT_MENU, exitSystem_Click));
_notifyIcon.ContextMenuStrip.Opening += ContextMenuStrip_Opening;
_notifyIcon.DoubleClick += notifyIcon_DoubleClick;
_notifyIcon.MouseUp += notifyIcon_MouseUp;
_notifyIcon.ShowBalloonTip(5000, Resources.BALLOON_TITLE , Resources.BALLOON_TEXT, ToolTipIcon.Info);
}
/// <summary>
/// When the application context is disposed, dispose things like the notify icon.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
if (_exampleForm != null)
_exampleForm.Dispose();
if (_notifyIcon != null)
_notifyIcon.Dispose();
}
}
private void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = false;
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
}
private void notifyIcon_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (MacroDeckWebViewForm.TrayLockVar == false)
{
if (_exampleForm == null)
{
ShowLogsForm();
}
else
{
if (_exampleForm.Visible == false)
{
_exampleForm.Left = Screen.FromPoint(Cursor.Position).WorkingArea.Right - _exampleForm.Width;
_exampleForm.Top = Screen.FromPoint(Cursor.Position).WorkingArea.Bottom - _exampleForm.Height;
_exampleForm.Show();
_exampleForm.Activate();
_exampleForm.ShowInTaskbar = false;
}
else if (_exampleForm.Visible == true)
{
_exampleForm.Hide();
}
}
}
}
else
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(_notifyIcon, null);
}
}
private ToolStripMenuItem ToolStripMenuItemWithHandler(string displayText, EventHandler eventHandler)
{
var item = new ToolStripMenuItem(displayText);
if (eventHandler != null) { item.Click += eventHandler; }
return item;
}
internal static class CommandLinePathResolver
{
private const int MAX_PATH = 260;
private static Lazy<Dictionary<string, string>> appPaths = new Lazy<Dictionary<string, string>>(LoadAppPaths);
private static Lazy<string[]> executableExtensions = new Lazy<string[]>(LoadExecutableExtensions);
public static string TryGetFullPathForCommand(string command)
{
if (Path.HasExtension(command))
return TryGetFullPathForFileName(command);
return TryGetFullPathByProbingExtensions(command);
}
private static string[] LoadExecutableExtensions() => Environment.GetEnvironmentVariable("PATHEXT").Split(';');
private static Dictionary<string, string> LoadAppPaths()
{
var appPaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths");
foreach (var subkeyName in key.GetSubKeyNames())
{
var subkey = key.OpenSubKey(subkeyName);
appPaths.Add(subkeyName, subkey.GetValue(string.Empty)?.ToString());
}
return appPaths;
}
private static string TryGetFullPathByProbingExtensions(string command)
{
foreach (var extension in executableExtensions.Value)
{
var result = TryGetFullPathForFileName(command + extension);
if (result != null)
return result;
}
return null;
}
private static string TryGetFullPathForFileName(string fileName) =>
TryGetFullPathFromPathEnvironmentVariable(fileName) ?? TryGetFullPathFromAppPaths(fileName);
private static string TryGetFullPathFromAppPaths(string fileName) =>
appPaths.Value.TryGetValue(fileName, out var path) ? path : null;
private static string TryGetFullPathFromPathEnvironmentVariable(string fileName)
{
if (fileName.Length >= MAX_PATH)
throw new ArgumentException($"The executable name '{fileName}' must have less than {MAX_PATH} characters.", nameof(fileName));
var sb = new StringBuilder(fileName, MAX_PATH);
return PathFindOnPath(sb, null) ? sb.ToString() : null;
}
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
}
private void showLogsItem_Click(object sender, EventArgs e) { ShowLogsForm(); }
private void openMacroDeck_Click(object sender, EventArgs e) { Process proc = Process.Start("C:\\Program Files\\Macro Deck\\Macro Deck 2.exe"); }
private void editWebTrayConfig_Click(object sender, EventArgs e) {
var notepadPlusPlus_Path = CommandLinePathResolver.TryGetFullPathForCommand("notepad++.exe");
var notepad_Path = CommandLinePathResolver.TryGetFullPathForCommand("notepad.exe");
if (notepadPlusPlus_Path != null)
{
Process proc = Process.Start("notepad++.exe", "\"" + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile + "\"");
}
else
{
Process proc = Process.Start("notepad.exe", "\"" + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile + "\"");
}
}
private void ShowLogsForm()
{
if (_exampleForm == null)
{
_exampleForm = new MacroDeckWebViewForm();
_exampleForm.Closed += logsForm_Closed; // avoid reshowing a disposed form
_exampleForm.StartPosition = FormStartPosition.Manual;
// _exampleForm.Left = Cursor.Position.X - _exampleForm.Width;
//_exampleForm.Top = Screen.PrimaryScreen.WorkingArea.Bottom - _exampleForm.Height;
_exampleForm.Left = Screen.FromPoint(Cursor.Position).WorkingArea.Right - _exampleForm.Width;
_exampleForm.Top = Screen.FromPoint(Cursor.Position).WorkingArea.Bottom - _exampleForm.Height;
_exampleForm.Show();
_exampleForm.Activate();
}
else
{
_exampleForm.Left = Screen.FromPoint(Cursor.Position).WorkingArea.Right - _exampleForm.Width;
_exampleForm.Top = Screen.FromPoint(Cursor.Position).WorkingArea.Bottom - _exampleForm.Height;
_exampleForm.Show();
_exampleForm.Activate();
}
}
// null out the forms so we know to create a new one.
private void logsForm_Closed(object sender, EventArgs e)
{
_exampleForm = null;
}
private void exitSystem_Click(object sender, EventArgs e)
{
this.ExitThread();
}
}
}