All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 Licensed under GNU General Public License v2
6 * (c) 2010, Nicolas Estibals
7 * (c) 2010-2012, Peter Hofmann
13 local tonumber = tonumber
15 local termfair = { name = "termfair" }
16 termfair.center = { name = "centerfair" }
18 local function do_fair(p, orientation)
19 local t = p.tag or screen[p.screen].selected_tag
23 if #cls == 0 then return end
25 if orientation == "west" then
26 -- Layout with fixed number of vertical columns (read from nmaster).
27 -- New windows align from left to right. When a row is full, a now
28 -- one above it is created. Like this:
31 -- +---+---+---+ +---+---+---+ +---+---+---+
32 -- | | | | | | | | | | | |
33 -- | 1 | | | -> | 2 | 1 | | -> | 3 | 2 | 1 | ->
34 -- | | | | | | | | | | | |
35 -- +---+---+---+ +---+---+---+ +---+---+---+
38 -- +---+---+---+ +---+---+---+ +---+---+---+
39 -- | 4 | | | | 5 | 4 | | | 6 | 5 | 4 |
40 -- +---+---+---+ -> +---+---+---+ -> +---+---+---+
41 -- | 3 | 2 | 1 | | 3 | 2 | 1 | | 3 | 2 | 1 |
42 -- +---+---+---+ +---+---+---+ +---+---+---+
44 -- How many vertical columns? Read from nmaster on the tag.
45 local num_x = tonumber(termfair.nmaster) or t.master_count
46 local ncol = tonumber(termfair.ncol) or t.column_count
48 if num_x <= 2 then num_x = 2 end
49 if ncol <= 1 then ncol = 1 end
50 local width = math.floor(wa.width/num_x)
52 local num_y = math.max(math.ceil(#cls / num_x), ncol)
53 local height = math.floor(wa.height/num_y)
54 local cur_num_x = num_x
58 local remaining_clients = #cls
60 -- We start the first row. Left-align by limiting the number of
62 if remaining_clients < num_x then
63 cur_num_x = remaining_clients
66 -- Iterate in reversed order.
68 -- Get x and y position.
70 local this_x = cur_num_x - at_x - 1
71 local this_y = num_y - at_y - 1
73 -- Calculate geometry.
75 if this_x == (num_x - 1) then
76 g.width = wa.width - (num_x - 1)*width
81 if this_y == (num_y - 1) then
82 g.height = wa.height - (num_y - 1)*height
87 g.x = wa.x + this_x*width
88 g.y = wa.y + this_y*height
90 if g.width < 1 then g.width = 1 end
91 if g.height < 1 then g.height = 1 end
95 remaining_clients = remaining_clients - 1
97 -- Next grid position.
100 -- Row full, create a new one above it.
104 -- We start a new row. Left-align.
105 if remaining_clients < num_x then
106 cur_num_x = remaining_clients
110 elseif orientation == "center" then
111 -- Layout with fixed number of vertical columns (read from nmaster).
112 -- Cols are centerded until there is nmaster columns, then windows
113 -- are stacked in the slave columns, with at most ncol clients per
114 -- column if possible.
116 -- with nmaster=3 and ncol=1 you'll have
118 -- +---+---+---+ +-+---+---+-+ +---+---+---+
119 -- | | | | | | | | | | | | |
120 -- | | 1 | | -> | | 1 | 2 | | -> | 1 | 2 | 3 | ->
121 -- | | | | | | | | | | | | |
122 -- +---+---+---+ +-+---+---+-+ +---+---+---+
125 -- +---+---+---+ +---+---+---+
126 -- | | | 3 | | | 2 | 4 |
127 -- + 1 + 2 +---+ -> + 1 +---+---+
128 -- | | | 4 | | | 3 | 5 |
129 -- +---+---+---+ +---+---+---+
131 -- How many vertical columns? Read from nmaster on the tag.
132 local num_x = tonumber(termfair.center.nmaster) or t.master_count
133 local ncol = tonumber(termfair.center.ncol) or t.column_count
135 if num_x <= 2 then num_x = 2 end
136 if ncol <= 1 then ncol = 1 end
138 local width = math.floor(wa.width / num_x)
141 -- Less clients than the number of columns, let's center it!
142 local offset_x = wa.x + (wa.width - #cls*width) / 2
144 local g = { y = wa.y }
147 if g.width < 1 then g.width = 1 end
148 if g.height < 1 then g.height = 1 end
149 g.x = offset_x + (i - 1) * width
150 p.geometries[cls[i]] = g
153 -- More clients than the number of columns, let's arrange it!
154 -- Master client deserves a special treatement
156 g.width = wa.width - (num_x - 1)*width
158 if g.width < 1 then g.width = 1 end
159 if g.height < 1 then g.height = 1 end
162 p.geometries[cls[1]] = g
164 -- Treat the other clients
166 -- Compute distribution of clients among columns
168 local remaining_clients = #cls-1
169 local ncol_min = math.ceil(remaining_clients/(num_x-1))
171 if ncol >= ncol_min then
172 for i = (num_x-1), 1, -1 do
173 if (remaining_clients-i+1) < ncol then
174 num_y[i] = remaining_clients-i + 1
178 remaining_clients = remaining_clients - num_y[i]
181 local rem = remaining_clients % (num_x-1)
183 for i = 1, num_x-1 do
187 for i = 1, num_x-1 do
188 num_y[i] = ncol_min - 1
191 num_y[num_x-1-i] = num_y[num_x-1-i] + 1
196 -- Compute geometry of the other clients
197 local nclient = 2 -- we start with the 2nd client
198 local wx = g.x + g.width
199 for i = 1, (num_x-1) do
200 local height = math.floor(wa.height / num_y[i])
202 for j = 0, (num_y[i]-2) do
208 if g.width < 1 then g.width = 1 end
209 if g.height < 1 then g.height = 1 end
210 p.geometries[cls[nclient]] = g
211 nclient = nclient + 1
217 g.height = wa.height - (num_y[i] - 1)*height
219 if g.width < 1 then g.width = 1 end
220 if g.height < 1 then g.height = 1 end
221 p.geometries[cls[nclient]] = g
222 nclient = nclient + 1
229 function termfair.center.arrange(p)
230 return do_fair(p, "center")
233 function termfair.arrange(p)
234 return do_fair(p, "west")