Skip to content

Commit 7c0a538

Browse files
committed
Added Container widget.
This wraps an existing widget, but allows for the wrapped widget to be swapped out for another (or even removed).
1 parent 736f83d commit 7c0a538

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/widget.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,63 @@ function Frame:render_background(cr)
460460
end
461461
end
462462

463+
--- A container that holds a widget, but can have that widget dynamically changed.
464+
-- @type Container
465+
local Container = util.class(Widget)
466+
w.Container = Container
467+
468+
--- @tparam Widget content Initial widget to be wrapped, or nil for no widget
469+
function Container:init(content)
470+
self:set_content(content, true)
471+
end
472+
473+
--- @tparam Widget content New widget to be wrapped, or nil for no widget
474+
-- @tparam boolean init Should only be set to true when initialising
475+
function Container:set_content(content, init)
476+
if not (init or self._content ~= content) then
477+
return
478+
end
479+
480+
self._content = content
481+
self.height = 0
482+
self.width = 0
483+
484+
self._switched = not init
485+
486+
if content and content.height then
487+
self.height = content.height
488+
end
489+
if content and content.width then
490+
self.width = content.width
491+
end
492+
end
493+
494+
function Container:layout(width, height)
495+
if self._content and self._content.layout then
496+
local children = self._content:layout(width, height)
497+
table.insert(children, 1, {self, 0, 0, width, height})
498+
return children
499+
end
500+
return {{self, 0, 0, width, height}}
501+
end
502+
503+
function Container:render(cr)
504+
if self._content and self._content.render then
505+
return self._content:render(cr)
506+
end
507+
end
508+
509+
function Container:update()
510+
local updated = false
511+
if self._content and self._content.update then
512+
updated = self._content:update()
513+
end
514+
515+
local switched = self._switched
516+
self._switched = false
517+
return switched or updated
518+
end
519+
463520
--- Common (abstract) base class for `StaticText` and `TextLine`.
464521
-- @type Text
465522
local Text = util.class(Widget)

0 commit comments

Comments
 (0)