import app from "ags/gtk4/app" import { execAsync } from "ags/process" import GLib from "gi://GLib?version=2.0" import Gio from "gi://Gio?version=2.0" const HOME = GLib.getenv("HOME") || "" const WALLPAPER_DIR = `${HOME}/wallpapers` const GREETER_DIR = "/var/tmp/ags-greeter" /** * Export the current palette and wallpaper for the greetd greeter. * The greeter runs as the `greeter` user, which cannot read the user's home * directory, and /tmp is empty at boot. /var/tmp persists across reboots and * is world-readable, so it works for both. */ export async function exportGreeterTheme() { try { await execAsync(["mkdir", "-p", GREETER_DIR]) const walCache = `${HOME}/.cache/wal/colors.scss` const tmpColors = `/tmp/ags-colors.scss` if (GLib.file_test(walCache, GLib.FileTest.EXISTS)) { await execAsync(["cp", walCache, tmpColors]) } await execAsync([ "sass", "--no-source-map", `${HOME}/.config/ags/greeter/style.scss`, `${GREETER_DIR}/greeter.css`, ]) const wallpaper = `${HOME}/.cache/current_wallpaper.jpg` if (GLib.file_test(wallpaper, GLib.FileTest.EXISTS)) { await execAsync(["cp", wallpaper, `${GREETER_DIR}/wallpaper.jpg`]) } } catch (error) { console.error("Failed to export greeter theme:", error) } } /** * Get a list of all wallpapers in the default wallpapers directory. */ export async function getWallpapers(): Promise { try { const dir = Gio.File.new_for_path(WALLPAPER_DIR) const iter = dir.enumerate_children( "standard::name", Gio.FileQueryInfoFlags.NONE, null, ) let info const files: string[] = [] while ((info = iter.next_file(null))) { const name = info.get_name() if (name.match(/\.(jpg|jpeg|png|gif)$/i)) { files.push(`${WALLPAPER_DIR}/${name}`) } } files.sort() return files } catch (error) { console.error("Failed to read wallpapers directory:", error) return [] } } /** * Synchronously ensure that a pywal colors file exists in /tmp * before AGS loads the initial SCSS file. */ export function initThemeSync() { const walCache = `${HOME}/.cache/wal/colors.scss` const tmpColors = `/tmp/ags-colors.scss` if (GLib.file_test(walCache, GLib.FileTest.EXISTS)) { // If Pywal has run before, copy its colors.scss to our safe /tmp location try { const [ok, content] = GLib.file_get_contents(walCache) if (ok) { GLib.file_set_contents(tmpColors, content) } } catch {} } else { // If Pywal hasn't run yet, provide a safe fallback so Sass compiles const fallback = ` $wallpaper: ""; $background: #1e1e1e; $foreground: #d4d4d4; $cursor: #d4d4d4; $color0: #1e1e1e; $color1: #f44747; $color2: #6a9955; $color3: #d7ba7d; $color4: #569cd6; $color5: #c586c0; $color6: #4ec9b0; $color7: #d4d4d4; $color8: #808080; $color9: #f44747; $color10: #6a9955; $color11: #d7ba7d; $color12: #569cd6; $color13: #c586c0; $color14: #4ec9b0; $color15: #d4d4d4; ` try { GLib.file_set_contents(tmpColors, fallback) } catch {} } } /** * Restore the last set wallpaper. */ export async function restoreTheme() { let themed = false try { const out = await execAsync(["cat", `${HOME}/.cache/wal/wal`]) const wallpaperPath = out.trim() if (wallpaperPath) { // setTheme() exports the greeter theme as its last step. await setTheme(wallpaperPath) themed = true } } catch {} if (!themed) await exportGreeterTheme() } /** * Apply the wallpaper using hyprpaper, generate colors with pywal, * and reload the AGS theme. */ async function applyTheme(wallpaperPath: string) { // 1. Set wallpaper with hyprpaper try { // Preload the wallpaper first await execAsync(["hyprctl", "hyprpaper", "preload", wallpaperPath]).catch( () => {}, ) const out = await execAsync("hyprctl monitors -j") const monitors = JSON.parse(out) for (const mon of monitors) { await execAsync([ "hyprctl", "hyprpaper", "wallpaper", `${mon.name},${wallpaperPath}`, ]) } } catch (error) { console.error("Failed to set wallpaper via hyprpaper:", error) } // 2. Generate colors with pywal16 try { // -q: quiet, -i: image, -n: skip setting wallpaper (hyprpaper does it) await execAsync([ "wal", "--contrast", "21.0", "-q", "-n", "-i", wallpaperPath, ]) // Add this line so hyprlock always knows the current wallpaper: await execAsync([ "cp", wallpaperPath, `${HOME}/.cache/current_wallpaper.jpg`, ]) } catch (error) { console.error("Failed to generate colors with pywal:", error) } // 3. Re-compile and apply AGS styles try { const agsDir = `${HOME}/.config/ags` const scss = `${agsDir}/style.scss` const css = `/tmp/ags-compiled-style.css` // Copy the newly generated Pywal colors to the /tmp directory try { const walCache = `${HOME}/.cache/wal/colors.scss` const tmpColors = `/tmp/ags-colors.scss` await execAsync(["cp", walCache, tmpColors]) } catch (e) { console.error("Failed to copy pywal colors to /tmp:", e) } // Compile SCSS to CSS using the sass CLI await execAsync(["sass", "--no-source-map", scss, css]) // Reset any previously applied CSS and apply the new one app.reset_css() app.apply_css(css) } catch (error) { console.error("Failed to recompile and apply AGS styles:", error) } // 4. Keep the greetd greeter in sync with the current palette/wallpaper await exportGreeterTheme() // 5. Run update script try { const agsDir = `${HOME}/.config/ags` await execAsync([`${agsDir}/update_script.sh`]) } catch (error) { console.error("Failed to run update script:", error) } } let themeQueue: Promise = Promise.resolve() /** * Set the wallpaper and palette. Calls are serialized so rapid theme switches * cannot run wal/sass concurrently and race on the same output files. */ export function setTheme(wallpaperPath: string): Promise { const run = () => applyTheme(wallpaperPath) themeQueue = themeQueue.then(run, run) return themeQueue }