Ported wibox.layout to lua 5.2

Tested with lua 5.1: all good

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
This commit is contained in:
Arvydas Sidorenko 2012-06-12 15:29:52 +02:00 committed by Uli Schlachter
parent 3f5eaf9c1b
commit 4e67027a97
9 changed files with 94 additions and 63 deletions

View File

@ -13,6 +13,7 @@ local pcall = pcall
local setmetatable = setmetatable local setmetatable = setmetatable
local capi = { button = button } local capi = { button = button }
local util = require("awful.util") local util = require("awful.util")
local wibox = require("wibox")
local imagebox = require("wibox.widget.imagebox") local imagebox = require("wibox.widget.imagebox")
local textbox = require("wibox.widget.textbox") local textbox = require("wibox.widget.textbox")

View File

@ -11,7 +11,8 @@ local type = type
local base = require("wibox.layout.base") local base = require("wibox.layout.base")
local widget_base = require("wibox.widget.base") local widget_base = require("wibox.widget.base")
module("wibox.layout.align") -- wibox.layout.align
local align = {}
-- Draw the given align layout. dir describes the orientation of the layout, "x" -- Draw the given align layout. dir describes the orientation of the layout, "x"
-- means horizontal while "y" is vertical. -- means horizontal while "y" is vertical.
@ -73,24 +74,24 @@ local function widget_changed(layout, old_w, new_w)
end end
--- Set the layout's first widget. This is the widget that is at the left/top --- Set the layout's first widget. This is the widget that is at the left/top
function set_first(layout, widget) function align.set_first(layout, widget)
widget_changed(layout, layout.first, widget) widget_changed(layout, layout.first, widget)
layout.first = widget layout.first = widget
end end
--- Set the layout's second widget. This is the centered one. --- Set the layout's second widget. This is the centered one.
function set_second(layout, widget) function align.set_second(layout, widget)
widget_changed(layout, layout.second, widget) widget_changed(layout, layout.second, widget)
layout.second = widget layout.second = widget
end end
--- Set the layout's third widget. This is the widget that is at the right/bottom --- Set the layout's third widget. This is the widget that is at the right/bottom
function set_third(layout, widget) function align.set_third(layout, widget)
widget_changed(layout, layout.third, widget) widget_changed(layout, layout.third, widget)
layout.third = widget layout.third = widget
end end
function reset(layout) function align.reset(layout)
for k, v in pairs({ "first", "second", "third" }) do for k, v in pairs({ "first", "second", "third" }) do
layout[v] = nil layout[v] = nil
end end
@ -110,7 +111,7 @@ local function get_layout(dir)
ret:emit_signal("widget::updated") ret:emit_signal("widget::updated")
end end
for k, v in pairs(_M) do for k, v in pairs(align) do
if type(v) == "function" then if type(v) == "function" then
ret[k] = v ret[k] = v
end end
@ -123,7 +124,7 @@ end
-- three widgets. The widget set via :set_left() is left-aligned. :set_right() -- three widgets. The widget set via :set_left() is left-aligned. :set_right()
-- sets a widget which will be right-aligned. The remaining space between those -- sets a widget which will be right-aligned. The remaining space between those
-- two will be given to the widget set via :set_middle(). -- two will be given to the widget set via :set_middle().
function horizontal() function align.horizontal()
local ret = get_layout("x") local ret = get_layout("x")
ret.set_left = ret.set_first ret.set_left = ret.set_first
@ -137,7 +138,7 @@ end
-- three widgets. The widget set via :set_top() is top-aligned. :set_bottom() -- three widgets. The widget set via :set_top() is top-aligned. :set_bottom()
-- sets a widget which will be bottom-aligned. The remaining space between those -- sets a widget which will be bottom-aligned. The remaining space between those
-- two will be given to the widget set via :set_middle(). -- two will be given to the widget set via :set_middle().
function vertical() function align.vertical()
local ret = get_layout("y") local ret = get_layout("y")
ret.set_top = ret.set_first ret.set_top = ret.set_first
@ -147,4 +148,6 @@ function vertical()
return ret return ret
end end
return align
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -8,11 +8,12 @@ local pairs = pairs
local pcall = pcall local pcall = pcall
local print = print local print = print
module("wibox.layout.base") -- wibox.layout.base
local base = {}
--- Figure out the geometry in device coordinate space. This will break if --- Figure out the geometry in device coordinate space. This will break if
-- someone rotates the coordinate space by a non-multiple of 90°. -- someone rotates the coordinate space by a non-multiple of 90°.
function rect_to_device_geometry(cr, x, y, width, height) function base.rect_to_device_geometry(cr, x, y, width, height)
local function min(a, b) local function min(a, b)
if a < b then return a end if a < b then return a end
return b return b
@ -40,7 +41,7 @@ end
-- @param y The position that the widget should get -- @param y The position that the widget should get
-- @param width The widget's width -- @param width The widget's width
-- @param height The widget's height -- @param height The widget's height
function draw_widget(wibox, cr, widget, x, y, width, height) function base.draw_widget(wibox, cr, widget, x, y, width, height)
-- Use save() / restore() so that our modifications aren't permanent -- Use save() / restore() so that our modifications aren't permanent
cr:save() cr:save()
@ -58,9 +59,11 @@ function draw_widget(wibox, cr, widget, x, y, width, height)
end end
-- Register the widget for input handling -- Register the widget for input handling
wibox:widget_at(widget, rect_to_device_geometry(cr, 0, 0, width, height)) wibox:widget_at(widget, base.rect_to_device_geometry(cr, 0, 0, width, height))
cr:restore() cr:restore()
end end
return base
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -9,7 +9,8 @@ local widget_base = require("wibox.widget.base")
local table = table local table = table
local pairs = pairs local pairs = pairs
module("wibox.layout.fixed") -- wibox.layout.fixed
local fixed = {}
--- Draw a fixed layout. Each widget gets just the space it asks for. --- Draw a fixed layout. Each widget gets just the space it asks for.
-- @param dir "x" for a horizontal layout and "y" for vertical. -- @param dir "x" for a horizontal layout and "y" for vertical.
@ -20,7 +21,7 @@ module("wibox.layout.fixed")
-- @param width The available width. -- @param width The available width.
-- @param height The available height. -- @param height The available height.
-- @return The total space needed by the layout. -- @return The total space needed by the layout.
function draw_fixed(dir, widgets, fill_space, wibox, cr, width, height) function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
local pos = 0 local pos = 0
for k, v in pairs(widgets) do for k, v in pairs(widgets) do
@ -53,7 +54,7 @@ function draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
end end
--- Add a widget to the given fixed layout --- Add a widget to the given fixed layout
function add(layout, widget) function fixed.add(layout, widget)
widget_base.check_widget(widget) widget_base.check_widget(widget)
table.insert(layout.widgets, widget) table.insert(layout.widgets, widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", layout._emit_updated)
@ -65,7 +66,7 @@ end
-- @param widgets The widgets to fit. -- @param widgets The widgets to fit.
-- @param orig_width The available width. -- @param orig_width The available width.
-- @param orig_height The available height. -- @param orig_height The available height.
function fit_fixed(dir, widgets, orig_width, orig_height) function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
local width, height = orig_width, orig_height local width, height = orig_width, orig_height
local used_in_dir, used_max = 0, 0 local used_in_dir, used_max = 0, 0
@ -101,7 +102,7 @@ function fit_fixed(dir, widgets, orig_width, orig_height)
end end
--- Reset a fixed layout. This removes all widgets from the layout. --- Reset a fixed layout. This removes all widgets from the layout.
function reset(layout) function fixed.reset(layout)
for k, v in pairs(layout.widgets) do for k, v in pairs(layout.widgets) do
v:disconnect_signal("widget::updated", layout._emit_updated) v:disconnect_signal("widget::updated", layout._emit_updated)
end end
@ -112,25 +113,25 @@ end
--- Set the layout's fill_space property. If this property is true, the last --- Set the layout's fill_space property. If this property is true, the last
-- widget will get all the space that is left. If this is false, the last widget -- widget will get all the space that is left. If this is false, the last widget
-- won't be handled specially and there can be space left unused. -- won't be handled specially and there can be space left unused.
function fill_space(layout, val) function fixed.fill_space(layout, val)
layout._fill_space = val layout._fill_space = val
layout:emit_signal("widget::updated") layout:emit_signal("widget::updated")
end end
local function get_layout(dir) local function get_layout(dir)
local function draw(layout, ...) local function draw(layout, ...)
draw_fixed(dir, layout.widgets, layout._fill_space, ...) fixed.draw_fixed(dir, layout.widgets, layout._fill_space, ...)
end end
local function fit(layout, ...) local function fit(layout, ...)
return fit_fixed(dir, layout.widgets, ...) return fixed.fit_fixed(dir, layout.widgets, ...)
end end
local ret = widget_base.make_widget() local ret = widget_base.make_widget()
ret.draw = draw ret.draw = draw
ret.fit = fit ret.fit = fit
ret.add = add ret.add = fixed.add
ret.reset = reset ret.reset = fixed.reset
ret.fill_space = fill_space ret.fill_space = fixed.fill_space
ret.widgets = {} ret.widgets = {}
ret.get_dir = function () return dir end ret.get_dir = function () return dir end
ret._emit_updated = function() ret._emit_updated = function()
@ -143,15 +144,17 @@ end
--- Returns a new horizontal fixed layout. Each widget will get as much space as it --- Returns a new horizontal fixed layout. Each widget will get as much space as it
-- asks for and each widget will be drawn next to its neighboring widget. -- asks for and each widget will be drawn next to its neighboring widget.
-- Widgets can be added via :add(). -- Widgets can be added via :add().
function horizontal() function fixed.horizontal()
return get_layout("x") return get_layout("x")
end end
--- Returns a new vertical fixed layout. Each widget will get as much space as it --- Returns a new vertical fixed layout. Each widget will get as much space as it
-- asks for and each widget will be drawn next to its neighboring widget. -- asks for and each widget will be drawn next to its neighboring widget.
-- Widgets can be added via :add(). -- Widgets can be added via :add().
function vertical() function fixed.vertical()
return get_layout("y") return get_layout("y")
end end
return fixed
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -11,7 +11,8 @@ local table = table
local pairs = pairs local pairs = pairs
local floor = math.floor local floor = math.floor
module("wibox.layout.flex") -- wibox.layout.flex
local flex = {}
local function round(x) local function round(x)
return floor(x + 0.5) return floor(x + 0.5)
@ -24,7 +25,7 @@ end
-- @param width The available width. -- @param width The available width.
-- @param height The available height. -- @param height The available height.
-- @return The total space needed by the layout. -- @return The total space needed by the layout.
function draw_flex(dir, widgets, wibox, cr, width, height) function flex.draw_flex(dir, widgets, wibox, cr, width, height)
local pos = 0 local pos = 0
local num = #widgets local num = #widgets
@ -72,7 +73,7 @@ end
local function get_layout(dir) local function get_layout(dir)
local function draw(layout, wibox, cr, width, height) local function draw(layout, wibox, cr, width, height)
draw_flex(dir, layout.widgets, wibox, cr, width, height) flex.draw_flex(dir, layout.widgets, wibox, cr, width, height)
end end
local function fit(layout, width, height) local function fit(layout, width, height)
@ -95,14 +96,16 @@ end
--- Returns a new horizontal flex layout. A flex layout shares the available space --- Returns a new horizontal flex layout. A flex layout shares the available space
-- equally among all widgets. Widgets can be added via :add(widget). -- equally among all widgets. Widgets can be added via :add(widget).
function horizontal() function flex.horizontal()
return get_layout("x") return get_layout("x")
end end
--- Returns a new vertical flex layout. A flex layout shares the available space --- Returns a new vertical flex layout. A flex layout shares the available space
-- equally among all widgets. Widgets can be added via :add(widget). -- equally among all widgets. Widgets can be added via :add(widget).
function vertical() function flex.vertical()
return get_layout("y") return get_layout("y")
end end
return flex
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -4,14 +4,17 @@
-- @release @AWESOME_VERSION@ -- @release @AWESOME_VERSION@
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
require("wibox.layout.base") -- wibox.layout
require("wibox.layout.fixed")
require("wibox.layout.align")
require("wibox.layout.flex")
require("wibox.layout.rotate")
require("wibox.layout.margin")
require("wibox.layout.mirror")
module("wibox.layout") return
{
base = require("wibox.layout.base");
fixed = require("wibox.layout.fixed");
align = require("wibox.layout.align");
flex = require("wibox.layout.flex");
rotate = require("wibox.layout.rotate");
margin = require("wibox.layout.margin");
mirror = require("wibox.layout.mirror");
}
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -10,10 +10,11 @@ local setmetatable = setmetatable
local base = require("wibox.layout.base") local base = require("wibox.layout.base")
local widget_base = require("wibox.widget.base") local widget_base = require("wibox.widget.base")
module("wibox.layout.margin") -- wibox.layout.margin
local margin = { mt = {} }
--- Draw a margin layout --- Draw a margin layout
function draw(layout, wibox, cr, width, height) function margin.draw(layout, wibox, cr, width, height)
local x = layout.left local x = layout.left
local y = layout.top local y = layout.top
local w = layout.right local w = layout.right
@ -27,7 +28,7 @@ function draw(layout, wibox, cr, width, height)
end end
--- Fit a margin layout into the given space --- Fit a margin layout into the given space
function fit(layout, width, height) function margin.fit(layout, width, height)
local extra_w = layout.left + layout.right local extra_w = layout.left + layout.right
local extra_h = layout.top + layout.bottom local extra_h = layout.top + layout.bottom
local w, h = 0, 0 local w, h = 0, 0
@ -38,7 +39,7 @@ function fit(layout, width, height)
end end
--- Set the widget that this layout adds a margin on. --- Set the widget that this layout adds a margin on.
function set_widget(layout, widget) function margin.set_widget(layout, widget)
if layout.widget then if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
end end
@ -51,7 +52,7 @@ function set_widget(layout, widget)
end end
--- Set all the margins to val. --- Set all the margins to val.
function set_margins(layout, val) function margin.set_margins(layout, val)
layout.left = val layout.left = val
layout.right = val layout.right = val
layout.top = val layout.top = val
@ -60,7 +61,7 @@ function set_margins(layout, val)
end end
--- Reset this layout. The widget will be unreferenced and the margins set to 0. --- Reset this layout. The widget will be unreferenced and the margins set to 0.
function reset(layout) function margin.reset(layout)
layout:set_widget(nil) layout:set_widget(nil)
layout:set_margins(0) layout:set_margins(0)
end end
@ -91,7 +92,7 @@ end
-- Create setters for each direction -- Create setters for each direction
for k, v in pairs({ "left", "right", "top", "bottom" }) do for k, v in pairs({ "left", "right", "top", "bottom" }) do
_M["set_" .. v] = function(layout, val) margin["set_" .. v] = function(layout, val)
layout[v] = val layout[v] = val
layout:emit_signal("widget::updated") layout:emit_signal("widget::updated")
end end
@ -106,7 +107,7 @@ end
local function new(widget, left, right, top, bottom) local function new(widget, left, right, top, bottom)
local ret = widget_base.make_widget() local ret = widget_base.make_widget()
for k, v in pairs(_M) do for k, v in pairs(margin) do
if type(v) == "function" then if type(v) == "function" then
ret[k] = v ret[k] = v
end end
@ -128,6 +129,10 @@ local function new(widget, left, right, top, bottom)
return ret return ret
end end
setmetatable(_M, { __call = function(_, ...) return new(...) end }) function margin.mt:__call(...)
return new(...)
end
return setmetatable(margin, margin.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -12,10 +12,11 @@ local setmetatable = setmetatable
local base = require("wibox.layout.base") local base = require("wibox.layout.base")
local widget_base = require("wibox.widget.base") local widget_base = require("wibox.widget.base")
module("wibox.layout.mirror") -- wibox.layout.mirror
local mirror = { mt = {} }
--- Draw this layout --- Draw this layout
function draw(layout, wibox, cr, width, height) function mirror.draw(layout, wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end if not layout.widget then return { width = 0, height = 0 } end
if not layout.horizontal and not layout.vertical then if not layout.horizontal and not layout.vertical then
layout.widget:draw(wibox, cr, width, height) layout.widget:draw(wibox, cr, width, height)
@ -44,7 +45,7 @@ function draw(layout, wibox, cr, width, height)
end end
--- Fit this layout into the given area --- Fit this layout into the given area
function fit(layout, ...) function mirror.fit(layout, ...)
if not layout.widget then if not layout.widget then
return 0, 0 return 0, 0
end end
@ -54,7 +55,7 @@ end
--- Set the widget that this layout mirrors. --- Set the widget that this layout mirrors.
-- @param layout The layout -- @param layout The layout
-- @param widget The widget to mirror -- @param widget The widget to mirror
function set_widget(layout, widget) function mirror.set_widget(layout, widget)
if layout.widget then if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
end end
@ -68,7 +69,7 @@ end
--- Reset this layout. The widget will be removed and the axes reset. --- Reset this layout. The widget will be removed and the axes reset.
-- @param layout The layout -- @param layout The layout
function reset(layout) function mirror.reset(layout)
layout.horizontal = false layout.horizontal = false
layout.vertical = false layout.vertical = false
layout:set_widget(nil) layout:set_widget(nil)
@ -77,7 +78,7 @@ end
--- Set the reflection of this mirror layout. --- Set the reflection of this mirror layout.
-- @param layout The layout -- @param layout The layout
-- @param reflection a table which contains new values for horizontal and/or vertical (booleans) -- @param reflection a table which contains new values for horizontal and/or vertical (booleans)
function set_reflection(layout, reflection) function mirror.set_reflection(layout, reflection)
if type(reflection) ~= 'table' then if type(reflection) ~= 'table' then
error("Invalid type of reflection for mirror layout: " .. error("Invalid type of reflection for mirror layout: " ..
type(reflection) .. " (should be a table)") type(reflection) .. " (should be a table)")
@ -93,7 +94,7 @@ end
--- Get the reflection of this mirror layout. --- Get the reflection of this mirror layout.
-- @param layout The layout -- @param layout The layout
-- @return a table of booleans with the keys "horizontal", "vertical". -- @return a table of booleans with the keys "horizontal", "vertical".
function get_reflection(layout) function mirror.get_reflection(layout)
return { horizontal = layout.horizontal, vertical = layout.vertical } return { horizontal = layout.horizontal, vertical = layout.vertical }
end end
@ -119,6 +120,10 @@ local function new()
return ret return ret
end end
setmetatable(_M, { __call = function(_, ...) return new(...) end }) function mirror.mt:__call(...)
return new(...)
end
return setmetatable(mirror, mirror.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -13,7 +13,8 @@ local tostring = tostring
local base = require("wibox.layout.base") local base = require("wibox.layout.base")
local widget_base = require("wibox.widget.base") local widget_base = require("wibox.widget.base")
module("wibox.layout.rotate") -- wibox.layout.rotate
local rotate = { mt = {} }
local function transform(layout, width, height) local function transform(layout, width, height)
local dir = layout:get_direction() local dir = layout:get_direction()
@ -24,7 +25,7 @@ local function transform(layout, width, height)
end end
--- Draw this layout --- Draw this layout
function draw(layout, wibox, cr, width, height) function rotate.draw(layout, wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end if not layout.widget then return { width = 0, height = 0 } end
cr:save() cr:save()
@ -50,7 +51,7 @@ function draw(layout, wibox, cr, width, height)
end end
--- Fit this layout into the given area --- Fit this layout into the given area
function fit(layout, width, height) function rotate.fit(layout, width, height)
if not layout.widget then if not layout.widget then
return 0, 0 return 0, 0
end end
@ -58,7 +59,7 @@ function fit(layout, width, height)
end end
--- Set the widget that this layout rotates. --- Set the widget that this layout rotates.
function set_widget(layout, widget) function rotate.set_widget(layout, widget)
if layout.widget then if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
end end
@ -71,14 +72,14 @@ function set_widget(layout, widget)
end end
--- Reset this layout. The widget will be removed and the rotation reset. --- Reset this layout. The widget will be removed and the rotation reset.
function reset(layout) function rotate.reset(layout)
layout.direction = nil layout.direction = nil
layout:set_widget(nil) layout:set_widget(nil)
end end
--- Set the direction of this rotating layout. Valid values are "north", "east", --- Set the direction of this rotating layout. Valid values are "north", "east",
-- "south" and "west". On an invalid value, this function will throw an error. -- "south" and "west". On an invalid value, this function will throw an error.
function set_direction(layout, dir) function rotate.set_direction(layout, dir)
local allowed = { local allowed = {
north = true, north = true,
east = true, east = true,
@ -95,7 +96,7 @@ function set_direction(layout, dir)
end end
--- Get the direction of this rotating layout --- Get the direction of this rotating layout
function get_direction(layout) function rotate.get_direction(layout)
return layout.direction or "north" return layout.direction or "north"
end end
@ -118,6 +119,10 @@ local function new()
return ret return ret
end end
setmetatable(_M, { __call = function(_, ...) return new(...) end }) function rotate.mt:__call(...)
return new(...)
end
return setmetatable(rotate, rotate.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80