From c1a6a1e91590e2f905dc7d6accce59d71db5e3a5 Mon Sep 17 00:00:00 2001 From: Allan Crooks Date: Mon, 27 Mar 2023 23:58:00 +0100 Subject: [PATCH 1/2] Added Container widget. This wraps an existing widget, but allows for the wrapped widget to be swapped out for another (or even removed). --- src/widget.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/widget.lua b/src/widget.lua index 234380a..c49785e 100644 --- a/src/widget.lua +++ b/src/widget.lua @@ -446,6 +446,69 @@ function Frame:render_background(cr) end end +--- A container that holds a widget, but can have that widget dynamically changed. +-- @type Container +local Container = util.class(Widget) +w.Container = Container + +--- @tparam Widget content Initial widget to be wrapped, or nil for no widget +function Container:init(content) + self:set_content(content, true) +end + +--- @tparam Widget content New widget to be wrapped, or nil for no widget +-- @tparam boolean init Should only be set to true when initialising +function Container:set_content(content, init) + if not (init or self._content ~= content) then + return + end + + self._content = content + self.height = 0 + self.width = 0 + + self._switched = not init + + if content and content.height then + self.height = content.height + end + if content and content.width then + self.width = content.width + end +end + +function Container:layout(width, height) + if self._content and self._content.layout then + local children = self._content:layout(width, height) + table.insert(children, 1, {self, 0, 0, width, height}) + return children + end + return {{self, 0, 0, width, height}} +end + +function Container:render(cr) + if self._content and self._content.render then + return self._content:render(cr) + end +end + +function Container:render_background(cr) + if self._content and self._content.render_background then + return self._content:render_background(cr) + end +end + +function Container:update() + local updated = false + if self._content and self._content.update then + updated = self._content:update() + end + + local switched = self._switched + self._switched = false + return switched or updated +end + --- Common (abstract) base class for `StaticText` and `TextLine`. -- @type Text local Text = util.class(Widget) From 842306ca6e8a85c3b83bdf0aeb24e4857e88c6dc Mon Sep 17 00:00:00 2001 From: Allan Crooks Date: Sun, 2 Apr 2023 22:21:26 +0100 Subject: [PATCH 2/2] Add example for container widget. --- examples/containers.lua | 101 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 examples/containers.lua diff --git a/examples/containers.lua b/examples/containers.lua new file mode 100644 index 0000000..2463cb8 --- /dev/null +++ b/examples/containers.lua @@ -0,0 +1,101 @@ +--- Conky config script + +-- Conky does not add our config directory to lua's PATH, so we do it manually +local script_dir = debug.getinfo(1, 'S').source:match("^@(.*/)") or "./" +package.path = script_dir .. "../?.lua;" .. package.path + +local util = require('src/util') +local data = require('src/data') +local widget = require('src/widget') +local polycore = require('src/polycore') + +local width = 200 +local height = 400 + +local dark_box_bg = {0.70, 0.70, 0.70, 1} +local light_box_bg = {0.83, 0.83, 0.83, 1} +local black = {0, 0, 0, 1} +local blackish = {0.2, 0.2, 0.2, 1} +local dark_grey = {0.5, 0.5, 0.5, 1} +local white = {1, 1, 1, 1} +local yellow = {1, 1, 0, 1} + +local dark_box = {white, dark_box_bg} +local light_box = {dark_grey, light_box_bg} + +local BoxStat = util.class(widget.Frame) + +function BoxStat:init(box_color, title, stat) + local title_color = box_color[1] + local title_w = widget.StaticText(title, {font_size=32, font_weight=CAIRO_FONT_WEIGHT_BOLD, color=title_color}) + + local stat_widget = widget.TextLine{align="right", font_size=30, color=blackish, margin={0, 0, 8, 0}} + function stat_widget:update() + self:set_text(data.conky_loader:get(stat)) + end + + local grp = {title_w, widget.Filler{height=12}, stat_widget} + widget.Frame.init(self, widget.Rows(grp), {padding={10,12,18,6}, background_color=box_color[2]}) +end + +--- Called once on startup to initialize widgets. +-- @treturn widget.Renderer +function polycore.setup() + + local rambox = BoxStat(dark_box, "RAM", "$mem") + local swapbox = BoxStat(light_box, "Swap", "$swap") + + -- Alternate between showing RAM usage and Swap usage + local memory_container = widget.Container(rambox) + function memory_container:update(count) + if (count % 8) < 4 then + self:set_content(rambox) + else + self:set_content(swapbox) + end + return widget.Container.update(self, count) + end + + local warn_text = widget.StaticText("CPU load\nis high!!", {align="center", font_size=26, font_weight=CAIRO_FONT_WEIGHT_BOLD, color=black}) + local warn_frame = widget.Frame(widget.Rows{warn_text}, {}) + + -- Show a message if the load average is over 4. + -- Otherwise, hide it. + local cpu_container = widget.Container(warn_frame, {}) + function cpu_container:update() + if tonumber(data.conky_loader:get("${loadavg 1}")) < 4 then + self:set_content(nil) + else + self:set_content(warn_frame) + end + return widget.Container.update(self, count) + end + + local loadavg = BoxStat(dark_box, "CPU Load", "${loadavg 1}") + + local root = widget.Rows({memory_container, cpu_container, loadavg}) + return widget.Renderer{root=root, width=width, height=height} +end + +local conkyrc = conky or {} +conkyrc.config = { + lua_load = script_dir .. "containers.lua", + lua_startup_hook = "conky_setup", + lua_draw_hook_pre = "conky_paint_background", + lua_draw_hook_post = "conky_update", + alignment = "middle_right", + background = false, + double_buffer = true, + use_xft = false, + gap_x = -6, + gap_y = -40, + maximum_width = width, + minimum_height = height, + minimum_width = width, + own_window = true, + own_window_class = 'Conky', + own_window_type = 'panel', + own_window_transparent = true, + times_in_seconds = true, +} +conkyrc.text = ""