forked from BurntSushi/wingo
/
client_drag.go
231 lines (200 loc) · 6.49 KB
/
client_drag.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil/ewmh"
"github.com/BurntSushi/wingo/cursors"
"github.com/BurntSushi/wingo/frame"
)
func (c *client) dragMoveBegin(rx, ry, ex, ey int) {
f := c.frame
moving := f.MovingState()
moving.Moving = true
moving.RootX, moving.RootY = rx, ry
// call for side-effect; makes sure parent window has a valid geometry
f.Parent().Geometry()
// unmax!
c.EnsureUnmax()
}
func (c *client) dragMoveStep(rx, ry, ex, ey int) {
f := c.frame
moving := f.MovingState()
newx := f.Geom().X() + rx - moving.RootX
newy := f.Geom().Y() + ry - moving.RootY
moving.RootX, moving.RootY = rx, ry
c.LayoutMove(newx, newy)
}
func (c *client) dragMoveEnd(rx, ry, ex, ey int) {
f := c.frame
frame.Reset(f)
// WM.headChoose(f.Client(), f.Geom())
moving := f.MovingState()
moving.Moving = false
moving.RootX, moving.RootY = 0, 0
}
func (c *client) dragResizeBegin(direction uint32,
rx, ry, ex, ey int) (bool, xproto.Cursor) {
f := c.frame
// call for side-effect; makes sure parent window has a valid geometry
f.Parent().Geometry()
resizing := f.ResizingState()
dir := direction
w, h := f.Geom().Width(), f.Geom().Height()
// If we aren't forcing a direction, we need to infer it based on
// where the mouse is in the window.
// (ex, ey) is the position of the mouse.
// We basically split the window into something like a tic-tac-toe board:
// -------------------------
// | | | |
// | A | | F |
// | | D | |
// --------- |--------
// | | | |
// | B |-------| G |
// | | | |
// --------- |--------
// | | E | |
// | C | | H |
// | | | |
// -------------------------
// Where A, B, C correspond to 'ex < w / 3'
// and F, G, H correspond to 'ex > w * 2 / 3'
// and D and E correspond to 'ex >= w / 3 && ex <= w * 2 / 3'
// The direction is not only important for assigning which cursor to display
// (where each of the above blocks gets its own cursor), but it is also
// important for choosing which parts of the geometry to change.
// For example, if the mouse is in 'H', then the width and height could
// be changed, but x and y cannot. Conversely, if the mouse is in 'A',
// all parts of the geometry can change: x, y, width and height.
// As one last example, if the mouse is in 'D', only y and height of the
// window can change.
if dir == ewmh.Infer {
if ex < w/3 {
switch {
case ey < h/3:
dir = ewmh.SizeTopLeft
case ey > h*2/3:
dir = ewmh.SizeBottomLeft
default: // ey >= h / 3 && ey <= h * 2 / 3
dir = ewmh.SizeLeft
}
} else if ex > w*2/3 {
switch {
case ey < h/3:
dir = ewmh.SizeTopRight
case ey > h*2/3:
dir = ewmh.SizeBottomRight
default: // ey >= h / 3 && ey <= h * 2 / 3
dir = ewmh.SizeRight
}
} else { // ex >= w / 3 && ex <= w * 2 / 3
switch {
case ey < h/2:
dir = ewmh.SizeTop
default: // ey >= h / 2
dir = ewmh.SizeBottom
}
}
}
// Find the right cursor
var cursor xproto.Cursor = 0
switch dir {
case ewmh.SizeTop:
cursor = cursors.TopSide
case ewmh.SizeTopRight:
cursor = cursors.TopRightCorner
case ewmh.SizeRight:
cursor = cursors.RightSide
case ewmh.SizeBottomRight:
cursor = cursors.BottomRightCorner
case ewmh.SizeBottom:
cursor = cursors.BottomSide
case ewmh.SizeBottomLeft:
cursor = cursors.BottomLeftCorner
case ewmh.SizeLeft:
cursor = cursors.LeftSide
case ewmh.SizeTopLeft:
cursor = cursors.TopLeftCorner
}
// Save some state that we'll need when computing a window's new geometry
resizing.Resizing = true
resizing.RootX, resizing.RootY = rx, ry
resizing.X, resizing.Y = f.Geom().X(), f.Geom().Y()
resizing.Width, resizing.Height = f.Geom().Width(), f.Geom().Height()
// Our geometry calculations depend upon which direction we're resizing.
// Namely, the direction determines which parts of the geometry need to
// be modified. Pre-compute those parts (i.e., x, y, width and/or height)
resizing.Xs = dir == ewmh.SizeLeft || dir == ewmh.SizeTopLeft ||
dir == ewmh.SizeBottomLeft
resizing.Ys = dir == ewmh.SizeTop || dir == ewmh.SizeTopLeft ||
dir == ewmh.SizeTopRight
resizing.Ws = dir == ewmh.SizeTopLeft || dir == ewmh.SizeTopRight ||
dir == ewmh.SizeRight || dir == ewmh.SizeBottomRight ||
dir == ewmh.SizeBottomLeft || dir == ewmh.SizeLeft
resizing.Hs = dir == ewmh.SizeTopLeft || dir == ewmh.SizeTop ||
dir == ewmh.SizeTopRight || dir == ewmh.SizeBottomRight ||
dir == ewmh.SizeBottom || dir == ewmh.SizeBottomLeft
// unmax!
c.EnsureUnmax()
return true, cursor
}
func (c *client) dragResizeStep(rx, ry, ex, ey int) {
f := c.frame
resizing := f.ResizingState()
diffx, diffy := rx-resizing.RootX, ry-resizing.RootY
newx, newy := f.Geom().X(), f.Geom().Y()
neww, newh := f.Geom().Width(), f.Geom().Height()
validw, validh := neww, newh
if resizing.Xs {
newx = resizing.X + diffx
}
if resizing.Ys {
newy = resizing.Y + diffy
}
if resizing.Ws {
if resizing.Xs {
neww = resizing.Width - diffx
} else {
neww = resizing.Width + diffx
}
leftRight := f.Left() + f.Right()
validw = c.ValidateWidth(neww-leftRight) + leftRight
// If validation changed our width, we need to make sure
// our x-value is appropriately changed
if resizing.Xs && validw != neww {
newx = resizing.X + resizing.Width - validw
}
}
if resizing.Hs {
if resizing.Ys {
newh = resizing.Height - diffy
} else {
newh = resizing.Height + diffy
}
topBot := f.Top() + f.Bottom()
validh = c.ValidateHeight(newh-topBot) + topBot
// If validation changed our height, we need to make sure
// our y-value is appropriately changed
if resizing.Ys && validh != newh {
newy = resizing.Y + resizing.Height - validh
}
}
c.LayoutMoveResize(newx, newy, validw, validh)
}
func (c *client) dragResizeEnd(rx, ry, ex, ey int) {
f := c.frame
// If windows are really slow to respond/resize, this may be necessary.
// If we don't, it's possible for the client to be out of whack inside
// the decorations.
// Example: Libreoffice in Xephyr. Try resizing it with the mouse and
// releasing the mouse button really quickly.
frame.Reset(f)
// WM.headChoose(f.Client(), f.Geom())
// just zero out the resizing state
resizing := f.ResizingState()
resizing.Resizing = false
resizing.RootX, resizing.RootY = 0, 0
resizing.X, resizing.Y = 0, 0
resizing.Width, resizing.Height = 0, 0
resizing.Xs, resizing.Ys = false, false
resizing.Ws, resizing.Hs = false, false
}