]> git.madduck.net Git - etc/awesome.git/blob - layout/centerfair.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wiki: some typos fixed
[etc/awesome.git] / layout / centerfair.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010,      Nicolas Estibals           
7       * (c) 2010-2012, Peter Hofmann              
8                                                   
9 --]]
10
11 local tag       = require("awful.tag")
12 local beautiful = require("beautiful")
13 local math      = { ceil  = math.ceil,
14                     floor = math.floor,
15                     max   = math.max }
16 local tonumber  = tonumber
17
18 local centerfair  = { name = "centerfair" }
19
20 function centerfair.arrange(p)
21     -- Layout with fixed number of vertical columns (read from nmaster).
22     -- Cols are centerded until there is nmaster columns, then windows
23     -- are stacked in the slave columns, with at most ncol clients per
24     -- column if possible.
25
26     -- with nmaster=3 and ncol=1 you'll have
27     --        (1)                (2)                (3)
28     --   +---+---+---+      +-+---+---+-+      +---+---+---+
29     --   |   |   |   |      | |   |   | |      |   |   |   |
30     --   |   | 1 |   |  ->  | | 1 | 2 | | ->   | 1 | 2 | 3 |  ->
31     --   |   |   |   |      | |   |   | |      |   |   |   |
32     --   +---+---+---+      +-+---+---+-+      +---+---+---+
33
34     --        (4)                (5)
35     --   +---+---+---+      +---+---+---+
36     --   |   |   | 3 |      |   | 2 | 4 |
37     --   + 1 + 2 +---+  ->  + 1 +---+---+
38     --   |   |   | 4 |      |   | 3 | 5 |
39     --   +---+---+---+      +---+---+---+
40
41     -- A useless gap (like the dwm patch) can be defined with
42     -- beautiful.useless_gap_width .
43     local useless_gap = tonumber(beautiful.useless_gap_width) or 0
44
45     -- Screen.
46     local wa = p.workarea
47     local cls = p.clients
48
49     -- How many vertical columns? Read from nmaster on the tag.
50     local t = tag.selected(p.screen)
51     local num_x = centerfair.nmaster or tag.getnmaster(t)
52     local ncol = centerfair.ncol or tag.getncol(t)
53
54     local width = math.floor((wa.width-(num_x+1)*useless_gap) / num_x)
55
56     local offset_y = wa.y + useless_gap
57     if #cls < num_x
58     then
59         -- Less clients than the number of columns, let's center it!
60         local offset_x = wa.x + useless_gap + (wa.width - #cls*width - (#cls+1)*useless_gap) / 2
61         local g = {}
62         g.width = width
63         g.height = wa.height - 2*useless_gap - 2
64         g.y = offset_y
65         for i = 1, #cls do
66             g.x = offset_x + (i - 1) * (width + useless_gap + 2)
67             cls[i]:geometry(g)
68         end
69     else
70         -- More clients than the number of columns, let's arrange it!
71         local offset_x = wa.x 
72         if useless_gap > 0 then
73            offset_x = offset_x 
74         end
75
76         -- Master client deserves a special treatement
77         local g = {}
78         g.width = wa.width - (num_x-1)*width -num_x*useless_gap - 2
79         g.height = wa.height - 2*useless_gap - 2
80         g.x = offset_x + useless_gap
81         g.y = offset_y
82         cls[1]:geometry(g)
83
84         -- Treat the other clients
85
86         -- Compute distribution of clients among columns
87         local num_y ={}
88         do
89             local remaining_clients = #cls-1
90             local ncol_min = math.ceil(remaining_clients/(num_x-1))
91             if ncol >= ncol_min
92             then
93                 for i = (num_x-1), 1, -1 do
94                     if (remaining_clients-i+1) < ncol
95                     then
96                         num_y[i] = remaining_clients-i + 1
97                     else
98                         num_y[i] = ncol
99                     end
100                     remaining_clients = remaining_clients - num_y[i]
101                 end
102             else
103                 local rem = remaining_clients % (num_x-1)
104                 if rem ==0
105                 then
106                     for i = 1, num_x-1 do
107                         num_y[i] = ncol_min
108                     end
109                 else
110                     for i = 1, num_x-1 do
111                         num_y[i] = ncol_min - 1
112                     end
113                     for i = 0, rem-1 do
114                         num_y[num_x-1-i] = num_y[num_x-1-i] + 1
115                     end
116                 end
117             end
118         end
119
120         -- Compute geometry of the other clients
121         local nclient = 2
122         g.x = g.x + g.width+useless_gap + 2
123         g.width = width
124
125         if useless_gap > 0 then
126             g.width = g.width - useless_gap/2 - 2
127         end
128
129         for i = 1, (num_x-1) do
130             to_remove = 2
131             g.height = math.floor((wa.height-useless_gap)/num_y[i]) 
132             g.y = offset_y
133             for j = 0, (num_y[i]-2) do
134                 cls[nclient]:geometry(g)
135                 nclient = nclient + 1
136                 g.y = g.y + g.height+useless_gap + 2
137                 to_remove = to_remove + 2
138             end
139             g.height = wa.height - num_y[i]*useless_gap - (num_y[i]-1)*g.height - useless_gap - to_remove
140             cls[nclient]:geometry(g)
141             nclient = nclient + 1
142             g.x = g.x+g.width+useless_gap + 2
143         end
144     end
145 end
146
147 return centerfair