title: Hide GUI when target window not active or minimized
body: <p>My code does the following:</p>
  
  <ul>
  <li><p>When a google.com window becomes active, it will show you a simple GUI.</p></li>
  <li><p>Then, if you switch to another tab, the GUI will be hidden.[1]</p></li>
  </ul>
  
  <p>[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 <code>google.com</code> and <code>apple.com</code>, and the second window with <code>amazon.com</code>. You can click <code>apple.com</code> tab as well as <code>amazon.com</code> tab, and in both cases the GUI will be hidden. Then, if you click <code>google.com</code> tab, the GUI will be shown again.</p>
  
  <p>The problem is, that I cannot figure out how to force the GUI to be hidden when the window (which contains <code>google.com</code> 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).  </p>
  
  <p>To sum this up, what I want is this:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><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
  </code></pre>
  
  <hr>
  
  <p>Edit. Here is the code as discussed in comments to Forivin's post. It is not part of the actual question.</p>
  
  <pre><code>#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
  </code></pre>