Merge wibox.layout.base and wibox.widget.base

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-06-14 15:57:33 +02:00
parent 9b8cbf7539
commit 3338718b93
8 changed files with 25 additions and 115 deletions

View File

@ -10,8 +10,7 @@ local table = table
local pairs = pairs local pairs = pairs
local type = type local type = type
local floor = math.floor local floor = math.floor
local base = require("wibox.layout.base") local base = require("wibox.widget.base")
local widget_base = require("wibox.widget.base")
local align = {} local align = {}
@ -46,7 +45,7 @@ function align:layout(context, width, height)
-- if all the space is taken, skip the rest, and draw just the middle -- if all the space is taken, skip the rest, and draw just the middle
-- widget -- widget
if size_second >= size_remains then if size_second >= size_remains then
return { widget_base.place_widget_at(self.second, 0, 0, width, height) } return { base.place_widget_at(self.second, 0, 0, width, height) }
else else
-- the middle widget is sized first, the outside widgets are given -- the middle widget is sized first, the outside widgets are given
-- the remaining space if available we will draw later -- the remaining space if available we will draw later
@ -84,7 +83,7 @@ function align:layout(context, width, height)
w = size_remains w = size_remains
end end
end end
table.insert(result, widget_base.place_widget_at(self.first, 0, 0, w, h)) table.insert(result, base.place_widget_at(self.first, 0, 0, w, h))
end end
-- size_remains will be <= 0 if first used all the space -- size_remains will be <= 0 if first used all the space
if self.third and size_remains > 0 then if self.third and size_remains > 0 then
@ -110,7 +109,7 @@ function align:layout(context, width, height)
end end
end end
local x, y = width - w, height - h local x, y = width - w, height - h
table.insert(result, widget_base.place_widget_at(self.third, x, y, w, h)) table.insert(result, base.place_widget_at(self.third, x, y, w, h))
end end
-- here we either draw the second widget in the space set aside for it -- here we either draw the second widget in the space set aside for it
-- in the beginning, or in the remaining space, if it is "inside" -- in the beginning, or in the remaining space, if it is "inside"
@ -133,7 +132,7 @@ function align:layout(context, width, height)
x = floor( (width -w)/2 ) x = floor( (width -w)/2 )
end end
end end
table.insert(result, widget_base.place_widget_at(self.second, x, y, w, h)) table.insert(result, base.place_widget_at(self.second, x, y, w, h))
end end
return result return result
end end
@ -212,7 +211,7 @@ function align:reset()
end end
local function get_layout(dir) local function get_layout(dir)
local ret = widget_base.make_widget() local ret = base.make_widget()
ret.dir = dir ret.dir = dir
for k, v in pairs(align) do for k, v in pairs(align) do

View File

