// Niri configuration for CachyOS
// For documentation and full reference, see: https://github.com/YaLTeR/niri/wiki

// ────────────── Input Configuration ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Input
cursor {
	xcursor-theme "Adwaita"
	xcursor-size 24
}

input {
    keyboard {
        xkb {
            layout "de" // Use the German keyboard layout
            variant "nodeadkeys"
        }
        numlock // Enable numlock on startup
    }

    touchpad {
        tap // Enable tap-to-click
        natural-scroll // Enable natural (macOS-style) scrolling
    }

    workspace-auto-back-and-forth // Enable workspace back & forth switching
}

// ────────────── Output Configuration ──────────────
// You can run `niri msg outputs` to get the correct name for your displays.
// You will have to remove "/-" and edit it before it takes effect.
// https://github.com/YaLTeR/niri/wiki/Configuration:-Outputs

output "DP-2" {
    mode "1920x1080@144.001" // Set resolution and refresh rate
    scale 1 // No scaling (use 2 for HiDPI)
    focus-at-startup // Main Display
}

output "HDMI-A-1" {
    mode "1920x1080@60.000" // Set resolution and refresh rate
    scale 1 // No scaling (use 2 for HiDPI)
    //off
}

// ────────────── Keybindings ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Key-Bindings

binds {
    // ---- Hotkey Overlay
    MOD+SHIFT+BACKSPACE                 { show-hotkey-overlay; }

    // ─── Noctalia Shell ───
    // Note: On NixOS, use "noctalia-shell" directly instead of "qs -c noctalia-shell"
    Mod+Shift+L                         hotkey-overlay-title="Lock Screen" { spawn "noctalia-shell" "ipc" "call" "lockScreen" "lock"; }
    Mod+S                               hotkey-overlay-title="Control Center" { spawn "noctalia-shell" "ipc" "call" "controlCenter" "toggle"; }
    Mod+Comma                           hotkey-overlay-title="Settings" { spawn "noctalia-shell" "ipc" "call" "settings" "toggle"; }
    Mod+Escape                          hotkey-overlay-title="Session Menu" { spawn "noctalia-shell" "ipc" "call" "sessionMenu" "toggle"; }
    Mod+N                               hotkey-overlay-title="Notification History" { spawn "noctalia-shell" "ipc" "call" "notifications" "toggleHistory"; }
    Mod+M                               hotkey-overlay-title="Wallpaper Selector" { spawn "noctalia-shell" "ipc" "call" "wallpaper" "toggle"; }

    // ─── Applications ───
    Mod+T                               hotkey-overlay-title="Open Terminal: ghostty" { spawn "ghostty"; }
    Mod+B                               hotkey-overlay-title="Open Browser: Zen" { spawn "zen"; }
    Mod+E                               hotkey-overlay-title="File Manager: Nautilus" { spawn "nautilus"; }
    Mod+Space repeat=false              hotkey-overlay-title="Open App Launcher" { spawn "vicinae" "toggle"; }
    // Mod+G                            hotkey-overlay-title="Open Google Gemini" { spawn "chromium" "--app=https://gemini.google.com"; }

    // ─── Audio Controls (via Noctalia for OSD) ───
    XF86AudioRaiseVolume                allow-when-locked=true { spawn "noctalia-shell" "ipc" "call" "volume" "increase"; }
    XF86AudioLowerVolume                allow-when-locked=true { spawn "noctalia-shell" "ipc" "call" "volume" "decrease"; }
    XF86AudioMute                       allow-when-locked=true { spawn "noctalia-shell" "ipc" "call" "volume" "muteOutput"; }
    XF86AudioMicMute                    allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"; }
    XF86AudioNext                       allow-when-locked=true { spawn-sh "playerctl next"; }
    XF86AudioPause                      allow-when-locked=true { spawn-sh "playerctl play-pause"; }
    XF86AudioPlay                       allow-when-locked=true { spawn-sh "playerctl play-pause"; }
    XF86AudioPrev                       allow-when-locked=true { spawn-sh "playerctl previous"; }

    // ─── Brightness Controls (via Noctalia for OSD) ───
    XF86MonBrightnessUp                 { spawn "noctalia-shell" "ipc" "call" "brightness" "increase"; }
    XF86MonBrightnessDown               { spawn "noctalia-shell" "ipc" "call" "brightness" "decrease"; }

    // ─── Window Movement and Focus ───
    MOD+Q                               { close-window; }

    MOD+LEFT                            { focus-column-left; }
    MOD+H                               { focus-column-left; }
    MOD+RIGHT                           { focus-column-right; }
    MOD+L                               { focus-column-right; }
    MOD+UP                              { focus-window-up; }
    MOD+K                               { focus-window-up; }
    MOD+DOWN                            { focus-window-down; }
    MOD+J                               { focus-window-down; }

    MOD+CTRL+LEFT                       { move-column-left; }
    MOD+CTRL+H                          { move-column-left; }
    MOD+CTRL+RIGHT                      { move-column-right; }
    MOD+CTRL+L                          { move-column-right; }
    MOD+CTRL+UP                         { move-window-up; }
    MOD+CTRL+K                          { move-window-up; }
    MOD+CTRL+DOWN                       { move-window-down; }
    MOD+CTRL+J                          { move-window-down; }

    MOD+HOME                            { focus-column-first; }
    MOD+END                             { focus-column-last; }
    MOD+CTRL+HOME                       { move-column-to-first; }
    MOD+CTRL+END                        { move-column-to-last; }

    MOD+SHIFT+LEFT                      { focus-monitor-left; }
    MOD+SHIFT+RIGHT                     { focus-monitor-right; }
    MOD+SHIFT+UP                        { focus-monitor-up; }
    MOD+SHIFT+DOWN                      { focus-monitor-down; }

    MOD+SHIFT+CTRL+LEFT                 { move-column-to-monitor-left; }
    MOD+SHIFT+CTRL+RIGHT                { move-column-to-monitor-right; }
    MOD+SHIFT+CTRL+UP                   { move-column-to-monitor-up; }
    MOD+SHIFT+CTRL+DOWN                 { move-column-to-monitor-down; }

    // ─── Workspace Switching ───
    MOD+WHEELSCROLLDOWN                 cooldown-ms=150 { focus-workspace-down; }
    MOD+WHEELSCROLLUP                   cooldown-ms=150 { focus-workspace-up; }
    MOD+CTRL+WHEELSCROLLDOWN            cooldown-ms=150 { move-column-to-workspace-down; }
    MOD+CTRL+WHEELSCROLLUP              cooldown-ms=150 { move-column-to-workspace-up; }

    MOD+WHEELSCROLLRIGHT                { focus-column-right; }
    MOD+WHEELSCROLLLEFT                 { focus-column-left; }
    MOD+CTRL+WHEELSCROLLRIGHT           { move-column-right; }
    MOD+CTRL+WHEELSCROLLLEFT            { move-column-left; }

    MOD+SHIFT+WHEELSCROLLDOWN           { focus-column-right; }
    MOD+SHIFT+WHEELSCROLLUP             { focus-column-left; }
    MOD+CTRL+SHIFT+WHEELSCROLLDOWN      { move-column-right; }
    MOD+CTRL+SHIFT+WHEELSCROLLUP        { move-column-left; }

    MOD+1                               { focus-workspace 1; }
    MOD+2                               { focus-workspace 2; }
    MOD+3                               { focus-workspace 3; }
    MOD+4                               { focus-workspace 4; }
    MOD+5                               { focus-workspace 5; }
    MOD+6                               { focus-workspace 6; }
    MOD+7                               { focus-workspace 7; }
    MOD+8                               { focus-workspace 8; }
    MOD+9                               { focus-workspace 9; }

    MOD+CTRL+1                          { move-column-to-workspace 1; }
    MOD+CTRL+2                          { move-column-to-workspace 2; }
    MOD+CTRL+3                          { move-column-to-workspace 3; }
    MOD+CTRL+4                          { move-column-to-workspace 4; }
    MOD+CTRL+5                          { move-column-to-workspace 5; }
    MOD+CTRL+6                          { move-column-to-workspace 6; }
    MOD+CTRL+7                          { move-column-to-workspace 7; }
    MOD+CTRL+8                          { move-column-to-workspace 8; }
    MOD+CTRL+9                          { move-column-to-workspace 9; }

    MOD+TAB                             { focus-workspace-previous; }

    // ─── Layout Controls ───
    MOD+CTRL+F                          { expand-column-to-available-width; }
    MOD+C                               { center-column; }
    MOD+CTRL+C                          { center-visible-columns; }
    MOD+MINUS                           { set-column-width "-10%"; }
    MOD+EQUAL                           { set-column-width "+10%"; }
    MOD+SHIFT+MINUS                     { set-window-height "-10%"; }
    MOD+SHIFT+EQUAL                     { set-window-height "+10%"; }

    // ─── Modes ───
    MOD+Shift+T                         { toggle-window-floating; }
    MOD+Shift+F                         { fullscreen-window; }
    MOD+F                               { maximize-column; }
    MOD+R                               { switch-preset-column-width; }
    MOD+W                               { toggle-column-tabbed-display; }

    // ─── Screenshots ───
    Print                       { screenshot; }
    Shift+Print                        { screenshot-screen; }
    CTRL+Print                        { screenshot-window; }

    // ─── Emergency Escape Key ───
    // Use this when a fullscreen app blocks your keybinds.
    // It disables any active keyboard shortcut inhibitor, restoring control.
    Mod+Shift+Escape                    allow-inhibiting=false { toggle-keyboard-shortcuts-inhibit; }

    // ─── Exit / Power ───
    CTRL+ALT+DELETE                     { quit; } // Also quits Niri
    MOD+SHIFT+P                         { power-off-monitors; } // Turn off screens (useful for OLED or privacy)
    MOD+D                               repeat=false { toggle-overview; }
}

