-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor.go
113 lines (96 loc) · 2.3 KB
/
cursor.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
package ui
import (
"github.com/axe/axe-go/pkg/ds"
"github.com/axe/axe-go/pkg/id"
)
type CursorEvent uint8
const (
CursorEventHover CursorEvent = iota
CursorEventDown
CursorEventDrag
CursorEventDisabled
)
type Cursors struct {
ds.EnumMap[CursorEvent, id.Identifier]
}
func NewCursors(m map[CursorEvent]id.Identifier) Cursors {
return Cursors{
EnumMap: ds.NewEnumMap(m),
}
}
func (c Cursors) OverCursor(b *Base, ignoreDragging bool) id.Identifier {
if b.IsDisabled() {
cursorDisabled := c.Get(CursorEventDisabled)
if !cursorDisabled.Empty() {
return cursorDisabled
}
}
if b.ui.Dragging == b && !ignoreDragging {
cursorDrag := c.Get(CursorEventDrag)
if !cursorDrag.Empty() {
return cursorDrag
}
}
return c.Get(CursorEventHover)
}
func (c Cursors) HandlePointer(ev *PointerEvent, b *Base) {
if b.IsDragging() {
return
}
cursorDisabled := c.Get(CursorEventDisabled)
cursorHover := c.Get(CursorEventHover)
cursorDown := c.Get(CursorEventDown)
cursorOver := c.OverCursor(b, false)
switch ev.Type {
case PointerEventEnter:
ev.SetCursor(cursorOver, b, false)
case PointerEventMove:
ev.SetCursor(cursorOver, b, false)
case PointerEventDown:
ev.SetCursor(cursorDown, b, false)
case PointerEventLeave:
ev.RemoveCursor(cursorHover)
ev.RemoveCursor(cursorDisabled)
case PointerEventUp:
ev.RemoveCursor(cursorDown)
ev.SetCursor(cursorOver, b, false)
}
}
func (c Cursors) HandleDrag(ev *DragEvent, b *Base) {
dragCursor := c.Get(CursorEventDrag)
overCursor := c.OverCursor(b, true)
switch ev.Type {
case DragEventStart:
ev.SetCursor(dragCursor, b, true)
case DragEventEnd:
ev.RemoveCursor(dragCursor)
ev.SetCursor(overCursor, b, true)
case DragEventCancel:
ev.RemoveCursor(dragCursor)
ev.SetCursor(overCursor, b, true)
}
}
type HasCursor struct {
Cursor *id.Identifier
CursorSet bool
CursorTrigger *Base
}
func (ev *HasCursor) RemoveCursor(cursor id.Identifier) {
if *ev.Cursor == 0 || *ev.Cursor == cursor {
*ev.Cursor = 0
}
}
func (ev *HasCursor) SetCursor(cursor id.Identifier, trigger *Base, force bool) {
if cursor.Empty() {
return
}
if (ev.CursorSet == true && trigger != ev.CursorTrigger) && !force {
return
}
*ev.Cursor = cursor
ev.CursorSet = true
ev.CursorTrigger = trigger
}
func cursorNil(id id.Identifier) bool {
return id.Empty()
}