include /etc/sway/config.d/10-service.conf
include /etc/sway/config.d/50-systemd-user.conf

# THEME
set $background /usr/share/backgrounds/wallpapers-juhraya/Austria_by_Rutger_Heijmerikx.jpg
set $gui-font Noto Sans 10
set $term-font Akkurat Mono
# light
# set $cursor-theme xcursor-breeze-snow
# set $gtk-theme catppuccin-latte-teal-standard+default
# set $icon-theme Papirus
# set $kvantum-theme catppuccin-latte-teal
# set $gtk-color-scheme prefer-light
# dark
set $cursor-theme xcursor-breeze
set $gtk-theme catppuccin-mocha-teal-standard+default
set $icon-theme Papirus-Dark
set $kvantum-theme catppuccin-mocha-teal
set $gtk-color-scheme prefer-dark
# colors
set $background-color #1E1E2E
set $text-color #CDD6F4
set $accent-color #89B4FA
# target                title   bg      text    indicator
client.focused          #556064 #F9CE5C #000000 #FDF6E3
client.focused_inactive #CB4B16 #2F3D44 #1ABC9C #454948
client.unfocused        #556064 #2F3D44 #1ABC9C #454948
client.urgent           #CB4B16 #FDF6E3 #1ABC9C #268BD2
client.placeholder      #000000 #0C0C0C #ffffff #000000
client.background       #2B2C2B

# DEFINIIONS
# controls
set $mod Mod4
set $left h
set $down j
set $up k
set $right l
# Workspace numbers
set $ws1 number 1
set $ws2 number 2
set $ws3 number 3
set $ws4 number 4
set $ws5 number 5
set $ws6 number 6
set $ws7 number 7
set $ws8 number 8
set $ws9 number 9
set $ws10 number 10
# terminal editor
set $term footclient
set $term_float footclient --app-id floating_shell --window-size-chars 120x45
# swayidle
set $locking swaylock --screenshots --clock --grace 10 --fade-in 0.5 --effect-blur 5x5 --effect-greyscale
# scaling
set $scale_up /usr/share/sway/scripts/scale.sh up
set $scale_down /usr/share/sway/scripts/scale.sh down
set $scale_default /usr/share/sway/scripts/scale.sh default
# audio
set $pulseaudio $term_float pulsemixer
set $sink_volume pactl get-sink-volume @DEFAULT_SINK@ | grep '^Volume:' | cut -d / -f 2 | tr -d ' ' | sed 's/%//'
set $source_volume pactl get-source-volume @DEFAULT_SOURCE@ | grep '^Volume:' | cut -d / -f 2 | tr -d ' ' | sed 's/%//'
set $volume_down $(pactl set-sink-volume @DEFAULT_SINK@ -5% && $sink_volume)
set $volume_up $(pactl set-sink-volume @DEFAULT_SINK@ +5% && $sink_volume)
set $volume_mute $(pactl set-sink-mute @DEFAULT_SINK@ toggle && pactl get-sink-mute @DEFAULT_SINK@ | sed -En "/no/ s/.*/$($sink_volume)/p; /yes/ s/.*/0/p")
set $mic_mute $(pactl set-source-mute @DEFAULT_SOURCE@ toggle && pactl get-source-mute @DEFAULT_SOURCE@ | sed -En "/no/ s/.*/$($source_volume)/p; /yes/ s/.*/0/p")
# rofi
set $rofi_theme "* {\nlightbg: $background-color;\nbackground: $background-color;\nlightfg: $accent-color;\nforeground: $text-color;\n}\nwindow {\nwidth: 25em;\n}\n"
set $menu rofi -show combi -combi-modi "drun,run" -terminal "$term" -ssh-command "{terminal} {ssh-client} {host} [-p {port}]" -run-shell-command "{terminal} {cmd}" -show-icons -font "$gui-font" -lines 10
# Screenshot commands
set $grimshot grimshot
set $pipe_output $grimshot save output -
set $pipe_selection $grimshot save area -
set $notify_paste [[ $(wl-paste -l) == "image/png" ]] && notify-send "Screenshot copied to clipboard"
set $swappy swappy -f -
set $upload_pipe curl -s -F "file=@-;filename=.png" https://x0.at/ | tee >(wl-copy) >(xargs notify-send)
set $screenshot_screen $pipe_output | $swappy && $notify_paste
set $screenshot_screen_upload $pipe_output | $upload_pipe
set $screenshot_selection $pipe_selection | $swappy && $notify_paste
set $screenshot_selection_upload $pipe_selection | $upload_pipe

