/
theme-mode
executable file
·98 lines (84 loc) · 2.28 KB
/
theme-mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
#
# Set or toggle light/dark themes on desktops with xsettingsd.
#
# Copyright (C) 2017-2023 Rafael Cavalcanti <https://rafaelc.org/dev>
# Licensed under GPLv3
readonly config_path="$HOME/.config/theme-mode/config"
readonly xsettingsd_config="$HOME/.config/xsettingsd/xsettingsd.conf"
main() {
if [[ $# -ne 1 ]]; then
usage
fi
check_desktop
get_config
set_target "$1"
apply
}
usage() {
printf "Usage: %s <light|dark|toggle>\n" "$(basename "$0")"
exit 1
}
log() {
printf "%s\n" "$*" 1>&2
}
get_config() {
if [[ ! -e "$config_path" ]]; then
printf "Config not found at %s.\n" "$config_path"
exit 1
fi
source "$config_path"
}
check_desktop() {
if ! pidof xsettingsd > /dev/null; then
log "This desktop is not supported."
exit 1
fi
}
set_target() {
case "${1:-}" in
light)
readonly target_theme="$theme_light"
readonly target_icon="$icon_light"
readonly target_scheme="prefer-light"
readonly target_wallpaper="${wallpaper_light:-}"
;;
dark)
readonly target_theme="$theme_dark"
readonly target_icon="$icon_dark"
readonly target_scheme="prefer-dark"
readonly target_wallpaper="${wallpaper_dark:-}"
;;
toggle)
set_target_toggle
;;
*)
usage
esac
}
set_target_toggle() {
local -r theme_current="$(awk -F' ' '/^Net\/ThemeName / {gsub("\"","", $2); print $2}' "$xsettingsd_config")"
case "$theme_current" in
"$theme_dark") set_target "light" ;;
"$theme_light") set_target "dark" ;;
*) set_target "dark" ;;
esac
}
apply() {
# Set on xsettingsd
sed -i "s#^Net/ThemeName .*\$#Net/ThemeName \"$target_theme\"#" "$xsettingsd_config"
sed -i "s#^Net/IconThemeName .*\$#Net/IconThemeName \"$target_icon\"#" "$xsettingsd_config"
killall -HUP xsettingsd
# Set theme on gnome
# This is relevant on dwm because:
# - prefer-dark sets some gnome applications to dark.
# - gtk-theme (used for theming "legacy" apps) sets Qt
# applications dark, since we are using qgnomeplatform.
gsettings set org.gnome.desktop.interface gtk-theme "'$target_theme'"
gsettings set org.gnome.desktop.interface color-scheme "'$target_scheme'"
# Set wallpaper
if [[ -n "${target_wallpaper:-}" ]]; then
set-wallpaper "$target_wallpaper"
fi
}
main "$@"