Commit 3b52355f authored by Roman Alifanov's avatar Roman Alifanov

service: big update

1. big fixes in the reaction mode for changing the config file. 2. current mode (light/dark) is now visible in thhelp,force-file-reactione config. 3. added flags: h,help,force-file-reaction 4. other code improvements
parent 01da45b5
...@@ -7,13 +7,38 @@ CONFIG_FILE="/etc/ximper-unified-theme-switcher/themes" ...@@ -7,13 +7,38 @@ CONFIG_FILE="/etc/ximper-unified-theme-switcher/themes"
HOME_CONFIG_DIR="$HOME/.config/ximper-unified-theme-switcher" HOME_CONFIG_DIR="$HOME/.config/ximper-unified-theme-switcher"
HOME_CONFIG_FILE="$HOME_CONFIG_DIR/themes" HOME_CONFIG_FILE="$HOME_CONFIG_DIR/themes"
# Функция для создания конфига в домашнем каталоге (TODO: перенести в GUI) FORCE_FILE_REACTION=false
# Функция для создания конфига в домашнем каталоге
create_home_config() { create_home_config() {
mkdir -p "$HOME_CONFIG_DIR" mkdir -p "$HOME_CONFIG_DIR"
touch "$HOME_CONFIG_FILE" touch "$HOME_CONFIG_FILE"
create_default_config_values "$HOME_CONFIG_FILE"
echo "$HOME_CONFIG_FILE" echo "$HOME_CONFIG_FILE"
} }
create_default_config_values() {
local CFG_PATH
CFG_PATH="$1"
if [ -w "$CFG_PATH" ]; then
if [ -z "$(shell_config_get "$CFG_PATH" KV_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" CURRENT_THEME "dark"
fi
if [ -z "$(shell_config_get "$CFG_PATH" KV_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" KV_LIGHT_THEME "KvGnome"
shell_config_set "$CFG_PATH" KV_DARK_THEME "KvGnomeDark"
fi
if [ -z "$(shell_config_get "$CFG_PATH" GTK3_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" GTK3_LIGHT_THEME "adw-gtk3"
shell_config_set "$CFG_PATH" GTK3_DARK_THEME "adw-gtk3-dark"
fi
fi
}
cfg_check() { cfg_check() {
# Проверка наличия конфига в /etc # Проверка наличия конфига в /etc
if [ -f "$CONFIG_FILE" ]; then if [ -f "$CONFIG_FILE" ]; then
...@@ -71,75 +96,97 @@ update_gtk3_theme() { ...@@ -71,75 +96,97 @@ update_gtk3_theme() {
# Function to check and update the theme # Function to check and update the theme
check_and_update_themes() { check_and_update_themes() {
local CURRENT_THEME local NEW_THEME
CURRENT_THEME=$(gsettings get org.gnome.desktop.interface color-scheme | awk -F"'" '{print $2}') NEW_THEME="$1"
local CFG_PATH local CFG_PATH
CFG_PATH=$(cfg_check) CFG_PATH=$(cfg_check)
create_default_config_values
if [ -z "$(shell_config_get "$CFG_PATH" KV_DARK_THEME)" ]; then shell_config_set "$CFG_PATH" CURRENT_THEME "$NEW_THEME"
shell_config_set "$CFG_PATH" KV_LIGHT_THEME "KvGnome"
shell_config_set "$CFG_PATH" KV_DARK_THEME "KvGnomeDark"
fi
if [ -z "$(shell_config_get "$CFG_PATH" GTK3_DARK_THEME)" ]; then case "$NEW_THEME" in
shell_config_set "$CFG_PATH" GTK3_LIGHT_THEME "adw-gtk3" "default"|"light"|"prefer-light")
shell_config_set "$CFG_PATH" GTK3_DARK_THEME "adw-gtk3-dark" update_kv_theme "light" "$CFG_PATH"
fi update_gtk3_theme "light" "$CFG_PATH"
;;
"dark"|"prefer-dark")
update_kv_theme "dark" "$CFG_PATH"
update_gtk3_theme "dark" "$CFG_PATH"
;;
esac
}
if [ "$CURRENT_THEME" == "default" ]; then OPTS=$(getopt -o h --long help,force-file-reaction -- "$@") || {
update_kv_theme "light" "$CFG_PATH" echo "Ошибка обработки опций."
update_gtk3_theme "light" "$CFG_PATH" exit 1
else
update_kv_theme "dark" "$CFG_PATH"
update_gtk3_theme "dark" "$CFG_PATH"
fi
} }
# Initial check and update eval set -- "$OPTS"
check_and_update_themes
while true; do
case "$1" in
-h|--help)
echo "Usage: $0 [options]"
echo "Options:"
echo " --help Show this help message"
echo " --force-file-reaction Force reaction to config file changes"
exit 0
;;
--force-file-reaction)
FORCE_FILE_REACTION=true
shift
;;
--)
shift
break
;;
*)
echo "Неизвестный аргумент: $1"
exit 1
;;
esac
done
main() { main() {
if check_gsettings_schema; then if [ "$FORCE_FILE_REACTION" = true ] || ! check_gsettings_schema; then
echo "reaction mode: after changes in config file"
create_home_config
local CONFIG_FILE
CONFIG_FILE=$(cfg_check)
while true; do
inotifywait -e attrib "$CONFIG_FILE" | \
while read -r directory events filename; do
NEW_THEME=$(shell_config_get "$CONFIG_FILE" CURRENT_THEME)
if [ -n "$NEW_THEME" ]; then
echo "Config file updated: $NEW_THEME"
check_and_update_themes "$NEW_THEME"
fi
done
done
else
echo "reaction mode: after dbus signal" echo "reaction mode: after dbus signal"
case "$XDG_SESSION_DESKTOP" in
gnome*)
echo "GNOME DETECTED"
local GNOME_DE=True
;;
Hyprland*)
echo "HYPRLAND DETECTED"
;;
*)
;;
esac
dbus-monitor "interface='org.freedesktop.portal.Settings',member=SettingChanged" | \ dbus-monitor "interface='org.freedesktop.portal.Settings',member=SettingChanged" | \
while IFS= read -r LINE; do while IFS= read -r LINE; do
local THEME local THEME
local NEW_THEME
local CURRENT_TIME local CURRENT_TIME
local LAST_UPDATE local LAST_UPDATE
THEME=$(echo "$LINE" | grep -E -o 'string "(prefer-dark|default)"') THEME=$(echo "$LINE" | grep -E -o 'string "(prefer-dark|default)"')
NEW_THEME=$(gsettings get org.gnome.desktop.interface color-scheme | awk -F"'" '{print $2}')
if [ -n "$THEME" ]; then CURRENT_TIME=$(date +%s%3N)
# Текущее время в миллисекундах
CURRENT_TIME=$(date +%s%3N) if [ -n "$THEME" ] && [ $((CURRENT_TIME - LAST_UPDATE)) -ge 150 ]; then
LAST_UPDATE=$CURRENT_TIME
if [ -z "$LAST_UPDATE" ] || [ $((CURRENT_TIME - LAST_UPDATE)) -ge 150 ]; then echo "D-Bus signal detected: $THEME"
LAST_UPDATE=$CURRENT_TIME check_and_update_themes "$NEW_THEME"
echo "$THEME"
check_and_update_themes
fi
fi fi
done done
else
echo "reaction mode: after changes in config file"
local CONFIG_FILE=$(cfg_check)
inotifywait -m -e modify "$CONFIG_FILE" | \
while read -r directory events filename; do
check_and_update_themes
done
fi fi
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment