From c2be60fb54eab8b53d22f0f5b7639dbf28609c3c Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 5 Dec 2015 12:25:25 +0100 Subject: [PATCH 1/2] 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. From f4077def8eb30fff0810fc4478d2002aa0ffbfb3 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 5 Dec 2015 12:31:33 +0100 Subject: [PATCH 2/2] Add :set_{width,height} to widgets (#291) This adds new functions to all widgets that allow to force a specific width/height. These values override the result from :fit(). Signed-off-by: Uli Schlachter --- lib/wibox/widget/base.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index d2d3a056e..8006aca5b 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -51,6 +51,28 @@ function base.widget:set_opacity(o) end end +--- Set the widget's width +-- @tparam number|nil s The width that the widget has. `nil` means to apply the +-- default mechanism of calling the `:fit` method. A number overrides the result +-- from `:fit`. +function base.widget:set_width(s) + if s ~= self._forced_width then + self._forced_width = s + self:emit_signal("widget::layout_changed") + end +end + +--- Set the widget's height +-- @tparam number|nil s The height that the widget has. `nil` means to apply the +-- default mechanism of calling the `:fit` method. A number overrides the result +-- from `:fit`. +function base.widget:set_height(s) + if s ~= self._forced_height then + self._forced_height = s + self:emit_signal("widget::layout_changed") + end +end + -- }}} -- {{{ Caches @@ -141,6 +163,10 @@ function base.fit_widget(parent, context, widget, width, height) end end + -- Apply forced size + w = widget._forced_width or w + h = widget._forced_height or h + -- Also sanitize the output. w = math.max(0, math.min(w, width)) h = math.max(0, math.min(h, height)) @@ -394,6 +420,10 @@ function base.make_widget(proxy, widget_name) -- Widget is fully opaque ret.opacity = 1 + -- Size is not restricted/forced + ret._forced_width = nil + ret._forced_height = nil + -- Make buttons work ret:connect_signal("button::press", function(...) return base.handle_button("press", ...)