Skip to content

Commit

Permalink
fix: restore focus when switching desktops
Browse files Browse the repository at this point in the history
Use built-in hotkey Alt-Escape instead of manually invoking WinActivate.
Credits: pmb6tz/windows-desktop-switcher#77 (comment)
Restoring the windows focus is actually necessary because of the
WinActivate(Shell_TrayWnd) which in turn fixes the taskbar flashing.
  • Loading branch information
imawizard committed Feb 1, 2023
1 parent 60e607a commit 5621361
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
18 changes: 0 additions & 18 deletions lib/miguru/miguru.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,9 @@ class MiguruWM extends WMEvents {
switch event {
case EV_DESKTOP_CHANGED:
this._delayed.Drop("hide")
this._delayed.Drop("activate")

this.activeWsIdx := args.now
debug("Current desktop changed from {} to {}", args.was, args.now)

b := this._activate.Bind(this, this.activeMonitor, this.activeWsIdx)
this._delayed.Add(b, 250, "activate")
case EV_DESKTOP_RENAMED:
debug("Desktop {} was renamed to {}", args.desktop, args.name)
case EV_DESKTOP_CREATED:
Expand Down Expand Up @@ -392,19 +388,5 @@ class MiguruWM extends WMEvents {
window.workspace.Disappear(window.handle)
}

_activate(monitor, wsIdx) {
if wsIdx !== this.activeWsIdx {
debug("Don't activate because workspace changed")
return
} else if monitor !== this.activeMonitor {
debug("Don't activate because monitor changed")
return
}

ws := this._workspaces[monitor, wsIdx]
hwnd := ws.ActiveWindow
if hwnd && hwnd !== WinExist("A") {
WinActivate("ahk_id" hwnd)
}
}
}
6 changes: 5 additions & 1 deletion lib/vd/vd.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,17 @@ class VD {
if !desktop.Ptr {
return false
}

try {
; Fails if called when task view is open
WinActivate("ahk_class Shell_TrayWnd")
} catch TargetError {
; Do nothing
}
return this.managerInternal.SwitchDesktop(desktop)
this.managerInternal.SwitchDesktop(desktop)

; Switch to last active window.
Send("!{Esc}")
}

; Sends a window to a specific desktop. Creates new desktops on demand if
Expand Down

0 comments on commit 5621361

Please sign in to comment.