0
votes

My code does the following:

  • When a google.com window becomes active, it will show you a simple GUI.

  • Then, if you switch to another tab, the GUI will be hidden.[1]

[1]. Note, that it works as well for the tabs opened in another browser window. For example, you may have 2 browser windows opened - the first window with google.com and apple.com, and the second window with amazon.com. You can click apple.com tab as well as amazon.com tab, and in both cases the GUI will be hidden. Then, if you click google.com tab, the GUI will be shown again.

The problem is, that I cannot figure out how to force the GUI to be hidden when the window (which contains google.com tab) is minimized. (And also, I cannot get rid of feeling that there are another errors, that are not visible while testing, but actually exist).

To sum this up, what I want is this:

As soon as a non-google window or tab is clicked/activated (excluding the AHK Gui) the Gui should be hidden and as soon as any google-window/tab is clicked/activated the Gui should be shown.

Here is my code:

#Persistent
SetTimer, Show_Gui, 300
Return

Show_Gui:
IfWinNotActive, foo
IfWinNotActive, Google
{
    Gui, Destroy
    Return
}
; Otherwise:
SetTimer, Show_Gui, Off
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, Show,, foo
WinWaitNotActive, foo
WinWaitNotActive, Google
SetTimer, Show_Gui, On
Return

Test1:
; do something
Return

Test2:
; do something
Return

Edit. Here is the code as discussed in comments to Forivin's post. It is not part of the actual question.

#Persistent

Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2

GroupAdd, myGroup, Google
GroupAdd, myGroup, foo

WinWaitActive, Google
Gui, Show,, foo
SetTimer, Gui_Visibility_Handler, 300
Return

Gui_Visibility_Handler:
    WinGet, googleWinState, MinMax, Google
    If (googleWinState = -1 || !WinExist("Google")) ;if window minimized or not existent
    {
        Gui, Hide
        If (googleWinState = -1) ;workaround for a bug where windows thinks
            Send, !{Esc}         ;that the minimized window is active
        WinWaitActive, Google
        Gui, Show
        Return
    }
    IfWinActive, Google
    {
        Gui, Show
        Return
    }
    IfWinNotActive, ahk_group MyGroup
    {
        Gui, Hide
        Return
    }
Return

Test1:
;do something
Return

Test2:
;do something
Return
2

2 Answers

2
votes
#Persistent
SetTitleMatchMode, 2

GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe chrome.exe
GroupAdd, Amazon_Apple_Group, Apple - Mozilla Firefox ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Apple - Google Chrome ahk_exe chrome.exe

Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
SetTimer, Show_Gui, 300
Return


Show_Gui:
IfWinActive, ahk_group Amazon_Apple_Group
{
SetTimer, Show_Gui, Off
Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On
}
Return


Test1:
Gui, Submit
; do sth
Return

Test2:
Gui, Submit
; do sth
return

https://autohotkey.com/docs/commands/GroupAdd.htm

"If a timer is disabled while its subroutine is currently running, that subroutine will continue until it completes." https://autohotkey.com/docs/commands/SetTimer.htm#Remarks

"SetTimer, Show_Gui, Off" turns the timer off until the following subroutine

Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On  

completes.

The subroutine automatically turns the timer on after completion.

1
votes

EDIT: Here is the new code as discussed in the comments:

#Persistent

Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, +hwndAhkGui

lastActive := WinExist("A")

SetTimer, Active_Window_Memory
SetTimer, Gui_Visibility_Handler, 300

Return

Gui_Visibility_Handler:
    WinGet, googleWinState, MinMax, Google
    If (googleWinState != -1 && IsGoogleWindow(currentActive)) ;if google is not minimized and: (google is active or: (ahk gui is active and google was active before that))
    {
        Gui, Show
    }
    Else If (currentActive = AhkGui && IsGoogleWindow(lastActive))
    {
        ;No need to do anything
    }
    Else
    {
        Gui, Hide
    }
Return

IsGoogleWindow(hWnd) {
    WinGetTitle, title, ahk_id %hWnd%
    If (InStr(title, " - Google Chrome"))
    {
        title := RegExReplace(title, " - Google Chrome$", "") ;remove the " - Google Chrome" from the title
    } 

    If (InStr(title, "Google"))
    {
        Return True
    }

    Return False
}

Active_Window_Memory:
    currentActive := WinExist("A")
    WinWaitNotActive, ahk_id %currentActive%
    lastActive := currentActive
Return

Test1:
; do something
Return

Test2:
; do something
Return