@ -1,83 +0,0 @@
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2010 Uli Schlachter
-- @release @AWESOME_VERSION@
-- @classmod wibox.layout.base
---------------------------------------------------------------------------
local xpcall = xpcall
local print = print
local cairo = require("lgi").cairo
local wbase = require("wibox.widget.base")
local base = {}
--- Fit a widget for the given available width and height
-- @param context The context in which we are fit.
-- @param widget The widget to fit (this uses widget:fit(width, height)).
-- @param width The available width for the widget
-- @param height The available height for the widget
-- @return The width and height that the widget wants to use
function base.fit_widget(context, widget, width, height)
if not widget.visible then
return 0, 0
end
-- Sanitize the input. This also filters out e.g. NaN.
local width = math.max(0, width)
local height = math.max(0, height)
local w, h = widget:fit(context, width, height)
-- Also sanitize the output.
w = math.max(0, math.min(w, width))
h = math.max(0, math.min(h, height))
return w, h
end
--- Draw a widget via a cairo context
-- @param context The context in which we are drawn.
-- @param cr The cairo context used
-- @param widget The widget to draw (this uses widget:draw(cr, width, height)).
-- @param x The position that the widget should get
-- @param y The position that the widget should get
-- @param width The widget's width
-- @param height The widget's height
function base.draw_widget(context, cr, widget, x, y, width, height)
if not widget.visible then
return
end
-- Use save() / restore() so that our modifications aren't permanent
cr:save()
-- Move (0, 0) to the place where the widget should show up
cr:translate(x, y)
-- Make sure the widget cannot draw outside of the allowed area
cr:rectangle(0, 0, width, height)
cr:clip()
if widget.opacity ~= 1 then
cr:push_group()
end
-- Let the widget draw itself
xpcall(function()
widget:draw(context, cr, width, height)
end, function(err)
print(debug.traceback("Error while drawing widget: "..tostring(err), 2))
end)
if widget.opacity ~= 1 then
cr:pop_group_to_source()
cr.operator = cairo.Operator.OVER
cr:paint_with_alpha(widget.opacity)
end
-- Register the widget for input handling
context:widget_at(widget, wbase.rect_to_device_geometry(cr, 0, 0, width, height))
cr:restore()
end
return base
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -5,8 +5,7 @@
-- @classmod wibox.layout.fixed -- @classmod wibox.layout.fixed
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
local base = require("wibox.layout.base") local base = require("wibox.widget.base")
local widget_base = require("wibox.widget.base")
local table = table local table = table
local pairs = pairs local pairs = pairs
@ -45,14 +44,14 @@ function fixed:layout(context, width, height)
(self.dir ~= "y" and pos-spacing > width) then (self.dir ~= "y" and pos-spacing > width) then
break break
end end
table.insert(result, widget_base.place_widget_at(v, x, y, w, h)) table.insert(result, base.place_widget_at(v, x, y, w, h))
end end
return result return result
end end
--- Add a widget to the given fixed layout --- Add a widget to the given fixed layout
function fixed:add(widget) function fixed:add(widget)
widget_base.check_widget(widget) base.check_widget(widget)
table.insert(self.widgets, widget) table.insert(self.widgets, widget)
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
@ -113,7 +112,7 @@ function fixed:fill_space(val)
end end
local function get_layout(dir) local function get_layout(dir)
local ret = widget_base.make_widget() local ret = base.make_widget()
for k, v in pairs(fixed) do for k, v in pairs(fixed) do
if type(v) == "function" then if type(v) == "function" then

View File

@ -5,8 +5,7 @@
-- @classmod wibox.layout.flex -- @classmod wibox.layout.flex
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
local base = require("wibox.layout.base") local base = require("wibox.widget.base")
local widget_base = require("wibox.widget.base")
local table = table local table = table
local pairs = pairs local pairs = pairs
local floor = math.floor local floor = math.floor
@ -48,7 +47,7 @@ function flex:layout(context, width, height)
x, y = round(pos), 0 x, y = round(pos), 0
w, h = floor(space_per_item), height w, h = floor(space_per_item), height
end end
table.insert(result, widget_base.place_widget_at(v, x, y, w, h)) table.insert(result, base.place_widget_at(v, x, y, w, h))
pos = pos + space_per_item + spacing pos = pos + space_per_item + spacing
@ -98,7 +97,7 @@ function flex:fit(context, orig_width, orig_height)
end end
function flex:add(widget) function flex:add(widget)
widget_base.check_widget(widget) base.check_widget(widget)
table.insert(self.widgets, widget) table.insert(self.widgets, widget)
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
@ -125,7 +124,7 @@ function flex:reset()
end end
local function get_layout(dir) local function get_layout(dir)
local ret = widget_base.make_widget() local ret = base.make_widget()
for k, v in pairs(flex) do for k, v in pairs(flex) do
if type(v) == "function" then if type(v) == "function" then

View File

