Skip to content
Open
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
48 changes: 14 additions & 34 deletions Source/App/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Globalization;
using System.Linq;
using System.Security.Policy;
using System.Collections.Concurrent;

namespace WindowsVirtualDesktopHelper {

Expand Down Expand Up @@ -49,7 +50,7 @@ internal class HotKeyAction {
private KeyboardHook KeyboardHooksJumpToDesktop = null;
private KeyboardHook _keyboardHooks = null;
private List<HotKeyAction> _keyboardHooksHotKeysAndActions = new List<HotKeyAction>(); // the registered hotkey actions
private Dictionary<int, IntPtr> VDDToLastFocusedWin = new Dictionary<int, IntPtr>();
private ConcurrentDictionary<int, IntPtr> VDDToLastFocusedWin = new ConcurrentDictionary<int, IntPtr>();
public IntPtr LastForegroundhWnd = IntPtr.Zero; //TODO: this should be private
public List<string> FGWindowHistory = new List<string>(); //TODO: this should be private // needed to detect if Task View was open
private List<int> _desktopNumberHistory = new List<int>(); // stores a list of most recent desktop numbers used
Expand Down Expand Up @@ -97,7 +98,6 @@ public App() {
}

#endregion

#region Virtual Desktop Methods

public void LoadVDAPI() {
Expand Down Expand Up @@ -171,12 +171,11 @@ private void _MonitorVDSwitch() {
try {
var newVDDisplayNumber = this.GetVDDisplayNumber(false);
if(newVDDisplayNumber != this.CurrentVDDisplayNumber) {
this.CurrentVDDisplayName = this.GetVDDisplayName(false);
this.CurrentVDDisplayNumber = newVDDisplayNumber;
//Util.Logging.WriteLine("Switched to " + this.CurrentVDDisplayNumber);
VDSwitchedSafe();
} else {
//storeLastWinFocused();
this.AppForm.BeginInvoke((Action)(() => {
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BeginInvoke is asynchronous and may cause race conditions. The VDSwitchedSafe() call inside the invoke block could execute after subsequent desktop switches, leading to incorrect state updates. Consider using Invoke for synchronous execution or adding proper synchronization.

Suggested change
this.AppForm.BeginInvoke((Action)(() => {
this.AppForm.Invoke((Action)(() => {

Copilot uses AI. Check for mistakes.
this.CurrentVDDisplayName = this.GetVDDisplayName(false);
this.CurrentVDDisplayNumber = newVDDisplayNumber;
VDSwitchedSafe();
}));
}
System.Threading.Thread.Sleep(100);
} catch(Exception e) {
Expand Down Expand Up @@ -236,6 +235,7 @@ public void VDSwitchedSafe() {
public void SwitchDesktopBackward() {
// We try the virtual desktop implementation API, but fallback to shortcut keys if it fails...
try {
_storeLastWinFocused();
VDAPI.SwitchBackward();
} catch(Exception e) {
Util.Logging.WriteLine("App: Error: SwitchDesktopBackward (VDAPI.SwitchBackward()): " + e.Message);
Expand All @@ -246,6 +246,7 @@ public void SwitchDesktopBackward() {
public void SwitchDesktopForward() {
// We try the virtual desktop implementation API, but fallback to shortcut keys if it fails...
try {
_storeLastWinFocused();
VDAPI.SwitchForward();
} catch(Exception e) {
Util.Logging.WriteLine("App: Error: SwitchDesktopForward (VDAPI.SwitchForward()): " + e.Message);
Expand Down Expand Up @@ -309,46 +310,25 @@ private void _MonitorFGWindowName() {
}


public void MonitorFocusedWindow() {
var thread = new Thread(new ThreadStart(_monitorFocusedWindow));
thread.Start();
}

private void _monitorFocusedWindow() {
while(true) {
try {
_storeLastWinFocused();
System.Threading.Thread.Sleep(200);
} catch(Exception e) {
Util.Logging.WriteLine("App: Error: _monitorFocusedWindow: " + e.Message);
System.Threading.Thread.Sleep(1000);
}
}
}

private void _storeLastWinFocused() {
IntPtr hWnd = Util.OS.GetForegroundWindow();
if(hWnd != IntPtr.Zero) {
var fgWindowName = Util.OS.GetForegroundWindowName();
var fgWindowType = Util.OS.GetHandleWndType(hWnd);
if(fgWindowType == "Shell_TrayWnd") return; // we ignore the icon tray, since this takes the focus away when we click the prev/next arrows
var displayNumber = (int)this.GetVDDisplayNumber(false);
if(VDDToLastFocusedWin.ContainsKey(displayNumber)) {
VDDToLastFocusedWin[displayNumber] = hWnd;
} else {
VDDToLastFocusedWin.Add(displayNumber, hWnd);
}
VDDToLastFocusedWin.AddOrUpdate(displayNumber, hWnd, (key, existingValue) => hWnd);
//Console.WriteLine($"store: display {displayNumber} hwnd {hWnd} ({fgWindowType})");
}
}

private void _restorePrevWinFocus() {
var displayNumber = (int)this.GetVDDisplayNumber(false);
if(VDDToLastFocusedWin.ContainsKey(displayNumber)) {
IntPtr lastWindowHandle = VDDToLastFocusedWin[displayNumber];
if(Util.OS.IsWindow(lastWindowHandle)) {
IntPtr lastWindowHandle;
if (VDDToLastFocusedWin.TryGetValue(displayNumber, out lastWindowHandle)) {
System.Threading.Thread.Sleep(50);
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded sleep duration (50ms) is a magic number that makes the timing behavior unclear. Consider defining this as a named constant or making it configurable to improve maintainability.

Copilot uses AI. Check for mistakes.
if (Util.OS.IsWindow(lastWindowHandle)) {
Util.OS.SetForegroundWindow(lastWindowHandle);
//Console.WriteLine("restore: "+ displayNumber + " "+ lastWindowHandle);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Source/Forms/AppForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ private void AppForm_Load(object sender, EventArgs e) {
App.Instance.MonitorSystemThemeSwitch();
App.Instance.MonitorVDisplayCount();
App.Instance.MonitorFGWindowName();
App.Instance.MonitorFocusedWindow();

App.Instance.UIUpdate();
}
Expand Down