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.
4 Licensed under GNU General Public License v2
6 * (c) 2013, Luke Bonham
7 * (c) 2010, Nicolas Estibals
8 * (c) 2010-2012, Peter Hofmann
12 local math = { ceil = math.ceil,
16 local tonumber = tonumber
18 local termfair = { name = "termfair" }
19 termfair.center = { name = "centerfair" }
21 local function do_fair(p, orientation)
22 local t = p.tag or screen[p.screen].selected_tag
26 if #cls == 0 then return end
28 if orientation == "west" then
29 -- Layout with fixed number of vertical columns (read from nmaster).
30 -- New windows align from left to right. When a row is full, a now
31 -- one above it is created. Like this:
34 -- +---+---+---+ +---+---+---+ +---+---+---+
35 -- | | | | | | | | | | | |
36 -- | 1 | | | -> | 2 | 1 | | -> | 3 | 2 | 1 | ->
37 -- | | | | | | | | | | | |
38 -- +---+---+---+ +---+---+---+ +---+---+---+
41 -- +---+---+---+ +---+---+---+ +---+---+---+
42 -- | 4 | | | | 5 | 4 | | | 6 | 5 | 4 |
43 -- +---+---+---+ -> +---+---+---+ -> +---+---+---+
44 -- | 3 | 2 | 1 | | 3 | 2 | 1 | | 3 | 2 | 1 |
45 -- +---+---+---+ +---+---+---+ +---+---+---+
47 -- How many vertical columns? Read from nmaster on the tag.
48 local num_x = tonumber(termfair.nmaster) or t.master_count
49 local ncol = tonumber(termfair.ncol) or t.column_count
51 if num_x <= 2 then num_x = 2 end
52 if ncol <= 1 then ncol = 1 end
53 local width = math.floor(wa.width/num_x)
55 local num_y = math.max(math.ceil(#cls / num_x), ncol)
56 local height = math.floor(wa.height/num_y)
57 local cur_num_x = num_x
61 local remaining_clients = #cls
63 -- We start the first row. Left-align by limiting the number of
65 if remaining_clients < num_x then
66 cur_num_x = remaining_clients
69 -- Iterate in reversed order.
71 -- Get x and y position.
73 local this_x = cur_num_x - at_x - 1
74 local this_y = num_y - at_y - 1
76 -- Calculate geometry.
78 if this_x == (num_x - 1) then
79 g.width = wa.width - (num_x - 1)*width
84 if this_y == (num_y - 1) then
85 g.height = wa.height - (num_y - 1)*height
90 g.x = wa.x + this_x*width
91 g.y = wa.y + this_y*height
93 if g.width < 1 then g.width = 1 end
94 if g.height < 1 then g.height = 1 end
98 remaining_clients = remaining_clients - 1
100 -- Next grid position.
102 if at_x == num_x then
103 -- Row full, create a new one above it.
107 -- We start a new row. Left-align.
108 if remaining_clients < num_x then
109 cur_num_x = remaining_clients
113 elseif orientation == "center" then
114 -- Layout with fixed number of vertical columns (read from nmaster).
115 -- Cols are centerded until there is nmaster columns, then windows
116 -- are stacked in the slave columns, with at most ncol clients per
117 -- column if possible.
119 -- with nmaster=3 and ncol=1 you'll have
121 -- +---+---+---+ +-+---+---+-+ +---+---+---+
122 -- | | | | | | | | | | | | |
123 -- | | 1 | | -> | | 1 | 2 | | -> | 1 | 2 | 3 | ->
124 -- | | | | | | | | | | | | |
125 -- +---+---+---+ +-+---+---+-+ +---+---+---+
128 -- +---+---+---+ +---+---+---+
129 -- | | | 3 | | | 2 | 4 |
130 -- + 1 + 2 +---+ -> + 1 +---+---+
131 -- | | | 4 | | | 3 | 5 |
132 -- +---+---+---+ +---+---+---+
134 -- How many vertical columns? Read from nmaster on the tag.
135 local num_x = tonumber(termfair.center.nmaster) or t.master_count
136 local ncol = tonumber(termfair.center.ncol) or t.column_count
138 if num_x <= 2 then num_x = 2 end
139 if ncol <= 1 then ncol = 1 end
141 local width = math.floor(wa.width / num_x)
144 -- Less clients than the number of columns, let's center it!
145 local offset_x = wa.x + (wa.width - #cls*width) / 2
147 local g = { y = wa.y }
150 if g.width < 1 then g.width = 1 end
151 if g.height < 1 then g.height = 1 end
152 g.x = offset_x + (i - 1) * width
153 p.geometries[cls[i]] = g
156 -- More clients than the number of columns, let's arrange it!
157 -- Master client deserves a special treatement
159 g.width = wa.width - (num_x - 1)*width
161 if g.width < 1 then g.width = 1 end
162 if g.height < 1 then g.height = 1 end
165 p.geometries[cls[1]] = g
167 -- Treat the other clients
169 -- Compute distribution of clients among columns
171 local remaining_clients = #cls-1
172 local ncol_min = math.ceil(remaining_clients/(num_x-1))
174 if ncol >= ncol_min then
175 for i = (num_x-1), 1, -1 do
176 if (remaining_clients-i+1) < ncol then
177 num_y[i] = remaining_clients-i + 1
181 remaining_clients = remaining_clients - num_y[i]
184 local rem = remaining_clients % (num_x-1)
186 for i = 1, num_x-1 do
190 for i = 1, num_x-1 do
191 num_y[i] = ncol_min - 1
194 num_y[num_x-1-i] = num_y[num_x-1-i] + 1
199 -- Compute geometry of the other clients
200 local nclient = 2 -- we start with the 2nd client
201 local wx = g.x + g.width
202 for i = 1, (num_x-1) do
203 local height = math.floor(wa.height / num_y[i])
205 for j = 0, (num_y[i]-2) do
211 if g.width < 1 then g.width = 1 end
212 if g.height < 1 then g.height = 1 end
213 p.geometries[cls[nclient]] = g
214 nclient = nclient + 1
220 g.height = wa.height - (num_y[i] - 1)*height
222 if g.width < 1 then g.width = 1 end
223 if g.height < 1 then g.height = 1 end
224 p.geometries[cls[nclient]] = g
225 nclient = nclient + 1
232 function termfair.center.arrange(p)
233 return do_fair(p, "center")
236 function termfair.arrange(p)
237 return do_fair(p, "west")