# AUTOSTART APPLICATIONS
exec /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
exec_always {
    # theming (from manjaro sway desktop config)
    /usr/share/sway/scripts/enable-gtk-theme.sh "$gtk-theme"
    /usr/share/sway/scripts/fontconfig.sh "monospace" "$term-font"
    gsettings set org.gnome.desktop.interface icon-theme "$icon-theme"
    gsettings set org.gnome.desktop.interface cursor-theme "$cursor-theme"
    gsettings set org.gnome.desktop.interface font-name "$gui-font"
    gsettings set org.gnome.desktop.input-sources show-all-sources true
    gsettings set org.gnome.desktop.interface monospace-font-name "$term-font"
    gsettings set org.freedesktop.appearance color-scheme "$gtk-color-scheme"
    gsettings set org.gnome.desktop.interface color-scheme "$gtk-color-scheme"
    kvantummanager --set "$kvantum-theme"
}
exec {
    pamac-tray-plasma
    wlsunset -l 59.91:10.76
    systemctl --user enable --now blueman-applet
    # idle lock
    systemctl --user enable --now swayidle
    systemctl --user enable --now idlehack
    # focus switch animations
    systemctl --user enable --now flashfocus
    # usb automount
    systemctl --user enable --now pcmanfm
    # workspace icons
    systemctl --user enable --now sworkstyle
    # foot server
    systemctl --user enable --now foot-server
}
exec_always {
    nm-applet
    calcurse --daemon
    # persist clipboard after close
    pkill -x wl-clip-persist
    wl-clip-persist --clipboard regular --all-mime-type-regex '(?i)^(?!image/x-inkscape-svg).+'
    # mako daemon
    pkill -x mako
    mako --font "$term-font" --text-color "$text-color" --border-color "$accent-color" --background-color "$background-color" --border-size 3 --width 400 --height 200 --padding 20 --margin 20 --default-timeout 15000
    # enable noisetorch
    noisetorch -u
    noisetorch -i
    # update rofi theme
    mkdir -p $HOME/.config/rofi/
    echo $rofi_theme > $HOME/.config/rofi/config.rasi
}

# CONFIGS
font $gui-font
focus_follows_mouse no
mouse_warping none
workspace_auto_back_and_forth no
bar {
    swaybar_command waybar
}
# don't show gaps if there's only one window on the desktop
smart_gaps on
gaps inner 20
# borders
default_border normal
default_floating_border normal
hide_edge_borders smart

# BINDINGS
include ~/.config/sway/local_config
bindsym $mod+Return exec $term
bindsym $mod+Shift+Return exec foot
bindsym $mod+Shift+q kill
bindsym $mod+d exec $menu
floating_modifier $mod normal
bindsym $mod+r reload
bindsym $mod+a focus parent
# Moving around
bindsym $mod+$left focus left
bindsym $mod+$down focus down
bindsym $mod+$up focus up
bindsym $mod+$right focus right
# Moving
bindsym $mod+Shift+$left move left
bindsym $mod+Shift+$down move down
bindsym $mod+Shift+$up move up
bindsym $mod+Shift+$right move right
# splits
bindsym $mod+b splith
bindsym $mod+s splitv
bindsym $mod+q split toggle
# split formats
bindsym $mod+w layout stacking
bindsym $mod+t layout tabbed
bindsym $mod+e layout toggle split
bindsym $mod+f fullscreen
# floating
bindsym $mod+Shift+space floating toggle
bindsym $mod+Ctrl+space sticky toggle
bindsym $mod+space focus mode_toggle
# scratchpad
bindsym $mod+Shift+minus move scratchpad
bindsym $mod+Shift+slash move scratchpad
bindsym $mod+minus scratchpad show
bindsym $mod+slash scratchpad show
# sound
bindsym Scroll_Lock exec $volume_mute
bindsym Print exec $volume_down
bindsym Pause exec $volume_up
# screenshots
bindsym $mod+Shift+s exec --no-startup-id $screenshot_selection
# scale
bindsym $mod+greater exec $scale_up
bindsym $mod+less exec $scale_down
bindsym $mod+equal exec $scale_default
# apps
bindsym $mod+F1 exec qalculate-qt
bindsym $mod+F2 exec qutebrowser
bindsym $mod+F3 exec quodlibet
bindsym $mod+F4 exec $term_float ncspot
bindsym $mod+n exec makoctl dismiss
bindsym $mod+Shift+n exec makoctl dismiss -a
bindsym $mod+p exec pcmanfm-qt
bindsym $mod+Shift+p exec $term_float yazi
bindsym $mod+u exec unipicker --command "rofi -dmenu | wl-copy"
bindsym $mod+period exec $term_float khal interactive
bindsym $mod+comma exec $term_float aerc

# Special keys
bindsym --locked XF86AudioMute exec $volume_mute
bindsym --locked XF86AudioLowerVolume exec $volume_down
bindsym --locked XF86AudioRaiseVolume exec $volume_up
bindsym --locked XF86AudioMicMute exec $mic_mute
bindsym --locked XF86MonBrightnessDown exec brightnessctl set 5%-
bindsym --locked XF86MonBrightnessUp exec brightnessctl set 5%+