// ────────────── Startup Applications ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Miscellaneous#spawn-sh-at-startup

    // Note: On NixOS, polkit agent is started via systemd user service (see desktop.nix)
    // spawn-sh-at-startup "polkit-gnome-authentication-agent-1 &"
    spawn-at-startup "noctalia-shell"
    spawn-sh-at-startup "sleep 2 && vicinae server"
    spawn-at-startup "xwayland-satellite"
    
    // Clipboard history (cliphist)
    spawn-at-startup "wl-paste" "--type" "text" "--watch" "cliphist" "store"
    spawn-at-startup "wl-paste" "--type" "image" "--watch" "cliphist" "store"
    
    spawn-sh-at-startup "steam -nochatui -nofriendsui -silent"
    // spawn-sh-at-startup "rclone mount google-drive: /home/pinj/google-drive/"
    prefer-no-csd // Disable program decorations
    screenshot-path null // Disable screenshot saving

// ────────────── Layout Settings ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Layout

    layout {
        gaps 5 // Gap between windows
        center-focused-column "never" // Don’t auto-center focused column
        background-color "transparent"


        preset-column-widths {
            proportion 0.33333
            proportion 0.5
            proportion 0.66667
        }

        focus-ring {
            width 2
            active-color "#6f6f6f"
            inactive-color "#6f6f6f"
        }

        shadow {
            softness 30
            spread 5
            offset x=0 y=5
            color "#0007"
        }

        struts {}
    }

