Implement basic functionality of tabbing
This commit is contained in:
86
main.go
86
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"syscall"
|
||||
|
||||
// _ "embed"
|
||||
@@ -19,7 +20,7 @@ var (
|
||||
)
|
||||
|
||||
// //go:embed duck2.jpg
|
||||
//var icon []byte
|
||||
// var icon []byte
|
||||
var Error *log.Logger
|
||||
var Warning *log.Logger
|
||||
|
||||
@@ -41,58 +42,43 @@ func init() {
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
const (
|
||||
ALT = 0xa4
|
||||
TILDE = 0xc0
|
||||
)
|
||||
var processes = []uintptr{}
|
||||
|
||||
func main() {
|
||||
systray.Run(onReady, nil)
|
||||
keyEvents := make(chan *hook.Event, 4096)
|
||||
hook.Register(hook.KeyDown, []string{}, func(e hook.Event) {
|
||||
keyEvents <- &e
|
||||
})
|
||||
hook.Register(hook.KeyUp, []string{}, func(e hook.Event) {
|
||||
keyEvents <- &e
|
||||
})
|
||||
go systray.Run(onReady, nil)
|
||||
|
||||
processes := make(map[uintptr]bool)
|
||||
keysDown := make(map[uint16]bool)
|
||||
go func() {
|
||||
for {
|
||||
key, ok := <-keyEvents
|
||||
if !ok {
|
||||
Error.Printf("Error reading key events")
|
||||
return
|
||||
}
|
||||
log.Printf("%#v", key)
|
||||
if key.Kind == hook.KeyDown {
|
||||
keysDown[key.Rawcode] = true
|
||||
} else {
|
||||
keysDown[key.Rawcode] = false
|
||||
}
|
||||
|
||||
if keysDown[ALT] && key.Rawcode == TILDE {
|
||||
hwnd, _, _ := GetForegroundWindow.Call()
|
||||
existing, ok := processes[hwnd]
|
||||
if !ok {
|
||||
processes[hwnd] = true
|
||||
} else {
|
||||
processes[hwnd] = !existing
|
||||
}
|
||||
}
|
||||
|
||||
// if key.Kind == hook.KeyUp {
|
||||
// StopSpam(key.Rawcode)
|
||||
// } else if key.Kind == hook.KeyDown {
|
||||
// DoSpam(key.Rawcode)
|
||||
// }
|
||||
hook.Register(hook.KeyDown, []string{"alt", "-"}, func(e hook.Event) {
|
||||
hwnd, _, _ := GetForegroundWindow.Call()
|
||||
if slices.Contains(processes, hwnd) {
|
||||
processes = slices.DeleteFunc(processes, func(i uintptr) bool {
|
||||
return i == hwnd
|
||||
})
|
||||
} 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()
|
||||
<-hook.Process(s)
|
||||
}
|
||||
func TabTo(idx int) {
|
||||
log.Printf("TabTo: %d", idx)
|
||||
log.Printf("%+v", processes)
|
||||
if idx > len(processes) {
|
||||
Warning.Printf("idx %d is greater than length of processes %d", idx, len(processes))
|
||||
return
|
||||
}
|
||||
hwnd := processes[idx]
|
||||
if hwnd == 0 {
|
||||
Warning.Printf("hwnd for idx %d is 0", idx)
|
||||
return
|
||||
}
|
||||
SetForegroundWindow.Call(hwnd)
|
||||
}
|
||||
|
||||
func onReady() {
|
||||
// Icons no workey... idk why...
|
||||
@@ -100,7 +86,15 @@ func onReady() {
|
||||
//systray.SetIcon(icon)
|
||||
systray.SetTitle("Awesome App")
|
||||
systray.SetTooltip("Pretty awesome超级棒")
|
||||
//mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
|
||||
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
|
||||
go func() {
|
||||
<-mQuit.ClickedCh
|
||||
fmt.Println("Requesting quit")
|
||||
systray.Quit()
|
||||
os.Exit(0)
|
||||
// TODO: Quit properly, not this exit bullshit
|
||||
fmt.Println("Finished quitting")
|
||||
}()
|
||||
|
||||
// Sets the icon of a menu item. Only available on Mac and Windows.
|
||||
//mQuit.SetIcon(icon)
|
||||
|
||||
Reference in New Issue
Block a user