From 8f3261d9dad28a524d61f54a12d0b92cb5ccf1b0 Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Sun, 28 Feb 2021 12:05:48 -0500 Subject: [PATCH] Revise the placement example. --- README.md | 2 +- layout.lua | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6875ed6..2dac9d5 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ For `new_placement_cb` the arguments are: 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. 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 diff --git a/layout.lua b/layout.lua index f6f8006..a7be1f6 100644 --- a/layout.lua +++ b/layout.lua @@ -493,7 +493,7 @@ end module.placement = {} -function module.placement.fair(c, instance, areas, geometry) +function module.placement.empty_then_fair(c, instance, areas, geometry) local area_client_count = {} for _, oc in ipairs(c.screen.tiled_clients) do 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 end end - local emptyness_max = nil + local choice_client_count = nil + local choice_spare_score = nil local choice = nil for i = 1, #areas do local a = areas[i] if a.habitable then - local emptyness = a.width * a.height / ((area_client_count[i] or 0) + 1) - if emptyness_max == nil or emptyness > emptyness_max then - emptyness_max = emptyness + -- +1 for the new client + local client_count = (area_client_count[i] or 0) + 1 + 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 end end end + instance.client_data[c].lu = choice + instance.client_data[c].rd = choice instance.client_data[c].area = choice geometry.x = areas[choice].x geometry.y = areas[choice].y