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 -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function only_on_screen:set_widget(widget) only_on_screen.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function only_on_screen:get_widget() function only_on_screen:get_widget()
return self._private.widget return self._private.widget

View File

@ -192,13 +192,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function arcchart:set_widget(widget) arcchart.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function arcchart:get_children() function arcchart:get_children()
return {self._private.widget} return {self._private.widget}

View File

@ -218,13 +218,7 @@ end
-- @tparam widget widget The widget to be disaplayed inside of the background -- @tparam widget widget The widget to be disaplayed inside of the background
-- area -- area
function background:set_widget(widget) background.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function background:get_widget() function background:get_widget()
return self._private.widget return self._private.widget

View File

@ -42,10 +42,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function constraint:set_widget(widget) constraint.set_widget = base.set_widget_common
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function constraint:get_widget() function constraint:get_widget()
return self._private.widget return self._private.widget

View File

@ -73,13 +73,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function margin:set_widget(widget) margin.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function margin:get_widget() function margin:get_widget()
return self._private.widget return self._private.widget

View File

@ -49,13 +49,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function mirror:set_widget(widget) mirror.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function mirror:get_widget() function mirror:get_widget()
return self._private.widget return self._private.widget

View File

@ -61,13 +61,11 @@ function place:fit(context, width, height)
and height or h and height or h
end end
function place:set_widget(widget) --- The widget to be placed.
if widget then -- @property widget
base.check_widget(widget) -- @tparam widget widget The widget
end
self._private.widget = widget place.set_widget = base.set_widget_common
self:emit_signal("widget::layout_changed")
end
function place:get_widget() function place:get_widget()
return self._private.widget return self._private.widget

View File

@ -129,13 +129,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function radialprogressbar:set_widget(widget) radialprogressbar.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function radialprogressbar:get_children() function radialprogressbar:get_children()
return {self._private.widget} return {self._private.widget}

View File

@ -62,13 +62,7 @@ end
-- @property widget -- @property widget
-- @tparam widget widget The widget -- @tparam widget widget The widget
function rotate:set_widget(widget) rotate.set_widget = base.set_widget_common
if widget then
base.check_widget(widget)
end
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function rotate:get_widget() function rotate:get_widget()
return self._private.widget return self._private.widget

View File

@ -277,10 +277,14 @@ function scroll:set_widget(widget)
if widget == self._private.widget then if widget == self._private.widget then
return return
end end
if widget then
base.check_widget(widget) local w = base.make_widget_from_value(widget)
if w then
base.check_widget(w)
end end
self._private.widget = widget
self._private.widget = w
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end

View File

@ -70,8 +70,9 @@ function fixed:add(...)
local args = { n=select('#', ...), ... } local args = { n=select('#', ...), ... }
assert(args.n > 0, "need at least one widget to add") assert(args.n > 0, "need at least one widget to add")
for i=1, args.n do for i=1, args.n do
base.check_widget(args[i]) local w = base.make_widget_from_value(args[i])
table.insert(self._private.widgets, args[i]) base.check_widget(w)
table.insert(self._private.widgets, w)
end end
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end

View File

@ -293,10 +293,9 @@ function grid:add(...)
assert(args.n > 0, "need at least one widget to add") assert(args.n > 0, "need at least one widget to add")
local row, column local row, column
for i=1, args.n do for i=1, args.n do
local w = args[i]
-- Get the next empty coordinate to insert the widget -- Get the next empty coordinate to insert the widget
row, column = self:get_next_empty(row, column) 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
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 col_span = (col_span and col_span > 0) and col_span or 1
-- check if the object is a widget -- check if the object is a widget
child = base.make_widget_from_value(child)
base.check_widget(child) base.check_widget(child)
-- test if the new widget superpose with existing ones -- test if the new widget superpose with existing ones

View File

@ -102,9 +102,16 @@ function manual_layout:layout(context, width, height)
end end
function manual_layout:add(...) function manual_layout:add(...)
local wdgs = {...} local wdgs = {}
local old_count = #self._private.widgets 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 -- Add the points
for k, v in ipairs(wdgs) do for k, v in ipairs(wdgs) do
@ -166,7 +173,7 @@ function manual_layout:add_at(widget, point)
end end
self._private.pos[#self._private.widgets+1] = point self._private.pos[#self._private.widgets+1] = point
self:add(widget) self:add(base.make_widget_from_value(widget))
end end
--- Move a widget (by index). --- Move a widget (by index).

View File

@ -355,8 +355,9 @@ function ratio:add(...)
local args = { n=select('#', ...), ... } local args = { n=select('#', ...), ... }
assert(args.n > 0, "need at least one widget to add") assert(args.n > 0, "need at least one widget to add")
for i=1, args.n do for i=1, args.n do
base.check_widget(args[i]) local w = base.make_widget_from_value(args[i])
table.insert(self._private.widgets, args[i]) base.check_widget(w)
table.insert(self._private.widgets, w)
end end
normalize(self) normalize(self)

View File

@ -155,6 +155,24 @@ function base.widget:get_all_children()
return ret return ret
end 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 --- 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 -- forward the signal. This is useful to track signals when there is a dynamic
-- set of containers and layouts wrapping the widget. -- set of containers and layouts wrapping the widget.