forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_controller.go
168 lines (139 loc) · 3.78 KB
/
mouse_controller.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gxui
import (
"github.com/badu/gxui/math"
"time"
)
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonMiddle
MouseButtonRight
)
type MouseState int
func (s MouseState) IsDown(b MouseButton) bool {
return s&(1<<uint(b)) != 0
}
type MouseEvent struct {
Window *WindowImpl
Button MouseButton
State MouseState
Modifier KeyboardModifier
Point math.Point // Local to the event receiver
WindowPoint math.Point
ScrollX, ScrollY int
}
var doubleClickTime = time.Millisecond * 300
type MouseController struct {
window *WindowImpl
focusController *FocusController
lastOver ControlPointList
lastDown map[MouseButton]ControlPointList
lastUpTime map[MouseButton]time.Time
}
func CreateMouseController(window *WindowImpl, focusCtrl *FocusController) *MouseController {
result := &MouseController{
window: window,
focusController: focusCtrl,
lastDown: make(map[MouseButton]ControlPointList),
lastUpTime: make(map[MouseButton]time.Time),
}
window.OnMouseMove(result.mouseMove)
window.OnMouseEnter(result.mouseMove)
window.OnMouseExit(result.mouseMove)
window.OnMouseDown(result.mouseDown)
window.OnMouseUp(result.mouseUp)
window.OnMouseScroll(result.mouseScroll)
return result
}
func (m *MouseController) updatePosition(event MouseEvent) {
ValidateHierarchy(m.window)
nowOver := TopControlsUnder(event.Point, m.window)
for _, point := range m.lastOver {
if !nowOver.Contains(point.Control) {
e := event
e.Point = point.Point
point.Control.MouseExit(e)
}
}
for _, point := range nowOver {
if !m.lastOver.Contains(point.Control) {
e := event
e.Point = point.Point
point.Control.MouseEnter(e)
}
}
m.lastOver = nowOver
}
func (m *MouseController) mouseMove(event MouseEvent) {
m.updatePosition(event)
for _, point := range m.lastOver {
e := event
e.Point = point.Point
point.Control.MouseMove(e)
}
}
func (m *MouseController) mouseDown(event MouseEvent) {
m.updatePosition(event)
for _, point := range m.lastOver {
e := event
e.Point = point.Point
point.Control.MouseDown(e)
}
m.lastDown[event.Button] = m.lastOver
}
func (m *MouseController) mouseUp(event MouseEvent) {
m.updatePosition(event)
for _, point := range m.lastDown[event.Button] {
e := event
e.Point = point.Point
point.Control.MouseUp(e)
}
setFocusCount := m.focusController.SetFocusCount()
dblClick := time.Since(m.lastUpTime[event.Button]) < doubleClickTime
clickConsumed := false
for i := len(m.lastDown[event.Button]) - 1; i >= 0; i-- {
point := m.lastDown[event.Button][i]
if p, found := m.lastOver.Find(point.Control); found {
event.Point = p
if (dblClick && point.Control.DoubleClick(event)) || (!dblClick && point.Control.Click(event)) {
clickConsumed = true
break
}
}
}
if !clickConsumed {
event.Point = event.WindowPoint
if dblClick {
m.window.DoubleClick(event)
} else {
m.window.Click(event)
}
}
focusSet := setFocusCount != m.focusController.SetFocusCount()
if !focusSet {
for i := len(m.lastDown[event.Button]) - 1; i >= 0; i-- {
point := m.lastDown[event.Button][i]
if m.lastOver.Contains(point.Control) && m.window.SetFocus(point.Control) {
focusSet = true
break
}
}
if !focusSet {
m.window.SetFocus(nil)
}
}
delete(m.lastDown, event.Button)
m.lastUpTime[event.Button] = time.Now()
}
func (m *MouseController) mouseScroll(event MouseEvent) {
m.updatePosition(event)
for i := len(m.lastOver) - 1; i >= 0; i-- {
point := m.lastOver[i]
e := event
e.Point = point.Point
point.Control.MouseScroll(e)
}
}