-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
99 lines (79 loc) · 1.94 KB
/
ui.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
package main
import (
"fmt"
"net"
"net/http"
_ "embed"
"github.com/go-vgo/robotgo"
"github.com/haroflow/go-macros/automation"
"github.com/haroflow/go-macros/automation/mouse"
hook "github.com/robotn/gohook"
"github.com/zserge/lorca"
)
var ui lorca.UI
// startUI shows www/index.html and blocks until the user closes the window.
func startUI() error {
var err error
ui, err = lorca.New("", "", 500, 600)
if err != nil {
return err
}
defer ui.Close()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return err
}
defer ln.Close()
http.HandleFunc("/help", helpHandler)
http.Handle("/", http.FileServer(http.FS(fs)))
go http.Serve(ln, nil)
go listenHotkeys()
initJavascriptVM()
registerJavascriptFunctions()
ui.Load(fmt.Sprintf("http://%s/www", ln.Addr()))
<-ui.Done()
return nil
}
func helpHandler(rw http.ResponseWriter, r *http.Request) {
type TmplData struct {
Commands []automation.Command
}
helpTemplate.Execute(rw, TmplData{
Commands: registeredCommands,
})
}
// listenHotkeys waits for key presses to determine what macro to run.
func listenHotkeys() {
evChan := robotgo.EventStart()
defer robotgo.EventEnd()
for e := range evChan {
// TODO customize key binding
if e.Kind == hook.KeyUp && e.Keycode == hook.Keycode["f2"] {
fmt.Println("f2 pressed")
if isRunningMacro.Load().(bool) {
fmt.Println("macro running... stopping macro")
stopMacros()
} else {
fmt.Println("nothing running... starting macro")
code := ui.Eval("getCode()").String()
go executeCode(code)
}
}
}
}
// registerJavascriptFunctions enables JS functions to call Go functions.
func registerJavascriptFunctions() {
ui.Bind("executeCode", executeCode)
ui.Bind("stopMacros", stopMacros)
ui.Bind("getMousePosition", getMousePosition)
}
type MousePosition struct {
X int `json:"x"`
Y int `json:"y"`
}
func getMousePosition() MousePosition {
return MousePosition{
mouse.GetX(),
mouse.GetY(),
}
}