update only when interval requires it

This commit is contained in:
Daniel Barlow 2022-03-23 23:35:27 +00:00
parent 10154115bf
commit b0fe8e1dff
2 changed files with 16 additions and 11 deletions

View File

@ -17,7 +17,6 @@ you are happy to exert that control in a Lua-based Lisp language.
* [] update image/label widget instead of destroying * [] update image/label widget instead of destroying
* [] allow height customisation * [] allow height customisation
```fennel ```fennel
(bar (bar
{ {

View File

@ -39,7 +39,6 @@
(fn update-button [button icon text] (fn update-button [button icon text]
(match (button:get_child) it (button:remove it)) (match (button:get_child) it (button:remove it))
(let [i (resolve icon)] (let [i (resolve icon)]
(print :update i (resolve text))
(if i (if i
(button:add (find-icon i)) (button:add (find-icon i))
(button:add (Gtk.Label {:label (resolve text)}))) (button:add (Gtk.Label {:label (resolve text)})))
@ -51,15 +50,18 @@
: poll : poll
: text : text
: on-click}] : on-click}]
(let [button (var last-update -1)
(Gtk.Button { :relief Gtk.ReliefStyle.NONE}) (let [button (Gtk.Button { :relief Gtk.ReliefStyle.NONE})
update #(update-button button icon text)] update (fn [now]
(update) (when (and interval (> now (+ last-update interval)))
(update-button button icon text)
(set last-update now)))]
(update 0)
{ {
: interval : interval
: poll : poll
: button : button
: update :update #(update $2)
})) }))
(fn make-layer-shell [window layer exclusive? anchors] (fn make-layer-shell [window layer exclusive? anchors]
@ -93,15 +95,19 @@
(box:pack_start i.button false false 0)) (box:pack_start i.button false false 0))
(window:add box))) (window:add box)))
;; we want to run each indicator's update function only when
;; more than `interval` ms has elapsed since it last ran
(fn run [] (fn run []
(GLib.timeout_add (GLib.timeout_add
0 0
1000 1000
(fn [] (fn []
(print :update) (let [now (/ (GLib.get_monotonic_time) 1000)]
(each [_ bar (ipairs bars)] (each [_ bar (ipairs bars)]
(each [_ indicator (ipairs bar.indicators)] (each [_ indicator (ipairs bar.indicators)]
(indicator:update))) (indicator:update now))))
true)) true))
(each [_ b (ipairs bars)] (each [_ b (ipairs bars)]
(make-layer-shell b.window :top true (make-layer-shell b.window :top true