2022-09-18 20:05:57 +00:00
#!/bin/bash
2023-08-03 14:40:26 +00:00
2023-08-06 00:14:02 +00:00
# Exit out if the same script is already running in the background
pidof -sq -o %PPID -x "$(basename "$0")" && exit
2023-08-10 15:03:39 +00:00
# Set the directory/file paths
2023-10-12 14:55:05 +00:00
declare -r flatpak_themes_dir="$HOME/.themes"
2023-10-14 20:06:49 +00:00
declare -r config_dir="${XDG_CONFIG_HOME:-$HOME/.config}"
2023-10-12 14:55:05 +00:00
declare -r profile="$HOME/.profile"
2023-08-06 00:14:02 +00:00
2023-10-14 20:06:49 +00:00
# Create directories if they don't exist
mkdir -p "$flatpak_themes_dir" "$config_dir" ||
{ echo "failed to make directories $flatpak_themes_dir & $config_dir"; exit 1; }
2023-08-06 00:14:02 +00:00
2023-08-08 15:06:38 +00:00
# Enable option for syncing the current theme with XFWM theme if not already enabled
2023-10-12 14:55:05 +00:00
[ "$(xfconf-query -c xsettings -p /Xfce/SyncThemes)" != 'true' ] &&
xfconf-query -c xsettings -p /Xfce/SyncThemes -n -t bool -s true
2023-08-08 15:06:38 +00:00
2023-08-08 13:58:10 +00:00
xdg_directory_lookup() {
# Go through each item in $XDG_DATA_DIRS and find the subdirectory $1
while read -r dir; do
2023-10-12 14:55:05 +00:00
local subdir="${dir%/}/$1"
2023-08-08 13:58:10 +00:00
if [ -d "$subdir" ]; then
echo "$subdir"
break
fi
done < <(printf '%s:%s\n' "$HOME/.local/share" "$XDG_DATA_DIRS" | tr ':' '\n')
}
2023-10-12 14:55:05 +00:00
theme_exists() {
local -r theme_dir=$(xdg_directory_lookup "themes/$1")
[ -d "$theme_dir" ]
}
2023-08-10 15:03:39 +00:00
# Function for setting variables in ~/.profile
profile_set_variable() {
# Return if the variable is already set to the correct value
grep -xq "\s*export\s\+$1=$2\s*" "$profile" && return
2023-10-12 14:55:05 +00:00
2023-08-10 15:03:39 +00:00
# If the variable is set to a different value then remove that line
grep -q "^\s*export\s\+$1=" "$profile" && sed -i "/^\s*export\s\+$1=/d" "$profile"
# Set the variable to the correct value under a comment mentioning this script
if grep -xq '\s*#\s*Values set by the fix-theming script\s*' "$profile"; then
sed -i "/^\s*#\s*Values set by the fix-theming script\s*$/a export $1=$2" "$profile"
elif grep -q '\s' "$profile"; then
2023-10-12 14:55:05 +00:00
# shellcheck disable=SC2015
tail -n1 "$profile" | grep -qx '\s*' &&
local -r begin='' || local -r begin='\n'
2023-08-10 15:03:39 +00:00
printf '%b# Values set by the fix-theming script\nexport %s=%s\n' "$begin" "$1" "$2" >> "$profile"
else
printf '# Values set by the fix-theming script\nexport %s=%s\n' "$1" "$2" > "$profile"
fi
2023-10-12 14:55:05 +00:00
2023-08-10 15:03:39 +00:00
logout_required='true'
}
2023-10-12 14:55:05 +00:00
set_QT_scaling() {
for var in QT_SCALE_FACTOR QT_AUTO_SCREEN_SCALE_FACTOR QT_ENABLE_HIGHDPI_SCALING; do
profile_set_variable "$var" "$1"
done
}
set_cursor_size() {
xfconf-query -c xsettings -p /Gtk/CursorThemeSize -s "$1"
}
set_xfwm_theme() {
xfconf-query -c xfwm4 -p /general/theme -n -t string -s "$1"
}
# Use the highDPI variant of the XFWM theme
use_hdpi_theme_variant() {
2023-10-25 10:52:28 +00:00
# shellcheck disable=SC2015
[ -n "$1" ] && local -r xfwm_theme="$1" ||
local -r xfwm_theme=$(xfconf-query -c xfwm4 -p /general/theme)
2023-10-12 14:55:05 +00:00
for variant in xhdpi hdpi; do
# If this variant of the theme is already enabled then break out of the loop
2023-10-25 10:52:28 +00:00
[ "${xfwm_theme##*-}" = "$variant" ] && break
2023-10-12 14:55:05 +00:00
# Store the name for this variant of the theme in highDPI_theme variable
2023-10-25 10:52:28 +00:00
local highDPI_theme=$xfwm_theme-$variant
2023-10-12 14:55:05 +00:00
if theme_exists "$highDPI_theme"; then
set_xfwm_theme "$highDPI_theme"
2023-10-25 10:52:28 +00:00
[ -z "$1" ] &&
2023-10-12 14:55:05 +00:00
notify-send "XFWM theme changed to $highDPI_theme" "Changed to the highDPI variant of your chosen theme because you've enabled highDPI on your system"
break
fi
done
2023-10-25 10:52:28 +00:00
if ! theme_exists "$highDPI_theme"; then
[ -n "$1" ] && theme_exists "$1" && set_xfwm_theme "$1"
fi
2023-10-12 14:55:05 +00:00
}
# Use the normal variant of the XFWM theme
use_normal_theme_variant() {
local -r current_xfwm_theme=$(xfconf-query -c xfwm4 -p /general/theme)
for variant in xhdpi hdpi; do
# Continue looping if this (highDPI) variant of the theme is not enabled
[ "${current_xfwm_theme##*-}" != "$variant" ] && continue
# Store the name for the normal variant of this theme in normal_theme variable
2023-10-25 10:52:28 +00:00
local normal_theme=${current_xfwm_theme%-"$variant"}
2023-10-12 14:55:05 +00:00
if theme_exists "$normal_theme"; then
set_xfwm_theme "$normal_theme"
notify-send "XFWM theme changed to $normal_theme" "Changed to the normal variant of your chosen theme because you've disabled highDPI on your system"
break
fi
done
}
# Change XFWM theme, QT apps scaling, and mouse cursor size according to
# the user's current window scaling (highDPI setting)
2023-08-08 13:58:10 +00:00
hdpi_theme_fix() {
2023-10-12 14:55:05 +00:00
local -r current_window_scaling=$(xfconf-query -c xsettings -p /Gdk/WindowScalingFactor)
local -r current_cursor_size=$(xfconf-query -c xsettings -p /Gtk/CursorThemeSize)
2023-08-08 13:58:10 +00:00
2023-08-10 15:03:39 +00:00
unset logout_required
2023-10-12 14:55:05 +00:00
if [ "$current_window_scaling" -eq '2' ]; then
# Increase scaling for QT apps
set_QT_scaling 2
# Use the highDPI variant of the XFWM theme
use_hdpi_theme_variant
if [ "$current_cursor_size" -eq 25 ]; then
set_cursor_size '45'
2023-08-08 13:58:10 +00:00
notify-send 'Mouse cursor size increased to 45' "Changed to a larger mouse cursor because you've enabled highDPI on your system"
fi
else
2023-10-12 14:55:05 +00:00
# Decrease scaling for QT apps
set_QT_scaling 1
# Use the normal variant of the XFWM theme
use_normal_theme_variant
if [ "$current_cursor_size" -eq 45 ]; then
set_cursor_size '25'
2023-08-08 13:58:10 +00:00
notify-send 'Mouse cursor size decreased to 25' "Changed to a smaller mouse cursor because you've disabled highDPI on your system"
fi
fi
2023-10-12 14:55:05 +00:00
2023-08-10 15:03:39 +00:00
# Show popup menu about logout if it's required
[ "$logout_required" = 'true' ] &&
2023-10-12 14:55:05 +00:00
yad --image "dialog-question" --title "Alert" --buttons-layout=center --text-align=center --button=yad-yes:0 --button=yad-no:1 --text 'Logout is required in order to apply your DPI preferences to QT apps.\nWould you like to logout now?' &&
2023-08-10 15:03:39 +00:00
xfce4-session-logout --logout
2023-08-08 13:58:10 +00:00
}
2022-09-22 14:51:45 +00:00
sync_theme() {
2023-08-06 13:08:58 +00:00
# Get the current system theme
2023-10-12 14:55:05 +00:00
local -r current_theme=$(xfconf-query -c xsettings -p /Net/ThemeName)
# Get the current window scaling factor (highDPI setting)
local -r current_window_scaling=$(xfconf-query -c xsettings -p /Gdk/WindowScalingFactor)
2023-08-06 00:14:02 +00:00
2023-08-06 13:08:58 +00:00
# Apply the current theme with gsettings
gsettings set org.gnome.desktop.interface gtk-theme "$current_theme"
2023-08-08 15:06:38 +00:00
2023-08-06 13:08:58 +00:00
# Apply the current theme for GTK and Libadwaita apps on flatpak
flatpak override --user --env=GTK_THEME="$current_theme"
2023-08-06 00:14:02 +00:00
2023-10-12 14:55:05 +00:00
# Apply the current theme for XFWM unless it's initial_sync on system reboot
if [ "$1" != 'initial_sync' ]; then
# Use the highDPI variant of the XFWM theme if the current_window_scaling factor is 2
2023-10-25 10:52:28 +00:00
if [ "$current_window_scaling" -eq '2' ]; then
use_hdpi_theme_variant "$current_theme"
else
set_xfwm_theme "$current_theme"
fi
2023-10-12 14:55:05 +00:00
fi
2023-08-08 13:58:10 +00:00
2023-10-15 08:48:52 +00:00
# Cleanup flatpak_themes_dir and remove ~/.config/gtk-4.0
rm -rf "${flatpak_themes_dir:?}"/{*,.*} "$config_dir/gtk-4.0"
2023-08-06 00:14:02 +00:00
2023-08-08 13:58:10 +00:00
# Find the directory where the current theme is stored
2023-10-12 14:55:05 +00:00
local -r current_theme_dir=$(xdg_directory_lookup "themes/$current_theme")
2023-10-14 20:06:49 +00:00
if [ -d "$current_theme_dir" ]; then
# Copy/sync current_theme_dir to flatpak_themes_dir
2023-08-08 13:58:10 +00:00
rsync -av --delete --progress "$current_theme_dir" "$flatpak_themes_dir"
2023-10-15 08:48:52 +00:00
# Create symlink for gtk-4.0 directory
ln -s "$current_theme_dir/gtk-4.0" "$config_dir"
2023-10-14 20:06:49 +00:00
fi
2023-08-06 00:14:02 +00:00
}
2022-09-22 14:51:45 +00:00
sync_font() {
2023-08-06 13:08:58 +00:00
# Get the current system font
2023-10-12 14:55:05 +00:00
local -r current_font=$(xfconf-query -c xsettings -p /Gtk/FontName)
# Apply the same font for the title of XFCE windows
xfconf-query -c xfwm4 -p /general/title_font -n -t string -s "$current_font"
2022-09-22 14:51:45 +00:00
}
2022-09-15 23:14:20 +00:00
2023-08-06 13:08:58 +00:00
# Initial synchronization
2023-10-12 14:55:05 +00:00
sync_theme 'initial_sync'
2022-09-22 14:51:45 +00:00
sync_font
2023-10-12 14:55:05 +00:00
# Monitor when the user changes their system theme/icons/font in XFCE and sync them as needed
2022-09-22 14:51:45 +00:00
while read -r line; do
2023-08-06 13:08:58 +00:00
case "$line" in
'set: /Net/ThemeName') sync_theme ;;
2023-08-08 13:58:10 +00:00
'set: /Gdk/WindowScalingFactor') hdpi_theme_fix ;;
2023-08-06 13:08:58 +00:00
'set: /Gtk/FontName') sync_font ;;
esac
2022-09-21 17:56:17 +00:00
done < <(xfconf-query -c xsettings -m)