FanaticGuru wrote: ↑04 Nov 2022, 19:20
I tried adding in "Suspend" and "Pause". At first, I thought this is simple and works great. Wondered why I did not include these in the first place. Then after some testing this time, I realized the problem. When something needs to be removed from the tray menu, the entire menu and submenus are deleted and then rebuilt. This causes the status of the checkmark for "Suspend" and "Pause" to be lost as the status is not actually stored, just toggled back and forth in the menu. At some point I will probably see about working around this issue so that "Suspend" and "Pause" can be added without the checkmarks getting out of sync.
Yes, true! Didn't really test that much. So I thought adding Pause and Suspend statuses. Then I realized that you have another great script - Hotkey Help - that can read other script's pause and suspend status. That seemed more interesting. So I made a function that uses a part of that script.
I made 3 changes:
1) added call to Function PausedOrSuspended to MenuBuild
Code: Select all
for Script_Name, Script in Scripts
if Script.Status
{
PID := Script.PID
try Menu, SubMenu_%PID%, DeleteAll
Menu, SubMenu_%PID%, Add, View Lines, ScriptCommand
Menu, SubMenu_%PID%, Add, View Variables, ScriptCommand
Menu, SubMenu_%PID%, Add, View Hotkeys, ScriptCommand
Menu, SubMenu_%PID%, Add, View Key History, ScriptCommand
Menu, SubMenu_%PID%, Add
Menu, SubMenu_%PID%, Add, &Open, ScriptCommand
Menu, SubMenu_%PID%, Add, &Edit, ScriptCommand
Menu, SubMenu_%PID%, Add, &Pause, ScriptCommand ;added command
Menu, SubMenu_%PID%, Add, &Suspend Hotkeys, ScriptCommand ;added command
Menu, SubMenu_%PID%, Add, &Reload, ScriptCommand
Menu, SubMenu_%PID%, Add, &Exit, ScriptCommand
Menu, Tray, Add, %Script_Name%, :SubMenu_%PID%
PausedOrSuspended(Script_Name, PID) ;added function
}
2) added MenuBuild to else in scriptcommand:
Code: Select all
else
{
cmd := Cmd_%cmd%
PostMessage, 0x111, %cmd%,,,ahk_pid %Pid%
if (cmd = "65403") or if (cmd = "65404") ;Pause or Suspend
gosub MenuBuild
}
3) added function PausedOrSuspended:
Code: Select all
PausedOrSuspended(Script_Name, PID) {
SetTitleMatchMode, 2
WinGet, script_id, ID, \%Script_Name%.ahk - ahk_class AutoHotkey
SendMessage, 0x211,,,, ahk_id %script_id% ; WM_ENTERMENULOOP
SendMessage, 0x212,,,, ahk_id %script_id% ; WM_EXITMENULOOP
; Get script status from its main menu.
mainMenu := DllCall("GetMenu", "uint", script_id)
fileMenu := DllCall("GetSubMenu", "uint", mainMenu, "int", 0)
isPaused := DllCall("GetMenuState", "uint", fileMenu, "uint", 4, "uint", 0x400) >> 3 & 1
isSuspended := DllCall("GetMenuState", "uint", fileMenu, "uint", 5, "uint", 0x400) >> 3 & 1
DllCall("CloseHandle", "uint", fileMenu)
DllCall("CloseHandle", "uint", mainMenu)
if isPaused
Menu, SubMenu_%PID%, Check, &Pause
if isSuspended
Menu, SubMenu_%PID%, Check, &Suspend Hotkeys
;3 - default suspend icon, 4 - default pause icon, 5 - default suspend and pause icon
if isPaused && isSuspended
ikoon := 5
else if isPaused
ikoon := 4
else if isSuspended
ikoon := 3
if ikoon
Menu, Tray, Icon, %Script_Name%, %A_AhkPath%, %ikoon%
sleep 1
}
Function PausedOrSuspended also adds respective icon to the menu before script's name.
Everything seems to be working so far. The line
sleep 1 is required. Without it the last script always gets marked as paused and suspended. Other commands also work inconsistently: sometimes refreshing a script gets marked as paused and suspended, sometimes not. Sometimes launching a script from Load menu gets marked as paused and suspended. Cant figure out why. Works fine with sleep.
I have to admit I don't know what
>> 3 & 1 means at the end of DllCall. I have seen this line written like this also:
(DllCall("GetMenuState", "uint", hMenu, "uint", 4, "uint", 0x400) & 0x8)!= 0
I know that isPaused and IsSuspended will aways be 1 if the script_id is missing (if the script was closed not using the main script).
I tried to check if scripts PID is the same as stored in the array scripts using:
Code: Select all
WinGet, checkPID, PID, \%ScriptName%.ahk - ahk_class AutoHotkey
or
Code: Select all
WinGet, checkPID, PID, ahk_id %ID%
But for some reason checkPID is always blank. Don't know why.