#!/usr/bin/env sh

# necessary to load scripting-addition during startup on macOS Big Sur
# *yabai --load-sa* is configured to run through sudo without a password
sudo yabai --load-sa
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"

# mission-control desktop labels
yabai -m space 1 --label code
yabai -m space 2 --label web
yabai -m space 3 --label mail
yabai -m space 4 --label chat
yabai -m space 5 --label social
yabai -m space 6 --label notes
yabai -m space 7 --label media
yabai -m space 8 --label games

# window rules
yabai -m rule --add app="^WezTerm$" space=code
yabai -m rule --add app="^(Safari|Firefox)$" space=web
yabai -m rule --add app="^Mail$" space=mail
yabai -m rule --add app="^Element$" space=chat
yabai -m rule --add app="^Discord$" space=social grid=1:1:0:0:1:1 manage=off
yabai -m rule --add app="^(Notes|Joplin)$" space=notes
yabai -m rule --add app="^(Spotify|Finamp|Musik)$" space=media
yabai -m rule --add app="^Steam Helper$" space=games manage=off

# Advanced Window rules
yabai -m rule --add app="^(Calculator|Rechner|System Preferences|Systemeinstellungen|Einstellungen|System Settings|Preferences|Aktivitätsanzeige|Archive Utility)$" manage=off
yabai -m rule --add app="^mpv$" manage=off border=off sticky=on layer=above opacity=1.0 grid=8:8:6:0:2:2
yabai -m rule --add app="^(JetBrains Toolbox|PyCharm)$" manage=off
yabai -m rule --add app="^(Karten|FaceTime|Home|Kalender|Kontakte|Erinnerungen|Notizen)$" manage=off
yabai -m rule --add app="^choose$" manage=off

# show digital colour meter topmost and on all spaces
yabai -m rule --add app="^Digital Colou?r Meter$" sticky=on

# refresh my Übersicht bar when the space changes
yabai -m signal --add event=space_changed \
    action="osascript -e 'tell application \"Übersicht\" to refresh widget id \"spaces-widget\"'"

# signals
yabai -m signal --add event=window_destroyed active=yes action="yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse &> /dev/null || yabai -m window --focus \$(yabai -m query --windows --space | jq .[0].id) &> /dev/null"
yabai -m signal --add event=window_minimized active=yes action="if \$(yabai -m query --windows --window \$YABAI_WINDOW_ID | jq -r '.\"is-floating\"'); then yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse &> /dev/null || yabai -m window --focus \$(yabai -m query --windows --space | jq .[0].id) &> /dev/null; fi"

# specific space settings
yabai -m config --space mail  layout stack
yabai -m config --space games layout float

# global settings
yabai -m config                                 \
    mouse_follows_focus          off            \
    focus_follows_mouse          autoraise            \
    window_origin_display        default        \
    window_placement             second_child   \
    window_zoom_persist          on             \
    window_topmost               off            \
    window_shadow                on             \
    window_animation_duration    0.0            \
    window_animation_frame_rate  120            \
    window_opacity_duration      0.0            \
    active_window_opacity        1.0            \
    normal_window_opacity        0.90           \
    window_opacity               off            \
    insert_feedback_color        0xffd75f5f     \
    active_window_border_color   0xff775759     \
    normal_window_border_color   0xff555555     \
    window_border_width          4              \
    window_border_radius         12             \
    window_border_blur           off            \
    window_border_hidpi          on             \
    window_border                off            \
    split_ratio                  0.50           \
    split_type                   auto           \
    auto_balance                 off            \
    top_padding                  12             \
    bottom_padding               12             \
    left_padding                 12             \
    right_padding                12             \
    window_gap                   06             \
    layout                       bsp            \
    mouse_modifier               fn             \
    mouse_action1                move           \
    mouse_action2                resize         \
    mouse_drop_action            swap

echo "yabai configuration loaded.."

