From e4e59c52faef38cf20c7659f6ba8378ec2d42b99 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Thu, 30 Oct 2025 19:54:23 +0100 Subject: [PATCH] [hypr] WIP: Switch colours at boot --- nixos/modules/desktop/waybar/default.nix | 6 ++- scripts/toggle-light-dark.sh | 66 +++++++++++++++++++----- stow/hyprland/.config/hypr/hyprland.conf | 2 + 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/nixos/modules/desktop/waybar/default.nix b/nixos/modules/desktop/waybar/default.nix index 273e4ce..4016c20 100644 --- a/nixos/modules/desktop/waybar/default.nix +++ b/nixos/modules/desktop/waybar/default.nix @@ -7,9 +7,11 @@ in { config = lib.mkIf cfg.enable { environment.systemPackages = with pkgs; [ - libnotify + jq # JSON parser + libnotify # Notifications + sunwait # Sunrise/sunset calculator + playerctl - jq j4-dmenu-desktop ]; diff --git a/scripts/toggle-light-dark.sh b/scripts/toggle-light-dark.sh index c4667bf..7fb8f0c 100755 --- a/scripts/toggle-light-dark.sh +++ b/scripts/toggle-light-dark.sh @@ -14,10 +14,27 @@ STATE_DIR="${HOME}/.local/state/sisyphus" STATE_FILE="${STATE_DIR}/theme" BG_DIR="${HOME}/Nextcloud/Afbeeldingen/wallpapers" +LATITUDE='50.50N' # Belgium +LONGITUDE='4.00E' + +# Mapping inputs to themes + +declare -A theme_name +theme_name['light']="${THEME_LIGHT}" +theme_name['dark']="${THEME_DARK}" + +declare -A theme_time +theme_time['DAY']="${THEME_LIGHT}" +theme_time['NIGHT']="${THEME_DARK}" + +# Mapping of current theme to next theme + declare -A theme_next theme_next[${THEME_LIGHT}]="${THEME_DARK}" theme_next[${THEME_DARK}]="${THEME_LIGHT}" +# Mapping of theme to applications + declare -A gsettings_alt gsettings_alt[${THEME_LIGHT}]='default' gsettings_alt[${THEME_DARK}]='prefer-dark' @@ -35,17 +52,14 @@ wallpaper[${THEME_DARK}]="bg-dark" ############# # Parse options -while getopts ":m:g" option; do +while getopts ":m:gt" option; do case "${option}" in m) - if [ "${OPTARG}" == 'light' ]; then - theme="${THEME_LIGHT}" - elif [ "${OPTARG}" == 'dark' ]; then - theme="${THEME_DARK}" - else + if [ -z "${theme_name[${OPTARG}]}" ]; then >&2 printf "Error: Invalid mode: '%s'.\nShould be either 'light' or 'dark'\n" "${option}" exit 1 fi + theme="${theme_name[${OPTARG}]}" ;; g) current_state="$(cat "${STATE_FILE}")" @@ -58,6 +72,22 @@ while getopts ":m:g" option; do "${gsettings_alt[${current_state}]}" "${next_state}" "${percentage:=0}" "${class:="none"}" exit 0 ;; + t) + if ! [ "$(command -v sunwait)" ]; then + >&2 printf "Error: sunwait command not found. Please install sunwait or do not use the -t option.\n" + exit 1 + fi + + current_time="$(sunwait poll "${LATITUDE}" "${LONGITUDE}")" + + if [ -z "${current_time}" ] || [ -z "${theme_time[${current_time}]}" ]; then + >&2 printf "Error: Invalid time returned by sunwait: '%s'.\nInspect the script for more details.\n" "${current_time}" + exit 1 + fi + + theme="${theme_time[${current_time}]}" + printf "Setting theme based on time of day: %s -> %s\n" "${current_time}" "${theme}" + ;; *) >&2 printf "Error: Invalid option: '%s'.\n" "${option}" exit 1 @@ -67,16 +97,26 @@ done shift $(( OPTIND - 1 )) # Check if the state file exists -if ! [ -d "$(dirname ${STATE_FILE})" ]; then - mkdir -p "$(dirname ${STATE_FILE})" +if ! [ -d "$(dirname "${STATE_FILE}")" ]; then + mkdir -p "$(dirname "${STATE_FILE}")" fi -# Choose next theme -previous_theme="$(cat ${STATE_FILE})" -if ! [[ -z "${previous_theme}" || "${theme}" ]]; then - theme="${theme_next[${previous_theme}]}" +# Determine the theme to set +if [ -z "${theme}" ]; then + # Read from state file + if [ -f "${STATE_FILE}" ]; then + previous_theme="$(cat "${STATE_FILE}")" + fi + + if [ -n "${previous_theme}" ]; then + theme="${theme_next[${previous_theme}]}" + else + theme="${THEME_DEFAULT}" + fi fi -echo "${theme:=${THEME_DEFAULT}}" > "${STATE_FILE}" + +# Store the new theme in the state file +echo "${theme}" > "${STATE_FILE}" ###################### ### Set all themes ### diff --git a/stow/hyprland/.config/hypr/hyprland.conf b/stow/hyprland/.config/hypr/hyprland.conf index 6dc3b6b..f46886e 100644 --- a/stow/hyprland/.config/hypr/hyprland.conf +++ b/stow/hyprland/.config/hypr/hyprland.conf @@ -245,6 +245,8 @@ exec-once = waybar # TODO Replace with something else? exec-once = wlsunset -t 2500 -l 50.51 -L 4.21 +# Set light/dark theme based on time +exec-once = bash ${SCRIPT_DIR}/toggle-light-dark.sh -t exec-once = nextcloud --background