Use the -m option in xfconf-query to monitor for changes #5

Merged
tio merged 1 commits from rokosun-patch-1 into main 2022-09-18 22:40:15 +00:00
Showing only changes of commit 73471917ef - Show all commits

View File

@ -1,44 +1,34 @@
#!/bin/sh #!/bin/bash
sync_theme() { while read -r line; do
# Get the current system theme
theme=$(xfconf-query -c xsettings -p /Net/ThemeName)
# Find the best match for the xfwm4 theme that corresponds with the current system theme
xfwm4_theme=$(find /usr/share/themes/ /usr/local/share/themes/ "$HOME"/.themes/ "$HOME"/.local/share/themes/ -mindepth 2 -maxdepth 2 -type d -name xfwm4 -printf '%h\n' 2>/dev/null | grep -o "/${theme}[^/]*$" | sort | head -n1)
# If a match is not found then use the Default theme
[ -z "$xfwm4_theme" ] && xfwm4_theme='Default'
# Apply the xfwm4 theme
xfconf-query -c xfwm4 -p /general/theme -n -t string -s "${xfwm4_theme#/}"
# Apply the current theme with gsettings
gsettings set org.gnome.desktop.interface gtk-theme "$theme"
}
sync_icon_theme() { # Check if the system theme has been changed
# Get the current icon theme if [ "$line" = 'set: /Net/ThemeName' ]; then
rokosun marked this conversation as resolved
Review

A minor thing: to replace if;then;fi with switch;case statement:

case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac

More details and examples are here.

This just adds some structure and also an easier to update if some more settings changes will be necessary to subsctibe to. But I leave it up to you to decide, you can skip it if you want 😉

A minor thing: to replace `if;then;fi` with `switch;case` statement: ``` case EXPRESSION in PATTERN_1) STATEMENTS ;; PATTERN_2) STATEMENTS ;; PATTERN_N) STATEMENTS ;; *) STATEMENTS ;; esac ``` More details and examples are [here](https://linuxize.com/post/bash-case-statement/). This just adds some structure and also an easier to update if some more settings changes will be necessary to subsctibe to. But I leave it up to you to decide, you can skip it if you want 😉
Review

Oh of course it makes sense to use a case statement here, why didn't I think of it before? lol 😄

Oh of course it makes sense to use a case statement here, why didn't I think of it before? lol 😄
Review

Oh, man.. Happens to me too quite often 😅

Oh, man.. Happens to me too quite often 😅
icon_theme=$(xfconf-query -c xsettings -p /Net/IconThemeName) # Get the current system theme
# Apply the same theme in qt5ct and qt6ct configuration theme=$(xfconf-query -c xsettings -p /Net/ThemeName)
sed -i "s/^icon_theme=.*$/icon_theme=$icon_theme/" "$HOME"/.config/qt5ct/qt5ct.conf # Find the best match for the xfwm4 theme that corresponds with the current system theme
sed -i "s/^icon_theme=.*$/icon_theme=$icon_theme/" "$HOME"/.config/qt6ct/qt6ct.conf xfwm4_theme=$(find /usr/share/themes/ /usr/local/share/themes/ "$HOME"/.themes/ "$HOME"/.local/share/themes/ -mindepth 2 -maxdepth 2 -type d -name xfwm4 -printf '%h\n' 2>/dev/null | grep -o "/${theme}[^/]*$" | sort | head -n1)
} # If a match is not found then use the Default theme
[ -z "$xfwm4_theme" ] && xfwm4_theme='Default'
# Apply the xfwm4 theme
xfconf-query -c xfwm4 -p /general/theme -n -t string -s "${xfwm4_theme#/}"
# Apply the current theme with gsettings
gsettings set org.gnome.desktop.interface gtk-theme "$theme"
while :; do # Check if the icon theme has been changed
# Get the current system theme elif [ "$line" = 'set: /Net/IconThemeName' ]; then
theme_new=$(xfconf-query -c xsettings -p /Net/ThemeName) # Get the current icon theme
# Get the current icon theme icon_theme=$(xfconf-query -c xsettings -p /Net/IconThemeName)
icon_theme_new=$(xfconf-query -c xsettings -p /Net/IconThemeName) # Apply the same theme in qt5ct and qt6ct configuration
# Get the current font name sed -i "s/^icon_theme=.*$/icon_theme=$icon_theme/" "$HOME"/.config/qt5ct/qt5ct.conf
font_new=$(xfconf-query -c xsettings -p /Gtk/FontName) sed -i "s/^icon_theme=.*$/icon_theme=$icon_theme/" "$HOME"/.config/qt6ct/qt6ct.conf
# Run sync_theme if the new system theme doesn't match the previous one # Check if the system font has been changed
[ "$theme_new" != "$theme_prev" ] && sync_theme elif [ "$line" = 'set: /Gtk/FontName' ]; then
# Run sync_icon_theme if the new icon theme doesn't match the previous one # Get the current system font
[ "$icon_theme_new" != "$icon_theme_prev" ] && sync_icon_theme font=$(xfconf-query -c xsettings -p /Gtk/FontName)
# If the new font doesn't match the previous one, apply the new font for the title of xfce windows # Apply the same font for the title of xfce windows
[ "$font_new" != "$font_prev" ] && xfconf-query -c xfwm4 -p /general/title_font -n -t string -s "$font_new" xfconf-query -c xfwm4 -p /general/title_font -n -t string -s "$font"
# The new values become the previous values fi
theme_prev=$theme_new
icon_theme_prev=$icon_theme_new
font_prev=$font_new
sleep 3 done < <(xfconf-query -c xsettings -m)
done