summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-14 06:27:42 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-14 06:27:42 +0800
commit63c7df65f7f4a64f482513b8e157cde8e5aacb37 (patch)
treed1b50e520cb55efd2c75aa39d115d8fbe8c083bf /scripts
parentacf40f1e9ec1bd5212d2d6da696470750e6573db (diff)
refactor(wallpaper): switch from hyprpaper to swaybg
Replace hyprpaper with swaybg due to a crash in hyprpaper 0.8.4 on this system. Update the wallpaper script to use wofi and persist the current wallpaper path.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/switch_wallpaper.sh93
1 files changed, 31 insertions, 62 deletions
diff --git a/scripts/switch_wallpaper.sh b/scripts/switch_wallpaper.sh
index ae7acf9..90842d0 100755
--- a/scripts/switch_wallpaper.sh
+++ b/scripts/switch_wallpaper.sh
@@ -1,72 +1,56 @@
#!/bin/bash
-# 壁纸目录
+# 壁纸切换:wofi 选择 -> 写入 current_wallpaper -> 重载 swaybg
+
WALLPAPER_DIR="/home/catilgrass/图片/Wallpapers"
-CONFIG_TEMPLATE="/home/catilgrass/.config/hypr/hyprpaper.conf.template"
-CONFIG_FILE="/home/catilgrass/.config/hypr/hyprpaper.conf"
+CURRENT_FILE="/home/catilgrass/.config/hypr/current_wallpaper"
+THUMB_DIR="/tmp/wallpaper_thumbs"
-# 检查壁纸目录是否存在
+# 检查壁纸目录
if [ ! -d "$WALLPAPER_DIR" ]; then
echo "错误: 壁纸目录不存在: $WALLPAPER_DIR"
exit 1
fi
-# 获取壁纸文件列表
-wallpapers=()
-while IFS= read -r -d '' file; do
- wallpapers+=("$file")
-done < <(find "$WALLPAPER_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.webp" \) -print0)
+# 获取壁纸文件列表(按文件名排序,支持空格/中文路径)
+mapfile -d '' wallpapers < <(find "$WALLPAPER_DIR" -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.webp" \) -print0 | sort -z)
-# 检查是否有壁纸文件
if [ ${#wallpapers[@]} -eq 0 ]; then
echo "错误: 在 $WALLPAPER_DIR 中没有找到壁纸文件"
exit 1
fi
-# 创建rofi选项列表
-options=()
+# 生成缩略图并组装 wofi 项
+# wofi 图片格式:img:缩略图路径:text:显示文本(wofi 会自动缩放到 image_size)
+mkdir -p "$THUMB_DIR"
+items=()
for wallpaper in "${wallpapers[@]}"; do
filename=$(basename "$wallpaper")
- options+=("$filename")
-done
-
-# 使用rofi选择壁纸
-# 生成缩略图并创建rofi选项
-rofi_options=()
-for i in "${!wallpapers[@]}"; do
- wallpaper="${wallpapers[$i]}"
- filename=$(basename "$wallpaper")
+ thumb="$THUMB_DIR/$filename.jpg"
- # 创建缩略图(临时文件)
- thumb_dir="/tmp/hyprpaper_thumbs"
- mkdir -p "$thumb_dir"
- thumb_file="$thumb_dir/$(basename "$wallpaper").jpg"
-
- # 如果缩略图不存在或壁纸文件更新了,重新生成
- if [ ! -f "$thumb_file" ] || [ "$wallpaper" -nt "$thumb_file" ]; then
- # 使用ImageMagick生成缩略图
- if command -v convert >/dev/null 2>&1; then
- convert "$wallpaper" -resize 200x150^ -gravity center -extent 200x150 "$thumb_file" 2>/dev/null
- fi
+ # 缩略图不存在或壁纸文件更新时重新生成
+ if [ ! -f "$thumb" ] || [ "$wallpaper" -nt "$thumb" ]; then
+ convert "$wallpaper" -resize 256x256^ -gravity center -extent 256x256 "$thumb" 2>/dev/null
fi
- # 如果生成了缩略图,添加到rofi选项
- if [ -f "$thumb_file" ]; then
- rofi_options+=("$filename" "$thumb_file")
+ if [ -f "$thumb" ]; then
+ items+=("img:$thumb:text:$filename")
else
- rofi_options+=("$filename")
+ items+=("text:$filename")
fi
done
-# 使用rofi选择壁纸,显示缩略图
-selected=$(printf '%s\x00icon\x1f%s\n' "${rofi_options[@]}" | rofi -dmenu -p "选择壁纸" -theme-str 'window {width: 650px;}' -show-icons)
+# wofi 选择
+# dmenu-parse_action=true:选中后输出只保留 text 部分(即文件名),去掉图片转义
+selected=$(printf '%s\n' "${items[@]}" | wofi --dmenu -I -i -p "选择壁纸" -W 600 -b \
+ -k /dev/null -D dmenu-parse_action=true -D image_size=96)
-# 如果用户取消了选择
+# 用户取消
if [ -z "$selected" ]; then
exit 0
fi
-# 找到对应的壁纸文件路径
+# 根据文件名找到完整路径
selected_wallpaper=""
for wallpaper in "${wallpapers[@]}"; do
if [ "$(basename "$wallpaper")" = "$selected" ]; then
@@ -76,31 +60,16 @@ for wallpaper in "${wallpapers[@]}"; do
done
if [ -z "$selected_wallpaper" ]; then
- echo "错误: 找不到选择的壁纸文件"
+ echo "错误: 找不到选择的壁纸文件: $selected"
exit 1
fi
-# 检查配置文件模板是否存在
-if [ ! -f "$CONFIG_TEMPLATE" ]; then
- echo "警告: 配置文件模板不存在,创建默认配置"
- # 创建默认配置
- cat > "$CONFIG_FILE" << EOF
-preload = $selected_wallpaper
-wallpaper = ,$selected_wallpaper
-splash = false
-EOF
-else
- # 使用模板文件并替换壁纸路径变量
- sed "s|\$wallpaper = %{PATH}|preload = $selected_wallpaper|g" "$CONFIG_TEMPLATE" | \
- sed "s|\$wallpaper|$selected_wallpaper|g" > "$CONFIG_FILE"
-fi
-
-# 重启hyprpaper
-if pgrep -x "hyprpaper" > /dev/null; then
- pkill -x hyprpaper
-fi
+# 写入当前壁纸路径(startup.lua 下次启动时读取)
+printf '%s\n' "$selected_wallpaper" > "$CURRENT_FILE"
-# 启动hyprpaper
-hyprpaper &
+# 重载 swaybg
+pkill -x swaybg 2>/dev/null
+sleep 0.2
+swaybg -i "$selected_wallpaper" -m fill > /dev/null 2>&1 &
echo "壁纸已切换到: $selected"