// ────────────── Animation Settings ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Animations
    animations {
        workspace-switch {
            spring damping-ratio=1.0 stiffness=1000 epsilon=0.0001
        }
        window-open {
            duration-ms 200
            curve "ease-out-quad"
        }
        window-close {
            duration-ms 200
            curve "ease-out-cubic"
        }
        horizontal-view-movement {
            spring damping-ratio=1.0 stiffness=900 epsilon=0.0001
        }
        window-movement {
            spring damping-ratio=1.0 stiffness=800 epsilon=0.0001
        }
        window-resize {
            spring damping-ratio=1.0 stiffness=1000 epsilon=0.0001
        }
        config-notification-open-close {
            spring damping-ratio=0.6 stiffness=1200 epsilon=0.001
        }
        screenshot-ui-open {
            duration-ms 300
            curve "ease-out-quad"
        }
        overview-open-close {
            spring damping-ratio=1.0 stiffness=900 epsilon=0.0001
        }
    }

// ────────────── Window Rules ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Window-Rules

    window-rule {
        // Rounded corners for a modern look.
        geometry-corner-radius 20

        // Clips window contents to the rounded corner boundaries.
        clip-to-geometry true
    }

    window-rule {
        match app-id=r#"firefox$"# title="^Picture-in-Picture$"
        // Always float Firefox PiP windows
        open-floating true
    }

    window-rule {
        match app-id=r#"org.gnome.Nautilus$"#
        // Always float Nautilus windows
        open-floating true
        // Set Window Proportions
        max-height 1500
        max-width 1500
    }

    window-rule {
        match app-id=r#"ghostty$"#
        draw-border-with-background false
    }

// ────────────── Layer Rules ──────────────
    // Set the regular wallpaper on the backdrop.
    layer-rule {
      match namespace="^noctalia-wallpaper*"
      place-within-backdrop true
    }

    // Optionally, disable the workspace shadows in the overview.
    overview {
      workspace-shadow {
        off
      }
      zoom 0.25
    }
    // Disable the hot corners.
    gestures {
        hot-corners {
            off
        }
    }

// ────────────── Environment Variables ──────────────
// https://github.com/YaLTeR/niri/wiki/Configuration:-Miscellaneous#environment

    environment {
        DISPLAY ":1"
        ELECTRON_OZONE_PLATFORM_HINT "auto"
        QT_QPA_PLATFORM "wayland"
        QT_QPA_PLATFORMTHEME "qt6ct"
        QT_WAYLAND_DISABLE_WINDOWDECORATION "1"
        XDG_SESSION_TYPE "wayland"
        XDG_CURRENT_DESKTOP "niri"
        QS_ICON_THEME "Papirus-Dark"
        XCURSOR_THEME "Adwaita"
    }

    hotkey-overlay {
        skip-at-startup
    }

debug {
    // Allows notification actions and window activation from Noctalia.
    honor-xdg-activation-with-invalid-serial
}

// ────────────── Noctalia Matugen Colorconfig ──────────────
include "./noctalia.kdl"
