Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以通过加入定时重新安装来设置libadwaita主题无法进行的Dark与Light的变化呢?大佬 #1214

Open
Liu020321 opened this issue Feb 6, 2025 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@Liu020321
Copy link

我使用的是Ubuntu24.04,使用libadwaita就无法亮暗切换,只能重新安装,不使用libadwaita就没法改变那个操作图标。
我拿ChatGPT生成了一段sh代码,通过运行sh文件来进行重新安装libadwaita主题以切换主题颜色。
可以通过设置ubuntu快捷命令的方式来快速调用sh文件:
gnome-terminal -- bash -c "/home/lht/timed_task.sh; exec bash"
或者使用桌面图标的方式:

[Desktop Entry]
Version=1.0
Name=夜间模式
Comment=这是夜间模式切换程序
Exec=gnome-terminal -- bash -c "/home/lht/timed_task.sh; exec bash"
Icon=/home/lht/software/timed/timed.png
Terminal=true
Type=Application
Categories=Utility;

我尝试使用corntab来定时设置但是失败了,每次到时间只完成了卸载操作,安装没有继续进行,不知道是为什么。只有使用系统terminal来运行才能完美运行。
sh代码:

#!/bin/bash

# 获取当前时间(小时),确保是两位数字,不带前导零
current_hour=$(date +'%H')

# 记录日志文件路径
log_file="/home/lht/Data/log/cron_output.log"

# 记录脚本开始时间
echo "Script started at $(date)" >> "$log_file"

# 记录早晚任务执行状态的标志文件
morning_flag="/tmp/has_run_morning"
evening_flag="/tmp/has_run_evening"

# 定义亮色模式和暗色模式的设置
light_mode="default"      # 亮色模式
dark_mode="prefer-dark"   # 暗色模式

# 创建标志文件(如果文件不存在)
if [ ! -f "$morning_flag" ]; then
    echo "0" > "$morning_flag"
fi
if [ ! -f "$evening_flag" ]; then
    echo "0" > "$evening_flag"
fi

# 执行命令1和命令2
execute_morning_commands() {
    cd /home/lht/Data/record/美化/WhiteSur-gtk-theme && /home/lht/Data/record/美化/WhiteSur-gtk-theme/install.sh -l -c light -HD -N mojave -a all -s standard --shell -i apple -h default >> "$log_file" 2>&1
    gsettings set org.gnome.desktop.interface color-scheme "$light_mode"
}

# 执行命令3和命令4
execute_evening_commands() {
    cd /home/lht/Data/record/美化/WhiteSur-gtk-theme && /home/lht/Data/record/美化/WhiteSur-gtk-theme/install.sh -l -HD -N mojave -a all -s standard --shell -i apple -h default >> "$log_file" 2>&1
    gsettings set org.gnome.desktop.interface color-scheme "$dark_mode"
}

# 切换用户主题
set_theme() {
    local theme=$1
    # 修改 GTK 主题
    gsettings set org.gnome.desktop.interface gtk-theme "$theme"
    # 修改 GNOME Shell 主题
    gsettings set org.gnome.shell.extensions.user-theme name "$theme"
    echo "Theme set to $theme at $(date)" >> "$log_file"
}

# 执行早晚任务的逻辑
execute_task() {
    if [[ "$(cat "$1")" -eq 0 ]]; then
        # 执行任务并设置标志文件
        echo "$2 task started at $(date)" >> "$log_file"
        if [[ "$2" == "morning" ]]; then
            execute_morning_commands
            set_theme "$3"
        elif [[ "$2" == "evening" ]]; then
            execute_evening_commands
            set_theme "$3"
        fi
        # 设置当前任务的标志文件为1,表示任务已执行
        echo "1" > "$1"
        # 设置另一任务的标志文件为0
        echo "0" > "$4"
    fi
}

# 检查当前标志文件状态,确保两个文件的状态不同
if [[ "$(cat "$morning_flag")" != "$(cat "$evening_flag")" ]]; then
    # 如果是7点到19点之间执行
    if [ "$current_hour" -ge 7 ] && [ "$current_hour" -lt 19 ]; then
        # 早上任务
        if [ ! -f "$morning_flag" ] || [ "$(cat "$morning_flag")" -eq 0 ]; then
            echo "Executing morning task at $(date)" >> "$log_file"
            execute_task "$morning_flag" "morning" "WhiteSur-Light" "$evening_flag"
        fi
    fi

    # 如果是19点到7点之间执行
    if [ "$current_hour" -ge 19 ] || [ "$current_hour" -lt 7 ]; then
        # 晚上任务
        if [ ! -f "$evening_flag" ] || [ "$(cat "$evening_flag")" -eq 0 ]; then
            echo "Executing evening task at $(date)" >> "$log_file"
            execute_task "$evening_flag" "evening" "WhiteSur-Dark" "$morning_flag"
        fi
    fi
else
    echo "Error: Both flags have the same value. Please check the state of $morning_flag and $evening_flag." >> "$log_file"
fi

# 输出脚本结束时间
echo "Script finished at $(date)" >> "$log_file"
@Liu020321 Liu020321 added the question Further information is requested label Feb 6, 2025
@vinceliuice
Copy link
Owner

只需要重新链接 $HOME/.config/gtk-4.0/ 里的 gtk-Dark.cssgtk-Light.css$HOME/.config/gtk-4.0/gtk.css 就行,不用重新安装

@Liu020321
Copy link
Author

只需要重新链接 $HOME/.config/gtk-4.0/ 里的 gtk-Dark.cssgtk-Light.css$HOME/.config/gtk-4.0/gtk.css 就行,不用重新安装

好的,明白了,白天试试,谢谢大佬。新年快乐🍻🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants