mirror of https://github.com/lcpz/lain.git
Uselesstile code refactoring
This commit is contained in:
parent
99b8b39679
commit
1925436185
|
@ -2,7 +2,7 @@
|
||||||
--[[
|
--[[
|
||||||
|
|
||||||
Licensed under GNU General Public License v2
|
Licensed under GNU General Public License v2
|
||||||
* (c) 2014 projektile
|
* (c) 2014 projektile, worron
|
||||||
* (c) 2013 Luke Bonham
|
* (c) 2013 Luke Bonham
|
||||||
* (c) 2009 Donald Ephraim Curtis
|
* (c) 2009 Donald Ephraim Curtis
|
||||||
* (c) 2008 Julien Danjolu
|
* (c) 2008 Julien Danjolu
|
||||||
|
@ -13,204 +13,180 @@ local tag = require("awful.tag")
|
||||||
local beautiful = require("beautiful")
|
local beautiful = require("beautiful")
|
||||||
local ipairs = ipairs
|
local ipairs = ipairs
|
||||||
local math = { floor = math.floor,
|
local math = { floor = math.floor,
|
||||||
|
ceil = math.ceil,
|
||||||
max = math.max,
|
max = math.max,
|
||||||
min = math.min }
|
min = math.min }
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
|
|
||||||
local uselesstile = {}
|
local uselesstile = {}
|
||||||
|
|
||||||
local function tile_group(cls, wa, orientation, fact, group, gap)
|
-- Transformation functions
|
||||||
|
local function flip(canvas, geometry)
|
||||||
-- Themes border width requires an offset
|
return {
|
||||||
local bw = tonumber(beautiful.border_width) or 0
|
-- vertical only
|
||||||
|
x = 2 * canvas.x + canvas.width - geometry.x - geometry.width,
|
||||||
-- get our orientation right
|
y = geometry.y,
|
||||||
local height = "height"
|
width = geometry.width,
|
||||||
local width = "width"
|
height = geometry.height
|
||||||
local x = "x"
|
}
|
||||||
local y = "y"
|
|
||||||
if orientation == "top" or orientation == "bottom" then
|
|
||||||
height = "width"
|
|
||||||
width = "height"
|
|
||||||
x = "y"
|
|
||||||
y = "x"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- make this more generic (not just width)
|
local function swap(geometry)
|
||||||
--if for top
|
return { x = geometry.y, y = geometry.x, width = geometry.height, height = geometry.width }
|
||||||
available = wa[width] - (group.coord - wa[x]) -- it's truly not here
|
|
||||||
|
|
||||||
-- find our total values
|
|
||||||
local total_fact = 0
|
|
||||||
local min_fact = 1
|
|
||||||
local size = group.size
|
|
||||||
for c = group.first,group.last do
|
|
||||||
-- determine the width/height based on the size_hint
|
|
||||||
local i = c - group.first +1
|
|
||||||
local size_hints = cls[c].size_hints
|
|
||||||
local size_hint = size_hints["min_"..width] or size_hints["base_"..width] or 0
|
|
||||||
size_hint = size_hint + cls[c].border_width*2
|
|
||||||
size = math.max(size_hint, size)
|
|
||||||
|
|
||||||
-- calculate the height
|
|
||||||
if not fact[i] then
|
|
||||||
fact[i] = min_fact
|
|
||||||
else
|
|
||||||
min_fact = math.min(fact[i],min_fact)
|
|
||||||
end
|
|
||||||
total_fact = total_fact + fact[i]
|
|
||||||
end
|
|
||||||
size = math.min(size, available)
|
|
||||||
local coord = wa[y]
|
|
||||||
local geom = {}
|
|
||||||
local used_size = 0
|
|
||||||
local unused = wa[height]
|
|
||||||
local stat_coord = wa[x]
|
|
||||||
--stat_coord = size
|
|
||||||
for c = group.first,group.last do
|
|
||||||
local i = c - group.first +1
|
|
||||||
geom[width] = size - (bw * 2)
|
|
||||||
geom[height] = math.floor(unused * fact[i] / total_fact) - (bw * 2)
|
|
||||||
geom[x] = group.coord
|
|
||||||
geom[y] = coord
|
|
||||||
|
|
||||||
coord = coord + geom[height] + 2 * bw
|
|
||||||
unused = unused - geom[height] - 2 * bw
|
|
||||||
total_fact = total_fact - fact[i]
|
|
||||||
used_size = math.max(used_size, geom[width] + 2 * bw)
|
|
||||||
|
|
||||||
-- Useless gap correction
|
|
||||||
geom.width = geom.width - gap
|
|
||||||
geom.height = geom.height - gap
|
|
||||||
|
|
||||||
geom = cls[c]:geometry(geom)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return used_size
|
-- Find geometry for column/row tiling
|
||||||
|
local function cut_area(wa, total, index, is_horizontal)
|
||||||
|
local wa = is_horizontal and swap(wa) or wa
|
||||||
|
local height = wa.height / total
|
||||||
|
|
||||||
|
local area = {
|
||||||
|
x = wa.x,
|
||||||
|
y = wa.y + (index - 1) * height,
|
||||||
|
width = wa.width,
|
||||||
|
height = height
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_horizontal then area = swap(area) end
|
||||||
|
|
||||||
|
return area
|
||||||
end
|
end
|
||||||
|
|
||||||
local function tile(param, orientation)
|
-- Client geometry correction depending on useless gap and window border
|
||||||
local t = tag.selected(param.screen)
|
local function size_correction(c, geometry, useless_gap)
|
||||||
orientation = orientation or "right"
|
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)
|
||||||
-- A useless gap (like the dwm patch) can be defined with
|
geometry.x = geometry.x + useless_gap / 2
|
||||||
-- beautiful.useless_gap_width .
|
geometry.y = geometry.y + useless_gap / 2
|
||||||
local useless_gap = tonumber(beautiful.useless_gap_width) or 0
|
|
||||||
if useless_gap < 0 then useless_gap = 0 end
|
|
||||||
|
|
||||||
-- A global border can be defined with
|
|
||||||
-- beautiful.global_border_width
|
|
||||||
local global_border = tonumber(beautiful.global_border_width) or 0
|
|
||||||
if global_border < 0 then global_border = 0 end
|
|
||||||
|
|
||||||
-- this handles are different orientations
|
|
||||||
local height = "height"
|
|
||||||
local width = "width"
|
|
||||||
local x = "x"
|
|
||||||
local y = "y"
|
|
||||||
if orientation == "top" or orientation == "bottom" then
|
|
||||||
height = "width"
|
|
||||||
width = "height"
|
|
||||||
x = "y"
|
|
||||||
y = "x"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local cls = param.clients
|
-- Tile group of clients in given area
|
||||||
|
-- @canvas need for proper transformation only
|
||||||
|
local function tile_column(canvas, area, list, useless_gap, transformation)
|
||||||
|
for i, c in ipairs(list) do
|
||||||
|
local g = cut_area(area, #list, i)
|
||||||
|
|
||||||
|
-- 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)
|
||||||
|
|
||||||
|
c:geometry(g)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--Main tile function
|
||||||
|
local function tile(p, orientation)
|
||||||
|
|
||||||
|
-- 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)
|
local nmaster = math.min(tag.getnmaster(t), #cls)
|
||||||
local nother = math.max(#cls - nmaster,0)
|
|
||||||
|
|
||||||
local mwfact = tag.getmwfact(t)
|
local mwfact = tag.getmwfact(t)
|
||||||
local wa = param.workarea
|
|
||||||
local ncol = tag.getncol(t)
|
|
||||||
|
|
||||||
-- Workarea size correction
|
if nmaster == 0 then
|
||||||
|
mwfact = 0
|
||||||
|
elseif nmaster == #cls then
|
||||||
|
mwfact = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Workarea size correction depending on useless gap and global border
|
||||||
wa.height = wa.height - 2 * global_border - useless_gap
|
wa.height = wa.height - 2 * global_border - useless_gap
|
||||||
wa.width = wa.width - 2 * global_border - useless_gap
|
wa.width = wa.width - 2 * global_border - useless_gap
|
||||||
wa.x = wa.x + useless_gap + global_border
|
wa.x = wa.x + useless_gap / 2 + global_border
|
||||||
wa.y = wa.y + useless_gap + global_border
|
wa.y = wa.y + useless_gap / 2 + global_border
|
||||||
|
|
||||||
local data = tag.getdata(t).windowfact
|
-- Find which transformation we need for given orientation
|
||||||
|
local transformation = {
|
||||||
|
swap = orientation == 'top' or orientation == 'bottom',
|
||||||
|
flip = orientation == 'left' or orientation == 'top'
|
||||||
|
}
|
||||||
|
|
||||||
if not data then
|
-- Swap workarea dimensions if orientation vertical
|
||||||
data = {}
|
if transformation.swap then wa = swap(wa) end
|
||||||
tag.getdata(t).windowfact = data
|
|
||||||
|
-- Split master and other windows
|
||||||
|
local cls_master, cls_other = {}, {}
|
||||||
|
|
||||||
|
for i, c in ipairs(cls) do
|
||||||
|
if i <= nmaster then
|
||||||
|
table.insert(cls_master, c)
|
||||||
|
else
|
||||||
|
table.insert(cls_other, c)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local coord = wa[x]
|
-- Tile master windows
|
||||||
local place_master = true
|
local master_area = {
|
||||||
if orientation == "left" or orientation == "top" then
|
x = wa.x,
|
||||||
-- if we are on the left or top we need to render the other windows first
|
y = wa.y,
|
||||||
place_master = false
|
width = nmaster > 0 and wa.width * mwfact or 0,
|
||||||
end
|
height = wa.height
|
||||||
|
}
|
||||||
|
|
||||||
-- this was easier than writing functions because there is a lot of data we need
|
tile_column(wa, master_area, cls_master, useless_gap, transformation)
|
||||||
for d = 1,2 do
|
|
||||||
if place_master and nmaster > 0 then
|
|
||||||
local size = wa[width]
|
|
||||||
if nother > 0 then
|
|
||||||
size = math.min(wa[width] * mwfact, wa[width] - (coord - wa[x]))
|
|
||||||
end
|
|
||||||
if not data[0] then
|
|
||||||
data[0] = {}
|
|
||||||
end
|
|
||||||
coord = coord + tile_group(cls, wa, orientation, data[0], {first=1, last=nmaster, coord = coord, size = size}, useless_gap)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not place_master and nother > 0 then
|
-- Tile other windows
|
||||||
local last = nmaster
|
local other_area = {
|
||||||
|
x = wa.x + master_area.width,
|
||||||
|
y = wa.y,
|
||||||
|
width = wa.width - master_area.width,
|
||||||
|
height = wa.height
|
||||||
|
}
|
||||||
|
|
||||||
-- we have to modify the work area size to consider left and top views
|
-- get column number for other windows
|
||||||
local wasize = wa[width]
|
local ncol = math.min(tag.getncol(t), #cls_other)
|
||||||
if nmaster > 0 and (orientation == "left" or orientation == "top") then
|
|
||||||
wasize = wa[width] - wa[width]*mwfact
|
-- split other windows to column groups
|
||||||
end
|
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
|
for i = 1, ncol do
|
||||||
-- Try to get equal width among remaining columns
|
local position = transformation.flip and ncol - i + 1 or i
|
||||||
local size = math.min((wasize - (coord - wa[x])) / (ncol - i + 1)) --+ (global_border/(ncol))/(ncol+i^2)
|
local rows = i <= last_small_column and rows_min or rows_min + 1
|
||||||
local first = last + 1
|
local column = {}
|
||||||
last = last + math.floor((#cls - last)/(ncol - i + 1))
|
|
||||||
-- tile the column and update our current x coordinate
|
for j = 1, rows do
|
||||||
if not data[i] then
|
table.insert(column, cls_other[client_index])
|
||||||
data[i] = {}
|
client_index = client_index + 1
|
||||||
end
|
|
||||||
coord = coord + tile_group(cls, wa, orientation, data[i], { first = first, last = last, coord = coord, size = size }, useless_gap)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
place_master = not place_master
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- and tile
|
||||||
|
local column_area = cut_area(other_area, ncol, position, true)
|
||||||
|
tile_column(wa, column_area, column, useless_gap, transformation)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
uselesstile.right = {}
|
-- Layout constructor
|
||||||
uselesstile.right.name = "uselesstile"
|
local function construct_layout(name, orientation)
|
||||||
uselesstile.right.arrange = tile
|
return {
|
||||||
|
name = name,
|
||||||
--- The main tile algo, on left.
|
-- @p screen number to tile
|
||||||
-- @param screen The screen number to tile.
|
arrange = function(p) return tile(p, orientation) end
|
||||||
uselesstile.left = {}
|
}
|
||||||
uselesstile.left.name = "uselesstileleft"
|
|
||||||
function uselesstile.left.arrange(p)
|
|
||||||
return tile(p, "left")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- The main tile algo, on bottom.
|
-- Build layouts with different tile direction
|
||||||
-- @param screen The screen number to tile.
|
uselesstile.right = construct_layout("uselesstile", "right")
|
||||||
uselesstile.bottom = {}
|
uselesstile.left = construct_layout("uselesstileleft", "left")
|
||||||
uselesstile.bottom.name = "uselesstilebottom"
|
uselesstile.bottom = construct_layout("uselesstilebottom", "bottom")
|
||||||
function uselesstile.bottom.arrange(p)
|
uselesstile.top = construct_layout("uselesstiletop", "top")
|
||||||
return tile(p, "bottom")
|
|
||||||
end
|
|
||||||
|
|
||||||
--- The main tile algo, on top.
|
|
||||||
-- @param screen The screen number to tile.
|
|
||||||
uselesstile.top = {}
|
|
||||||
uselesstile.top.name = "uselesstiletop"
|
|
||||||
function uselesstile.top.arrange(p)
|
|
||||||
return tile(p, "top")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
-- Module aliase
|
||||||
uselesstile.arrange = uselesstile.right.arrange
|
uselesstile.arrange = uselesstile.right.arrange
|
||||||
uselesstile.name = uselesstile.right.name
|
uselesstile.name = uselesstile.right.name
|
||||||
|
|
||||||
return uselesstile
|
return uselesstile
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue