wibox.template: Remove the `args` in favor of passing the template directly.

It didn't do much and I wanted to make the syntax a drop-in replacement
of `wibox.widget {}`.
This commit is contained in:
Emmanuel Lepage Vallee 2022-10-22 20:32:12 -07:00
parent 67b2b26683
commit 3765efaccc
7 changed files with 71 additions and 85 deletions

View File

@ -362,34 +362,28 @@ end
-- @method clone
-- @treturn wibox.widget.template The copy.
function template:clone()
return template {
template = self._private.widget_template
}
return template(self._private.widget_template)
end
--- Create a new `wibox.widget.template` instance.
-- @tparam[opt] table args
-- @tparam[opt] table|widget args.template The widget template to use.
-- @tparam[opt] function args.update_callback The callback function to update
-- @tparam[opt] table tmpl
-- @tparam[opt] function tmpl.update_callback The callback function to update
-- the widget.
-- @tparam[opt] boolean args.update_now Update the widget after its
-- @tparam[opt] boolean tmpl.update_now Update the widget after its
-- construction. This will call the `:update()` method with no parameter.
-- @treturn wibox.widget.template The new instance.
-- @constructorfct wibox.widget.template
function template.new(args)
args = args or {}
function template.new(tmpl)
tmpl = tmpl or {}
local ret = wbase.make_widget(nil, nil, { enable_properties = true })
gtable.crush(ret, template, true)
ret._private.connections = {}
ret:set_template(args.template)
ret:set_update_callback(args.update_callback)
ret:set_update_now(args.update_now)
-- Apply the received buttons, visible, forced_width and so on
gtable.crush(ret, args)
ret:set_template(tmpl)
ret:set_update_callback(tmpl.update_callback)
ret:set_update_now(tmpl.update_now)
rawset(ret, "_is_template", true)
@ -412,9 +406,7 @@ function template.make_from_value(value)
if rawget(value, "_is_template") then return value:clone() end
return template.new {
template = value
}
return template.new(value)
end
function template.mt:__call(...)

View File

@ -13,29 +13,27 @@ client.focus = client.gen_fake{
--DOC_HIDE_END
local my_template_widget = wibox.widget.template {
template = {
{
{
{
set_icon = function(self, icon)
self.image = gears.surface(icon)
end,
id = "icon_role",
widget = wibox.widget.imagebox
},
{
id = "title_role",
widget = wibox.widget.textbox
},
widget = wibox.layout.fixed.horizontal,
set_icon = function(self, icon)
self.image = gears.surface(icon)
end,
id = "icon_role",
widget = wibox.widget.imagebox
},
widget = wibox.container.background,
id = "background_role",
set_urgent = function(self, status)
self.bg = status and "#ff0000" or nil
end,
forced_width = 200, --DOC_HIDE
forced_height = 24, --DOC_HIDE
}
{
id = "title_role",
widget = wibox.widget.textbox
},
widget = wibox.layout.fixed.horizontal,
},
widget = wibox.container.background,
id = "background_role",
set_urgent = function(self, status)
self.bg = status and "#ff0000" or nil
end,
forced_width = 200, --DOC_HIDE
forced_height = 24, --DOC_HIDE
}
--DOC_NEWLINE

View File

@ -6,7 +6,7 @@ local wibox = require("wibox")
--DOC_HIDE_END
local my_template_widget = wibox.widget.template {
template = wibox.widget.textbox,
widget = wibox.widget.textbox,
update_callback = function(template_widget, args)
local text = args.text or "???"
template_widget.widget.text = text

View File

@ -23,37 +23,35 @@ client.focus = client.gen_fake{
--DOC_NEWLINE
local default_template = wibox.widget.template {
template = {
{
{
{
{
forced_height = 16,
forced_width = 16,
shape = gears.shape.circle,
widget = wibox.widget.separator
},
margins = 3,
widget = wibox.container.margin
forced_height = 16,
forced_width = 16,
shape = gears.shape.circle,
widget = wibox.widget.separator
},
{
set_color = function(self, color)
self.text = color
end,
text = "N/A",
widget = wibox.widget.textbox
},
spacing = 5,
widget = wibox.layout.fixed.horizontal,
margins = 3,
widget = wibox.container.margin
},
set_color = function(self, color)
self.border_color = gears.color.to_rgba_string(color):sub(1,7).."44"
self.fg = color
end,
border_width = 1,
shape = gears.shape.octogon,
widget = wibox.container.background,
forced_width = 100, --DOC_HIDE
}
{
set_color = function(self, color)
self.text = color
end,
text = "N/A",
widget = wibox.widget.textbox
},
spacing = 5,
widget = wibox.layout.fixed.horizontal,
},
set_color = function(self, color)
self.border_color = gears.color.to_rgba_string(color):sub(1,7).."44"
self.fg = color
end,
border_width = 1,
shape = gears.shape.octogon,
widget = wibox.container.background,
forced_width = 100, --DOC_HIDE
}
--DOC_NEWLINE

View File

@ -10,8 +10,8 @@ local wibox = require("wibox")
-- Build the default widget used as a fallback if user doesn't provide a template
local default_widget = {
template = wibox.widget.textbox,
text = "N/A",
widget = wibox.widget.textbox,
text = "N/A",
update_callback = function(widget_template, args)
local text = args and args.text or "???"
widget_template.widget.text = text

View File

@ -17,7 +17,7 @@ end
-- Instanciate the widget with a custom template
local custom_widget = concrete_widget_template_builder {
widget_template = {
template = wibox.widget.imagebox,
widget = wibox.widget.imagebox,
update_callback = function (template, args)
if args and args.text == "default text" then
template.widget.image = "/path/to/image.png"

View File

@ -15,23 +15,21 @@ client.focus = client.gen_fake{
--DOC_HIDE_END
local my_template_widget = wibox.widget.template {
template = {
{
{
{
id = "icon_role",
widget = wibox.widget.imagebox
},
{
id = "title_role",
widget = wibox.widget.textbox
},
widget = wibox.layout.fixed.horizontal,
id = "icon_role",
widget = wibox.widget.imagebox
},
id = "background_role",
widget = wibox.container.background,
forced_width = 200, --DOC_HIDE
forced_height = 24, --DOC_HIDE
}
{
id = "title_role",
widget = wibox.widget.textbox
},
widget = wibox.layout.fixed.horizontal,
},
id = "background_role",
widget = wibox.container.background,
forced_width = 200, --DOC_HIDE
forced_height = 24, --DOC_HIDE
}
--DOC_NEWLINE