Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #67 from sdias/fix-window-focus
Browse files Browse the repository at this point in the history
Fixed focus management.
  • Loading branch information
sdias authored Jul 1, 2017
2 parents 8b7ed82 + 026c79e commit ade88bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Binary file modified libraries/virtual-desktop-accessor.dll
Binary file not shown.
32 changes: 28 additions & 4 deletions virtual-desktop-enhancer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ global RegisterPostMessageHookProc := DllCall("GetProcAddress", Ptr, hVirtualDes
global UnregisterPostMessageHookProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "UnregisterPostMessageHook", "Ptr")
global GetCurrentDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "GetCurrentDesktopNumber", "Ptr")
global GetDesktopCountProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "GetDesktopCount", "Ptr")
global IsWindowOnCurrentVirtualDesktopProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsWindowOnCurrentVirtualDesktop", "Ptr")
global IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsWindowOnDesktopNumber", "Ptr")
global MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "MoveWindowToDesktopNumber", "Ptr")
global IsPinnedWindowProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsPinnedWindow", "Ptr")
global PinWindowProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "PinWindow", "Ptr")
Expand Down Expand Up @@ -323,12 +323,14 @@ OnTogglePinAppPress() {
}

OnDesktopSwitch(n:=1) {
; Give focus first, then display the popup, otherwise the popup could
; steal the focus from the legitimate window until it disappears.
_FocusIfRequested()
if (TooltipsEnabled) {
_ShowTooltipForDesktopSwitch(n)
}
_ChangeAppearance(n)
_ChangeBackground(n)
_FocusIfRequested()

if (previousDesktopNo) {
_RunProgramWhenSwitchingFromDesktop(previousDesktopNo)
Expand Down Expand Up @@ -568,16 +570,38 @@ _ChangeAppearance(n:=1) {
}
}

; Only give focus to the foremost window if it has been requested.
_FocusIfRequested() {
if (doFocusAfterNextSwitch) {
_Focus()
doFocusAfterNextSwitch=0
}
}

; Give focus to the foremost window on the desktop.
_Focus() {
WinActivate, ahk_class Shell_TrayWnd
SendEvent !{Esc}
foremostWindowId := _GetForemostWindowIdOnDesktop(_GetCurrentDesktopNumber())
WinActivate, ahk_id %foremostWindowId%
}

; Select the ahk_id of the foremost window in a given virtual desktop.
_GetForemostWindowIdOnDesktop(n) {
if (n == 0) {
n := 10
}
; Desktop count starts at 1 for this script, but at 0 for Windows.
n -= 1

; winIDList contains a list of windows IDs ordered from the top to the bottom for each desktop.
WinGet winIDList, list
Loop % winIDList {
windowID := % winIDList%A_Index%
windowIsOnDesktop := DllCall(IsWindowOnDesktopNumberProc, UInt, WindowID, UInt, n)
; Select the first (and foremost) window which is in the specified desktop.
if (WindowIsOnDesktop == 1) {
return WindowID
}
}
}

_ShowTooltip(message:="") {
Expand Down

0 comments on commit ade88bb

Please sign in to comment.