diff --git a/virgo.c b/virgo.c index 2359d80..a042296 100644 --- a/virgo.c +++ b/virgo.c @@ -23,6 +23,7 @@ typedef struct { HWND *windows; unsigned count; + HWND focus; } Windows; typedef struct { @@ -41,6 +42,18 @@ typedef struct { Trayicon trayicon; } Virgo; +static void save_current_desktops_focus(Windows* desktops) { + desktops->focus = GetForegroundWindow(); +} + +static unsigned is_valid_window(HWND hwnd); +static void restore_current_desktops_focus(Windows* desktops) { + if (!desktops->focus || !is_valid_window(desktops->focus)) { + return; + } + SetForegroundWindow(desktops->focus); +} + static void *stb__sbgrowf(void *arr, unsigned increment, unsigned itemsize) { unsigned dbl_cur = arr ? 2 * stb__sbm(arr) : 0; @@ -291,8 +304,10 @@ static void virgo_go_to_desk(Virgo *v, unsigned desk) return; } virgo_update(v); + save_current_desktops_focus(&v->desktops[v->current]); windows_hide(&v->desktops[v->current]); windows_show(&v->desktops[desk]); + restore_current_desktops_focus(&v->desktops[desk]); v->current = desk; trayicon_set(&v->trayicon, v->current + 1); }