diff --git a/README.md b/README.md index 216523d..5336aa4 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,15 @@ If you already have your own `~/.config/gtk-{3,4}.0/gtk.css`, install refuses to | `~/.config/omarchy/themed/gtk.css.tpl` | libadwaita named colors from the theme's `colors.toml` | | `~/.config/omarchy/hooks/theme-set.d/20-gtk-colors` | copies the rendered `gtk.css` to `gtk-3.0` and `gtk-4.0` | | `~/.config/gtk-3.0/gtk.css`, `~/.config/gtk-4.0/gtk.css` | generated on every theme change | -| `~/.local/share/nautilus-python/extensions/omarchy_theme_live.py` | reloads `gtk-4.0/gtk.css` inside Nautilus whenever it changes | +| `~/.local/share/nautilus-python/extensions/omarchy_theme_live.py` | reloads the theme's `gtk.css` inside Nautilus the moment the theme changes | ## How the live recolor works GTK reads `gtk.css` once, when an app starts, and Nautilus also stays alive for 60 seconds after its last window closes, so without help a theme change only shows up in a freshly started Nautilus. The extension (run by `nautilus-python`, which Omarchy installs) loads the -same file as an extra style provider inside the Nautilus process and watches it, so open -windows recolor within a fraction of a second of a theme switch. +theme's `gtk.css` as an extra style provider inside the Nautilus process and reloads it as +soon as Omarchy swaps in the new theme directory, before the slower theme-set hooks run, so +open windows recolor within about 0.3 seconds of a theme switch. Other GTK apps still pick the colors up the next time they start. diff --git a/omarchy-gtk-theme-sync b/omarchy-gtk-theme-sync index bf8b4be..fb5ecdf 100755 --- a/omarchy-gtk-theme-sync +++ b/omarchy-gtk-theme-sync @@ -6,7 +6,7 @@ # ~/.config/omarchy/themed/gtk.css.tpl color template, rendered per theme # ~/.config/omarchy/hooks/theme-set.d/20-gtk-colors copies the result to GTK's config # ~/.local/share/nautilus-python/extensions/omarchy_theme_live.py -# live-reloads those colors inside Nautilus +# live-reloads the theme colors inside Nautilus # All of these live in your home directory, so `omarchy update` leaves them alone. # # Usage: omarchy-gtk-theme-sync [--icons] [--force] @@ -122,8 +122,9 @@ write_extension() { mkdir -p "$(dirname "$EXT")" cat >"$EXT" <<'PY' # Generated by omarchy-gtk-theme-sync: recolor Nautilus live when the Omarchy theme changes. -# GTK reads ~/.config/gtk-4.0/gtk.css once at startup. This loads the same file as an extra -# style provider inside the Nautilus process and reloads it whenever the file changes. +# GTK reads ~/.config/gtk-4.0/gtk.css once at startup. This loads the theme's gtk.css as an +# extra style provider inside the Nautilus process and reloads it the moment Omarchy swaps in +# a new theme directory, without waiting for the theme-set hooks, which run last. import os import gi @@ -132,23 +133,29 @@ gi.require_version("Gtk", "4.0") gi.require_version("Gdk", "4.0") from gi.repository import Gdk, Gio, GLib, Gtk # noqa: E402 -CSS_PATH = os.path.expanduser("~/.config/gtk-4.0/gtk.css") -DEBOUNCE_MS = 150 +STATE_DIR = os.path.expanduser("~/.local/state/omarchy/current") +CSS_PATHS = ( + os.path.join(STATE_DIR, "theme", "gtk.css"), + os.path.expanduser("~/.config/gtk-4.0/gtk.css"), +) +DEBOUNCE_MS = 40 _state = {} def _reload(): _state.pop("pending", None) - try: - _state["provider"].load_from_path(CSS_PATH) - except GLib.Error: - pass # file mid-write or missing; the next change event retries + for path in CSS_PATHS: + try: + _state["provider"].load_from_path(path) + break + except GLib.Error: + continue # theme dir mid-swap or file missing; the next event retries return GLib.SOURCE_REMOVE def _on_change(_monitor, _file, _other, _event): - # cp fires several events per write; collapse them into one reload. + # Omarchy does rm -rf then mv on the theme dir; collapse those events into one reload. if "pending" not in _state: _state["pending"] = GLib.timeout_add(DEBOUNCE_MS, _reload) @@ -162,9 +169,11 @@ def _setup(): _reload() # One step above the user's own gtk.css so these colors always win. Gtk.StyleContext.add_provider_for_display(display, provider, Gtk.STYLE_PROVIDER_PRIORITY_USER + 1) - monitor = Gio.File.new_for_path(CSS_PATH).monitor_file(Gio.FileMonitorFlags.NONE, None) - monitor.connect("changed", _on_change) - _state["monitor"] = monitor + for watched in (STATE_DIR, os.path.dirname(CSS_PATHS[1])): + if os.path.isdir(watched): + monitor = Gio.File.new_for_path(watched).monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, None) + monitor.connect("changed", _on_change) + _state[watched] = monitor return GLib.SOURCE_REMOVE