reload stylesheet on change

This commit is contained in:
Daniel Barlow 2022-04-05 20:23:17 +00:00
parent 229ba07b80
commit 89d629bdda
2 changed files with 40 additions and 17 deletions

View File

@ -2,6 +2,7 @@
: GtkLayerShell : GtkLayerShell
: Gdk : Gdk
: GdkPixbuf : GdkPixbuf
: Gio
: GLib : GLib
: cairo } (require :lgi)) : cairo } (require :lgi))
@ -13,16 +14,35 @@
(local HEIGHT 48) (local HEIGHT 48)
(fn load-styles [pathname] (local css-provider
(let [style-provider (Gtk.CssProvider) (let [p (Gtk.CssProvider)]
(success err) (style-provider:load_from_path pathname)]
(if success
(Gtk.StyleContext.add_provider_for_screen (Gtk.StyleContext.add_provider_for_screen
(Gdk.Screen.get_default) (Gdk.Screen.get_default)
style-provider p
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
p))
(fn load-styles [pathname]
(let [(success err) (css-provider:load_from_path pathname)]
(or success
(print "failed to load stylesheet" err)))) (print "failed to load stylesheet" err))))
(var file-monitor nil)
(fn watch-stylesheet [pathname]
(let [f (Gio.File.new_for_path pathname)
(monitor err)
(Gio.File.monitor f Gio.FileMonitorFlags.NONE nil)]
(load-styles pathname)
(if (not monitor) (print :watch-stylesheet err))
(set file-monitor monitor)
(doto monitor
(tset
:on_changed
#(print "changed "
(load-styles (: $2 :get_path)))))))
(fn resolve [f] (fn resolve [f]
(match (type f) (match (type f)
"string" f "string" f
@ -184,7 +204,7 @@
(each [_ b (ipairs bars)] (each [_ b (ipairs bars)]
(make-layer-shell b.window :top true (make-layer-shell b.window :top true
(collect [_ edge (ipairs b.anchor)] (collect [_ edge (ipairs b.anchor)]
edge 1)) edge 0))
(b.window:show_all)) (b.window:show_all))
(Gtk.main)) (Gtk.main))
@ -192,5 +212,6 @@
: bar : bar
: indicator : indicator
: run : run
:stylesheet load-styles :stylesheet watch-stylesheet
: file-monitor ;don't let this get GCed
} }

View File

@ -1,13 +1,15 @@
.indicator { .indicator {
background-color: #ff00ff; color: #eef;
} background-color: #000;
.indicator.yellow { font-size: 11px;
background-color: #ff0;
} }
box.hey { .indicator * {
font-size: 18px; margin: 5px 8px;
padding: 5px 5px;
background-color: #225;
} }
label {
margin: 0px 12px; .bar {
background-color: #000;
} }