Revise the placement example.

This commit is contained in:
Xinhao Yuan 2021-02-28 12:05:48 -05:00
parent c804572ac9
commit 8f3261d9da
2 changed files with 16 additions and 6 deletions

View File

@ -97,7 +97,7 @@ For `new_placement_cb` the arguments are:
The callback places the new client by changing its geometry and client data. The callback places the new client by changing its geometry and client data.
Note that after the callback machi will validate the geometry and fit into the areas. Note that after the callback machi will validate the geometry and fit into the areas.
So no need to set the `.area`, `.lu`, or `.rd` field of the client data in the callback. So no need to set the `.area`, `.lu`, or `.rd` field of the client data in the callback.
See `placement.fair` in `layout.lua` for an example. See `placement.empty_then_fair` in `layout.lua` for an example.
## The layout editor and commands ## The layout editor and commands

View File

@ -493,7 +493,7 @@ end
module.placement = {} module.placement = {}
function module.placement.fair(c, instance, areas, geometry) function module.placement.empty_then_fair(c, instance, areas, geometry)
local area_client_count = {} local area_client_count = {}
for _, oc in ipairs(c.screen.tiled_clients) do for _, oc in ipairs(c.screen.tiled_clients) do
local cd = instance.client_data[oc] local cd = instance.client_data[oc]
@ -501,18 +501,28 @@ function module.placement.fair(c, instance, areas, geometry)
area_client_count[cd.area] = (area_client_count[cd.area] or 0) + 1 area_client_count[cd.area] = (area_client_count[cd.area] or 0) + 1
end end
end end
local emptyness_max = nil local choice_client_count = nil
local choice_spare_score = nil
local choice = nil local choice = nil
for i = 1, #areas do for i = 1, #areas do
local a = areas[i] local a = areas[i]
if a.habitable then if a.habitable then
local emptyness = a.width * a.height / ((area_client_count[i] or 0) + 1) -- +1 for the new client
if emptyness_max == nil or emptyness > emptyness_max then local client_count = (area_client_count[i] or 0) + 1
emptyness_max = emptyness local spare_score = a.width * a.height / client_count
if choice == nil or (choice_client_count > 1 and client_count == 1) then
choice_client_count = client_count
choice_spare_score = spare_score
choice = i
elseif (choice_client_count > 1) == (client_count > 1) and choice_spare_score < spare_score then
choice_client_count = client_count
choice_spare_score = spare_score
choice = i choice = i
end end
end end
end end
instance.client_data[c].lu = choice
instance.client_data[c].rd = choice
instance.client_data[c].area = choice instance.client_data[c].area = choice
geometry.x = areas[choice].x geometry.x = areas[choice].x
geometry.y = areas[choice].y geometry.y = areas[choice].y