From c2be60fb54eab8b53d22f0f5b7639dbf28609c3c Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 5 Dec 2015 12:25:25 +0100 Subject: [PATCH] wibox.widget.base: Factor out functions available on all widgets Signed-off-by: Uli Schlachter --- lib/wibox/widget/base.lua | 76 ++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index f8a59770e..d2d3a056e 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -15,6 +15,44 @@ local table = table 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 -- Indexes are widgets, allow them to be garbage-collected @@ -135,16 +173,6 @@ function base.layout_widget(parent, context, widget, width, height) 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 -- called directly. 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 ret.widget_buttons = {} - ret.buttons = base.buttons + + -- Widget is visible + ret.visible = true + + -- Widget is fully opaque + ret.opacity = 1 -- Make buttons work ret:connect_signal("button::press", function(...) @@ -390,24 +423,9 @@ function base.make_widget(proxy, widget_name) clear_caches(ret) end) - -- Add visible property and setter. - ret.visible = true - function ret: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 - - -- 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 + -- Add functions + for k, v in pairs(base.widget) do + ret[k] = v end -- Add __tostring method to metatable.