2016-04-21 10:03:20 +02:00
|
|
|
---------------------------------------------------------------------------
|
2019-02-22 08:42:43 +01:00
|
|
|
--- An extensible mouse resizing handler.
|
2016-04-21 10:03:20 +02:00
|
|
|
--
|
2017-02-15 23:36:48 +01:00
|
|
|
-- This module offers a resizing and moving mechanism for drawables such as
|
2016-04-21 10:03:20 +02:00
|
|
|
-- clients and wiboxes.
|
|
|
|
--
|
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
|
|
|
-- @copyright 2016 Emmanuel Lepage Vallee
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @submodule mouse
|
2016-04-21 10:03:20 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local aplace = require("awful.placement")
|
2019-10-20 08:09:51 +02:00
|
|
|
local capi = {mousegrabber = mousegrabber, client = client}
|
2016-06-10 23:53:55 +02:00
|
|
|
local beautiful = require("beautiful")
|
2019-10-20 08:09:51 +02:00
|
|
|
local floating = require("awful.layout.suit.floating")
|
2016-04-21 10:03:20 +02:00
|
|
|
|
|
|
|
local module = {}
|
|
|
|
|
|
|
|
local mode = "live"
|
|
|
|
local req = "request::geometry"
|
|
|
|
local callbacks = {enter={}, move={}, leave={}}
|
|
|
|
|
2016-06-10 23:53:55 +02:00
|
|
|
local cursors = {
|
2016-09-11 05:56:10 +02:00
|
|
|
["mouse.move" ] = "fleur",
|
|
|
|
["mouse.resize" ] = "cross",
|
|
|
|
["mouse.resize_left" ] = "sb_h_double_arrow",
|
|
|
|
["mouse.resize_right" ] = "sb_h_double_arrow",
|
|
|
|
["mouse.resize_top" ] = "sb_v_double_arrow",
|
|
|
|
["mouse.resize_bottom" ] = "sb_v_double_arrow",
|
|
|
|
["mouse.resize_top_left" ] = "top_left_corner",
|
|
|
|
["mouse.resize_top_right" ] = "top_right_corner",
|
|
|
|
["mouse.resize_bottom_left" ] = "bottom_left_corner",
|
|
|
|
["mouse.resize_bottom_right"] = "bottom_right_corner",
|
2016-06-10 23:53:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--- The resize cursor name.
|
|
|
|
-- @beautiful beautiful.cursor_mouse_resize
|
2016-07-26 14:57:05 +02:00
|
|
|
-- @tparam[opt=cross] string cursor
|
2016-06-10 23:53:55 +02:00
|
|
|
|
|
|
|
--- The move cursor name.
|
|
|
|
-- @beautiful beautiful.cursor_mouse_move
|
2016-07-26 14:57:05 +02:00
|
|
|
-- @tparam[opt=fleur] string cursor
|
2016-06-10 23:53:55 +02:00
|
|
|
|
2016-04-21 10:03:20 +02:00
|
|
|
--- Set the resize mode.
|
|
|
|
-- The available modes are:
|
|
|
|
--
|
2017-02-15 23:36:48 +01:00
|
|
|
-- * **live**: Resize the layout everytime the mouse moves.
|
|
|
|
-- * **after**: Resize the layout only when the mouse is released.
|
2016-04-21 10:03:20 +02:00
|
|
|
--
|
|
|
|
-- Some clients, such as XTerm, may lose information if resized too often.
|
|
|
|
--
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @function awful.mouse.resize.set_mode
|
2016-04-21 10:03:20 +02:00
|
|
|
-- @tparam string m The mode
|
|
|
|
function module.set_mode(m)
|
|
|
|
assert(m == "live" or m == "after")
|
|
|
|
mode = m
|
|
|
|
end
|
|
|
|
|
2016-11-09 21:46:58 +01:00
|
|
|
--- Add an initialization callback.
|
|
|
|
-- This callback will be executed before the mouse grabbing starts.
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @function awful.mouse.resize.add_enter_callback
|
2016-04-21 10:03:20 +02:00
|
|
|
-- @tparam function cb The callback (or nil)
|
|
|
|
-- @tparam[default=other] string context The callback context
|
|
|
|
function module.add_enter_callback(cb, context)
|
2016-11-09 21:46:58 +01:00
|
|
|
context = context or "other"
|
|
|
|
callbacks.enter[context] = callbacks.enter[context] or {}
|
2016-04-21 10:03:20 +02:00
|
|
|
table.insert(callbacks.enter[context], cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a "move" callback.
|
|
|
|
-- This callback is executed in "after" mode (see `set_mode`) instead of
|
|
|
|
-- applying the operation.
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @function awful.mouse.resize.add_move_callback
|
2016-04-21 10:03:20 +02:00
|
|
|
-- @tparam function cb The callback (or nil)
|
|
|
|
-- @tparam[default=other] string context The callback context
|
|
|
|
function module.add_move_callback(cb, context)
|
|
|
|
context = context or "other"
|
|
|
|
callbacks.move[context] = callbacks.move[context] or {}
|
|
|
|
table.insert(callbacks.move[context], cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a "leave" callback
|
|
|
|
-- This callback is executed just before the `mousegrabber` stop
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @function awful.mouse.resize.add_leave_callback
|
2016-04-21 10:03:20 +02:00
|
|
|
-- @tparam function cb The callback (or nil)
|
|
|
|
-- @tparam[default=other] string context The callback context
|
|
|
|
function module.add_leave_callback(cb, context)
|
|
|
|
context = context or "other"
|
|
|
|
callbacks.leave[context] = callbacks.leave[context] or {}
|
|
|
|
table.insert(callbacks.leave[context], cb)
|
|
|
|
end
|
|
|
|
|
2017-04-10 21:57:12 +02:00
|
|
|
--- Resize the drawable.
|
2016-04-21 10:03:20 +02:00
|
|
|
--
|
|
|
|
-- Valid `args` are:
|
|
|
|
--
|
2017-02-15 23:36:48 +01:00
|
|
|
-- * *enter_callback*: A function called before the `mousegrabber` starts.
|
|
|
|
-- * *move_callback*: A function called when the mouse moves.
|
|
|
|
-- * *leave_callback*: A function called before the `mousegrabber` is released.
|
|
|
|
-- * *mode*: The resize mode.
|
2016-04-21 10:03:20 +02:00
|
|
|
--
|
2016-04-25 04:09:14 +02:00
|
|
|
-- @function awful.mouse.resize
|
2017-04-10 21:57:12 +02:00
|
|
|
-- @tparam client client A client.
|
|
|
|
-- @tparam[default=mouse.resize] string context The resizing context.
|
|
|
|
-- @tparam[opt={}] table args A set of `awful.placement` arguments.
|
2020-01-26 01:24:00 +01:00
|
|
|
-- @request wibox geometry mouse.resize granted Requests to resize the wibox.
|
2016-04-21 10:03:20 +02:00
|
|
|
local function handler(_, client, context, args) --luacheck: no unused_args
|
|
|
|
args = args or {}
|
|
|
|
context = context or "mouse.resize"
|
|
|
|
|
|
|
|
local placement = args.placement
|
|
|
|
|
|
|
|
if type(placement) == "string" and aplace[placement] then
|
|
|
|
placement = aplace[placement]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extend the table with the default arguments
|
|
|
|
args = setmetatable(
|
|
|
|
{
|
|
|
|
placement = placement or aplace.resize_to_mouse,
|
|
|
|
mode = args.mode or mode,
|
|
|
|
pretend = true,
|
|
|
|
},
|
|
|
|
{__index = args or {}}
|
|
|
|
)
|
|
|
|
|
|
|
|
local geo
|
|
|
|
|
|
|
|
for _, cb in ipairs(callbacks.enter[context] or {}) do
|
|
|
|
geo = cb(client, args)
|
|
|
|
|
|
|
|
if geo == false then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.enter_callback then
|
|
|
|
geo = args.enter_callback(client, args)
|
|
|
|
|
|
|
|
if geo == false then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
geo = nil
|
|
|
|
|
2016-06-10 23:53:55 +02:00
|
|
|
-- Select the cursor
|
|
|
|
local tcontext = context:gsub('[.]', '_')
|
2016-09-11 05:56:10 +02:00
|
|
|
local corner = args.corner and ("_".. args.corner) or ""
|
2016-06-10 23:53:55 +02:00
|
|
|
|
2016-09-11 05:56:10 +02:00
|
|
|
local cursor = beautiful["cursor_"..tcontext]
|
|
|
|
or cursors[context..corner]
|
|
|
|
or cursors[context]
|
|
|
|
or "fleur"
|
2016-06-10 23:53:55 +02:00
|
|
|
|
2016-04-21 10:03:20 +02:00
|
|
|
-- Execute the placement function and use request::geometry
|
2022-07-03 08:10:31 +02:00
|
|
|
capi.mousegrabber.run(function (coords)
|
2016-04-21 10:03:20 +02:00
|
|
|
if not client.valid then return end
|
|
|
|
|
2017-11-05 20:29:47 +01:00
|
|
|
-- Resize everytime the mouse moves (default behavior) in live mode,
|
|
|
|
-- otherwise keep the current geometry
|
|
|
|
geo = setmetatable(
|
|
|
|
args.mode == "live" and args.placement(client, args) or client:geometry(),
|
|
|
|
{__index=args}
|
|
|
|
)
|
2016-04-21 10:03:20 +02:00
|
|
|
|
|
|
|
-- Execute the move callbacks. This can be used to add features such as
|
|
|
|
-- snap or adding fancy graphical effects.
|
|
|
|
for _, cb in ipairs(callbacks.move[context] or {}) do
|
|
|
|
-- If something is returned, assume it is a modified geometry
|
|
|
|
geo = cb(client, geo, args) or geo
|
|
|
|
|
|
|
|
if geo == false then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.move_callback then
|
|
|
|
geo = args.move_callback(client, geo, args)
|
|
|
|
|
|
|
|
if geo == false then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- In case it was modified
|
2017-11-05 20:29:47 +01:00
|
|
|
if geo then
|
|
|
|
setmetatable(geo, {__index=args})
|
|
|
|
end
|
2016-04-21 10:03:20 +02:00
|
|
|
|
|
|
|
if args.mode == "live" then
|
|
|
|
-- Ask the resizing handler to resize the client
|
2017-02-15 23:36:48 +01:00
|
|
|
client:emit_signal(req, context, geo)
|
2016-04-21 10:03:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Quit when the button is released
|
2022-07-03 08:10:31 +02:00
|
|
|
for _,v in pairs(coords.buttons) do
|
2016-04-21 10:03:20 +02:00
|
|
|
if v then return true end
|
|
|
|
end
|
|
|
|
|
2017-04-10 21:57:12 +02:00
|
|
|
-- Only resize after the mouse is released, this avoids losing content
|
|
|
|
-- in resize sensitive apps such as XTerm or allows external modules
|
2016-11-09 21:46:58 +01:00
|
|
|
-- to implement custom resizing.
|
2016-04-21 10:03:20 +02:00
|
|
|
if args.mode == "after" then
|
|
|
|
-- Get the new geometry
|
|
|
|
geo = args.placement(client, args)
|
|
|
|
|
|
|
|
-- Ask the resizing handler to resize the client
|
2017-02-15 23:36:48 +01:00
|
|
|
client:emit_signal(req, context, geo)
|
2016-04-21 10:03:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
geo = nil
|
|
|
|
|
|
|
|
for _, cb in ipairs(callbacks.leave[context] or {}) do
|
|
|
|
geo = cb(client, geo, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.leave_callback then
|
|
|
|
geo = args.leave_callback(client, geo, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not geo then return false end
|
|
|
|
|
|
|
|
-- In case it was modified
|
|
|
|
setmetatable(geo,{__index=args})
|
|
|
|
|
2017-02-15 23:36:48 +01:00
|
|
|
client:emit_signal(req, context, geo)
|
2016-04-21 10:03:20 +02:00
|
|
|
|
|
|
|
return false
|
2016-06-10 23:53:55 +02:00
|
|
|
end, cursor)
|
2016-04-21 10:03:20 +02:00
|
|
|
end
|
|
|
|
|
2019-10-20 08:09:51 +02:00
|
|
|
function module._resize_handler(c, context, hints)
|
|
|
|
if hints and context and context:find("mouse.*") then
|
|
|
|
-- This handler only handle the floating clients. If the client is tiled,
|
|
|
|
-- then it let the layouts handle it.
|
|
|
|
local t = c.screen.selected_tag
|
|
|
|
local lay = t and t.layout or nil
|
|
|
|
|
|
|
|
if (lay and lay == floating) or c.floating then
|
|
|
|
c:geometry {
|
|
|
|
x = hints.x,
|
|
|
|
y = hints.y,
|
|
|
|
width = hints.width,
|
|
|
|
height = hints.height,
|
|
|
|
}
|
|
|
|
elseif lay and lay.resize_handler then
|
|
|
|
lay.resize_handler(c, context, hints)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
capi.client.connect_signal("request::geometry", module._resize_handler)
|
|
|
|
|
2016-04-21 10:03:20 +02:00
|
|
|
return setmetatable(module, {__call=handler})
|
2016-12-31 14:07:13 +01:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|