Skip to content

Commit

Permalink
v1.3
Browse files Browse the repository at this point in the history
- Legacy handler no longer shows on startup if MTT is unexpectedly closed with no windows hidden.
  • Loading branch information
sandwichdoge authored Apr 16, 2018
1 parent 62ee3e4 commit 81dceae
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions MinimizeToTray.au3
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
;//Minimize to tray
;//[email protected]
#include <Array.au3>
$sVersion = "1.2"
$sVersion = "1.3"

HotKeySet("!{f1}", "HideCurrentWnd")
HotKeySet("!{f2}", "RestoreLastWnd")
Expand All @@ -8,9 +10,9 @@ HotKeySet("+{esc}", "ExitS")


;//$aHiddenWndList = Array that contains handles of all hidden windows.
;//$aTrayItems_Wnd = Array that contains handles of tray items that indicate names of hidden windows.
;//$aTrayItemHandles = Array that contains tray items that indicate names of hidden windows.
;//Elements of these 2 arrays must be perfectly in sync with each other.
Global $aHiddenWndList[0] = [], $aTrayItems_Wnd[0] = []
Global $aHiddenWndList[0] = [], $aTrayItemHandles[0] = []
Global $hLastWnd;//Handle of the last window that was hidden

Opt('TrayAutoPause', 0)
Expand All @@ -22,20 +24,22 @@ TrayCreateItem("");//Create a straight line
$hTrayHelp = TrayCreateItem("Quick manual")
$hTrayExit = TrayCreateItem("Exit (Shift+Esc)")
TrayTip("MinimizeToTray " & $sVersion, "Press [Alt+F1] to hide currently active Window." & @CRLF _
& "Press [Alt+F2] to restore last hidden Window." & @CRLF _
& "Hidden Windows are stored in MTT tray icon.", 5)
& "Press [Alt+F2] to restore last hidden Window." & @CRLF _
& "Hidden Windows are stored in MTT tray icon.", 5)

;//Legacy windows from last run are loaded on startup if available, this should only happen if MTT was unexpectedly closed while some windows were still hidden.
$aPrevWndTitleList = FileReadToArray("MTTlog.txt")
If not @error Then
For $i = 0 To UBound($aPrevWndTitleList) - 1
$hTrayWnd = TrayCreateItem($aPrevWndTitleList[$i] & " - Legacy", -1, 0);, $hTrayMenuShowSelectWnd)
_ArrayAdd($aTrayItems_Wnd, $hTrayWnd)
_ArrayAdd($aHiddenWndList, WinGetHandle($aPrevWndTitleList[$i]))
Next
If UBound($aPrevWndTitleList) Then
TrayTip("", "You have " & UBound($aPrevWndTitleList) & " legacy Window(s) waiting to be restored!", 4)
EndIf
If Not @error Then
For $i = 0 To UBound($aPrevWndTitleList) - 1
If StringLen($aPrevWndTitleList[$i]) >= 1 Then
$hTrayWnd = TrayCreateItem($aPrevWndTitleList[$i] & " - Legacy", -1, 0);, $hTrayMenuShowSelectWnd)
_ArrayAdd($aTrayItemHandles, $hTrayWnd)
_ArrayAdd($aHiddenWndList, WinGetHandle($aPrevWndTitleList[$i]))
EndIf
Next
If UBound($aTrayItemHandles) Then
TrayTip("", "You have " & UBound($aTrayItemHandles) & " legacy Window(s) waiting to be restored!", 4)
EndIf
EndIf

;//Main Loop
Expand All @@ -49,8 +53,8 @@ While 1
Case $hTrayHelp
Help()
EndSwitch
For $i = 0 To UBound($aTrayItems_Wnd) - 1
If $hTrayMsg = $aTrayItems_Wnd[$i] Then
For $i = 0 To UBound($aTrayItemHandles) - 1
If $hTrayMsg = $aTrayItemHandles[$i] Then
RestoreWnd($aHiddenWndList[$i])
ExitLoop
EndIf
Expand All @@ -62,58 +66,58 @@ WEnd
Func RestoreLastWnd()
;//Restore last hidden window.
RestoreWnd($hLastWnd)
EndFunc
EndFunc ;==>RestoreLastWnd

Func RestoreWnd($hfWnd)
Local $nIndex = _ArraySearch($aHiddenWndList, $hfWnd)
WinSetState($hfWnd, "", @SW_SHOW)
If $nIndex >= 0 Then
TrayItemDelete($aTrayItems_Wnd[$nIndex])
TrayItemDelete($aTrayItemHandles[$nIndex])
_ArrayDelete($aHiddenWndList, $nIndex)
_ArrayDelete($aTrayItems_Wnd, $nIndex)
_ArrayDelete($aTrayItemHandles, $nIndex)
;//Delete window's name from log file
$sLog = FileRead("MTTlog.txt")
$sLogN = StringReplace($sLog, WinGetTitle($hfWnd), "")
FileWrite(FileOpen("MTTlog.txt", 2), $sLogN)
EndIf
EndFunc
EndFunc ;==>RestoreWnd

Func HideWnd($hfWnd)
WinSetState($hfWnd, "", @SW_HIDE)
_ArrayAdd($aHiddenWndList, $hfWnd)
$hTrayWnd = TrayCreateItem(WinGetTitle($hfWnd), -1, 0);, $hTrayMenuShowSelectWnd)
_ArrayAdd($aTrayItems_Wnd, $hTrayWnd)
_ArrayAdd($aTrayItemHandles, $hTrayWnd)
;//Write window's name to log file for legacy restoration in case of unexpected crash.
FileWrite("MTTlog.txt", WinGetTitle($hfWnd) & @CRLF)
$hLastWnd = $hfWnd
EndFunc
EndFunc ;==>HideWnd

Func HideCurrentWnd()
;//Hide currently active window.
HideWnd(WinGetHandle("[ACTIVE]"))
EndFunc
EndFunc ;==>HideCurrentWnd

Func RestoreAllWnd()
;//Show all windows hidden during this session.
For $i = 0 To UBound($aHiddenWndList) - 1
WinSetState($aHiddenWndList[$i], "", @SW_SHOW)
Next
For $i = 0 To UBound($aTrayItems_Wnd) - 1
TrayItemDelete($aTrayItems_Wnd[$i])
For $i = 0 To UBound($aTrayItemHandles) - 1
TrayItemDelete($aTrayItemHandles[$i])
_ArrayDelete($aHiddenWndList, $i)
Next
Global $aTrayItems_Wnd[0] = []
Global $aTrayItemHandles[0] = []
FileDelete("MTTlog.txt")
EndFunc
EndFunc ;==>RestoreAllWnd

Func Help()
MsgBox(64, "MinimizeToTray " & $sVersion, "Press [Alt+F1] to hide currently active Window." & @CRLF _
& "Press [Alt+F2] to restore last hidden Window." & @CRLF _
& "Hidden Windows are stored in MTT tray icon." & @CRLF & @CRLF _
& "evorlet@gmail.com")
EndFunc
& "Press [Alt+F2] to restore last hidden Window." & @CRLF _
& "Hidden Windows are stored in MTT tray icon." & @CRLF & @CRLF _
& "sandwichdoge@gmail.com")
EndFunc ;==>Help

Func ExitS()
RestoreAllWnd()
Exit
EndFunc
EndFunc ;==>ExitS

0 comments on commit 81dceae

Please sign in to comment.