From a43cd1c0e8ddc8f03b7c6a4bcaa4ef467a9f708e Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Sun, 28 Jul 2024 23:29:23 +0545 Subject: [PATCH] read a single json file for appeareance --- config/.settings/color-scheme | 1 - config/.settings/custom-color | 1 - config/.settings/generation-scheme | 1 - config/.settings/swww-anim | 1 - config/ags/apps/settings/appearance.ts | 34 ++++++++++++-- config/ags/assets/settings.json | 6 +++ config/ags/scripts/dark-theme.sh | 48 +++++++++++++------- config/ags/variables.ts | 61 +++++--------------------- config/hypr/scripts/wallpaper.py | 50 +++++---------------- 9 files changed, 91 insertions(+), 112 deletions(-) delete mode 100644 config/.settings/color-scheme delete mode 100644 config/.settings/custom-color delete mode 100644 config/.settings/generation-scheme delete mode 100644 config/.settings/swww-anim create mode 100644 config/ags/assets/settings.json diff --git a/config/.settings/color-scheme b/config/.settings/color-scheme deleted file mode 100644 index 4520e2f7..00000000 --- a/config/.settings/color-scheme +++ /dev/null @@ -1 +0,0 @@ -dark diff --git a/config/.settings/custom-color b/config/.settings/custom-color deleted file mode 100644 index c86c3f35..00000000 --- a/config/.settings/custom-color +++ /dev/null @@ -1 +0,0 @@ -none \ No newline at end of file diff --git a/config/.settings/generation-scheme b/config/.settings/generation-scheme deleted file mode 100644 index f24f3153..00000000 --- a/config/.settings/generation-scheme +++ /dev/null @@ -1 +0,0 @@ -tonalSpot \ No newline at end of file diff --git a/config/.settings/swww-anim b/config/.settings/swww-anim deleted file mode 100644 index da44ff8f..00000000 --- a/config/.settings/swww-anim +++ /dev/null @@ -1 +0,0 @@ -wipe \ No newline at end of file diff --git a/config/ags/apps/settings/appearance.ts b/config/ags/apps/settings/appearance.ts index 6dbf9039..ffaa3b41 100644 --- a/config/ags/apps/settings/appearance.ts +++ b/config/ags/apps/settings/appearance.ts @@ -1,4 +1,4 @@ -import { theme, theme_settings, generation_scheme_file } from "variables.ts"; +import { theme, theme_settings, settings_file } from "variables.ts"; const GLib = imports.gi.GLib; import Gtk from "gi://Gtk?version=3.0"; import config from "services/configuration.ts"; @@ -25,6 +25,18 @@ const color_schemes = { } }; +export const updateGenerationScheme = async (scheme: string) => { + try { + const settingsContent = await Utils.readFile(settings_file); + const settings = JSON.parse(settingsContent); + settings["generation-scheme"] = scheme; + await Utils.writeFile(JSON.stringify(settings, null, 2), settings_file); + } catch (error) { + print(`Failed to update generation-scheme in settings.json: ${error}`); + } +}; + + let theme_reload_lock = false; const color_generator = `${GLib.get_home_dir()}/dotfiles/material-colors/generate.py`; @@ -86,7 +98,14 @@ const ReloadTheme = () => { theme_reload_lock = false; }) .then(() => { - Utils.writeFile("none", `${GLib.get_home_dir()}/dotfiles/.settings/custom-color`).catch(print); + // Update custom-color in settings.json + Utils.readFileAsync(settings_file) + .then((out) => { + const settings = JSON.parse(out); + settings["custom-color"] = "none"; + Utils.writeFile(JSON.stringify(settings, null, 2), settings_file).catch(print); + }) + .catch(print); }) .catch(print); } @@ -99,7 +118,14 @@ const ReloadTheme = () => { theme_reload_lock = false; }) .then(() => { - Utils.writeFile(color_to_write, `${GLib.get_home_dir()}/dotfiles/.settings/custom-color`).catch(print); + // Update custom-color in settings.json + Utils.readFileAsync(settings_file) + .then((out) => { + const settings = JSON.parse(out); + settings["custom-color"] = color_to_write; + Utils.writeFile(JSON.stringify(settings, null, 2), settings_file).catch(print); + }) + .catch(print); }) .catch((err) => { print(err); @@ -243,7 +269,7 @@ const ColorScheme = () => self.connect("changed", () => { selected = self.get_active_text(); theme_settings.scheme.setValue(color_schemes[selected!]); - Utils.writeFile(color_schemes[selected!], generation_scheme_file).catch(print); + updateGenerationScheme(color_schemes[selected!]); ReloadTheme(); }); } diff --git a/config/ags/assets/settings.json b/config/ags/assets/settings.json new file mode 100644 index 00000000..3d429136 --- /dev/null +++ b/config/ags/assets/settings.json @@ -0,0 +1,6 @@ +{ + "custom-color": "none", + "swww-anim": "wipe", + "color-scheme": "dark", + "generation-scheme": "tonalSpot" +} \ No newline at end of file diff --git a/config/ags/scripts/dark-theme.sh b/config/ags/scripts/dark-theme.sh index c8de00ff..2dd17900 100755 --- a/config/ags/scripts/dark-theme.sh +++ b/config/ags/scripts/dark-theme.sh @@ -1,31 +1,49 @@ #!/bin/bash + GENERATOR="$HOME/dotfiles/material-colors/generate.py" -FILE="$HOME/dotfiles/.settings/color-scheme" -SCHEME_FILE="$HOME/dotfiles/.settings/generation-scheme" -GENERATION_SCHEME="tonalSpot" -GENERATION_SCHEME=$(cat $SCHEME_FILE) +SETTINGS_FILE="$HOME/dotfiles/ags/assets/settings.json" + +# Function to read a value from the JSON file +get_json_value() { + local key=$1 + jq -r --arg key "$key" '.[$key]' $SETTINGS_FILE +} + +# Function to update a value in the JSON file +update_json_value() { + local key=$1 + local value=$2 + jq --arg key "$key" --arg value "$value" \ + '.[$key] = $value' $SETTINGS_FILE > "$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" $SETTINGS_FILE +} + toggle() { - VALUE=$(cat $FILE) + local color_scheme=$(get_json_value "color-scheme") + local generation_scheme=$(get_json_value "generation-scheme") - if [ "$VALUE" == "dark" ]; then - python -O $GENERATOR -R --color-scheme light --scheme $GENERATION_SCHEME - echo "light" >$FILE - - elif [ "$VALUE" == "light" ]; then - python -O $GENERATOR -R --color-scheme dark --scheme $GENERATION_SCHEME - echo "dark" >$FILE + if [ "$color_scheme" == "dark" ]; then + python -O $GENERATOR -R --color-scheme light --scheme $generation_scheme + update_json_value "color-scheme" "light" + elif [ "$color_scheme" == "light" ]; then + python -O $GENERATOR -R --color-scheme dark --scheme $generation_scheme + update_json_value "color-scheme" "dark" else - echo "Unknown value: $VALUE" + echo "Unknown color scheme: $color_scheme" fi } set() { - python -O $GENERATOR -R --color-scheme $1 --scheme $GENERATION_SCHEME - echo $1 >$FILE + local new_scheme=$1 + local generation_scheme=$(get_json_value "generation-scheme") + python -O $GENERATOR -R --color-scheme $new_scheme --scheme $generation_scheme + update_json_value "color-scheme" $new_scheme } +# Process arguments if [[ "$1" == "--toggle" ]]; then toggle elif [[ "$1" == "--set" ]]; then set $2 +else + echo "Usage: $0 --toggle | --set " fi diff --git a/config/ags/variables.ts b/config/ags/variables.ts index e45fee79..343c4fe9 100644 --- a/config/ags/variables.ts +++ b/config/ags/variables.ts @@ -100,9 +100,7 @@ export const kernel_name = await Utils.execAsync(`${App.configDir}/scripts/syste export const hostname = await Utils.execAsync(`${App.configDir}/scripts/system.sh --hostname`); export const current_os = await Utils.execAsync(`${App.configDir}/scripts/system.sh --os`); -export const custom_color_file = `${GLib.get_home_dir()}/dotfiles/.settings/custom-color`; -export const generation_scheme_file = `${GLib.get_home_dir()}/dotfiles/.settings/generation-scheme`; -export const color_scheme_file = `${GLib.get_home_dir()}/dotfiles/.settings/color-scheme`; +export const settings_file = `${GLib.get_home_dir()}/dotfiles/ags/assets/settings.json`; export const wallpaper_cache_file = `${GLib.get_home_dir()}/.cache/current_wallpaper`; function checkBrightness() { @@ -116,29 +114,17 @@ export const current_brightness = Variable(100, { poll: [500, checkBrightness] }); -function readFiles() { - Utils.readFileAsync(custom_color_file) + +function readSettingsFile() { + Utils.readFileAsync(settings_file) .then((out) => { - let _out = out; - if (_out.startsWith("-")) { - _out = _out.substring(1); - } - theme_settings.color.setValue(_out.trim()); + const settings = JSON.parse(out); + theme_settings.color.setValue(settings["custom-color"].trim()); + theme_settings.scheme.setValue(settings["generation-scheme"].trim()); + theme.setValue(settings["color-scheme"].trim()); }) .catch(print); - Utils.readFileAsync(generation_scheme_file) - .then((out) => { - theme_settings.scheme.setValue(out); - }) - .catch(print); - - Utils.readFileAsync(color_scheme_file) - .then((out) => { - theme.setValue(out.trim()); - }) - .catch((err) => {}); - Utils.readFileAsync(wallpaper_cache_file) .then((out) => { current_wallpaper.setValue(out.trim()); @@ -147,36 +133,9 @@ function readFiles() { Utils.writeFileSync(current_wallpaper.value, wallpaper_cache_file); }); } -readFiles(); - -Utils.monitorFile(custom_color_file, () => { - Utils.readFileAsync(custom_color_file) - .then((out) => { - let _out = out; - if (_out.startsWith("-")) { - _out = _out.substring(1); - } - theme_settings.color.setValue(_out.trim()); - }) - .catch((err) => {}); -}); - -Utils.monitorFile(generation_scheme_file, () => { - Utils.readFileAsync(generation_scheme_file) - .then((out) => { - theme_settings.scheme.setValue(out.trim()); - }) - .catch((err) => {}); -}); - -Utils.monitorFile(color_scheme_file, () => { - Utils.readFileAsync(color_scheme_file) - .then((out) => { - theme.setValue(out.trim()); - }) - .catch((err) => {}); -}); +readSettingsFile(); +Utils.monitorFile(settings_file, readSettingsFile); Utils.monitorFile(wallpaper_cache_file, () => { Utils.readFileAsync(wallpaper_cache_file) .then((out) => { diff --git a/config/hypr/scripts/wallpaper.py b/config/hypr/scripts/wallpaper.py index 2d0047b5..7b9672c0 100644 --- a/config/hypr/scripts/wallpaper.py +++ b/config/hypr/scripts/wallpaper.py @@ -7,11 +7,10 @@ import subprocess import asyncio import random as _random import colorsys - +import json lock_file_path = '/tmp/wallpaper.lock' - def acquire_lock(): global lock_file lock_file = open(lock_file_path, 'w') @@ -23,25 +22,18 @@ def acquire_lock(): print("Another instance of the script is already running.") sys.exit(1) - def release_lock(): fcntl.flock(lock_file, fcntl.LOCK_UN) lock_file.close() os.remove(lock_file_path) - def hue_to_numeric_hex(hue): hue = hue / 360.0 - rgb = colorsys.hls_to_rgb(hue, 0.5, 1.0) - hex_color_str = '#{:02x}{:02x}{:02x}'.format(int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)) - numeric_hex_color = int(hex_color_str.lstrip('#'), 16) - return numeric_hex_color - parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument('-I', '--image', type=str, help="Image") @@ -60,6 +52,17 @@ status = args.status HOME = os.path.expanduser("~") +# Load configuration from JSON file +config_file = f"{HOME}/dotfiles/ags/assets/settings.json" # Path to your JSON configuration file + +with open(config_file, 'r') as f: + config = json.load(f) + +color_scheme = config.get("color-scheme", "dark") +custom_color = config.get("custom-color", "none") +generation_scheme = config.get("generation-scheme", "tonalSpot") +transition_type = config.get("swww-anim", "wipe") + module_path = f'{HOME}/dotfiles/material-colors' sys.path.append(module_path) @@ -73,48 +76,27 @@ spec.loader.exec_module(GENERATOR) # type: ignore cache_file = f"{HOME}/.cache/current_wallpaper" square = f"{HOME}/.cache/square_wallpaper.png" png = f"{HOME}/.cache/current_wallpaper.png" -color_scheme_file = f"{HOME}/dotfiles/.settings/color-scheme" -custom_color_file = f"{HOME}/dotfiles/.settings/custom-color" -generation_scheme_file = f"{HOME}/dotfiles/.settings/generation-scheme" -swww_animation_file = f"{HOME}/dotfiles/.settings/swww-anim" - def current_state(str: str): with open(status, 'w') as f: f.write(str) - def send_notify(label: str, desc: str): if not notify: return subprocess.run(["notify-send", label, desc]) - def state(name: str | None, label: str | None, desc: str | None): if name is not None: current_state(name) if label is not None: send_notify(label, desc or "") - def join(*args): return os.path.join(*args) - async def main(): - global color_scheme, custom_color state("init", None, None) - with open(color_scheme_file) as f: - color_scheme = f.read().strip() - - with open(custom_color_file) as f: - custom_color = f.read().strip() - - with open(generation_scheme_file) as f: - generation_scheme = f.read().strip() - - with open(swww_animation_file) as f: - swww_animation = f.read().strip() new_wallpaper = f"{HOME}/Pictures/wallpapers/1.jpg" @@ -141,11 +123,6 @@ async def main(): # ----------------------------------------------------- # Set the new wallpaper # ----------------------------------------------------- - - transition_type = "wipe" - # transition_type = "outer" - # transition_type = "random" - wallpaper_engine = "swww" # Default wallpaper engine state("changing", "Changing wallpaper...", with_image) @@ -198,7 +175,6 @@ async def main(): await asyncio.gather(square_task, png_task) state("finish", "Wallpaper procedure complete!", with_image) - async def png_image(wallpaper: str): _from = (".jpg", "jpeg") if not wallpaper.endswith(_from): @@ -220,7 +196,6 @@ async def png_image(wallpaper: str): else: print(":: JPG successfully converted to PNG!") - async def square_image(wallpaper): with_image = f"with image {wallpaper}" state(None, "Creating square version...", with_image) @@ -238,7 +213,6 @@ async def square_image(wallpaper): else: print(":: Square image created!") - if __name__ == "__main__": acquire_lock() try: