From c2f8691b9cd9dd4a0521aaf64e7a9633f9394ffc Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Sun, 7 Jul 2019 16:19:18 -0400 Subject: [PATCH] refactoring --- editor.lua | 36 +++++------------------------------- layout.lua | 44 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/editor.lua b/editor.lua index 37d0d1c..d6fd0b2 100644 --- a/editor.lua +++ b/editor.lua @@ -1,3 +1,7 @@ +local machi = { + layout = require(".layout"), +} + local api = { beautiful = require("beautiful"), wibox = require("wibox"), @@ -56,36 +60,6 @@ local function set_region(c, r) api.layout.arrange(c.screen) end ---- 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 - --- fit the client into the machi of the screen -- @param c the client to fit -- @param cycle whether to cycle the region if the window is already in machi @@ -99,7 +73,7 @@ local function fit_region(c, cycle) current_region = c.machi_region or 1 if not is_tiling(c) then -- find out which region has the most intersection, calculated by a cap b / a cup b - c.machi_region = find_region(c, regions) + c.machi_region = machi.layout.find_region(c, regions) set_tiling(c) elseif cycle then if current_region >= #regions then diff --git a/layout.lua b/layout.lua index a765b95..5f1c342 100644 --- a/layout.lua +++ b/layout.lua @@ -1,23 +1,52 @@ +--- 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 + function do_arrange(p, priv) local wa = p.workarea local cls = p.clients local regions = priv.regions + if #regions == 0 then return end + for i, c in ipairs(cls) do if c.floating then print("Ignore client " .. tostring(c)) else - local region if c.machi_region == nil then - c.machi_region = 1 - region = 1 + c.machi_region = find_region(c, regions) elseif c.machi_region > #regions then - region = #regions + c.machi_region = #regions elseif c.machi_region <= 1 then - region = 1 - else - region = c.machi_region + c.machi_region = region = 1 end + local region = c.machi_region p.geometries[c] = { x = regions[region].x, @@ -83,4 +112,5 @@ end return { create = create, + find_region = find_region, }