]> 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:

wiki updated
[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
93             g.x = wa.x + this_x * width
94             g.y = wa.y + this_y * height
95
96             if useless_gap > 0
97             then
98                 -- Top and left clients are shrinked by two steps and
99                 -- get moved away from the border. Other clients just
100                 -- get shrinked in one direction.
101                 
102                 gap_factor = (useless_gap / 100) * 2
103
104                 if this_x == 0
105                 then
106                     g.width = g.width - (2 + gap_factor) * useless_gap
107                     g.x = g.x + useless_gap
108                 else
109                     g.width = g.width - (1 + gap_factor) * useless_gap
110                 end
111
112                 if this_y == 0
113                 then
114                     g.height = g.height - (2 + gap_factor) * useless_gap
115                     g.y = g.y + useless_gap
116                 else
117                     g.height = g.height - (1 + gap_factor) * useless_gap
118                 end
119             end
120             c:geometry(g)
121             remaining_clients = remaining_clients - 1
122
123             -- Next grid position.
124             at_x = at_x + 1
125             if at_x == num_x
126             then
127                 -- Row full, create a new one above it.
128                 at_x = 0
129                 at_y = at_y + 1
130
131                 -- We start a new row. Left-align.
132                 if remaining_clients < num_x
133                 then
134                     cur_num_x = remaining_clients
135                 end
136             end
137         end
138     end
139 end
140
141 return termfair