diff --git a/DAIRemote/DAIRemoteApplicationUI.cs b/DAIRemote/DAIRemoteApplicationUI.cs index 63d803a..44f6dd8 100644 --- a/DAIRemote/DAIRemoteApplicationUI.cs +++ b/DAIRemote/DAIRemoteApplicationUI.cs @@ -35,14 +35,8 @@ public DAIRemoteApplicationUI() InitializeDisplayProfilesList(); // Initialize the form & listbox used for showing display profiles list // Listen for display profile changes - FileSystemWatcher displayProfileDirWatcher = new(DisplayConfig.GetDisplayProfilesDirectory()) - { - NotifyFilter = NotifyFilters.FileName - }; - displayProfileDirWatcher.Created += OnProfilesChanged; - displayProfileDirWatcher.Deleted += OnProfilesChanged; - displayProfileDirWatcher.Renamed += OnProfilesChanged; - displayProfileDirWatcher.EnableRaisingEvents = true; + DisplayProfileWatcher.Initialize(DisplayConfig.GetDisplayProfilesDirectory()); + DisplayProfileWatcher.ProfileChanged += OnProfilesChanged; } protected override void OnHandleCreated(EventArgs e) @@ -59,17 +53,10 @@ protected override void OnHandleCreated(EventArgs e) private void OnProfilesChanged(object sender, FileSystemEventArgs e) { - if (this.InvokeRequired) - { - this.BeginInvoke((MethodInvoker)delegate - { - InitializeDisplayProfilesLayouts(); - }); - } - else + this.BeginInvoke((MethodInvoker)delegate { InitializeDisplayProfilesLayouts(); - } + }); } private void InitializeDisplayProfilesLayouts() @@ -132,10 +119,15 @@ private void DAIRemoteApplicationUI_FormClosing(object sender, FormClosingEventA private void InitializeAudioDropDown() { + int panelWidth = (int)(this.ClientSize.Width * 0.8); // 80% of form width + int panelHeight = (int)(this.ClientSize.Height * 0.27); // 27% of form height + int panelX = 9; // Offset from the left + int panelY = this.ClientSize.Height - panelHeight - 16; // Offset from the bottom + this.audioFormPanel = new Panel { - Location = new System.Drawing.Point(12, 460), - Size = new System.Drawing.Size(760, 370), + Location = new System.Drawing.Point(panelX, panelY), + Size = new System.Drawing.Size(panelWidth, panelHeight), }; audioForm = AudioOutputForm.GetInstance(audioManager); @@ -276,6 +268,7 @@ private void BtnSetDisplayProfileHotkey_click(object sender, EventArgs e) if (!string.IsNullOrEmpty(profile)) { trayIconManager.GetHotkeyManager().ShowHotkeyInput(profile, () => DisplayConfig.SetDisplaySettings(profile)); + trayIconManager.RefreshSystemTray(); } } } diff --git a/DAIRemote/DisplayProfileWatcher.cs b/DAIRemote/DisplayProfileWatcher.cs new file mode 100644 index 0000000..5c8bb1b --- /dev/null +++ b/DAIRemote/DisplayProfileWatcher.cs @@ -0,0 +1,33 @@ +public static class DisplayProfileWatcher +{ + private static FileSystemWatcher fileSystemWatcher; + private static readonly object LockObject = new(); + + public static event FileSystemEventHandler ProfileChanged; + + public static void Initialize(string directoryPath) + { + if (fileSystemWatcher != null) return; + + lock (LockObject) + { + if (fileSystemWatcher == null) + { + fileSystemWatcher = new FileSystemWatcher(directoryPath) + { + NotifyFilter = NotifyFilters.FileName, + EnableRaisingEvents = true + }; + + fileSystemWatcher.Created += OnProfileChanged; + fileSystemWatcher.Deleted += OnProfileChanged; + fileSystemWatcher.Renamed += OnProfileChanged; + } + } + } + + private static void OnProfileChanged(object sender, FileSystemEventArgs e) + { + ProfileChanged?.Invoke(sender, e); + } +} \ No newline at end of file diff --git a/DAIRemote/SystemTray.cs b/DAIRemote/SystemTray.cs index 9b81fd7..6cdaaa1 100644 --- a/DAIRemote/SystemTray.cs +++ b/DAIRemote/SystemTray.cs @@ -63,14 +63,8 @@ private void InitializeTrayIcon() }; // Listen for display profile changes - FileSystemWatcher displayProfileDirWatcher = new(DisplayConfig.GetDisplayProfilesDirectory()) - { - NotifyFilter = NotifyFilters.FileName - }; - displayProfileDirWatcher.Created += OnProfilesChanged; - displayProfileDirWatcher.Deleted += OnProfilesChanged; - displayProfileDirWatcher.Renamed += OnProfilesChanged; - displayProfileDirWatcher.EnableRaisingEvents = true; + DisplayProfileWatcher.Initialize(DisplayConfig.GetDisplayProfilesDirectory()); + DisplayProfileWatcher.ProfileChanged += OnProfilesChanged; // Listen to AudioDeviceManager's event handler for changes audioManager.AudioDevicesUpdated += OnAudioDevicesChanged; @@ -93,34 +87,28 @@ private ContextMenuStrip CreateTrayMenu() return menu; } - private void OnProfilesChanged(object sender, FileSystemEventArgs e) + public void RefreshSystemTray() { - if (form.InvokeRequired) + form.BeginInvoke((MethodInvoker)delegate { - form.BeginInvoke((MethodInvoker)delegate - { - PopulateTrayMenu(trayMenu); - }); - } - else + PopulateTrayMenu(trayMenu); + }); + } + + private void OnProfilesChanged(object sender, FileSystemEventArgs e) + { + form.BeginInvoke((MethodInvoker)delegate { PopulateTrayMenu(trayMenu); - } + }); } private void OnAudioDevicesChanged(List devices) { - if (form.InvokeRequired) - { - form.BeginInvoke((MethodInvoker)delegate - { - PopulateTrayMenu(trayMenu); - }); - } - else + form.BeginInvoke((MethodInvoker)delegate { PopulateTrayMenu(trayMenu); - } + }); } private void PopulateTrayMenu(ContextMenuStrip menu) @@ -239,7 +227,6 @@ private void PopulateTrayMenu(ContextMenuStrip menu) ToolStripMenuItem profileSaveItem = new(fileName, monitorIcon, (sender, e) => SaveProfile(profile)); - // Setting up hotkey funtionality for each profile hotkeyText = hotkeyManager.hotkeyConfigs.ContainsKey(fileName) ? $" ({hotkeyManager.GetHotkeyText(hotkeyManager.hotkeyConfigs[fileName])})"