]> git.madduck.net Git - etc/awesome.git/blob - layout/termfair.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:

small code fixes
[etc/awesome.git] / layout / termfair.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local tag       = require("awful.tag")
11 local beautiful = require("beautiful")
12 local math      = { ceil  = math.ceil,
13                     floor = math.floor,
14                     max   = math.max }
15 local tonumber  = tonumber
16
17 local termfair  = { name = "termfair" }
18
19 function termfair.arrange(p)
20     -- Layout with fixed number of vertical columns (read from nmaster).
21     -- New windows align from left to right. When a row is full, a now
22     -- one above it is created. Like this:
23
24     --        (1)                (2)                (3)
25     --   +---+---+---+      +---+---+---+      +---+---+---+
26     --   |   |   |   |      |   |   |   |      |   |   |   |
27     --   | 1 |   |   |  ->  | 2 | 1 |   |  ->  | 3 | 2 | 1 |  ->
28     --   |   |   |   |      |   |   |   |      |   |   |   |
29     --   +---+---+---+      +---+---+---+      +---+---+---+
30
31     --        (4)                (5)                (6)
32     --   +---+---+---+      +---+---+---+      +---+---+---+
33     --   | 4 |   |   |      | 5 | 4 |   |      | 6 | 5 | 4 |
34     --   +---+---+---+  ->  +---+---+---+  ->  +---+---+---+
35     --   | 3 | 2 | 1 |      | 3 | 2 | 1 |      | 3 | 2 | 1 |
36     --   +---+---+---+      +---+---+---+      +---+---+---+
37
38     -- A useless gap (like the dwm patch) can be defined with
39     -- beautiful.useless_gap_width.
40     local useless_gap = tonumber(beautiful.useless_gap_width) or 0
41
42     -- Screen.
43     local wa = p.workarea
44     local cls = p.clients
45
46     -- How many vertical columns?
47     local t = tag.selected(p.screen)
48     local num_x = termfair.nmaster or tag.getnmaster(t)
49
50     -- Do at least "desired_y" rows.
51     local desired_y = termfair.ncol or tag.getncol(t)
52
53     if #cls > 0
54     then
55         local num_y = math.max(math.ceil(#cls / num_x), desired_y)
56         local cur_num_x = num_x
57         local at_x = 0
58         local at_y = 0
59         local remaining_clients = #cls
60         local width = math.floor(wa.width / num_x)
61         local height = math.floor(wa.height / num_y)
62
63         -- We start the first row. Left-align by limiting the number of
64         -- available slots.
65         if remaining_clients < num_x
66         then
67             cur_num_x = remaining_clients
68         end
69
70         -- Iterate in reversed order.
71         for i = #cls,1,-1
72         do
73             -- Get x and y position.
74             local c = cls[i]
75             local this_x = cur_num_x - at_x - 1
76             local this_y = num_y - at_y - 1
77
78             -- Calc geometry.
79             local g = {}
80             if this_x == (num_x - 1)
81             then
82                 g.width = wa.width - (num_x - 1) * width
83             else
84                 g.width = width
85             end
86             if this_y == (num_y - 1)
87             then
88                 g.height = wa.height - (num_y - 1) * height
89             else
90                 g.height = height
91             end
92             g.x = wa.x + this_x * width
93             g.y = wa.y + this_y * height
94             if useless_gap > 0
95             then
96                 -- Top and left clients are shrinked by two steps and
97                 -- get moved away from the border. Other clients just
98                 -- get shrinked in one direction.
99                 if this_x == 0
100                 then
101                     g.width = g.width - 2 * useless_gap
102                     g.x = g.x + useless_gap
103                 else
104                     g.width = g.width - useless_gap
105                 end
106
107                 if this_y == 0
108                 then
109                     g.height = g.height - 2 * useless_gap
110                     g.y = g.y + useless_gap
111                 else
112                     g.height = g.height - useless_gap
113                 end
114             end
115             c:geometry(g)
116             remaining_clients = remaining_clients - 1
117
118             -- Next grid position.
119             at_x = at_x + 1
120             if at_x == num_x
121             then
122                 -- Row full, create a new one above it.
123                 at_x = 0
124                 at_y = at_y + 1
125
126                 -- We start a new row. Left-align.
127                 if remaining_clients < num_x
128                 then
129                     cur_num_x = remaining_clients
130                 end
131             end
132         end
133     end
134 end
135
136 return termfair