diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-11-20 21:39:14 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-14 05:08:56 +0800 |
| commit | 25c5e0b1704b7d7cc4a37619a508c592f6a794c0 (patch) | |
| tree | 3de9056d74619d0922b85e4920af29b5648844de /scripts | |
Add initial Hyprland configuration
This commit introduces the base Hyprland setup including: - Monitor
configuration for dual display setup - Default applications and
keybindings - Window management rules and animations - Autostart
services (waybar, hyprpaper, fcitx5) - Custom scripts for window
toggling - Hyprspace plugin for workspace overview
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/switch_wallpaper.sh | 106 | ||||
| -rwxr-xr-x | scripts/toggle_float_center.sh | 18 | ||||
| -rwxr-xr-x | scripts/toggle_laptop_monitor.sh | 153 |
3 files changed, 277 insertions, 0 deletions
diff --git a/scripts/switch_wallpaper.sh b/scripts/switch_wallpaper.sh new file mode 100755 index 0000000..ae7acf9 --- /dev/null +++ b/scripts/switch_wallpaper.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +# 壁纸目录 +WALLPAPER_DIR="/home/catilgrass/图片/Wallpapers" +CONFIG_TEMPLATE="/home/catilgrass/.config/hypr/hyprpaper.conf.template" +CONFIG_FILE="/home/catilgrass/.config/hypr/hyprpaper.conf" + +# 检查壁纸目录是否存在 +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) + +# 检查是否有壁纸文件 +if [ ${#wallpapers[@]} -eq 0 ]; then + echo "错误: 在 $WALLPAPER_DIR 中没有找到壁纸文件" + exit 1 +fi + +# 创建rofi选项列表 +options=() +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_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 + fi + + # 如果生成了缩略图,添加到rofi选项 + if [ -f "$thumb_file" ]; then + rofi_options+=("$filename" "$thumb_file") + else + rofi_options+=("$filename") + fi +done + +# 使用rofi选择壁纸,显示缩略图 +selected=$(printf '%s\x00icon\x1f%s\n' "${rofi_options[@]}" | rofi -dmenu -p "选择壁纸" -theme-str 'window {width: 650px;}' -show-icons) + +# 如果用户取消了选择 +if [ -z "$selected" ]; then + exit 0 +fi + +# 找到对应的壁纸文件路径 +selected_wallpaper="" +for wallpaper in "${wallpapers[@]}"; do + if [ "$(basename "$wallpaper")" = "$selected" ]; then + selected_wallpaper="$wallpaper" + break + fi +done + +if [ -z "$selected_wallpaper" ]; then + echo "错误: 找不到选择的壁纸文件" + 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 + +# 启动hyprpaper +hyprpaper & + +echo "壁纸已切换到: $selected" diff --git a/scripts/toggle_float_center.sh b/scripts/toggle_float_center.sh new file mode 100755 index 0000000..889430c --- /dev/null +++ b/scripts/toggle_float_center.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# 使用 grep 检查浮动状态 +FLOATING=$(hyprctl activewindow | grep -oP 'floating:\s*\K\w+') + +if [ "$FLOATING" = "0" ] || [ -z "$FLOATING" ]; then + # 平铺 -> 浮动 + hyprctl dispatch togglefloating + sleep 0.05 + hyprctl dispatch resizeactive exact 50% 75% + hyprctl dispatch centerwindow + sleep 0.05 + # 移动鼠标到屏幕中心(近似窗口中心) + hyprctl dispatch movecursor 50% 50% +else + # 浮动 -> 平铺 + hyprctl dispatch togglefloating +fi diff --git a/scripts/toggle_laptop_monitor.sh b/scripts/toggle_laptop_monitor.sh new file mode 100755 index 0000000..a84e4d8 --- /dev/null +++ b/scripts/toggle_laptop_monitor.sh @@ -0,0 +1,153 @@ +#!/bin/bash +# ~/.config/hypr/scripts/toggle_laptop_screen.sh + +LAPTOP="eDP-1" +CONFIG_DIR="$HOME/.config/hypr" +CACHE_FILE="$CONFIG_DIR/monitor_preferred" + +# 确保配置目录存在 +mkdir -p "$CONFIG_DIR" + +# 从缓存获取最佳模式 +get_cached_best_mode() { + local monitor="$1" + if [ -f "$CACHE_FILE" ]; then + grep "^$monitor=" "$CACHE_FILE" | cut -d= -f2- + fi +} + +# 保存最佳模式到缓存 +save_cached_best_mode() { + local monitor="$1" + local mode="$2" + + if [ -f "$CACHE_FILE" ]; then + # 删除旧的缓存 + grep -v "^$monitor=" "$CACHE_FILE" > "$CACHE_FILE.tmp" 2>/dev/null || true + mv "$CACHE_FILE.tmp" "$CACHE_FILE" 2>/dev/null || touch "$CACHE_FILE" + else + touch "$CACHE_FILE" + fi + + # 保存新的缓存 + echo "$monitor=$mode" >> "$CACHE_FILE" +} + +# 检测外接显示器并选择最佳模式 +detect_external_best_mode() { + # 获取所有非笔记本显示器 + EXTERNAL_MONITORS=$(hyprctl monitors | grep "Monitor" | grep -v "$LAPTOP") + + if [ -z "$EXTERNAL_MONITORS" ]; then + echo "" + return + fi + + # 取第一个外接显示器 + EXTERNAL_NAME=$(echo "$EXTERNAL_MONITORS" | head -1 | awk '{print $2}' | tr -d '()') + + # 首先尝试从缓存获取 + CACHED_MODE=$(get_cached_best_mode "$EXTERNAL_NAME") + if [ -n "$CACHED_MODE" ] && [ "$CACHED_MODE" != "preferred" ]; then + echo "$EXTERNAL_NAME,$CACHED_MODE" + return + fi + + # 获取该显示器支持的所有模式 + MODES=$(hyprctl monitors --list | grep -A 50 "$EXTERNAL_NAME" | tail -n +2 | grep -E "^[[:space:]]*[0-9]+x[0-9]+@[0-9]+") + + if [ -z "$MODES" ]; then + echo "$EXTERNAL_NAME" + save_cached_best_mode "$EXTERNAL_NAME" "preferred" + return + fi + + # 解析并选择最佳模式 + BEST_MODE="" + BEST_AREA=0 + BEST_REFRESH=0 + + while IFS= read -r MODE; do + # 清理空格 + MODE=$(echo "$MODE" | xargs) + + # 解析分辨率宽高和刷新率 + if [[ "$MODE" =~ ([0-9]+)x([0-9]+)@([0-9]+) ]]; then + WIDTH="${BASH_REMATCH[1]}" + HEIGHT="${BASH_REMATCH[2]}" + REFRESH="${BASH_REMATCH[3]}" + + # 比较逻辑:先比面积,再比刷新率 + AREA=$((WIDTH * HEIGHT)) + + if [ "$AREA" -gt "$BEST_AREA" ]; then + # 更大的分辨率 + BEST_AREA="$AREA" + BEST_REFRESH="$REFRESH" + BEST_MODE="$MODE" + elif [ "$AREA" -eq "$BEST_AREA" ] && [ "$REFRESH" -gt "$BEST_REFRESH" ]; then + # 相同分辨率,更高刷新率 + BEST_REFRESH="$REFRESH" + BEST_MODE="$MODE" + fi + fi + done <<< "$MODES" + + # 保存到缓存 + if [ -n "$BEST_MODE" ]; then + save_cached_best_mode "$EXTERNAL_NAME" "$BEST_MODE" + echo "$EXTERNAL_NAME,$BEST_MODE" + else + save_cached_best_mode "$EXTERNAL_NAME" "preferred" + echo "$EXTERNAL_NAME" + fi +} + +# 检查笔记本是否启用 +is_enabled() { + hyprctl monitors | grep -q "$LAPTOP" && ! hyprctl monitors | grep "$LAPTOP" | grep -q "disabled" +} + +# 检查是否安全 +safe_to_disable() { + ENABLED_COUNT=$(hyprctl monitors | grep "Monitor" | grep -c "disabled") + TOTAL_COUNT=$(hyprctl monitors | grep -c "Monitor") + [ "$ENABLED_COUNT" -eq 0 ] && [ "$TOTAL_COUNT" -gt 1 ] +} + +# 主逻辑 +EXTERNAL_INFO=$(detect_external_best_mode) + +if is_enabled; then + if safe_to_disable; then + if [ -n "$EXTERNAL_INFO" ]; then + # 解析外接显示器信息 + IFS=',' read -r EXTERNAL_NAME EXTERNAL_MODE <<< "$EXTERNAL_INFO" + + if [ -n "$EXTERNAL_MODE" ]; then + hyprctl keyword monitor "$EXTERNAL_NAME, $EXTERNAL_MODE, 0x0, 1" + else + hyprctl keyword monitor "$EXTERNAL_NAME, preferred, 0x0, 1" + fi + fi + + hyprctl keyword monitor "$LAPTOP, disable" + notify-send "显示器" "笔记本屏幕已禁用" + else + notify-send -u critical "错误" "不能禁用唯一显示器" + fi +else + hyprctl keyword monitor "$LAPTOP, 2560x1440@240, 1920x0, 1" + + if [ -n "$EXTERNAL_INFO" ]; then + IFS=',' read -r EXTERNAL_NAME EXTERNAL_MODE <<< "$EXTERNAL_INFO" + + if [ -n "$EXTERNAL_MODE" ]; then + hyprctl keyword monitor "$EXTERNAL_NAME, $EXTERNAL_MODE, 0x0, 1" + else + hyprctl keyword monitor "$EXTERNAL_NAME, preferred, 0x0, 1" + fi + fi + + notify-send "显示器" "笔记本屏幕已启用" +fi |
