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

alsa, alsabar: make amixer call customizable; fixes #111 in general
[etc/awesome.git] / layout / uselessfair.lua
1
2 --[[
3
4      Licensed under GNU General Public License v2
5       * (c) 2014,      projektile, worron
6       * (c) 2013,      Luke Bonham
7       * (c) 2012,      Josh Komoroske
8       * (c) 2010-2012, Peter Hofmann
9
10 --]]
11
12 local beautiful = require("beautiful")
13 local ipairs    = ipairs
14 local math      = { ceil = math.ceil, sqrt = math.sqrt, floor = math.floor, max = math.max }
15 local tonumber  = tonumber
16
17 local uselessfair = {}
18
19 -- Transformation functions
20 local function swap(geometry)
21     return { x = geometry.y, y = geometry.x, width = geometry.height, height = geometry.width }
22 end
23
24 -- Client geometry correction depending on useless gap and window border
25 local function size_correction(c, geometry, useless_gap)
26     geometry.width  = math.max(geometry.width  - 2 * c.border_width - useless_gap, 1)
27     geometry.height = math.max(geometry.height - 2 * c.border_width - useless_gap, 1)
28     geometry.x = geometry.x + useless_gap / 2
29     geometry.y = geometry.y + useless_gap / 2
30 end
31
32 -- Main tiling function
33 local function fair(p, orientation)
34
35     -- Theme vars
36     local useless_gap = beautiful.useless_gap_width or 0
37     local global_border = beautiful.global_border_width or 0
38
39     -- Aliases
40     local wa = p.workarea
41     local cls = p.clients
42
43     -- Nothing to tile here
44     if #cls == 0 then return end
45
46     -- Workarea size correction depending on useless gap and global border
47     wa.height = wa.height - 2 * global_border - useless_gap
48     wa.width  = wa.width -  2 * global_border - useless_gap
49     wa.x = wa.x + useless_gap / 2 + global_border
50     wa.y = wa.y + useless_gap / 2 + global_border
51
52     -- Geometry calculation
53     local row, col = 0, 0
54
55     local rows = math.ceil(math.sqrt(#cls))
56     local cols = math.ceil(#cls / rows)
57
58     for i, c in ipairs(cls) do
59         local g = {}
60
61         -- find tile orientation for current client and swap geometry if need
62         local need_swap = (orientation == "east" and #cls <= 2) or (orientation == "south" and #cls > 2)
63         local area = need_swap and swap(wa) or wa
64
65         -- calculate geometry
66         if #cls < (cols * rows) and row == cols - 1 then
67             g.width = area.width / (rows - ((cols * rows) - #cls))
68         else
69             g.width = area.width / rows
70         end
71
72         g.height = area.height / cols
73         g.x = area.x + col * g.width
74         g.y = area.y + row * g.height
75
76         -- turn back to real if geometry was swapped
77         if need_swap then g = swap(g) end
78
79         -- window size correction depending on useless gap and window border
80         size_correction(c, g, useless_gap)
81
82         -- set geometry
83         c:geometry(g)
84
85         -- update tile grid coordinates
86         col = i % rows
87         row = math.floor(i / rows)
88     end
89 end
90
91 -- Layout constructor
92 local function construct_layout(name, direction)
93     return {
94         name = name,
95         -- @p screen The screen number to tile
96         arrange = function(p) return fair(p, direction) end
97     }
98 end
99
100 -- Build layouts with different tile direction
101 uselessfair.vertical   = construct_layout("uselessfair", "south")
102 uselessfair.horizontal = construct_layout("uselessfairh", "east")
103
104 -- Module aliase
105 uselessfair.arrange = uselessfair.vertical.arrange
106 uselessfair.name = uselessfair.vertical.name
107
108 return uselessfair