Skip to content

Commit

Permalink
✨ singleActive mode works with minimize
Browse files Browse the repository at this point in the history
  • Loading branch information
john-walks-slow committed Nov 22, 2023
1 parent 1e41854 commit c43c71d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 55 deletions.
1 change: 0 additions & 1 deletion scripts/setupShortcuts.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ setupShortcuts(shortcutConfig) {
}
_setupShortcut(entry) {
global config
global activatedWnd
if (entry["hotkey"] == "") {
return
}
Expand Down
130 changes: 76 additions & 54 deletions scripts/toggleWnd.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

wndHandlers := Map()
lastActive := 0
activatedWnd := 0
activatedWnds := []

timerCallback() {
global activatedWnd
global activatedWnds
global lastActive
if (activatedWnd) {
if (activatedWnds) {
try {
currentWnd := WinGetID("A")
if (activatedWnd !== currentWnd) {
if (!hasVal(activatedWnds, currentWnd)) {
lastActive := currentWnd
}
}
}
}
startTimer() {
SetTimer(timerCallback, 500)
; SetTimer(timerCallback, 500)
}
stopTimer() {
SetTimer(timerCallback, 0)
; SetTimer(timerCallback, 0)
}


; Toggle between hidden and shown
toggleWnd(id, entry := unset) {
global wndHandlers
global activatedWnd
global activatedWnds
global lastActive
if (id && WinExist(id)) {
if (!config["misc"]["minimizeInstead"]) {
Expand All @@ -46,9 +46,16 @@ toggleWnd(id, entry := unset) {
}
}
else if (IsSet(entry)) {
if (activatedWnd) {
_hide(activatedWnd)
if (config["misc"]["singleActiveWindow"] && activatedWnds.Length > 0) {
if (!config["misc"]["minimizeInstead"])
_hide(activatedWnds[1], false)
else
_minimize(activatedWnds[1])
}
_run(id, entry)
}
_run(id, entry) {

Run(entry["run"])
if (entry["wnd_title"] !== "") {
id := WinWait(entry["wnd_title"])
Expand All @@ -59,82 +66,97 @@ toggleWnd(id, entry := unset) {
}
id := WinGetLatest()
}
activatedWnd := id
activatedWnds.push(id)
}
_hide(id, restoreLastActive := true) {
try {
WinHide(id)
}
activatedWnd := 0
try {
if (restoreLastActive && lastActive) {
WinActivate(lastActive)
deleteVal(activatedWnds, id)
try {
if (restoreLastActive) {
if (activatedWnds.Length > 0) {
WinActivate(activatedWnds[1])
}
else if (lastActive)
WinActivate(lastActive)
}
}
}
lastActive := lastActive
; Handle exit, try to reuse handler
if (wndHandlers.Get(String(id), false)) {
exitHandler := wndHandlers[String(id)]
} else {
; id is remembered in closure
exitHandler := (e, c) {
try {
isVisible := WinGetStyle(id) & 0x10000000
if (!isVisible) {
WinShow(id)

; Handle exit, try to reuse handler
if (wndHandlers.Get(String(id), false)) {
exitHandler := wndHandlers[String(id)]
} else {
; id is remembered in closure
exitHandler := (e, c) {
try {
isVisible := WinGetStyle(id) & 0x10000000
if (!isVisible) {
WinShow(id)
}
}
}
; Keep a record of bound exitHandlers
wndHandlers.Set(String(id), exitHandler)
}
; Keep a record of bound exitHandlers
wndHandlers.Set(String(id), exitHandler)
OnExit(exitHandler, 1)
OnError(exitHandler, 1)
}
OnExit(exitHandler, 1)
OnError(exitHandler, 1)

}
_show(id) {
lastlastActive := lastActive
lastActive := WinGetID("A")
WinShow(id)
WinActivate(id)
if (config["misc"]["singleActiveWindow"]) {
if (activatedWnd && activatedWnd !== id) {
_hide(activatedWnd, false)
lastActive := lastlastActive
try {
try {
currentWnd := WinGetID("A")
if (!hasVal(activatedWnds, currentWnd)) {
lastActive := currentWnd
}
}
if (config["misc"]["singleActiveWindow"] && activatedWnds.Length > 0 && activatedWnds[1] !== id) {
_hide(activatedWnds[1], false)
}
WinShow(id)
WinActivate(id)

pushDedupe(activatedWnds, id)
; Remove exit handler
if (wndHandlers.Get(String(id), false)) {
OnExit(wndHandlers[String(id)], 0)
OnError(wndHandlers[String(id)], 0)
}
}
activatedWnd := id
; Remove exit handler
if (wndHandlers.Get(String(id), false)) {
OnExit(wndHandlers[String(id)], 0)
OnError(wndHandlers[String(id)], 0)
}
}
_minimize(id) {
try {
WinMinimize(id)
if (config["misc"]["singleActiveWindow"]) {
deleteVal(activatedWnds, id)
}
}
}
_restore(id) {
try {
WinRestore(id)
WinActivate(id)
; lastActive := WinGetID("A")
if (config["misc"]["singleActiveWindow"]) {
if (activatedWnd && activatedWnd !== id) {
_minimize(activatedWnd)
if (activatedWnds.Has(1)) {
if (activatedWnds[1] !== id) {
_minimize(activatedWnds[1])
}
}
}
activatedWnd := id
}

try {
if (WinGetMinMax(id) == -1)
WinRestore(id)
WinActivate(id)
activatedWnds := [id]
}
}
return id
}
clearWndHandlers() {
global wndHandlers
global activatedWnd
global activatedWnds
global lastActive
activatedWnd := 0
activatedWnds := []
lastActive := 0
for id, handler in wndHandlers {
try {
Expand Down

0 comments on commit c43c71d

Please sign in to comment.