layout-machi/layout.lua

117 lines
3.3 KiB
Lua
Raw Normal View History

2019-07-07 22:19:18 +02:00
--- find the best region for the area-like object
-- @param c area-like object - table with properties x, y, width, and height
-- @param regions array of area-like objects
-- @return the index of the best region
local function find_region(c, regions)
local choice = 1
local choice_value = nil
local c_area = c.width * c.height
for i, a in ipairs(regions) do
local x_cap = max(0, min(c.x + c.width, a.x + a.width) - max(c.x, a.x))
local y_cap = max(0, min(c.y + c.height, a.y + a.height) - max(c.y, a.y))
local cap = x_cap * y_cap
-- -- a cap b / a cup b
-- local cup = c_area + a.width * a.height - cap
-- if cup > 0 then
-- local itx_ratio = cap / cup
-- if choice_value == nil or choice_value < itx_ratio then
-- choice_value = itx_ratio
-- choice = i
-- end
-- end
-- a cap b
if choice_value == nil or choice_value < cap then
choice = i
choice_value = cap
end
end
return choice
end
2019-07-04 23:32:05 +02:00
function do_arrange(p, priv)
local wa = p.workarea
local cls = p.clients
local regions = priv.regions
2019-07-07 22:19:18 +02:00
if #regions == 0 then return end
2019-07-04 23:32:05 +02:00
for i, c in ipairs(cls) do
if c.floating then
print("Ignore client " .. tostring(c))
else
if c.machi_region == nil then
2019-07-07 22:19:18 +02:00
c.machi_region = find_region(c, regions)
2019-07-05 21:08:17 +02:00
elseif c.machi_region > #regions then
2019-07-07 22:19:18 +02:00
c.machi_region = #regions
2019-07-05 21:08:17 +02:00
elseif c.machi_region <= 1 then
2019-07-07 22:20:16 +02:00
c.machi_region = 1
2019-07-04 23:32:05 +02:00
end
2019-07-07 22:19:18 +02:00
local region = c.machi_region
2019-07-04 23:32:05 +02:00
p.geometries[c] = {
x = regions[region].x,
y = regions[region].y,
width = regions[region].width,
height = regions[region].height,
}
print("Put client " .. tostring(c) .. " to region " .. region)
end
end
end
function create()
2019-07-05 23:04:18 +02:00
local priv = { regions = {} }
2019-07-04 23:32:05 +02:00
local function set_regions(regions)
priv.regions = regions
end
local function get_regions()
return priv.regions
end
2019-07-05 23:04:18 +02:00
-- move the closest region regardingly to the center distance
local function resize_handler(c, context, h)
if context ~= "mouse.move" then return end
if #priv.regions == 0 then return end
local center_x = h.x + h.width / 2
local center_y = h.y + h.height / 2
local choice = 1
local choice_value = nil
for i, r in ipairs(priv.regions) do
local r_x = r.x + r.width / 2
local r_y = r.y + r.height / 2
local dis = (r_x - center_x) * (r_x - center_x) + (r_y - center_y) * (r_y - center_y)
if choice_value == nil or choice_value > dis then
choice = i
choice_value = dis
end
end
if c.machi_region ~= choice then
c.machi_region = choice
c.x = priv.regions[choice].x
c.y = priv.regions[choice].y
c.width = priv.regions[choice].width
c.height = priv.regions[choice].height
end
end
2019-07-04 23:32:05 +02:00
return {
arrange = function (p) do_arrange(p, priv) end,
get_region_count = function () return #priv.regions end,
set_regions = set_regions,
get_regions = get_regions,
2019-07-06 00:42:56 +02:00
resize_handler = resize_handler,
2019-07-04 23:32:05 +02:00
}
end
return {
create = create,
2019-07-07 22:19:18 +02:00
find_region = find_region,
2019-07-04 23:32:05 +02:00
}