From b852716638d05305621de1447507f3de20205227 Mon Sep 17 00:00:00 2001 From: Forward <30380883+stepforwards@users.noreply.github.com> Date: Thu, 13 Aug 2020 20:17:42 +0800 Subject: [PATCH] Update virgo.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add: Restore focus when switching desktops --- virgo.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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); }