import app from "ags/gtk3/app" import { Astal, Gtk } from "ags/gtk3" import { createPoll } from "ags/time" import { For } from "ags" import { sh, shell } from "../lib/utils" export const THEMEPICKER_WINDOW = "theme-picker" const COLUMNS = 3 type Theme = { slug: string; name: string; swatches: string[] } const swatchProgram = "{ key=$1; value=$2; gsub(/[ \\t]/, \"\", key); gsub(/[ \"\\t]/, \"\", value); " + "if (value ~ /^#/) palette[key] = value } " + "END { split(\"color1 color2 color3 color4 color5 color6\", legacy, \" \"); " + "split(\"red green yellow blue magenta cyan\", named, \" \"); " + "for (i = 1; i <= 6; i++) { " + "value = (legacy[i] in palette) ? palette[legacy[i]] : ((named[i] in palette) ? palette[named[i]] : \"\"); " + "if (value != \"\") out = (out == \"\" ? value : out \",\" value) } print out }" const listCommand = "{ find \"$HOME/.config/omarchy/themes\" -mindepth 1 -maxdepth 1 -type d 2>/dev/null; " + "find \"$OMARCHY_PATH/themes\" -mindepth 1 -maxdepth 1 -type d 2>/dev/null; } | " + "while read -r d; do " + "[ -f \"$d/colors.toml\" ] || continue; " + "n=$(basename \"$d\"); " + "[ \"$n\" = blob-dynamic ] && continue; " + "c=$(awk -F'=' '" + swatchProgram + "' \"$d/colors.toml\"); " + "p=$(echo \"$n\" | sed -E 's/(^|-)([a-z])/\\1\\u\\2/g; s/-/ /g'); " + "echo \"$n|$p|$c\"; " + "done | awk -F'|' '!seen[$1]++' | sort -t'|' -k2,2" const themes = createPoll( [], 5000, shell(listCommand), (stdout) => stdout .split("\n") .filter((line) => line.length > 0) .map((line) => { const [slug, name, swatchStr] = line.split("|") return { slug, name: name ?? slug, swatches: (swatchStr ?? "").split(",").filter(Boolean) } }), ) const currentThemeName = createPoll( "", 2000, shell("cat \"$HOME/.local/state/omarchy/current/theme.name\" 2>/dev/null"), (stdout) => stdout.trim(), ) const isDynamic = currentThemeName.as((name) => name === "blob-dynamic") const rows = themes.as((list) => { const chunks: Theme[][] = [] for (let index = 0; index < list.length; index += COLUMNS) { chunks.push(list.slice(index, index + COLUMNS)) } return chunks }) const applyTheme = (slug: string) => { app.toggle_window(THEMEPICKER_WINDOW) sh(`"$HOME/scripts/blob_theme.sh" "${slug}"`) } const applyDynamic = () => { app.toggle_window(THEMEPICKER_WINDOW) sh(`"$HOME/scripts/blob_theme.sh" --dynamic`) } function Swatches({ colors }: { colors: string[] }) { return ( {colors.map((color) => ( ))} ) } function ThemeChip({ theme }: { theme: Theme }) { const active = currentThemeName.as((name) => name === theme.slug) return ( ) } function DynamicChip() { return ( ) } export default function ThemePicker() { const hasThemes = themes.as((list) => list.length > 0) const isEmpty = themes.as((list) => list.length === 0) return ( row.map((t) => t.slug).join("|")}> {(row: Theme[]) => ( {row.map((theme) => ( ))} )} ) }