exec "/usr/lib/polkit-kde-authentication-agent-1"

exec_always {
    pkill kanshi
    pkill swayidle
    pkill swaync
    pkill waybar
}

exec_always "sleep 1"

### Variables
set $mod Mod4
set $left h
set $down j
set $up k
set $right l
set $term kitty
set $files nautilus
set $browser flatpak run io.gitlab.librewolf-community
set $bitwarden flatpak run com.bitwarden.desktop
set $menu wofi
set $scripts "$HOME/.config/scripts"
set $newScript "$HOME/.config/scripts/sessions.sh"

set $next_or_new swaymsg -r -t get_workspaces | jq -r --arg OUTPUT $(swaymsg -t get_outputs -r | jq -r '.[] | select(.focused == true) | .name') '(. | (max_by(.num) | .num)) as $max | [.[] | select(.output == $OUTPUT)] | (max_by(.num) | .num) as $maxOutput | (.[] | select(.focused == true) | .num) as $current | if $maxOutput > $current then "next_on_output" else $max + 1 end' 
set $previous_or_first swaymsg -r -t get_workspaces | jq -r --arg OUTPUT $(swaymsg -t get_outputs -r | jq -r '.[] | select(.focused == true) | .name') '(. | (max_by(.num) | .num)) as $max | [.[] | select(.output == $OUTPUT)] | (min_by(.num) | .num) as $minOutput | (.[] | select(.focused == true) | .num) as $current | if $minOutput < $current then "prev_on_output" else $current end'

