-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.cs
187 lines (162 loc) · 6.73 KB
/
Form.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
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Toggle_Muter
{
public partial class Form : System.Windows.Forms.Form
{
private readonly NotifyIcon _notifyIcon;
private readonly GlobalKeyboardHook _keyboardHook;
private readonly SettingsManager _settingsManager;
private ConfigureHotkeyForm? _configureHotkeyForm;
public Form()
{
InitializeComponent();
_settingsManager = new SettingsManager(this, _keyboardHook);
_keyboardHook = new GlobalKeyboardHook(this, _settingsManager);
// Register the global keyboard hook
GlobalKeyboardHook.RegisterHook();
// Initialize the system tray icon
_notifyIcon = new NotifyIcon
{
Text = "Toggle Muter",
Visible = true,
ContextMenuStrip = new ContextMenuStrip()
};
LoadSystemTrayIcon();
SetupContextMenu();
// Wire up the Form_Closing event handler
FormClosing += Form_FormClosing;
}
// Set up the context menu for the system tray icon
private void SetupContextMenu()
{
// "Autostart" menu item (checkbox)
var autostartMenuItem = new ToolStripMenuItem("Autostart")
{
CheckOnClick = true,
Checked = IsAppSetToRunAtStartup()
};
autostartMenuItem.Click += AutostartMenuItem_Click;
_notifyIcon.ContextMenuStrip.Items.Add(autostartMenuItem);
// Separator
_notifyIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator());
// "Configure hotkey..." menu item (button)
_notifyIcon.ContextMenuStrip.Items.Add("Configure Hotkey...", null, MenuConfigureHotkey_Click);
// "Monochromatic Icon" menu item (checkbox)
var monochromaticIconMenuItem = new ToolStripMenuItem("Monochromatic Icon")
{
CheckOnClick = true,
Checked = _settingsManager.GetMonochromaticSysTrayIcon()
};
monochromaticIconMenuItem.Click += MonochromaticIconMenuItem_Click;
_notifyIcon.ContextMenuStrip.Items.Add(monochromaticIconMenuItem);
// Separator
_notifyIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator());
// "Exit" menu item (button)
_notifyIcon.ContextMenuStrip.Items.Add("Exit", null, MenuExit_Click);
}
// Load the system tray icon based on the settings
private void LoadSystemTrayIcon()
{
string iconResourceName = _settingsManager.GetMonochromaticSysTrayIcon()
? "Toggle_Muter.mono_icon.ico"
: "Toggle_Muter.black_icon.ico";
using (var iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(iconResourceName))
{
if (iconStream != null)
{
_notifyIcon.Icon = new Icon(iconStream);
}
else
{
MessageBox.Show("Failed to load the system tray icon.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// Check if the application is set to run at startup
private bool IsAppSetToRunAtStartup()
{
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
{
return key?.GetValue("Toggle Muter") != null;
}
}
// Set the application to run at startup based on the provided value
private void SetAppToRunAtStartup(bool runAtStartup)
{
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
{
if (runAtStartup)
{
key?.SetValue("Toggle Muter", Application.ExecutablePath);
}
else
{
key?.DeleteValue("Toggle Muter", false);
}
}
}
// Event handler for the "Configure Hotkey..." menu item
private void MenuConfigureHotkey_Click(object? sender, EventArgs e)
{
if (_configureHotkeyForm == null || _configureHotkeyForm.IsDisposed)
{
_configureHotkeyForm?.Dispose();
_configureHotkeyForm = new ConfigureHotkeyForm(_settingsManager);
_configureHotkeyForm.Show();
}
else
{
_configureHotkeyForm.BringToFront();
}
}
// Event handler for the "Monochromatic Icon" menu item
private void MonochromaticIconMenuItem_Click(object? sender, EventArgs e)
{
if (sender is ToolStripMenuItem menuItem && menuItem != null)
{
_settingsManager.SetMonochromaticSysTrayIcon(menuItem.Checked);
LoadSystemTrayIcon();
}
}
// Event handler for the "Autostart" menu item
private void AutostartMenuItem_Click(object? sender, EventArgs e)
{
if (sender is ToolStripMenuItem menuItem)
{
SetAppToRunAtStartup(menuItem?.Checked ?? false);
}
}
// Event handler for the "Exit" menu item
private void MenuExit_Click(object? sender, EventArgs e)
{
Close();
}
// Event handler for the form closing event
private void Form_FormClosing(object? sender, FormClosingEventArgs e)
{
GlobalKeyboardHook.UnregisterHook();
_configureHotkeyForm?.Dispose();
}
// Adjust the mute status of the current application in focus
public void AdjustMuteStatus()
{
GetWindowThreadProcessId(GetForegroundWindow(), out var processId);
var muteStatus = VolumeMixer.GetApplicationMute((int)processId);
if (muteStatus == null)
{
Debug.WriteLine("The audio session of the application in focus could not be detected.");
return;
}
VolumeMixer.SetApplicationMute((int)processId, !muteStatus.Value);
Debug.WriteLine($"Current application '{processId}' has been {(muteStatus.Value ? "unmuted" : "muted")}.");
}
// Import the necessary Win32 API functions
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
}
}