Skip to content

Instantly share code, notes, and snippets.

@lautarodragan
Last active May 15, 2023 14:08
Show Gist options
  • Save lautarodragan/4b9b476ac3219ada41f8f8aae02497b2 to your computer and use it in GitHub Desktop.
Save lautarodragan/4b9b476ac3219ada41f8f8aae02497b2 to your computer and use it in GitHub Desktop.
Pop!_OS Theme Toggle Keyboard Shortcut

Pop!_OS Theme Toggle Keyboard Shortcut

This script toggles both the Pop!_OS theme between Pop-dark and Pop and the gnome color-theme preference between prefer-light and prefer-dark at once. We need to toggle both because some apps use one and some the other. In particular, the CSS feature that allows detection of user preferences uses org.gnome.desktop.interface color-scheme, while native apps mostly use the gtk-theme.

To use it, you could just run the script manually from a terminal (. theme-toggle.sh or chmod a+x theme-toggle.sh and then ./theme-toggle.sh), but that's no fun.

Instead, you can add a desktop-wide keyboard shortcut that triggers this script.

To do so, open Settings (gnome-control-center) and go to Keyboard > Keyboard Shortcuts > View and Customize Keyboard Shortcuts.

Choose Custom Shortcuts and then add a new one with the command /bin/sh /path/to/theme-toggle.sh and whatever name and shortcut you wish.

function theme-set() {
echo "Setting theme to $1"
gsettings set org.gnome.desktop.interface gtk-theme $([[ $1 = 'light' ]] && echo 'Pop' || echo 'Pop-dark')
gsettings set org.gnome.desktop.interface color-scheme $([[ $1 = 'light' ]] && echo 'prefer-light' || echo 'prefer-dark')
}
function theme-toggle() {
local currentTheme=$(gsettings get org.gnome.desktop.interface color-scheme | tr -d "'")
echo "Current theme is $currentTheme"
if [[ "$currentTheme" = 'prefer-dark' ]]
then
theme-set 'light'
else
theme-set "dark"
fi
}
theme-toggle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment