margin: Enable the property system
And add the missing documentation
This commit is contained in:
parent
832e9ed054
commit
8869257940
|
@ -8,23 +8,23 @@
|
||||||
---------------------------------------------------------------------------
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local type = type
|
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local base = require("wibox.widget.base")
|
local base = require("wibox.widget.base")
|
||||||
local gcolor = require("gears.color")
|
local gcolor = require("gears.color")
|
||||||
local cairo = require("lgi").cairo
|
local cairo = require("lgi").cairo
|
||||||
|
local util = require("awful.util")
|
||||||
|
|
||||||
local margin = { mt = {} }
|
local margin = { mt = {} }
|
||||||
|
|
||||||
-- Draw a margin layout
|
-- Draw a margin layout
|
||||||
function margin:draw(_, cr, width, height)
|
function margin:draw(_, cr, width, height)
|
||||||
local x = self.left
|
local x = self._private.left
|
||||||
local y = self.top
|
local y = self._private.top
|
||||||
local w = self.right
|
local w = self._private.right
|
||||||
local h = self.bottom
|
local h = self._private.bottom
|
||||||
local color = self.color
|
local color = self._private.color
|
||||||
|
|
||||||
if not self.widget or width <= x + w or height <= y + h then
|
if not self._private.widget or width <= x + w or height <= y + h then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,45 +39,52 @@ end
|
||||||
|
|
||||||
-- Layout a margin layout
|
-- Layout a margin layout
|
||||||
function margin:layout(_, width, height)
|
function margin:layout(_, width, height)
|
||||||
if self.widget then
|
if self._private.widget then
|
||||||
local x = self.left
|
local x = self._private.left
|
||||||
local y = self.top
|
local y = self._private.top
|
||||||
local w = self.right
|
local w = self._private.right
|
||||||
local h = self.bottom
|
local h = self._private.bottom
|
||||||
|
|
||||||
return { base.place_widget_at(self.widget, x, y, width - x - w, height - y - h) }
|
return { base.place_widget_at(self._private.widget, x, y, width - x - w, height - y - h) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Fit a margin layout into the given space
|
-- Fit a margin layout into the given space
|
||||||
function margin:fit(context, width, height)
|
function margin:fit(context, width, height)
|
||||||
local extra_w = self.left + self.right
|
local extra_w = self._private.left + self._private.right
|
||||||
local extra_h = self.top + self.bottom
|
local extra_h = self._private.top + self._private.bottom
|
||||||
local w, h = 0, 0
|
local w, h = 0, 0
|
||||||
if self.widget then
|
if self._private.widget then
|
||||||
w, h = base.fit_widget(self, context, self.widget, width - extra_w, height - extra_h)
|
w, h = base.fit_widget(self, context, self._private.widget, width - extra_w, height - extra_h)
|
||||||
end
|
end
|
||||||
|
|
||||||
if self._draw_empty == false and (w == 0 or h == 0) then
|
if self._private.draw_empty == false and (w == 0 or h == 0) then
|
||||||
return 0, 0
|
return 0, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return w + extra_w, h + extra_h
|
return w + extra_w, h + extra_h
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set the widget that this layout adds a margin on.
|
--- The widget to be wrapped the the margins.
|
||||||
|
-- @property widget
|
||||||
|
-- @tparam widget widget The widget
|
||||||
|
|
||||||
function margin:set_widget(widget)
|
function margin:set_widget(widget)
|
||||||
if widget then
|
if widget then
|
||||||
base.check_widget(widget)
|
base.check_widget(widget)
|
||||||
end
|
end
|
||||||
self.widget = widget
|
self._private.widget = widget
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function margin:get_widget()
|
||||||
|
return self._private.widget
|
||||||
|
end
|
||||||
|
|
||||||
-- Get the number of children element
|
-- Get the number of children element
|
||||||
-- @treturn table The children
|
-- @treturn table The children
|
||||||
function margin:get_children()
|
function margin:get_children()
|
||||||
return {self.widget}
|
return {self._private.widget}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Replace the layout children
|
-- Replace the layout children
|
||||||
|
@ -88,36 +95,50 @@ function margin:set_children(children)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set all the margins to val.
|
--- Set all the margins to val.
|
||||||
|
-- @property margins
|
||||||
-- @tparam number val The margin value
|
-- @tparam number val The margin value
|
||||||
|
|
||||||
function margin:set_margins(val)
|
function margin:set_margins(val)
|
||||||
if self.left == val and
|
if self._private.left == val and
|
||||||
self.right == val and
|
self._private.right == val and
|
||||||
self.top == val and
|
self._private.top == val and
|
||||||
self.bottom == val then
|
self._private.bottom == val then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
self.left = val
|
self._private.left = val
|
||||||
self.right = val
|
self._private.right = val
|
||||||
self.top = val
|
self._private.top = val
|
||||||
self.bottom = val
|
self._private.bottom = val
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set the margins color to create a border.
|
--- Set the margins color to create a border.
|
||||||
|
-- @property color
|
||||||
-- @param color A color used to fill the margin.
|
-- @param color A color used to fill the margin.
|
||||||
|
|
||||||
function margin:set_color(color)
|
function margin:set_color(color)
|
||||||
self.color = color and gcolor(color)
|
self._private.color = color and gcolor(color)
|
||||||
self:emit_signal("widget::redraw_needed")
|
self:emit_signal("widget::redraw_needed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function margin:get_color()
|
||||||
|
return self._private.color
|
||||||
|
end
|
||||||
|
|
||||||
--- Draw the margin even if the content size is 0x0 (default: true)
|
--- Draw the margin even if the content size is 0x0 (default: true)
|
||||||
|
-- @function draw_empty
|
||||||
-- @tparam boolean draw_empty Draw nothing is content is 0x0 or draw the margin anyway
|
-- @tparam boolean draw_empty Draw nothing is content is 0x0 or draw the margin anyway
|
||||||
|
|
||||||
function margin:set_draw_empty(draw_empty)
|
function margin:set_draw_empty(draw_empty)
|
||||||
self._draw_empty = draw_empty
|
self._private.draw_empty = draw_empty
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function margin:get_draw_empty()
|
||||||
|
return self._private.draw_empty
|
||||||
|
end
|
||||||
|
|
||||||
--- Reset this layout. The widget will be unreferenced, the margins set to 0
|
--- Reset this layout. The widget will be unreferenced, the margins set to 0
|
||||||
-- and the color erased
|
-- and the color erased
|
||||||
function margin:reset()
|
function margin:reset()
|
||||||
|
@ -127,36 +148,32 @@ function margin:reset()
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set the left margin that this layout adds to its widget.
|
--- Set the left margin that this layout adds to its widget.
|
||||||
-- @param layout The layout you are modifying.
|
|
||||||
-- @param margin The new margin to use.
|
-- @param margin The new margin to use.
|
||||||
-- @name set_left
|
-- @property left
|
||||||
-- @class function
|
|
||||||
|
|
||||||
--- Set the right margin that this layout adds to its widget.
|
--- Set the right margin that this layout adds to its widget.
|
||||||
-- @param layout The layout you are modifying.
|
|
||||||
-- @param margin The new margin to use.
|
-- @param margin The new margin to use.
|
||||||
-- @name set_right
|
-- @property right
|
||||||
-- @class function
|
|
||||||
|
|
||||||
--- Set the top margin that this layout adds to its widget.
|
--- Set the top margin that this layout adds to its widget.
|
||||||
-- @param layout The layout you are modifying.
|
|
||||||
-- @param margin The new margin to use.
|
-- @param margin The new margin to use.
|
||||||
-- @name set_top
|
-- @property top
|
||||||
-- @class function
|
|
||||||
|
|
||||||
--- Set the bottom margin that this layout adds to its widget.
|
--- Set the bottom margin that this layout adds to its widget.
|
||||||
-- @param layout The layout you are modifying.
|
|
||||||
-- @param margin The new margin to use.
|
-- @param margin The new margin to use.
|
||||||
-- @name set_bottom
|
-- @property bottom
|
||||||
-- @class function
|
|
||||||
|
|
||||||
-- Create setters for each direction
|
-- Create setters for each direction
|
||||||
for _, v in pairs({ "left", "right", "top", "bottom" }) do
|
for _, v in pairs({ "left", "right", "top", "bottom" }) do
|
||||||
margin["set_" .. v] = function(layout, val)
|
margin["set_" .. v] = function(layout, val)
|
||||||
if layout[v] == val then return end
|
if layout._private[v] == val then return end
|
||||||
layout[v] = val
|
layout._private[v] = val
|
||||||
layout:emit_signal("widget::layout_changed")
|
layout:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
margin["get_" .. v] = function(layout)
|
||||||
|
return layout._private[v]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns a new margin container.
|
--- Returns a new margin container.
|
||||||
|
@ -170,13 +187,9 @@ end
|
||||||
-- @treturn table A new margin container
|
-- @treturn table A new margin container
|
||||||
-- @function wibox.container.margin
|
-- @function wibox.container.margin
|
||||||
local function new(widget, left, right, top, bottom, color, draw_empty)
|
local function new(widget, left, right, top, bottom, color, draw_empty)
|
||||||
local ret = base.make_widget()
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
||||||
|
|
||||||
for k, v in pairs(margin) do
|
util.table.crush(ret, margin, true)
|
||||||
if type(v) == "function" then
|
|
||||||
ret[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
ret:set_left(left or 0)
|
ret:set_left(left or 0)
|
||||||
ret:set_right(right or 0)
|
ret:set_right(right or 0)
|
||||||
|
@ -197,6 +210,10 @@ function margin.mt:__call(...)
|
||||||
return new(...)
|
return new(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--@DOC_widget_COMMON@
|
||||||
|
|
||||||
|
--@DOC_object_COMMON@
|
||||||
|
|
||||||
return setmetatable(margin, margin.mt)
|
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
|
||||||
|
|
Loading…
Reference in New Issue