dotfiles/awesome/rc_parts/constrain.lua

110 lines
2.1 KiB
Lua
Raw Normal View History

2023-01-18 23:37:21 +00:00
-- init -- {{{1
-- luacheck: globals mouse
local mouse = mouse
local root = client
local naughty = require('naughty')
local state = {
2023-01-21 15:34:13 +00:00
client = nil,
signals = {},
2023-01-18 23:37:21 +00:00
}
local function notify(msg, keep) -- {{{1
2023-01-21 15:34:13 +00:00
local timeout = 3
if keep then
timeout = 0
end
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
naughty.notify({ text = msg, timeout = timeout })
2023-01-18 23:37:21 +00:00
end
local function set_bounds() -- {{{1
2023-01-21 15:34:13 +00:00
local c = state.client
state.x_min = c.x + 5
state.x_max = c.x + c.width - 5
state.y_min = c.y + 5
state.y_max = c.y + c.height - 5
2023-01-18 23:37:21 +00:00
end
local function reposition() -- {{{1
2023-01-21 15:34:13 +00:00
local pos = mouse.coords()
local new_pos = {}
if pos.x > state.x_max then
new_pos.x = state.x_max
elseif pos.x < state.x_min then
new_pos.x = state.x_min
else
new_pos.x = pos.x
end
if pos.y > state.y_max then
new_pos.y = state.y_max
elseif pos.y < state.y_min then
new_pos.y = state.y_min
else
new_pos.y = pos.y
end
--new_pos.x = state.x_min + (state.x_max - state.x_min) / 2
--new_pos.y = state.y_min + (state.y_max - state.y_min) / 2
mouse.coords(new_pos, false)
2023-01-18 23:37:21 +00:00
end
local function connect(signal, func) -- {{{1
2023-01-21 15:34:13 +00:00
state.client:connect_signal(signal, func)
state.signals[signal] = func
2023-01-18 23:37:21 +00:00
end
local function disconnect() -- {{{1
2023-01-21 15:34:13 +00:00
for signal, func in pairs(state.signals) do
state.client:disconnect_signal(signal, func)
state.signals[signal] = nil
end
2023-01-18 23:37:21 +00:00
end
local function release() -- {{{1
2023-01-21 15:34:13 +00:00
disconnect()
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
root.disconnect_signal('mouse::move', reposition)
root.disconnect_signal('mouse::leave', reposition)
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
notify('Mouse released')
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
state.client = nil
2023-01-18 23:37:21 +00:00
end
local function constrain(c) -- {{{1
2023-01-21 15:34:13 +00:00
state.client = c or mouse.current_client
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
if state.client == nil then
return
end
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
set_bounds()
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
notify('Constrainging mouse to ' .. state.client.name)
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
root.connect_signal('mouse::move', reposition)
root.connect_signal('mouse::leave', reposition)
2023-01-18 23:37:21 +00:00
2023-01-21 15:34:13 +00:00
connect('mouse::move', reposition)
connect('mouse::leave', reposition)
connect('property::size', set_bounds)
connect('property::position', set_bounds)
connect('unmanage', release)
2023-01-18 23:37:21 +00:00
end
-- public interface -- {{{1
return {
2023-01-21 15:34:13 +00:00
constrain = constrain,
release = release,
toggle = function(c)
if state.client then
release()
else
constrain(c)
end
end,
2023-01-18 23:37:21 +00:00
}