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

View File

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

View File

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

View File

@ -23,37 +23,35 @@ client.focus = client.gen_fake{
--DOC_NEWLINE --DOC_NEWLINE
local default_template = wibox.widget.template { local default_template = wibox.widget.template {
template = { {
{ {
{ {
{ forced_height = 16,
forced_height = 16, forced_width = 16,
forced_width = 16, shape = gears.shape.circle,
shape = gears.shape.circle, widget = wibox.widget.separator
widget = wibox.widget.separator
},
margins = 3,
widget = wibox.container.margin
}, },
{ margins = 3,
set_color = function(self, color) widget = wibox.container.margin
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" set_color = function(self, color)
self.fg = color self.text = color
end, end,
border_width = 1, text = "N/A",
shape = gears.shape.octogon, widget = wibox.widget.textbox
widget = wibox.container.background, },
forced_width = 100, --DOC_HIDE 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 --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 -- Build the default widget used as a fallback if user doesn't provide a template
local default_widget = { local default_widget = {
template = wibox.widget.textbox, widget = wibox.widget.textbox,
text = "N/A", text = "N/A",
update_callback = function(widget_template, args) update_callback = function(widget_template, args)
local text = args and args.text or "???" local text = args and args.text or "???"
widget_template.widget.text = text widget_template.widget.text = text

View File

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

View File

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