@ -9,7 +9,6 @@
return return
{ {
base = require("wibox.layout.base");
fixed = require("wibox.layout.fixed"); fixed = require("wibox.layout.fixed");
align = require("wibox.layout.align"); align = require("wibox.layout.align");
flex = require("wibox.layout.flex"); flex = require("wibox.layout.flex");

View File

@ -11,8 +11,7 @@ local pi = math.pi
local type = type local type = type
local setmetatable = setmetatable local setmetatable = setmetatable
local tostring = tostring local tostring = tostring
local base = require("wibox.layout.base") local base = require("wibox.widget.base")
local widget_base = require("wibox.widget.base")
local Matrix = require("lgi").cairo.Matrix local Matrix = require("lgi").cairo.Matrix
local rotate = { mt = {} } local rotate = { mt = {} }
@ -61,7 +60,7 @@ end
--- Set the widget that this layout rotates. --- Set the widget that this layout rotates.
function rotate:set_widget(widget) function rotate:set_widget(widget)
if widget then if widget then
widget_base.check_widget(widget) base.check_widget(widget)
end end
self.widget = widget self.widget = widget
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
@ -102,7 +101,7 @@ end
-- @param[opt] widget The widget to display. -- @param[opt] widget The widget to display.
-- @param[opt] dir The direction to rotate to. -- @param[opt] dir The direction to rotate to.
local function new(widget, dir) local function new(widget, dir)
local ret = widget_base.make_widget() local ret = base.make_widget()
for k, v in pairs(rotate) do for k, v in pairs(rotate) do
if type(v) == "function" then if type(v) == "function" then

View File

@ -7,7 +7,6 @@
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local color = require("gears.color") local color = require("gears.color")
local layout_base = require("wibox.layout.base")
local surface = require("gears.surface") local surface = require("gears.surface")
local cairo = require("lgi").cairo local cairo = require("lgi").cairo
local setmetatable = setmetatable local setmetatable = setmetatable
@ -53,7 +52,7 @@ function background:fit(context, width, height)
return 0, 0 return 0, 0
end end
return layout_base.fit_widget(context, self.widget, width, height) return base.fit_widget(context, self.widget, width, height)
end end
--- Set the widget that is drawn on top of the background --- Set the widget that is drawn on top of the background

View File

@ -6,19 +6,18 @@
local object = require("gears.object") local object = require("gears.object")
local cache = require("gears.cache") local cache = require("gears.cache")
local matrix_equals = require("gears.matrix").equals local matrix_equals = require("gears.matrix").equals
local wbase = require("wibox.widget.base") local base = require("wibox.widget.base")
local lbase = require("wibox.layout.base")
local say = require("say") local say = require("say")
local assert = require("luassert") local assert = require("luassert")
local spy = require("luassert.spy") local spy = require("luassert.spy")
local stub = require("luassert.stub") local stub = require("luassert.stub")
local real_draw_widget = wbase.draw_widget local real_draw_widget = base.draw_widget
local widgets_drawn = nil local widgets_drawn = nil
-- This function would reject stubbed widgets -- This function would reject stubbed widgets
local real_check_widget = wbase.check_widget local real_check_widget = base.check_widget
wbase.check_widget = function() base.check_widget = function()
end end
local function stub_draw_widget(wibox, cr, widget, x, y, width, height) local function stub_draw_widget(wibox, cr, widget, x, y, width, height)
@ -36,7 +35,7 @@ local function widget_fit(state, arguments)
local widget = arguments[1] local widget = arguments[1]
local given = arguments[2] local given = arguments[2]
local expected = arguments[3] local expected = arguments[3]
local w, h = lbase.fit_widget({ "fake context" }, widget, given[1], given[2]) local w, h = base.fit_widget({ "fake context" }, widget, given[1], given[2])
local fits = expected[1] == w and expected[2] == h local fits = expected[1] == w and expected[2] == h
if state.mod == fits then if state.mod == fits then
@ -117,12 +116,12 @@ return {
end, end,
stub_draw_widget = function() stub_draw_widget = function()
lbase.draw_widget = stub_draw_widget base.draw_widget = stub_draw_widget
widgets_drawn = {} widgets_drawn = {}
end, end,
revert_draw_widget = function() revert_draw_widget = function()
lbase.draw_widget = real_draw_widget base.draw_widget = real_draw_widget
widgets_drawn = nil widgets_drawn = nil
end, end,