#!/bin/bash

# 目標視窗名稱
TARGET=$1

# 獲取目前所有已開啟的視窗名稱 (提取冒號前文字並去空格)
OPENED_WINDOWS=$(eww active-windows | cut -d: -f1 | tr -d ' ')

# 所有受管理的彈出視窗清單
MANAGED_WINDOWS=("calendar" "volume_popup" "brightness_popup")

# 遍歷目前已開啟的視窗
for win in $OPENED_WINDOWS; do
    # 檢查該視窗是否在受管名單內，且不是目標視窗
    # 我們不關閉 "bar" 視窗
    if [[ " ${MANAGED_WINDOWS[@]} " =~ " ${win} " ]] && [ "$win" != "$TARGET" ]; then
        eww close "$win"
    fi
done

# 如果有目標視窗，則切換其顯示狀態
if [ -n "$TARGET" ]; then
    eww open --toggle "$TARGET"
fi
