From 0600a8f119af996bc04c68f4d4105cbd2b53dbd2 Mon Sep 17 00:00:00 2001
From: rokosun <rokosun@noreply.git.trom.tf>
Date: Wed, 25 Oct 2023 12:52:28 +0200
Subject: [PATCH] Don't set the XFWM theme twice

Now the script won't set the XFWM theme twice on hiDPI systems every time the user changes their theme.
Also replaced read only variables inside loops with mutable ones.
---
 bin/periodic/fix-theming | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/bin/periodic/fix-theming b/bin/periodic/fix-theming
index ee8f389..ced9230 100755
--- a/bin/periodic/fix-theming
+++ b/bin/periodic/fix-theming
@@ -71,22 +71,27 @@ set_xfwm_theme() {
 
 # Use the highDPI variant of the XFWM theme
 use_hdpi_theme_variant() {
-	local -r current_xfwm_theme=$(xfconf-query -c xfwm4 -p /general/theme)
+	# shellcheck disable=SC2015
+	[ -n "$1" ] && local -r xfwm_theme="$1" ||
+		local -r xfwm_theme=$(xfconf-query -c xfwm4 -p /general/theme)
 
 	for variant in xhdpi hdpi; do
 		# If this variant of the theme is already enabled then break out of the loop
-		[ "${current_xfwm_theme##*-}" = "$variant" ] && break
+		[ "${xfwm_theme##*-}" = "$variant" ] && break
 
 		# Store the name for this variant of the theme in highDPI_theme variable
-		local -r highDPI_theme=$current_xfwm_theme-$variant
+		local highDPI_theme=$xfwm_theme-$variant
 
 		if theme_exists "$highDPI_theme"; then
 			set_xfwm_theme "$highDPI_theme"
-			[ "$1" != 'disable_notification' ] &&
+			[ -z "$1" ] &&
 				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
+	if ! theme_exists "$highDPI_theme"; then
+		[ -n "$1" ] && theme_exists "$1" && set_xfwm_theme "$1"
+	fi
 }
 
 # Use the normal variant of the XFWM theme
@@ -98,7 +103,7 @@ use_normal_theme_variant() {
 		[ "${current_xfwm_theme##*-}" != "$variant" ] && continue
 
 		# Store the name for the normal variant of this theme in normal_theme variable
-		local -r normal_theme=${current_xfwm_theme%-"$variant"}
+		local normal_theme=${current_xfwm_theme%-"$variant"}
 
 		if theme_exists "$normal_theme"; then
 			set_xfwm_theme "$normal_theme"
@@ -156,10 +161,12 @@ sync_theme() {
 
 	# Apply the current theme for XFWM unless it's initial_sync on system reboot
 	if [ "$1" != 'initial_sync' ]; then
-		set_xfwm_theme "$current_theme"
-
 		# Use the highDPI variant of the XFWM theme if the current_window_scaling factor is 2
-		[ "$current_window_scaling" -eq '2' ] && use_hdpi_theme_variant 'disable_notification'
+		if [ "$current_window_scaling" -eq '2' ]; then
+			use_hdpi_theme_variant "$current_theme"
+		else
+			set_xfwm_theme "$current_theme"
+		fi
 	fi
 
 	# Cleanup flatpak_themes_dir and remove ~/.config/gtk-4.0
-- 
2.39.5