basecamp / omakub

Opinionated Ubuntu Setup
https://omakub.org
4.28k stars 345 forks source link

Light/dark mode switching #200

Closed florentdestremau closed 1 month ago

florentdestremau commented 1 month ago

I made a local test to try and have 2 themes, one dark and one light: ezgif com-optimize

Here's my quick script (based on omakub 1.0 for theme switching). Would you be intereseted in incorporating it ? Don't know how this would work, I guess a LIGHT_THEME and DARK_THEME could be saved somewhere ? Also, the monitoring of gsettings could probably be cleaner but it Just Works for now :smile:

#!/bin/bash

# Function to execute when dark mode is enabled
dark_mode_on() {
    echo "Dark mode enabled, setting \"Tokyo Night\" theme"
    export DESIRED_THEME="tokyo-night"
    set_theme
}

# Function to execute when light mode is enabled
light_mode_on() {
    echo "Light mode enabled, setting \"Red Pine\" theme"
    export DESIRED_THEME="rose-pine"
    set_theme
}

set_theme() {
    # Create a temporary gum function that returns the desired theme
    function gum() {
        if [[ $1 == "choose" ]]; then
            echo "$DESIRED_THEME"
        else
            command gum "$@"
        fi
    }

    # Run the script with the overridden gum function
    ( gum() { if [[ $1 == "choose" ]]; then echo "$DESIRED_THEME"; else command gum "$@"; fi; }; source $OMAKUB_PATH/bin/omakub-theme )
}

check_mode() {
    local mode=$(gsettings get org.gnome.desktop.interface color-scheme)
    if [[ $mode == *"prefer-dark"* ]]; then
        dark_mode_on
    else
        light_mode_on
    fi
}

# Initial check
check_mode

# Monitor changes in the settings
while true; do
    current_mode=$(gsettings get org.gnome.desktop.interface color-scheme)
    if [ "$current_mode" != "$previous_mode" ]; then
        previous_mode=$current_mode
        check_mode
    fi
    sleep 1
done
arafays commented 1 month ago

@florentdestremau for noobs like me, while they add this in the next one can you guide the existing users, so they can test this. (What I mean to say is I am a dummy and dont know how to use the script I need step by step instructions)

florentdestremau commented 1 month ago

copy the code into a file like watch-dark-mode.sh, then run

chmod +x watch-dark-mode.sh
bash watch-darkmode.sh

You can now switch dark/light mode and see what happens