Skip to content

Commit

Permalink
Fixed Watcher Race Conditions & Ensured Application Hotkey Refreshes …
Browse files Browse the repository at this point in the history
…System Tray (#124)

* Tried ensuring the audio drop down takes into account different resolutions.
* Fixed race conditions occurring from FileSystemWatcher for the application UI and system tray.
* Ensured system tray gets refreshed once hotkeys are set through the application UI.
  • Loading branch information
Fahim-zzz authored Nov 30, 2024
1 parent bb6bad6 commit 30028f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
31 changes: 12 additions & 19 deletions DAIRemote/DAIRemoteApplicationUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
}
33 changes: 33 additions & 0 deletions DAIRemote/DisplayProfileWatcher.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
41 changes: 14 additions & 27 deletions DAIRemote/SystemTray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string> devices)
{
if (form.InvokeRequired)
{
form.BeginInvoke((MethodInvoker)delegate
{
PopulateTrayMenu(trayMenu);
});
}
else
form.BeginInvoke((MethodInvoker)delegate
{
PopulateTrayMenu(trayMenu);
}
});
}

private void PopulateTrayMenu(ContextMenuStrip menu)
Expand Down Expand Up @@ -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])})"
Expand Down

0 comments on commit 30028f5

Please sign in to comment.