Compare commits

...

2 Commits

Author SHA1 Message Date
cd32ec980a Fix swapping 2024-11-03 12:50:31 +01:00
0b1d073e4a Implement swap and refactor a lil 2024-11-03 12:44:33 +01:00
2 changed files with 61 additions and 16 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
main.log main.log
__debug_bin3523166607.exe

74
main.go
View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"slices" "slices"
"strconv"
"syscall" "syscall"
// _ "embed" // _ "embed"
@@ -42,43 +43,86 @@ func init() {
log.Lmicroseconds|log.Lshortfile) log.Lmicroseconds|log.Lshortfile)
} }
var processes = []uintptr{} var windows = make([]uintptr, 10, 10)
func main() { func main() {
go systray.Run(onReady, nil) go systray.Run(onReady, nil)
hook.Register(hook.KeyDown, []string{"alt", "-"}, func(e hook.Event) { hook.Register(hook.KeyDown, []string{"alt", "-"}, func(e hook.Event) {
hwnd, _, _ := GetForegroundWindow.Call() hwnd, _, _ := GetForegroundWindow.Call()
if slices.Contains(processes, hwnd) { TrackWindow(hwnd)
processes = slices.DeleteFunc(processes, func(i uintptr) bool { })
return i == hwnd
for i := 1; i <= 9; i++ {
tabIndex := i - 1
log.Printf("Registering tab %d", tabIndex)
hook.Register(hook.KeyDown, []string{"alt", strconv.Itoa(i)}, func(e hook.Event) {
TabTo(tabIndex)
})
hook.Register(hook.KeyDown, []string{"alt", "shift", strconv.Itoa(i)}, func(e hook.Event) {
Swap(tabIndex)
}) })
} else {
processes = append(processes, hwnd)
} }
log.Printf("%#v", processes)
})
hook.Register(hook.KeyDown, []string{"alt", "1"}, func(e hook.Event) {
TabTo(0)
})
s := hook.Start() s := hook.Start()
<-hook.Process(s) <-hook.Process(s)
} }
func TabTo(idx int) { func TabTo(idx int) {
log.Printf("TabTo: %d", idx) log.Printf("TabTo: %d", idx)
log.Printf("%+v", processes) log.Printf("%+v", windows)
if idx > len(processes) { if idx > cap(windows)-1 {
Warning.Printf("idx %d is greater than length of processes %d", idx, len(processes)) Warning.Printf("idx %d is greater than length of windows %d", idx, cap(windows))
return return
} }
hwnd := processes[idx] hwnd := windows[idx]
if hwnd == 0 { if hwnd == 0 {
Warning.Printf("hwnd for idx %d is 0", idx) Warning.Printf("hwnd for idx %d is 0", idx)
return return
} }
SetForegroundWindow.Call(hwnd) SetForegroundWindow.Call(hwnd)
} }
func Swap(idx int) {
log.Printf("Swap: %d", idx)
if idx > cap(windows)-1 {
Warning.Printf("idx %d is greater than length of windows %d", idx, cap(windows))
return
}
hwnd, _, _ := GetForegroundWindow.Call()
if !IsTracked(hwnd) {
TrackWindow(hwnd)
}
log.Printf("Swapping %d to %d", hwnd, idx)
for i := 0; i < cap(windows); i++ {
if windows[i] == hwnd {
windows[i] = windows[idx]
windows[idx] = hwnd
log.Printf("Swapped %d to %d at index %d", hwnd, idx, i)
break
}
}
log.Printf("windows swapped: %+v", windows)
}
func TrackWindow(hwnd uintptr) {
if IsTracked(hwnd) {
log.Printf("Untracking window: %d", hwnd)
windows = slices.DeleteFunc(windows, func(i uintptr) bool {
return i == hwnd
})
} else {
log.Printf("Tracking window: %d", hwnd)
for i := 0; i < cap(windows); i++ {
if windows[i] == 0 {
windows[i] = hwnd
log.Printf("Tracked window: %d at index %d", hwnd, i)
return
}
}
}
}
func IsTracked(hwnd uintptr) bool {
return slices.Contains(windows, hwnd)
}
func onReady() { func onReady() {
// Icons no workey... idk why... // Icons no workey... idk why...