Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Watcher Race Conditions & Ensured Application Hotkey Refreshes System Tray #124

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading