diff --git a/lib/awful/widget/only_on_screen.lua b/lib/awful/widget/only_on_screen.lua index 37ce050df..b88ede6f6 100644 --- a/lib/awful/widget/only_on_screen.lua +++ b/lib/awful/widget/only_on_screen.lua @@ -51,13 +51,7 @@ end -- @property widget -- @tparam widget widget The widget -function only_on_screen:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +only_on_screen.set_widget = base.set_widget_common function only_on_screen:get_widget() return self._private.widget diff --git a/lib/wibox/container/arcchart.lua b/lib/wibox/container/arcchart.lua index 2c5a0551c..73653c44a 100644 --- a/lib/wibox/container/arcchart.lua +++ b/lib/wibox/container/arcchart.lua @@ -192,13 +192,7 @@ end -- @property widget -- @tparam widget widget The widget -function arcchart:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +arcchart.set_widget = base.set_widget_common function arcchart:get_children() return {self._private.widget} diff --git a/lib/wibox/container/background.lua b/lib/wibox/container/background.lua index b1941b4eb..de0cb2ef3 100644 --- a/lib/wibox/container/background.lua +++ b/lib/wibox/container/background.lua @@ -218,13 +218,7 @@ end -- @tparam widget widget The widget to be disaplayed inside of the background -- area -function background:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +background.set_widget = base.set_widget_common function background:get_widget() return self._private.widget diff --git a/lib/wibox/container/constraint.lua b/lib/wibox/container/constraint.lua index 756ddb03b..7ebda4d25 100644 --- a/lib/wibox/container/constraint.lua +++ b/lib/wibox/container/constraint.lua @@ -42,10 +42,7 @@ end -- @property widget -- @tparam widget widget The widget -function constraint:set_widget(widget) - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +constraint.set_widget = base.set_widget_common function constraint:get_widget() return self._private.widget diff --git a/lib/wibox/container/margin.lua b/lib/wibox/container/margin.lua index 715552376..e21cb7fa8 100644 --- a/lib/wibox/container/margin.lua +++ b/lib/wibox/container/margin.lua @@ -73,13 +73,7 @@ end -- @property widget -- @tparam widget widget The widget -function margin:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +margin.set_widget = base.set_widget_common function margin:get_widget() return self._private.widget diff --git a/lib/wibox/container/mirror.lua b/lib/wibox/container/mirror.lua index fa86ec867..0e50b9d97 100644 --- a/lib/wibox/container/mirror.lua +++ b/lib/wibox/container/mirror.lua @@ -49,13 +49,7 @@ end -- @property widget -- @tparam widget widget The widget -function mirror:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +mirror.set_widget = base.set_widget_common function mirror:get_widget() return self._private.widget diff --git a/lib/wibox/container/place.lua b/lib/wibox/container/place.lua index fca974191..1eaab15e2 100644 --- a/lib/wibox/container/place.lua +++ b/lib/wibox/container/place.lua @@ -61,13 +61,11 @@ function place:fit(context, width, height) and height or h end -function place:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +--- The widget to be placed. +-- @property widget +-- @tparam widget widget The widget + +place.set_widget = base.set_widget_common function place:get_widget() return self._private.widget diff --git a/lib/wibox/container/radialprogressbar.lua b/lib/wibox/container/radialprogressbar.lua index b7bfa055f..47eefc679 100644 --- a/lib/wibox/container/radialprogressbar.lua +++ b/lib/wibox/container/radialprogressbar.lua @@ -129,13 +129,7 @@ end -- @property widget -- @tparam widget widget The widget -function radialprogressbar:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +radialprogressbar.set_widget = base.set_widget_common function radialprogressbar:get_children() return {self._private.widget} diff --git a/lib/wibox/container/rotate.lua b/lib/wibox/container/rotate.lua index 5a3f784cd..43ace693f 100644 --- a/lib/wibox/container/rotate.lua +++ b/lib/wibox/container/rotate.lua @@ -62,13 +62,7 @@ end -- @property widget -- @tparam widget widget The widget -function rotate:set_widget(widget) - if widget then - base.check_widget(widget) - end - self._private.widget = widget - self:emit_signal("widget::layout_changed") -end +rotate.set_widget = base.set_widget_common function rotate:get_widget() return self._private.widget diff --git a/lib/wibox/container/scroll.lua b/lib/wibox/container/scroll.lua index 06413078f..cb1bf4397 100644 --- a/lib/wibox/container/scroll.lua +++ b/lib/wibox/container/scroll.lua @@ -277,10 +277,14 @@ function scroll:set_widget(widget) if widget == self._private.widget then return end - if widget then - base.check_widget(widget) + + local w = base.make_widget_from_value(widget) + + if w then + base.check_widget(w) end - self._private.widget = widget + + self._private.widget = w self:emit_signal("widget::layout_changed") self:emit_signal("widget::redraw_needed") end diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index c9d2e74ce..33551a053 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -70,8 +70,9 @@ function fixed:add(...) local args = { n=select('#', ...), ... } assert(args.n > 0, "need at least one widget to add") for i=1, args.n do - base.check_widget(args[i]) - table.insert(self._private.widgets, args[i]) + local w = base.make_widget_from_value(args[i]) + base.check_widget(w) + table.insert(self._private.widgets, w) end self:emit_signal("widget::layout_changed") end diff --git a/lib/wibox/layout/grid.lua b/lib/wibox/layout/grid.lua index 1322a6e9e..4675717b6 100644 --- a/lib/wibox/layout/grid.lua +++ b/lib/wibox/layout/grid.lua @@ -293,10 +293,9 @@ function grid:add(...) assert(args.n > 0, "need at least one widget to add") local row, column for i=1, args.n do - local w = args[i] -- Get the next empty coordinate to insert the widget row, column = self:get_next_empty(row, column) - self:add_widget_at(w, row, column, 1, 1) + self:add_widget_at(args[i], row, column, 1, 1) end end @@ -317,6 +316,7 @@ function grid:add_widget_at(child, row, col, row_span, col_span) col_span = (col_span and col_span > 0) and col_span or 1 -- check if the object is a widget + child = base.make_widget_from_value(child) base.check_widget(child) -- test if the new widget superpose with existing ones diff --git a/lib/wibox/layout/manual.lua b/lib/wibox/layout/manual.lua index d263141b2..03f8c61a4 100644 --- a/lib/wibox/layout/manual.lua +++ b/lib/wibox/layout/manual.lua @@ -102,9 +102,16 @@ function manual_layout:layout(context, width, height) end function manual_layout:add(...) - local wdgs = {...} + local wdgs = {} local old_count = #self._private.widgets - gtable.merge(self._private.widgets, {...}) + + for _, v in ipairs {...} do + local w = base.make_widget_from_value(v) + base.check_widget(w) + table.insert(wdgs, w) + end + + gtable.merge(self._private.widgets, wdgs) -- Add the points for k, v in ipairs(wdgs) do @@ -166,7 +173,7 @@ function manual_layout:add_at(widget, point) end self._private.pos[#self._private.widgets+1] = point - self:add(widget) + self:add(base.make_widget_from_value(widget)) end --- Move a widget (by index). diff --git a/lib/wibox/layout/ratio.lua b/lib/wibox/layout/ratio.lua index 0e8f18ac6..63550aca3 100644 --- a/lib/wibox/layout/ratio.lua +++ b/lib/wibox/layout/ratio.lua @@ -355,8 +355,9 @@ function ratio:add(...) local args = { n=select('#', ...), ... } assert(args.n > 0, "need at least one widget to add") for i=1, args.n do - base.check_widget(args[i]) - table.insert(self._private.widgets, args[i]) + local w = base.make_widget_from_value(args[i]) + base.check_widget(w) + table.insert(self._private.widgets, w) end normalize(self) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index bf4b87f8c..9587c4094 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -155,6 +155,24 @@ function base.widget:get_all_children() return ret end +--- Common implementation of the `:set_widget()` method exposed by many +-- other widgets. +-- +-- Use this if your widget has no custom logic when setting the widget. +-- +-- @usage +-- rawset(my_custom_widget, "set_widget", wibox.widget.base.set_widget_common) +function base.set_widget_common(self, widget) + local w = widget and base.make_widget_from_value(widget) + + if w then + base.check_widget(w) + end + + self._private.widget = w + self:emit_signal("widget::layout_changed") +end + --- Emit a signal and ensure all parent widgets in the hierarchies also -- forward the signal. This is useful to track signals when there is a dynamic -- set of containers and layouts wrapping the widget.