From b7669161a0524fc1c7ddb2d9c0f3e31de9a903a5 Mon Sep 17 00:00:00 2001 From: Alastair Robertson Date: Wed, 9 Sep 2015 22:44:10 +0100 Subject: [PATCH] Add optional limit for nmaster/ncol based on number of tiled windows Closes https://github.com/awesomeWM/awesome/pull/430. --- awesomerc.lua | 12 +++++----- lib/awful/tag.lua | 56 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/awesomerc.lua b/awesomerc.lua index d9c7d0796..6a28cf1b6 100755 --- a/awesomerc.lua +++ b/awesomerc.lua @@ -253,12 +253,12 @@ globalkeys = awful.util.table.join( awful.key({ modkey, "Control" }, "r", awesome.restart), awful.key({ modkey, "Shift" }, "q", awesome.quit), - awful.key({ modkey, }, "l", function () awful.tag.incmwfact( 0.05) end), - awful.key({ modkey, }, "h", function () awful.tag.incmwfact(-0.05) end), - awful.key({ modkey, "Shift" }, "h", function () awful.tag.incnmaster( 1) end), - awful.key({ modkey, "Shift" }, "l", function () awful.tag.incnmaster(-1) end), - awful.key({ modkey, "Control" }, "h", function () awful.tag.incncol( 1) end), - awful.key({ modkey, "Control" }, "l", function () awful.tag.incncol(-1) end), + awful.key({ modkey, }, "l", function () awful.tag.incmwfact( 0.05) end), + awful.key({ modkey, }, "h", function () awful.tag.incmwfact(-0.05) end), + awful.key({ modkey, "Shift" }, "h", function () awful.tag.incnmaster( 1, nil, true) end), + awful.key({ modkey, "Shift" }, "l", function () awful.tag.incnmaster(-1, nil, true) end), + awful.key({ modkey, "Control" }, "h", function () awful.tag.incncol( 1, nil, true) end), + awful.key({ modkey, "Control" }, "l", function () awful.tag.incncol(-1, nil, true) end), awful.key({ modkey, }, "space", function () awful.layout.inc( 1) end), awful.key({ modkey, "Shift" }, "space", function () awful.layout.inc(-1) end), diff --git a/lib/awful/tag.lua b/lib/awful/tag.lua index 6a664dc5d..470447e57 100644 --- a/lib/awful/tag.lua +++ b/lib/awful/tag.lua @@ -26,6 +26,9 @@ local capi = root = root } +-- we use require("awful.client") inside functions to prevent circular dependencies. +local client + local tag = { mt = {} } -- Private data @@ -335,9 +338,10 @@ function tag.setscreen(t, s) end --- Get a tag's screen --- @param t tag object +-- @param[opt] t tag object -- @return Screen number function tag.getscreen(t) + local t = t or tag.selected() return tag.getproperty(t, "screen") end @@ -460,9 +464,27 @@ end --- Increase the number of master windows. -- @param add Value to add to number of master windows. --- @param t The tag to modify, if null tag.selected() is used. -function tag.incnmaster(add, t) - tag.setnmaster(tag.getnmaster(t) + add, t) +-- @param[opt] t The tag to modify, if null tag.selected() is used. +-- @tparam[opt=false] boolean sensible Limit nmaster based on the number of +-- visible tiled windows? +function tag.incnmaster(add, t, sensible) + if sensible then + client = client or require("awful.client") + local ntiled = #client.tiled(tag.getscreen(t)) + + local nmaster = tag.getnmaster(t) + if nmaster > ntiled then + nmaster = ntiled + end + + local newnmaster = nmaster + add + if newnmaster > ntiled then + newnmaster = ntiled + end + tag.setnmaster(newnmaster, t) + else + tag.setnmaster(tag.getnmaster(t) + add, t) + end end @@ -500,9 +522,29 @@ end --- Increase number of column windows. -- @param add Value to add to number of column windows. --- @param t The tag to modify, if null tag.selected() is used. -function tag.incncol(add, t) - tag.setncol(tag.getncol(t) + add, t) +-- @param[opt] t The tag to modify, if null tag.selected() is used. +-- @tparam[opt=false] boolean sensible Limit ncol based on the number of visible +-- tiled windows? +function tag.incncol(add, t, sensible) + if sensible then + client = client or require("awful.client") + local ntiled = #client.tiled(tag.getscreen(t)) + local nmaster = tag.getnmaster(t) + local nsecondary = ntiled - nmaster + + local ncol = tag.getncol(t) + if ncol > nsecondary then + ncol = nsecondary + end + + local newncol = ncol + add + if newncol > nsecondary then + newncol = nsecondary + end + tag.setncol(newncol, t) + else + tag.setncol(tag.getncol(t) + add, t) + end end --- View no tag.