include /etc/sway/config-vars.d/*
exec dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP=sway

exec_always {
  gsettings set org.gnome.desktop.interface gtk-theme 'catppuccin-mocha-mauve-standard+default'
}

### Output configuration
output * bg ~/.config/configfiles/wallpaper.jpg fill
exec_always "kanshi"

### Idle configuration
exec_always swayidle -w \
timeout 300 'swaylock -f' \
timeout 420  'swaymsg "output * dpms off"' \
resume 'swaymsg "output * dpms on"' \
before-sleep 'swaylock -f'

exec_always swayidle -w \
timeout 600 'systemctl suspend' \

### Input configuration
input "type:touchpad" {
    dwt enabled
    tap enabled
    middle_emulation enabled
    natural_scroll disabled
    scroll_factor 0.5
}

input "type:pointer" {
    accel_profile "flat" 
    pointer_accel 0
}

# Guesture
bindgesture swipe:right workspace next
bindgesture swipe:left workspace prev

# Notification Daemon
exec_always swaync

# Toggle control center
bindsym $mod+control+n exec swaync-client -t -sw

### Key bindings

# Basics:
  # Start a terminal
  bindsym $mod+t exec $term

  # File Browser
  bindsym $mod+f exec $files
  
  # Browser
  bindsym $mod+b exec $browser

  # Bitwarden
  bindsym $mod+Shift+b exec $bitwarden

  # Kill focused window
  bindsym $mod+q kill

  # Start your launcher
  bindsym $mod+space exec $menu

  # Start your goland
  bindsym $mod+n exec 'zsh -c ". $newScript new"'
  bindsym $mod+Shift+n exec 'zsh -c ". $newScript goland-new"'
  
  # Audio Switch
  bindsym $mod+Shift+s exec 'bash -c ". $scripts/audioswitch.sh"'

  # Drag floating windows by holding down $mod and left mouse button.
  # Resize them with right mouse button + $mod.
  # Despite the name, also works for non-floating windows.
  # Change normal to inverse to use left mouse button for resizing and right
  # mouse button for dragging.
  #floating_modifier control normal

  # Reload the configuration file
  bindsym $mod+Shift+c reload

  # Lock
  bindsym $mod+Shift+Escape exec swaylock -f

  # Exit sway (logs you out of your Wayland session)
  bindsym $mod+Shift+e exec swaynag -t warning -m 'You pressed the exit shortcut. Do you really want to exit sway? This will end your Wayland session.' -B 'Yes, exit sway' 'swaymsg exit'

# Moving around:

  # Move your focus around
  bindsym $mod+$left focus left
  bindsym $mod+$down focus down
  bindsym $mod+$up focus up
  bindsym $mod+$right focus right
  # Or use $mod+[up|down|left|right]
  bindsym $mod+Left focus left
  bindsym $mod+Down focus down
  bindsym $mod+Up focus up
  bindsym $mod+Right focus right

  # Move the focused window with the same, but add Shift
  bindsym $mod+Shift+$left move left
  bindsym $mod+Shift+$down move down
  bindsym $mod+Shift+$up move up
  bindsym $mod+Shift+$right move right
  # Ditto, with arrow keys
  bindsym $mod+Shift+Left move left
  bindsym $mod+Shift+Down move down
  bindsym $mod+Shift+Up move up
  bindsym $mod+Shift+Right move right


# Workspaces:
  
  # Switch to workspace
  bindsym $mod+1 workspace number 1
  bindsym $mod+2 workspace number 2
  bindsym $mod+3 workspace number 3
  bindsym $mod+4 workspace number 4
  bindsym $mod+5 workspace number 5
  bindsym $mod+6 workspace number 6
  bindsym $mod+7 workspace number 7
  bindsym $mod+8 workspace number 8
  bindsym $mod+9 workspace number 9
  bindsym $mod+0 workspace number 10
  
  # Move focused container to workspace
  bindsym $mod+control+1 move container to workspace number 1;workspace number 1
  bindsym $mod+control+2 move container to workspace number 2;workspace number 2
  bindsym $mod+control+3 move container to workspace number 3;workspace number 3
  bindsym $mod+control+4 move container to workspace number 4;workspace number 4
  bindsym $mod+control+5 move container to workspace number 5;workspace number 5
  bindsym $mod+control+6 move container to workspace number 6;workspace number 6
  bindsym $mod+control+7 move container to workspace number 7;workspace number 7
  bindsym $mod+control+8 move container to workspace number 8;workspace number 8
  bindsym $mod+control+9 move container to workspace number 9;workspace number 9
  bindsym $mod+control+0 move container to workspace number 10;workspace number 10


  bindsym $mod+control+l exec swaymsg "workspace $($next_or_new)"
  bindsym $mod+control+h exec swaymsg "workspace $($previous_or_first)"

  bindsym $mod+Shift+control+$right exec swaymsg "move container to workspace $($next_or_new), workspace next_on_output"
  bindsym $mod+Shift+control+$left exec swaymsg "move container to workspace $($previous_or_first), workspace prev_on_output"

 # bindsym $mod+control+l workspace next
 # bindsym $mod+control+h workspace prev
 # 
 # # Move window to next or previous workspace
 # bindsym $mod+Shift+control+$left move container to workspace prev; workspace prev
 # bindsym $mod+Shift+control+$right move container to workspace next; workspace next

 # bindsym $mod+control+$left move workspace to output left
 # bindsym $mod+control+$down move workspace to output down
 # bindsym $mod+control+$up move workspace to output up
 # bindsym $mod+control+$right move workspace to output right

# Layout stuff:

  # You can "split" the current object of your focus with
  # $mod+b or $mod+v, for horizontal and vertical splits
  # respectively.
  #bindsym $mod+b splith
  #bindsym $mod+v splitv

  # Switch the current container between different layout styles
#  bindsym $mod+s layout stacking
  bindsym $mod+w layout tabbed
  bindsym $mod+e layout toggle split

  # Make the current focus fullscreen
  bindsym $mod+m fullscreen

  # Toggle the current focus between tiling and floating mode
  bindsym $mod+Shift+space floating toggle

  # Swap focus between the tiling area and the floating area
  #bindsym $mod+space focus mode_toggle

  # Move focus to the parent container
  bindsym $mod+a focus parent

# Resizing containers:

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 Left resize shrink width 10px
  bindsym Down resize grow height 10px
  bindsym Up resize shrink height 10px
  bindsym Right resize grow width 10px

  bindsym Return mode "default"
  bindsym Escape mode "default"
}
bindsym $mod+r mode "resize"

# Status Bar:
bar {
  swaybar_command waybar
  workspace_buttons yes
}

# Audio
bindsym XF86AudioRaiseVolume exec "wpctl set-volume -l 1.5 @DEFAULT_AUDIO_SINK@ 5%+; pkill -RTMIN+8 waybar"
bindsym XF86AudioLowerVolume exec "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-; pkill -RTMIN+8 waybar"
bindsym XF86AudioMute exec "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle; pkill -RTMIN+8 waybar"

# Brightness
bindsym XF86MonBrightnessDown exec brightnessctl s 10%-
bindsym XF86MonBrightnessUp exec brightnessctl s +10%


# ScreenShot
bindsym $mod+p exec grim -g "$(slurp)" - | wl-copy

bindsym $mod+shift+p exec grim - | wl-copy

# Border
include catppuccin-mocha
#client.focused "#ff78ff" "#ff77ff" "#ffffff" "#ff77ff"
default_border pixel 2
gaps inner 2

# target                 title     bg    text   indicator  border
client.focused           $lavender $base $text  $rosewater $lavender
client.focused_inactive  $overlay0 $base $text  $rosewater $overlay0
client.unfocused         $overlay0 $base $text  $rosewater $overlay0
client.urgent            $peach    $base $peach $overlay0  $peach
client.placeholder       $overlay0 $base $text  $overlay0  $overlay0
client.background        $base

include /etc/sway/config.d/*
