-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.go
180 lines (169 loc) · 3.64 KB
/
mouse.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
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"image"
"time"
"github.com/as/event"
"github.com/as/text"
"github.com/as/text/find"
"github.com/as/ui/tag"
"github.com/as/ui/win"
"golang.org/x/mobile/event/mouse"
)
func Button(n uint) uint {
return 1 << n
}
func HasButton(n, mask uint) bool {
return Button(n)&mask != 0
}
var (
last uint
lastpt image.Point
t0 = time.Now()
)
func procButton(e mouse.Event) {
double := false
if last == down {
if time.Since(t0) < time.Second/2 && lastpt.In(image.Rect(-3, -3, 3, 3).Add(p(e))) {
double = true
}
}
t0 = time.Now()
last = down
lastpt = p(e)
t := actTag
w, _ := act.(*win.Win)
if w == nil {
return
}
s0, s1 := w.Dot()
q0 := w.IndexOf(p(e)) + w.Origin()
q1 := q0
act.Select(q0, q1)
repaint()
switch down {
case Button(1):
if double {
q0, q1 = find.FreeExpand(w, q0)
double = false
w.Select(q0, q1)
} else {
// In Acme and Sam, the double click action doesn't maintain
// a hold on the selection if the mouse is moved out of a rectangular
// region. I don't do the same thing here because it's sometimes
// advantageous to make the selection hold for a scrolling select
// operation.
q0, q1, e = sweepFunc(w, e, D.Mouse)
}
for down != 0 {
w.Select(q0, q1)
if HasButton(2, down) {
tag.Snarf(w, e)
q1 = q0
} else if HasButton(3, down) {
q0, q1 = tag.Paste(w, e)
}
repaint()
e = rel(readmouse(<-D.Mouse), t)
}
t0 = time.Now()
w.Select(q0, q1)
case Button(2):
q0, q1, _ := sweepFunc(w, e, D.Mouse)
if q0 == q1 {
if text.Region3(q0, s0, s1) == 0 {
q0, q1 = s0, s1
} else {
q0, q1 = find.ExpandFile(w.Bytes(), q0)
}
}
w.Select(s0, s1)
acmd(event.Cmd{
Name: t.FileName(),
From: t, To: []event.Editor{w},
Rec: event.Rec{Q0: q0, Q1: q0, P: w.Bytes()[q0:q1]},
})
case Button(3):
q0, q1, _ := sweepFunc(w, e, D.Mouse)
if q0 == q1 && text.Region3(q0, s0, s1) != 0 {
// Non-zero selection; so we want to look here explicitly
q0, q1 = find.ExpandFile(w.Bytes(), q0)
}
w.Select(s0, s1) // undo the sweep
g.Look(event.Look{
Name: t.FileName(),
From: act, // The source can be the tag or the body
To: []event.Editor{actTag.Body}, // But the target is always the tag's body
Rec: event.Rec{
Q0: q0,
Q1: q1,
P: act.Bytes()[q0:q1],
},
})
}
}
// moveMouse(pt image.Point) // defined in mouse_other.go and mouse_linux.go
func MoveMouse(address interface{}) {
switch a := address.(type) {
case *win.Win:
p0, _ := a.Frame.Dot()
moveMouse(a.Loc().Min.Add(a.PointOf(p0)))
case image.Point:
moveMouse(a)
return
case int64:
w, _ := act.(*win.Win)
if w == nil {
return
}
p0, _ := w.Frame.Dot()
moveMouse(w.PointOf(p0))
return
}
}
func sweepFunc(w *win.Win, e mouse.Event, mc <-chan mouse.Event) (q0, q1 int64, e1 mouse.Event) {
start := down
q0, q1 = w.Dot()
w.Sq = q0
for down == start {
w.Sq, q0, q1 = sweep(w, e, w.Sq, q0, q1)
w.Select(q0, q1)
repaint()
e = rel(readmouse(<-mc), w)
}
return q0, q1, e
}
func cursorNop(p image.Point) {}
func shouldCursor(p Plane) (fn func(image.Point)) {
switch p.(type) {
case Named:
return cursorNop
default:
return moveMouse
}
}
func ajump2(ed text.Editor, cursor bool) {
fn := moveMouse
if !cursor {
fn = nil
}
if ed, ok := ed.(text.Jumper); ok {
ed.Jump(fn)
}
}
func ajump(p interface{}, cursor func(image.Point)) {
switch p := p.(type) {
case nil:
return //TODO(as): error message without a recursive call
case *tag.Tag:
if p != nil {
cursor(p.Loc().Min)
}
case text.Jumper:
p.Jump(cursor)
case Plane:
if cursor == nil {
cursor = shouldCursor(p)
}
cursor(p.Loc().Min)
}
}