# resize
mode "resize" {
    bindsym $left resize shrink width 10px
    bindsym $down resize grow height 10px
    bindsym $up resize shrink height 10px
    bindsym $right resize grow width 10px
    bindsym Shift+$left resize shrink width 50px
    bindsym Shift+$down resize grow height 50px
    bindsym Shift+$up resize shrink height 50px
    bindsym Shift+$right resize grow width 50px

    # Return to default mode
    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+z mode "resize"

# shut down, restart and locking
set $mode_system System (l) lock, (e) exit, (s) suspend, (r) reboot, (p) shutdown
mode "$mode_system" {
    bindsym e exec --no-startup-id swaymsg exit, mode "default"
    bindsym s exec --no-startup-id systemctl suspend, mode "default"
    bindsym r exec --no-startup-id systemctl reboot, mode "default"
    bindsym p exec --no-startup-id systemctl poweroff -i, mode "default"
    bindsym l exec --no-startup-id $locking, mode "default"

    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+Shift+e mode "$mode_system"

# config tools
set $mode_config (b) bt, (c) cooling, (d) display, (g) gparted, (k) kvantum, (l) lxappearance, (m) manjaro, (p) packages, (q) qt5, (Q) qt6, (s) sound, (u) upgrade
mode "$mode_config" {
    # bindsym b exec --no-startup-id blueman-manager, mode "default"
    bindsym c exec --no-startup-id coolercontrol, mode "default"
    bindsym d exec --no-startup-id wdisplays, mode "default"
    bindsym g exec --no-startup-id gparted, mode "default"
    bindsym k exec --no-startup-id kvantummanager, mode "default"
    bindsym l exec --no-startup-id lxappearance, mode "default"
    bindsym m exec --no-startup-id manjaro-settings-manager, mode "default"
    bindsym p exec --no-startup-id pamac-manager, mode "default"
    bindsym q exec --no-startup-id qt5ct, mode "default"
    bindsym Shift+q exec --no-startup-id qt6ct, mode "default"
    bindsym s exec --no-startup-id pavucontrol-qt, mode "default"
    bindsym u exec --no-startup-id $term_float topgrade --only config_update --only system, mode "default"

    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+c mode "$mode_config"

# config tools
set $mode_monitor (b) btop, (h) htop, (s) isd, (j) journal, (n) nvtop
mode "$mode_monitor" {
    bindsym b exec $term_float btop, mode "default"
    bindsym h exec $term_float htop, mode "default"
    bindsym s exec $term_float isd, mode "default"
    bindsym j exec $term_float lazyjournal, mode "default"
    bindsym n exec $term_float nvtop, mode "default"

    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+m mode "$mode_monitor"

# WINDOWS
for_window [all] title_format "%app_id - %title"

# set floating mode for generated windows
for_window [title="(?:Open|Save) (?:File|Folder|As)"] floating enable
for_window [title="(?:Open|Save) (?:File|Folder|As)"] resize set 800 600
for_window [title="Save File"] floating enable
for_window [window_role="pop-up"] floating enable
for_window [window_role="bubble"] floating enable
for_window [window_role="task_dialog"] floating enable
for_window [window_role="Preferences"] floating enable
for_window [window_type="dialog"] floating enable
for_window [window_type="menu"] floating enable

# remove title
for_window [app_id="foot"] border pixel 2
for_window [class="discord"] border pixel 2
for_window [app_id="thunderbird"] border pixel 2
for_window [app_id="qutebrowser"] border pixel 2

# Open specific applications in floating mode
for_window [app_id="GParted"] floating enable
for_window [app_id="qalculate-qt"] floating enable
for_window [class="pavucontrol-qt"] floating enable

# https://github.com/ValveSoftware/steam-for-linux/issues/1040
for_window [app_id="^steam$"] floating enable
for_window [app_id="^Steam$"] floating enable
for_window [app_id="^steam$" title="^Steam$"] floating disable

# set floating mode for specific applications
for_window [app_id="thunderbird"] border pixel 2
for_window [app_id="thunderbird" title=".*Reminder"] floating enable
for_window [app_id="floating_shell"] floating enable, border pixel 1

# Don't allow applications to inhibit shortcuts, i.e. grab total control of the
# keyboard. Chrome currently abuses this by enabling it for all "--app=..." shortcuts.
seat * shortcuts_inhibitor disable

# IO
output * bg $background fill
input type:keyboard {
    xkb_layout "us"
    xkb_variant "altgr-weur"
    xkb_options "caps:escape"
    repeat_delay 400
    repeat_rate 50
}
input type:mouse {
    accel_profile "flat"
    pointer_accel 0.5
}
