widget: Mutualize all set_widget implementation to behave the same

Now always call both check_widget and make_widget_from_value. This
should make it a lot less confusing when randomly trying to create
a widget as all ways to do it slowly converge toward an unified
one.
This commit is contained in:
Emmanuel Lepage Vallee 2018-12-26 23:54:10 -05:00
parent 349b75994f
commit 82db9180b1
15 changed files with 56 additions and 72 deletions

View File

@ -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

View File

@ -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}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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).

View File

@ -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)

View File

@ -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.