lain/layout/uselesstile.lua

229 lines
6.8 KiB
Lua
Raw Normal View History

2013-09-07 12:06:42 +02:00
--[[
2015-07-03 12:17:31 +02:00
Licensed under GNU General Public License v2
* (c) 2014, projektile, worron
* (c) 2013, Luke Bonham
* (c) 2009, Donald Ephraim Curtis
* (c) 2008, Julien Danjolu
2013-09-07 12:06:42 +02:00
--]]
local tag = require("awful.tag")
local beautiful = require("beautiful")
local ipairs = ipairs
2013-09-13 18:15:25 +02:00
local math = { floor = math.floor,
2015-01-10 17:52:08 +01:00
ceil = math.ceil,
2013-09-07 12:06:42 +02:00
max = math.max,
min = math.min }
local tonumber = tonumber
local uselesstile = {}
2015-01-10 17:52:08 +01:00
-- Transformation functions
local function flip(canvas, geometry)
return {
-- vertical only
x = 2 * canvas.x + canvas.width - geometry.x - geometry.width,
y = geometry.y,
width = geometry.width,
height = geometry.height
}
end
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
local function swap(geometry)
return { x = geometry.y, y = geometry.x, width = geometry.height, height = geometry.width }
end
2015-02-12 18:16:14 +01:00
-- Find geometry for secondary windows column
local function cut_column(wa, n, index)
local width = math.floor(wa.width / n)
2015-02-12 18:16:14 +01:00
local area = { x = wa.x + (index - 1) * width, y = wa.y, width = width, height = wa.height }
2015-01-10 17:52:08 +01:00
2015-02-12 18:16:14 +01:00
return area
end
2013-09-07 12:06:42 +02:00
2015-02-12 18:16:14 +01:00
-- Find geometry for certain window in column
local function cut_row(wa, factor, index, used)
local height = math.floor(wa.height * factor.window[index] / factor.total)
2015-02-12 18:16:14 +01:00
local area = { x = wa.x, y = wa.y + used, width = wa.width, height = height }
2015-01-10 17:52:08 +01:00
return area
end
-- Client geometry correction depending on useless gap and window border
local function size_correction(c, geometry, useless_gap)
geometry.width = math.max(geometry.width - 2 * c.border_width - useless_gap, 1)
geometry.height = math.max(geometry.height - 2 * c.border_width - useless_gap, 1)
geometry.x = math.floor(geometry.x + useless_gap / 2)
geometry.y = math.floor(geometry.y + useless_gap / 2)
2013-09-07 12:06:42 +02:00
end
2015-02-12 18:16:14 +01:00
-- Check size factor for group of clients and calculate total
local function calc_factor(n, winfactors)
local factor = { window = winfactors, total = 0, min = 1 }
for i = 1, n do
if not factor.window[i] then
factor.window[i] = factor.min
else
factor.min = math.min(factor.window[i], factor.min)
if factor.window[i] < 0.05 then factor.window[i] = 0.05 end
end
factor.total = factor.total + factor.window[i]
end
return factor
end
2015-01-10 17:52:08 +01:00
-- Tile group of clients in given area
-- @canvas need for proper transformation only
2015-02-12 18:16:14 +01:00
-- @winfactors table with clients size factors
local function tile_column(canvas, area, list, useless_gap, transformation, winfactors)
local used = 0
local factor = calc_factor(#list, winfactors)
2015-01-10 17:52:08 +01:00
for i, c in ipairs(list) do
2015-02-12 18:16:14 +01:00
local g = cut_row(area, factor, i, used)
if i == #list then g.height = area.height - used end
2015-02-12 18:16:14 +01:00
used = used + g.height
2015-01-10 17:52:08 +01:00
-- swap workarea dimensions
if transformation.flip then g = flip(canvas, g) end
if transformation.swap then g = swap(g) end
-- useless gap and border correction
size_correction(c, g, useless_gap)
2015-01-10 17:52:08 +01:00
c:geometry(g)
2013-09-07 12:06:42 +02:00
end
2015-01-10 17:52:08 +01:00
end
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
--Main tile function
local function tile(p, orientation)
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
-- Theme vars
local useless_gap = beautiful.useless_gap_width or 0
local global_border = beautiful.global_border_width or 0
-- Aliases
local wa = p.workarea
local cls = p.clients
local t = tag.selected(p.screen)
-- Nothing to tile here
if #cls == 0 then return end
-- Get tag prop
local nmaster = math.min(tag.getnmaster(t), #cls)
2013-09-07 12:06:42 +02:00
local mwfact = tag.getmwfact(t)
2015-01-10 17:52:08 +01:00
if nmaster == 0 then
mwfact = 0
elseif nmaster == #cls then
mwfact = 1
end
2015-02-12 18:16:14 +01:00
-- clients size factor
local data = tag.getdata(t).windowfact
if not data then
data = {}
tag.getdata(t).windowfact = data
end
2015-01-10 17:52:08 +01:00
-- Workarea size correction depending on useless gap and global border
wa.height = wa.height - 2 * global_border - useless_gap
wa.width = wa.width - 2 * global_border - useless_gap
2015-01-10 17:52:08 +01:00
wa.x = wa.x + useless_gap / 2 + global_border
wa.y = wa.y + useless_gap / 2 + global_border
2015-01-10 17:52:08 +01:00
-- Find which transformation we need for given orientation
local transformation = {
swap = orientation == 'top' or orientation == 'bottom',
flip = orientation == 'left' or orientation == 'top'
}
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
-- Swap workarea dimensions if orientation vertical
if transformation.swap then wa = swap(wa) end
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
-- Split master and other windows
local cls_master, cls_other = {}, {}
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
for i, c in ipairs(cls) do
if i <= nmaster then
table.insert(cls_master, c)
else
table.insert(cls_other, c)
2013-09-07 12:06:42 +02:00
end
end
2015-01-10 17:52:08 +01:00
-- Tile master windows
local master_area = {
x = wa.x,
y = wa.y,
width = nmaster > 0 and math.floor(wa.width * mwfact) or 0,
2015-01-10 17:52:08 +01:00
height = wa.height
}
2015-02-12 18:16:14 +01:00
if not data[0] then data[0] = {} end
tile_column(wa, master_area, cls_master, useless_gap, transformation, data[0])
2015-01-10 17:52:08 +01:00
-- Tile other windows
local other_area = {
x = wa.x + master_area.width,
y = wa.y,
width = wa.width - master_area.width,
height = wa.height
}
-- get column number for other windows
local ncol = math.min(tag.getncol(t), #cls_other)
2015-05-19 14:11:36 +02:00
if ncol == 0 then ncol = 1 end
2015-01-10 17:52:08 +01:00
-- split other windows to column groups
local last_small_column = ncol - #cls_other % ncol
local rows_min = math.floor(#cls_other / ncol)
local client_index = 1
for i = 1, ncol do
local position = transformation.flip and ncol - i + 1 or i
local rows = i <= last_small_column and rows_min or rows_min + 1
local column = {}
for j = 1, rows do
table.insert(column, cls_other[client_index])
client_index = client_index + 1
end
2013-09-07 12:06:42 +02:00
2015-02-12 18:16:14 +01:00
-- and tile
local column_area = cut_column(other_area, ncol, position)
if not data[i] then data[i] = {} end
tile_column(wa, column_area, column, useless_gap, transformation, data[i])
2015-01-10 17:52:08 +01:00
end
2013-09-07 12:06:42 +02:00
end
2015-01-10 17:52:08 +01:00
-- Layout constructor
local function construct_layout(name, orientation)
return {
name = name,
-- @p screen number to tile
arrange = function(p) return tile(p, orientation) end
}
2013-09-07 12:06:42 +02:00
end
2015-01-10 17:52:08 +01:00
-- Build layouts with different tile direction
uselesstile.right = construct_layout("uselesstile", "right")
uselesstile.left = construct_layout("uselesstileleft", "left")
uselesstile.bottom = construct_layout("uselesstilebottom", "bottom")
uselesstile.top = construct_layout("uselesstiletop", "top")
2013-09-07 12:06:42 +02:00
2015-01-10 17:52:08 +01:00
-- Module aliase
2013-09-07 12:06:42 +02:00
uselesstile.arrange = uselesstile.right.arrange
uselesstile.name = uselesstile.right.name
return uselesstile