-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (100 loc) · 3.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"log"
"github.com/andrew-1234/shimmy/mover"
hook "github.com/robotn/gohook"
)
var movePropsMap map[string]mover.MoveProps
func main() {
distance := flag.Int("p", 10, "Pixel distance to move the window")
debug := flag.Bool("d", false, "Debug mode")
flag.Parse()
movePropsMap = initMovePropsMap(*distance)
shimmy(*debug, *distance)
}
func shimmy(debug bool, distance int) {
if debug {
fmt.Println("Debug mode enabled, logging keypress events. Press q to exit the program")
evChan := hook.Start()
defer hook.End()
for ev := range evChan {
if ev.Rawcode == 12 {
break
}
fmt.Println("hook: ", ev)
}
return
}
var CyanBright = "\033[36;1m"
var Yellow = "\033[0;35m"
var Reset = "\033[0m"
var desc = `
+-----------------------------+--------------------------+
| Key | Desc. |
+-----------------------------+--------------------------+
| ctrl shift q | exit |
| ctrl cmd up/down/left/right | move window in direction |
+-----------------------------+--------------------------+
`
fmt.Println("\n" + CyanBright + "Starting shimmy" + Reset)
print("---------------\n\n")
fmt.Println(Yellow+" Pixel distance =", distance, Reset)
fmt.Println(desc)
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(sch hook.Event) {
fmt.Println("you pressed ctrl-shift-q, exiting...")
hook.End()
})
registerMoveHooks()
start := hook.Start()
<-hook.Process(start)
}
func initMovePropsMap(distance int) map[string]mover.MoveProps {
movePropsMap = make(map[string]mover.MoveProps)
directions := []string{"right", "left", "up", "down"}
for _, direction := range directions {
props, err := mover.NewMoveProps(distance, direction)
if err != nil {
log.Fatalf("Error initializing MoveProps for direction %s: %v", direction, err)
}
movePropsMap[direction] = props
}
return movePropsMap
}
func registerMoveHooks() {
hook.Register(hook.KeyDown, []string{"cmd", "ctrl", "right"}, func(sch hook.Event) {
movePropsRight := movePropsMap["right"]
err := mover.MoveWindow(&movePropsRight)
if err != nil {
fmt.Println(err)
}
})
hook.Register(hook.KeyDown, []string{"cmd", "ctrl", "left"}, func(sch hook.Event) {
movePropsLeft := movePropsMap["left"]
err := mover.MoveWindow(&movePropsLeft)
if err != nil {
fmt.Println(err)
}
})
hook.Register(hook.KeyDown, []string{"cmd", "ctrl", "up"}, func(sch hook.Event) {
movePropsUp := movePropsMap["up"]
err := mover.MoveWindow(&movePropsUp)
if err != nil {
fmt.Println(err)
}
})
// My down arrow doesn't register a KeyDown event but using KeyHold works
hook.Register(hook.KeyHold, []string{"cmd", "ctrl", "down"}, func(sch hook.Event) {
movePropsDown := movePropsMap["down"]
err := mover.MoveWindow(&movePropsDown)
if err != nil {
fmt.Println(err)
}
})
}
// start move mode with function tab
// stop move mode with function tab
// move window with hjkl
// Switching to an app that doesnt have a visible window and trying to move it
// crashes the program