Repository navigation
Expand file tree
/
Copy pathmouse.lua.in
More file actions
491 lines (439 loc) · 19.4 KB
/
Copy pathmouse.lua.in
File metadata and controls
491 lines (439 loc) · 19.4 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
-- Grab environment we need
local layout = require("awful.layout")
local tag = require("awful.tag")
local hooks = require("awful.hooks")
local aclient = require("awful.client")
local widget = require("awful.widget")
local util = require("awful.util")
local type = type
local math = math
local ipairs = ipairs
local capi =
{
mouse = mouse,
screen = screen,
client = client,
mousegrabber = mousegrabber,
}
--- Mouse module for awful
module("awful.mouse")
client = {}
wibox = {}
function client_under_pointer()
local obj = capi.mouse.object_under_pointer()
if type(obj) == "client" then
return obj
end
end
function wibox_under_pointer()
local obj = capi.mouse.object_under_pointer()
if type(obj) == "wibox" then
return obj
end
end
function widget_under_pointer()
local obj, obj2 = capi.mouse.object_under_pointer()
if type(obj2) == "widget" then
return obj2
end
end
local function snap_outside(g, sg, snap)
if g.x < snap + sg.x + sg.width and g.x > sg.x + sg.width then
g.x = sg.x + sg.width
elseif g.x + g.width < sg.x and g.x + g.width > sg.x - snap then
g.x = sg.x - g.width
end
if g.y < snap + sg.y + sg.height and g.y > sg.y + sg.height then
g.y = sg.y + sg.height
elseif g.y + g.height < sg.y and g.y + g.height > sg.y - snap then
g.y = sg.y - g.height
end
return g
end
local function snap_inside(g, sg, snap)
if math.abs(g.x) < snap + sg.x and g.x > sg.x then
g.x = sg.x
elseif math.abs((sg.x + sg.width) - (g.x + g.width)) < snap then
g.x = sg.x + sg.width - g.width
end
if math.abs(g.y) < snap + sg.y and g.y > sg.y then
g.y = sg.y
elseif math.abs((sg.y + sg.height) - (g.y + g.height)) < snap then
g.y = sg.y + sg.height - g.height
end
return g
end
--- Snap a client to the closest client or screen edge.
-- @param c The client to snap.
-- @param snap The pixel to snap clients.
-- @param x The client x coordinate.
-- @param y The client y coordinate.
-- @param fixed_x True if the client isn't allowed to move in the x direction.
-- @param fixed_y True if the client isn't allowed to move in the y direction.
function client.snap(c, snap, x, y, fixed_x, fixed_y)
local snap = snap or 8
local c = c or client.focus
local cur_geom = c:geometry()
local geom = c:geometry()
geom.x = x or geom.x
geom.y = y or geom.y
geom = snap_inside(geom, capi.screen[c.screen].geometry, snap)
geom = snap_inside(geom, capi.screen[c.screen].workarea, snap)
for k, snapper in ipairs(aclient.visible(c.screen)) do
if snapper ~= c then
geom = snap_outside(geom, snapper:geometry(), snap)
end
end
-- It's easiest to undo changes afterwards if they're not allowed
if fixed_x then geom.x = cur_geom.x end
if fixed_y then geom.y = cur_geom.y end
return geom
end
--- Move a client.
-- @param c The client to move, or the focused one if nil.
-- @param snap The pixel to snap clients.
function client.move(c, snap)
local c = c or capi.client.focus
if not c then return end
if c.fullscreen
or c.type == "desktop"
or c.type == "splash"
or c.type == "dock" then
return
end
c:raise()
local orig = c:geometry()
local m_c = capi.mouse.coords()
local dist_x = m_c.x - orig.x
local dist_y = m_c.y - orig.y
-- Only allow moving in the non-maximized directions
local fixed_x = c.maximized_horizontal
local fixed_y = c.maximized_vertical
local function ug(c, prop)
if prop == "geometry" then
local g = c:geometry()
capi.mouse.coords({ x = g.x + dist_x, y = g.y + dist_y })
end
end
capi.mousegrabber.run(function (mouse)
hooks.property.unregister(ug)
for k, v in ipairs(mouse.buttons) do
if v then
local lay = layout.get(c.screen)
if lay == layout.suit.floating or aclient.floating.get(c) then
local x = mouse.x - dist_x
local y = mouse.y - dist_y
c:geometry(client.snap(c, snap, x, y, fixed_x, fixed_y))
if layout.get(c.screen) ~= layout.suit.floating and not aclient.floating.get(c) then
hooks.property.register(ug)
end
elseif lay ~= layout.suit.magnifier then
c.screen = capi.mouse.screen
if layout.get(c.screen) ~= layout.suit.floating then
local c_u_m = client_under_pointer()
if c_u_m and not aclient.floating.get(c_u_m) then
if c_u_m ~= c then
c:swap(c_u_m)
end
end
else
hooks.property.register(ug)
end
end
return true
end
end
return false
end, "fleur")
end
client.dragtotag = { }
--- Move a client to a tag by drag'n'dropping it over a taglist widget
-- @param c The client to move
function client.dragtotag.widget(c, side)
capi.mousegrabber.run(function (mouse)
local button_down = false
for _, v in ipairs(mouse.buttons) do
if v then button_down = true end
end
if not button_down then
local w = widget_under_pointer()
if w and widget.taglist.gettag(w) then
local t = widget.taglist.gettag(w)
if t.screen ~= c.screen then
aclient.movetoscreen(c, t.screen)
end
aclient.movetotag(t, c)
end
return false
end
return true
end, "fleur")
end
--- Move a client to a tag by dragging it onto the left / right side of the screen
-- @param c The client to move
function client.dragtotag.border(c)
capi.mousegrabber.run(function (mouse)
local button_down = false
for _, v in ipairs(mouse.buttons) do
if v then button_down = true end
end
local wa = capi.screen[c.screen].workarea
if mouse.x >= wa.x + wa.width then
capi.mouse.coords({ x = wa.x + wa.width - 1 })
elseif mouse.x <= wa.x then
capi.mouse.coords({ x = wa.x + 1 })
end
if not button_down then
local tags = capi.screen[c.screen]:tags()
local t = tag.selected()
local idx
for i, v in ipairs(tags) do
if v == t then
idx = i
end
end
if mouse.x > wa.x + wa.width - 10 then
local newtag = tags[util.cycle(#tags, idx + 1)]
aclient.movetotag(newtag, c)
tag.viewnext()
elseif mouse.x < wa.x + 10 then
local newtag = tags[util.cycle(#tags, idx - 1)]
aclient.movetotag(newtag, c)
tag.viewprev()
end
return false
end
return true
end, "fleur")
end
--- Move the wibox under the cursor
--@param w The wibox to move, or none to use that under the pointer
function wibox.move(w)
if not w then w = wibox_under_pointer() end
if not w then return end
local offset = {
x = w:geometry()["x"] - capi.mouse.coords()["x"],
y = w:geometry()["y"] - capi.mouse.coords()["y"]
}
capi.mousegrabber.run(function (mouse)
local button_down = false
if w.position == "floating" then
w:geometry({
x = capi.mouse.coords()["x"] + offset["x"],
y = capi.mouse.coords()["y"] + offset["y"],
})
else
local wa = capi.screen[capi.mouse.screen].workarea
if capi.mouse.coords()["y"] > wa.y + wa.height - 10 then
w.position = "bottom"
elseif capi.mouse.coords()["y"] < wa.y + 10 then
w.position = "top"
elseif capi.mouse.coords()["x"] > wa.x + wa.width - 10 then
w.position = "right"
elseif capi.mouse.coords()["x"] < wa.x + 10 then
w.position = "left"
end
w.screen = capi.mouse.screen
end
for k, v in ipairs(mouse.buttons) do
if v then button_down = true end
end
if not button_down then
return false
end
return true
end, "fleur")
end
--- Get a client corner coordinates.
-- @param c The client to get corner from, focused one by default.
-- @param corner The corner to use: auto, top_left, top_right, bottom_left,
-- bottom_right. Default is auto, and auto find the nearest corner.
-- @return Actual used corner and x and y coordinates.
function client.corner(c, corner)
local c = c or capi.client.focus
if not c then return end
local g = c:geometry()
if not corner or corner == "auto" then
local m_c = capi.mouse.coords()
if math.abs(g.y - m_c.y) < math.abs(g.y + g.height - m_c.y) then
if math.abs(g.x - m_c.x) < math.abs(g.x + g.width - m_c.x) then
corner = "top_left"
else
corner = "top_right"
end
else
if math.abs(g.x - m_c.x) < math.abs(g.x + g.width - m_c.x) then
corner = "bottom_left"
else
corner = "bottom_right"
end
end
end
local x, y
if corner == "top_right" then
x = g.x + g.width
y = g.y
elseif corner == "top_left" then
x = g.x
y = g.y
elseif corner == "bottom_left" then
x = g.x
y = g.y + g.height
else
x = g.x + g.width
y = g.y + g.height
end
return corner, x, y
end
local function client_resize_magnifier(c, corner)
local corner, x, y = client.corner(c, corner)
capi.mouse.coords({ x = x, y = y })
local wa = capi.screen[c.screen].workarea
local center_x = wa.x + wa.width / 2
local center_y = wa.y + wa.height / 2
local maxdist_pow = (wa.width^2 + wa.height^2) / 4
capi.mousegrabber.run(function (mouse)
for k, v in ipairs(mouse.buttons) do
if v then
local dx = center_x - mouse.x
local dy = center_y - mouse.y
local dist = dx^2 + dy^2
-- New master width factor
local mwfact = dist / maxdist_pow
tag.setmwfact(math.min(math.max(0.01, mwfact), 0.99), tag.selected(c.screen))
return true
end
end
return false
end, corner .. "_corner")
end
local function client_resize_tiled(c, lay)
local wa = capi.screen[c.screen].workarea
local mwfact = tag.getmwfact()
local cursor
if lay == layout.suit.tile then
capi.mouse.coords({ x = wa.x + wa.width * mwfact })
cursor = "sb_h_double_arrow"
elseif lay == layout.suit.tile.left then
capi.mouse.coords({ x = wa.x + wa.width * (1 - mwfact) })
cursor = "sb_h_double_arrow"
elseif lay == layout.suit.tile.bottom then
capi.mouse.coords({ y = wa.y + wa.height * mwfact })
cursor = "sb_v_double_arrow"
else
capi.mouse.coords({ y = wa.y + wa.height * (1 - mwfact) })
cursor = "sb_v_double_arrow"
end
capi.mousegrabber.run(function (mouse)
for k, v in ipairs(mouse.buttons) do
if v then
local fact_x = (mouse.x - wa.x) / wa.width
local fact_y = (mouse.y - wa.y) / wa.height
local mwfact
if lay == layout.suit.tile then
mwfact = fact_x
elseif lay == layout.suit.tile.left then
mwfact = 1 - fact_x
elseif lay == layout.suit.tile.bottom then
mwfact = fact_y
else
mwfact = 1 - fact_y
end
tag.setmwfact(math.min(math.max(mwfact, 0.01), 0.99), tag.selected(c.screen))
return true
end
end
return false
end, cursor)
end
local function client_resize_floating(c, corner, fixed_x, fixed_y)
local corner, x, y = client.corner(c, corner)
local g = c:geometry()
-- Warp mouse pointer
capi.mouse.coords({ x = x, y = y })
capi.mousegrabber.run(function (mouse)
for k, v in ipairs(mouse.buttons) do
if v then
-- Ignore screen changes
if not aclient.floating.get(c)
and capi.mouse.screen ~= c.screen then
return true
end
local ng
if corner == "bottom_right" then
ng = { width = mouse.x - g.x,
height = mouse.y - g.y }
elseif corner == "bottom_left" then
ng = { x = mouse.x,
width = (g.x + g.width) - mouse.x,
height = mouse.y - g.y }
elseif corner == "top_left" then
ng = { x = mouse.x,
width = (g.x + g.width) - mouse.x,
y = mouse.y,
height = (g.y + g.height) - mouse.y }
else
ng = { width = mouse.x - g.x,
y = mouse.y,
height = (g.y + g.height) - mouse.y }
end
if ng.width <= 0 then ng.width = nil end
if ng.height <= 0 then ng.height = nil end
if fixed_x then ng.width = g.width ng.x = g.x end
if fixed_y then ng.height = g.height ng.y = g.y end
c:geometry(ng)
-- Get real geometry that has been applied
-- in case we honor size hints
-- XXX: This should be rewritten when size
-- hints are available from Lua.
local rg = c:geometry()
if corner == "bottom_right" then
ng = {}
elseif corner == "bottom_left" then
ng = { x = (g.x + g.width) - rg.width }
elseif corner == "top_left" then
ng = { x = (g.x + g.width) - rg.width,
y = (g.y + g.height) - rg.height }
else
ng = { y = (g.y + g.height) - rg.height }
end
c:geometry({ x = ng.x, y = ng.y })
return true
end
end
return false
end, corner .. "_corner")
end
--- Resize a client.
-- @param c The client to resize, or the focused one by default.
-- @param corner The corner to grab on resize. Auto detected by default.
function client.resize(c, corner)
local c = c or capi.client.focus
if not c then return end
if c.fullscreen
or c.type == "desktop"
or c.type == "splash"
or c.type == "dock" then
return
end
-- Do not allow maximized clients to be resized by mouse
local fixed_x = c.maximized_horizontal
local fixed_y = c.maximized_vertical
local lay = layout.get(c.screen)
if lay == layout.suit.floating or aclient.floating.get(c) then
return client_resize_floating(c, corner, fixed_x, fixed_y)
elseif lay == layout.suit.tile
or lay == layout.suit.tile.left
or lay == layout.suit.tile.top
or lay == layout.suit.tile.bottom then
return client_resize_tiled(c, lay)
elseif lay == layout.suit.magnifier then
return client_resize_magnifier(c, corner)
end
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80