-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.go
77 lines (64 loc) · 1.85 KB
/
box.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
package components
import (
"strconv"
"syscall/js"
"github.com/hexops/vecty"
"github.com/hexops/vecty/elem"
"github.com/hexops/vecty/event"
)
type Box struct {
vecty.Core
x, y int
listening bool
dragStartBoxX, dragStartBoxY int
dragStartMouseX, dragStartMouseY int
mouseMoveFunc js.Func
Children func() vecty.ComponentOrHTML `vecty:"slot"`
}
func (b *Box) Render() vecty.ComponentOrHTML {
return elem.Div(
vecty.Markup(
vecty.Style("position", "relative"),
vecty.Style("background-color", "#ffc0c0"),
vecty.Style("width", "100%"),
vecty.Style("height", "100%"),
),
elem.Div(
vecty.Markup(
vecty.Style("position", "absolute"),
vecty.Style("top", strconv.Itoa(b.y)+"px"),
vecty.Style("left", strconv.Itoa(b.x)+"px"),
vecty.Style("cursor", "grab"),
vecty.Style("user-select", "none"),
event.MouseDown(b.onMouseDown),
vecty.MarkupIf(b.listening,
event.MouseUp(b.stopListeningForMouseMove),
),
),
b.Children(),
),
)
}
func (b *Box) onMouseDown(v *vecty.Event) {
b.stopListeningForMouseMove(v)
b.listening = true
b.dragStartBoxX = b.x
b.dragStartBoxY = b.y
b.dragStartMouseX = v.Value.Get("pageX").Int()
b.dragStartMouseY = v.Value.Get("pageY").Int()
b.mouseMoveFunc = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
e := args[0]
b.x = e.Get("pageX").Int() - b.dragStartMouseX + b.dragStartBoxX
b.y = e.Get("pageY").Int() - b.dragStartMouseY + b.dragStartBoxY
vecty.Rerender(b)
return nil
})
js.Global().Call("addEventListener", "mousemove", b.mouseMoveFunc)
vecty.Rerender(b)
}
func (b *Box) stopListeningForMouseMove(v *vecty.Event) {
b.listening = false
js.Global().Call("removeEventListener", "mousemove", b.mouseMoveFunc)
b.mouseMoveFunc.Release()
vecty.Rerender(b)
}