wibox.widget.base: Factor out functions available on all widgets

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-12-05 12:25:25 +01:00
parent 0516203610
commit c2be60fb54
1 changed files with 47 additions and 29 deletions

View File

@ -15,6 +15,44 @@ local table = table
local base = {} local base = {}
-- {{{ Functions on widgets
--- Functions available on all widgets
base.widget = {}
--- Set/get a widget's buttons.
-- @param _buttons The table of buttons that should bind to the widget.
function base.widget:buttons(_buttons)
if _buttons then
self.widget_buttons = _buttons
end
return self.widget_buttons
end
--- Set a widget's visible property
-- @tparam boolean b Wether the widget is visible at all
function base.widget:set_visible(b)
if b ~= self.visible then
self.visible = b
self:emit_signal("widget::layout_changed")
-- In case something ignored fit and drew the widget anyway
self:emit_signal("widget::redraw_needed")
end
end
--- Set a widget's opacity
-- @tparam number o The opacity to use (a number from 0 to 1). 0 is fully
-- transparent while 1 is fully opaque.
function base.widget:set_opacity(o)
if o ~= self.opacity then
self.opacity = o
self:emit_signal("widget::redraw")
end
end
-- }}}
-- {{{ Caches -- {{{ Caches
-- Indexes are widgets, allow them to be garbage-collected -- Indexes are widgets, allow them to be garbage-collected
@ -135,16 +173,6 @@ function base.layout_widget(parent, context, widget, width, height)
end end
end end
--- Set/get a widget's buttons.
-- This function is available on widgets created by @{make_widget}.
function base:buttons(_buttons)
if _buttons then
self.widget_buttons = _buttons
end
return self.widget_buttons
end
-- Handle a button event on a widget. This is used internally and should not be -- Handle a button event on a widget. This is used internally and should not be
-- called directly. -- called directly.
function base.handle_button(event, widget, x, y, button, modifiers, geometry) function base.handle_button(event, widget, x, y, button, modifiers, geometry)
@ -359,7 +387,12 @@ function base.make_widget(proxy, widget_name)
-- No buttons yet -- No buttons yet
ret.widget_buttons = {} ret.widget_buttons = {}
ret.buttons = base.buttons
-- Widget is visible
ret.visible = true
-- Widget is fully opaque
ret.opacity = 1
-- Make buttons work -- Make buttons work
ret:connect_signal("button::press", function(...) ret:connect_signal("button::press", function(...)
@ -390,24 +423,9 @@ function base.make_widget(proxy, widget_name)
clear_caches(ret) clear_caches(ret)
end) end)
-- Add visible property and setter. -- Add functions
ret.visible = true for k, v in pairs(base.widget) do
function ret:set_visible(b) ret[k] = v
if b ~= self.visible then
self.visible = b
self:emit_signal("widget::layout_changed")
-- In case something ignored fit and drew the widget anyway
self:emit_signal("widget::redraw_needed")
end
end
-- Add opacity property and setter.
ret.opacity = 1
function ret:set_opacity(b)
if b ~= self.opacity then
self.opacity = b
self:emit_signal("widget::redraw")
end
end end
-- Add __tostring method to metatable. -- Add __tostring method to metatable.