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

new imap widget
[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 =
18 {
19     name    = "termfair",
20
21     -- You can set the number of columns and rows,
22     -- -- otherwise they are read from awful.tag
23     nmaster = 0, -- columns
24     ncol    = 0  -- rows
25 }
26
27 function termfair.arrange(p)
28     -- Layout with fixed number of vertical columns (read from nmaster).
29     -- New windows align from left to right. When a row is full, a now
30     -- one above it is created. Like this:
31
32     --        (1)                (2)                (3)
33     --   +---+---+---+      +---+---+---+      +---+---+---+
34     --   |   |   |   |      |   |   |   |      |   |   |   |
35     --   | 1 |   |   |  ->  | 2 | 1 |   |  ->  | 3 | 2 | 1 |  ->
36     --   |   |   |   |      |   |   |   |      |   |   |   |
37     --   +---+---+---+      +---+---+---+      +---+---+---+
38
39     --        (4)                (5)                (6)
40     --   +---+---+---+      +---+---+---+      +---+---+---+
41     --   | 4 |   |   |      | 5 | 4 |   |      | 6 | 5 | 4 |
42     --   +---+---+---+  ->  +---+---+---+  ->  +---+---+---+
43     --   | 3 | 2 | 1 |      | 3 | 2 | 1 |      | 3 | 2 | 1 |
44     --   +---+---+---+      +---+---+---+      +---+---+---+
45
46     -- A useless gap (like the dwm patch) can be defined with
47     -- beautiful.useless_gap_width.
48     local useless_gap = tonumber(beautiful.useless_gap_width)
49     if useless_gap == nil
50     then
51         useless_gap = 0
52     end
53
54     -- Screen.
55     local wa = p.workarea
56     local cls = p.clients
57
58     -- How many vertical columns?
59     local t = tag.selected(p.screen)
60     local num_x
61     if termfair.nmaster ~= 0
62     then
63         num_x = termfair.nmaster
64     else
65         num_x = tag.getnmaster(t)
66     end
67
68     -- Do at least "desired_y" rows.
69     local desired_y
70     if termfair.ncol ~= 0
71     then
72         desired_y = termfair.ncol
73     else
74         desired_y = tag.getncol(t)
75     end
76
77     if #cls > 0
78     then
79         local num_y = math.max(math.ceil(#cls / num_x), desired_y)
80         local cur_num_x = num_x
81         local at_x = 0
82         local at_y = 0
83         local remaining_clients = #cls
84         local width = math.floor(wa.width / num_x)
85         local height = math.floor(wa.height / num_y)
86
87         -- We start the first row. Left-align by limiting the number of
88         -- available slots.
89         if remaining_clients < num_x
90         then
91             cur_num_x = remaining_clients
92         end
93
94         -- Iterate in reversed order.
95         for i = #cls,1,-1
96         do
97             -- Get x and y position.
98             local c = cls[i]
99             local this_x = cur_num_x - at_x - 1
100             local this_y = num_y - at_y - 1
101
102             -- Calc geometry.
103             local g = {}
104             if this_x == (num_x - 1)
105             then
106                 g.width = wa.width - (num_x - 1) * width
107             else
108                 g.width = width
109             end
110             if this_y == (num_y - 1)
111             then
112                 g.height = wa.height - (num_y - 1) * height
113             else
114                 g.height = height
115             end
116             g.x = wa.x + this_x * width
117             g.y = wa.y + this_y * height
118             if useless_gap > 0
119             then
120                 -- Top and left clients are shrinked by two steps and
121                 -- get moved away from the border. Other clients just
122                 -- get shrinked in one direction.
123                 if this_x == 0
124                 then
125                     g.width = g.width - 2 * useless_gap
126                     g.x = g.x + useless_gap
127                 else
128                     g.width = g.width - useless_gap
129                 end
130
131                 if this_y == 0
132                 then
133                     g.height = g.height - 2 * useless_gap
134                     g.y = g.y + useless_gap
135                 else
136                     g.height = g.height - useless_gap
137                 end
138             end
139             c:geometry(g)
140             remaining_clients = remaining_clients - 1
141
142             -- Next grid position.
143             at_x = at_x + 1
144             if at_x == num_x
145             then
146                 -- Row full, create a new one above it.
147                 at_x = 0
148                 at_y = at_y + 1
149
150                 -- We start a new row. Left-align.
151                 if remaining_clients < num_x
152                 then
153                     cur_num_x = remaining_clients
154                 end
155             end
156         end
157     end
158 end
159
160 return termfair