// This config is in the KDL format: https://kdl.dev
// "/-" comments out the following node.
// Check the wiki for a full description of the configuration:
// https://github.com/YaLTeR/niri/wiki/Configuration:-Overview

environment {
    DISPLAY ":0"
    ELECTRON_OZONE_PLATFORM_HINT "auto"
    QT_QPA_PLATFORMTHEME "qt5ct"
}

// https://github.com/YaLTeR/niri/wiki/Configuration:-Input
input {
    keyboard {
        xkb {
            // You can set rules, model, layout, variant and options.
            // For more information, see xkeyboard-config(7).
            layout "se,us"
            options "caps:escape,compose:menu"
        }
    }

    // Focus windows and outputs automatically when moving the mouse into them.
    focus-follows-mouse max-scroll-amount="0%"

{% if hostname == "sputnik" %}
    tablet {
        map-to-output "Microstep MSI MAG342CQR DB6H262101804"
    }
{% end %}
}

clipboard {
    disable-primary // disable middle-click paste
}

// https://github.com/YaLTeR/niri/wiki/Configuration:-Outputs
// `niri msg outputs`
output "Microstep MSI MAG342CQR DB6H262101804" {
    mode "3440x1440@144.000"
    position x=1152 y=387
    scale 1.25
}

output "Dell Inc. DELL U2724DE 1MV5L04" {
    mode "2560x1440@120.000"
    position x=0 y=0
    transform "270"
    scale 1.25

    // layout {
    //     gaps 0
    // }
}

output "eDP-1" {
    scale 1.8
    // position x=0 y=0
}

output "DP-2" {
    scale 1.25
}

// Settings that influence how windows are positioned and sized.
// Find more information on the wiki:
// https://github.com/YaLTeR/niri/wiki/Configuration:-Layout
layout {
    // Set gaps around windows in logical pixels.
    gaps 12

    // Struts shrink the area occupied by windows, similarly to layer-shell panels.
    // You can think of them as a kind of outer gaps. They are set in logical pixls.
    // Left and right struts will cause the next window to the side to always be visible.
    // Top and bottom struts will simply add outer gaps in addition to the area occupied by
    // layer-shell panels and regular gaps.
    struts {
        left -12
        right -12
        top -12
        bottom -12
    }

    // When to center a column when changing focus, options are:
    // - "never", default behavior, focusing an off-screen column will keep at the left
    //   or right edge of the screen.
    // - "always", the focused column will always be centered.
    // - "on-overflow", focusing a column will center it if it doesn't fit
    //   together with the previously focused column.
    center-focused-column "never"

    // You can customize the widths that "switch-preset-column-width" (Mod+R) toggles between.
    preset-column-widths {
        fixed 948 // roughly fits a single vim buffer
        //proportion 0.33333
        proportion 0.5
        proportion 0.66667
    }

    // You can change the default width of the new windows.
    default-column-width { proportion 0.5; }
    // If you leave the brackets empty, the windows themselves will decide their initial width.
    // default-column-width {}

    focus-ring {
        off
    }

    shadow {
        on
    }

    // You can also add a border. It's similar to the focus ring, but always visible.
    border {
        // The settings are the same as for the focus ring.
        // If you enable the border, you probably want to disable the focus ring.
        width 2
        active-color "#f65d76" // akira pink
        inactive-color "#505050"

        // active-gradient from="#ffbb66" to="#ffc880" angle=45 relative-to="workspace-view"
        // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view"
    }
}

spawn-at-startup "swayidle" "-w" // ~/.config/swayidle/config
spawn-at-startup "dms" "run"
spawn-at-startup "tailscale" "systray"

// Uncomment this line to ask the clients to omit their client-side decorations if possible.
// If the client will specifically ask for CSD, the request will be honored.
// Additionally, clients will be informed that they are tiled, removing some rounded corners.
prefer-no-csd

// You can change the path where screenshots are saved.
// A ~ at the front will be expanded to the home directory.
// The path is formatted with strftime(3) to give you the screenshot date and time.
screenshot-path "~/screenshots/%Y-%m-%d_%H-%M-%S.png"

// You can also set this to null to disable saving screenshots to disk.
// screenshot-path null

// Animation settings.
// The wiki explains how to configure individual animations:
// https://github.com/YaLTeR/niri/wiki/Configuration:-Animations
animations {
    // Slow down all animations by this factor. Values below 1 speed them up instead.
    // slowdown 3.0

    workspace-switch {
        spring damping-ratio=0.8 stiffness=500 epsilon=0.0001
    }

    window-movement {
        spring damping-ratio=0.8 stiffness=500 epsilon=0.0001
    }

    window-resize {
        spring damping-ratio=0.5 stiffness=500 epsilon=0.0001
    }

    window-open {
        duration-ms 600
        custom-shader r#"
// make the window fall into place with a slight rotation
vec4 slide_in_from_right(vec3 coords_geo, vec3 size_geo) {
    // For this shader, set animation curve to linear for best results.

    // Simulate decelleration: square the (linear) progress.
    float progress = 1.0 - niri_clamped_progress * niri_clamped_progress;

    // Get our rotation pivot point coordinates at the bottom center of the window.
    vec2 coords = (coords_geo.xy - vec2(0.5, 1.0)) * size_geo.xy;

    // Move the window in from the right
    coords.x -= progress * 200.0;

    // Randomize rotation direction and maximum angle.
    float random = (niri_random_seed - 0.5) / 2.0;
    random = sign(random) - random;
    float max_angle = 0.05 * random;

    // Rotate the window around our pivot point.
    float angle = progress * max_angle;
    mat2 rotate = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
    coords = rotate * coords;

    // Transform the coordinates back.
    coords_geo = vec3(coords / size_geo.xy + vec2(0.5, 1.0), 1.0);

    // Sample the window texture.
    vec3 coords_tex = niri_geo_to_tex * coords_geo;
    vec4 color = texture2D(niri_tex, coords_tex.st);

    // Multiply by alpha to fade out.
    return color * niri_clamped_progress;
}

// this is the main function
vec4 open_color(vec3 coords_geo, vec3 size_geo) {
    return slide_in_from_right(coords_geo, size_geo);
}
        "#
    }

    window-close {
        duration-ms 600
        custom-shader r#"
// make the window "fall down" with slight rotation.
vec4 fall_and_rotate(vec3 coords_geo, vec3 size_geo) {
    // For this shader, set animation curve to linear for best results.

    // Simulate an accelerated fall: square the (linear) progress.
    float progress = niri_clamped_progress * niri_clamped_progress;

    // Get our rotation pivot point coordinates at the bottom center of the window.
    vec2 coords = (coords_geo.xy - vec2(0.5, 1.0)) * size_geo.xy;

    // Move the window down to simulate a fall.
    coords.y -= progress * 200.0;

    // Randomize rotation direction and maximum angle.
    float random = (niri_random_seed - 0.5) / 2.0;
    random = sign(random) - random;
    float max_angle = 0.05 * random;

    // Rotate the window around our pivot point.
    float angle = progress * max_angle;
    mat2 rotate = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
    coords = rotate * coords;

    // Transform the coordinates back.
    coords_geo = vec3(coords / size_geo.xy + vec2(0.5, 1.0), 1.0);

    // Sample the window texture.
    vec3 coords_tex = niri_geo_to_tex * coords_geo;
    vec4 color = texture2D(niri_tex, coords_tex.st);

    // Multiply by alpha to fade out.
    return color * (1.0 - niri_clamped_progress);
}

vec4 close_color(vec3 coords_geo, vec3 size_geo) {
    return fall_and_rotate(coords_geo, size_geo);
}
        "#
    }
}

// Window rules let you adjust behavior for individual windows.
// Find more information on the wiki:
// https://github.com/YaLTeR/niri/wiki/Configuration:-Window-Rules

window-rule {
    geometry-corner-radius 22
    clip-to-geometry true
}

// Indicate screencasted windows with red colors.
window-rule {
    match is-window-cast-target=true

    focus-ring {
        active-color "#f38ba8"
        inactive-color "#7d0d2d"
    }

    border {
        inactive-color "#7d0d2d"
    }

    shadow {
        color "#7d0d2d70"
    }

    tab-indicator {
        active-color "#f38ba8"
        inactive-color "#7d0d2d"
    }
}

window-rule {
    match title="Volume Indicator"
    geometry-corner-radius 35
    min-width 300
    max-width 300
    min-height 100
    max-height 100
    open-floating true
    default-floating-position x=35 y=35 relative-to="top-right"
}

window-rule {
    match app-id="Mullvad VPN"
    default-floating-position x=5 y=10 relative-to="bottom-left"
}

// block out password managers from screen capture.
window-rule {
    match app-id=r#"^org\.keepassxc\.KeePassXC$"#
    match app-id=r#"^org\.gnome\.World\.Secrets$"#

    block-out-from "screen-capture"
}

window-rule {
    match title="Virtual Machine Manager"
    match app-id="virt-manager"
    //max-width 400
}

window-rule {
    match app-id="com.nextcloud.desktopclient.nextcloud"
    open-floating true
    default-floating-position x=5 y=10 relative-to="bottom-left"
}

binds {
    // Keys consist of modifiers separated by + signs, followed by an XKB key name
    // in the end. To find an XKB name for a particular key, you may use a program
    // like wev.
    //
    // "Mod" is a special modifier equal to Super when running on a TTY, and to Alt
    // when running as a winit window.
    //
    // Most actions that you can bind here can also be invoked programmatically with
    // `niri msg action do-something`.

    // Mod-Shift-/, which is usually the same as Mod-?,
    // shows a list of important hotkeys.
    Mod+Shift+Plus { show-hotkey-overlay; }

    Mod+Return { spawn "alacritty"; }
    Mod+Plus { spawn "loginctl" "lock-session"; }
    Mod+E { spawn "loginctl" "lock-session"; }

    // TODO: why do i have to use spawn-sh?
    Mod+D { spawn "dms" "ipc" "call" "widget" "toggle" "launcherButton"; }

    // You can also use a shell:
    // Mod+T { spawn "bash" "-c" "notify-send hello && exec alacritty"; }

    // Example volume keys mappings for PipeWire & WirePlumber.
    // The allow-when-locked=true property makes them work even when the session is locked.
    XF86AudioRaiseVolume  allow-when-locked=true { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1+"; }
    XF86AudioLowerVolume  allow-when-locked=true { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1-"; }
    XF86AudioMute         allow-when-locked=true { spawn "wpctl" "set-mute" "@DEFAULT_AUDIO_SINK@" "toggle"; }
    XF86AudioMicMute      allow-when-locked=true { spawn "wpctl" "set-mute" "@DEFAULT_AUDIO_SOURCE@" "toggle"; }
    XF86MonBrightnessDown allow-when-locked=true { spawn "brightnessctl" "--min-value=10" "set" "10%-"; }
    XF86MonBrightnessUp   allow-when-locked=true { spawn "brightnessctl" "--min-value=10" "set" "+10%"; }

    Mod+Q { close-window; }

    Mod+Space { switch-focus-between-floating-and-tiling; }
    Mod+Ctrl+Space { toggle-window-floating; }

    Mod+Left  { focus-column-left; }
    Mod+Down  { focus-window-down; }
    Mod+Up    { focus-window-up; }
    Mod+Right { focus-column-right; }
    Mod+H     { focus-column-left; }
    Mod+J     { focus-window-or-workspace-down; }
    Mod+K     { focus-window-or-workspace-up; }
    Mod+L     { focus-column-right; }

    Mod+Ctrl+Left  { move-column-left; }
    Mod+Ctrl+Down  { move-window-down; }
    Mod+Ctrl+Up    { move-window-up; }
    Mod+Ctrl+Right { move-column-right; }
    Mod+Ctrl+H     { move-column-left; }
    Mod+Ctrl+J     { move-window-down-or-to-workspace-down; }
    Mod+Ctrl+K     { move-window-up-or-to-workspace-up; }
    Mod+Ctrl+L     { move-column-right; }

    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+Down  { focus-monitor-down; }
    Mod+Shift+Up    { focus-monitor-up; }
    Mod+Shift+Right { focus-monitor-right; }
    Mod+Shift+H     { focus-monitor-left; }
    Mod+Shift+J     { focus-monitor-down; }
    Mod+Shift+K     { focus-monitor-up; }
    Mod+Shift+L     { focus-monitor-right; }

    Mod+Shift+Ctrl+Left  { move-column-to-monitor-left; }
    Mod+Shift+Ctrl+Down  { move-column-to-monitor-down; }
    Mod+Shift+Ctrl+Up    { move-column-to-monitor-up; }
    Mod+Shift+Ctrl+Right { move-column-to-monitor-right; }
    Mod+Shift+Ctrl+H     { move-column-to-monitor-left; }
    Mod+Shift+Ctrl+J     { move-column-to-monitor-down; }
    Mod+Shift+Ctrl+K     { move-column-to-monitor-up; }
    Mod+Shift+Ctrl+L     { move-column-to-monitor-right; }

    Mod+Page_Down      { focus-workspace-down; }
    Mod+Page_Up        { focus-workspace-up; }
    Mod+U              { focus-workspace-down; }
    Mod+I              { focus-workspace-up; }
    Mod+Ctrl+Page_Down { move-column-to-workspace-down; }
    Mod+Ctrl+Page_Up   { move-column-to-workspace-up; }
    Mod+Ctrl+U         { move-column-to-workspace-down; }
    Mod+Ctrl+I         { move-column-to-workspace-up; }

    Mod+Shift+Page_Down { move-workspace-down; }
    Mod+Shift+Page_Up   { move-workspace-up; }
    Mod+Shift+U         { move-workspace-down; }
    Mod+Shift+I         { move-workspace-up; }

    Mod+Backspace       { toggle-overview; }

    // You can bind mouse wheel scroll ticks using the following syntax.
    // These binds will change direction based on the natural-scroll setting.
    //
    // To avoid scrolling through workspaces really fast, you can use
    // the cooldown-ms property. The bind will be rate-limited to this value.
    // You can set a cooldown on any bind, but it's most useful for the wheel.
    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; }

    // Usually scrolling up and down with Shift in applications results in
    // horizontal scrolling; these binds replicate that.
    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; }

    // Similarly, you can bind touchpad scroll "ticks".
    // Touchpad scrolling is continuous, so for these binds it is split into
    // discrete intervals.
    // These binds are also affected by touchpad's natural-scroll, so these
    // example binds are "inverted", since we have natural-scroll enabled for
    // touchpads by default.
    // Mod+TouchpadScrollDown { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.02+"; }
    // Mod+TouchpadScrollUp   { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.02-"; }

    // You can refer to workspaces by index. However, keep in mind that
    // niri is a dynamic workspace system, so these commands are kind of
    // "best effort". Trying to refer to a workspace index bigger than
    // the current workspace count will instead refer to the bottommost
    // (empty) workspace.
    //
    // For example, with 2 workspaces + 1 empty, indices 3, 4, 5 and so on
    // will all refer to the 3rd workspace.
    Mod+1 { spawn "wmutils" "switch-workspace" "1"; }
    Mod+2 { spawn "wmutils" "switch-workspace" "2"; }
    Mod+3 { spawn "wmutils" "switch-workspace" "3"; }
    Mod+4 { spawn "wmutils" "switch-workspace" "4"; }
    Mod+5 { spawn "wmutils" "switch-workspace" "5"; }
    Mod+6 { spawn "wmutils" "switch-workspace" "6"; }
    Mod+7 { spawn "wmutils" "switch-workspace" "7"; }
    Mod+8 { spawn "wmutils" "switch-workspace" "8"; }
    Mod+9 { spawn "wmutils" "switch-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; }

    // Alternatively, there are commands to move just a single window:
    // Mod+Ctrl+1 { move-window-to-workspace 1; }

    // Switches focus between the current and the previous workspace.
    // Mod+Tab { focus-workspace-previous; }

    Mod+Comma  { consume-window-into-column; }
    Mod+Period { expel-window-from-column; }
    Mod+W      { toggle-column-tabbed-display; }

    // There are also commands that consume or expel a single window to the side.
    // Mod+BracketLeft  { consume-or-expel-window-left; }
    // Mod+BracketRight { consume-or-expel-window-right; }

    Mod+R { switch-preset-column-width; }
    Mod+F { maximize-column; }
    Mod+Shift+F { fullscreen-window; }
    Mod+C { center-column; }

    // Finer width adjustments.
    // This command can also:
    // * set width in pixels: "1000"
    // * adjust width in pixels: "-5" or "+5"
    // * set width as a percentage of screen width: "25%"
    // * adjust width as a percentage of screen width: "-10%" or "+10%"
    // Pixel sizes use logical, or scaled, pixels. I.e. on an output with scale 2.0,
    // set-column-width "100" will make the column occupy 200 physical screen pixels.
    Mod+Minus { set-column-width "-10%"; }
    Mod+Equal { set-column-width "+10%"; }

    // Finer height adjustments when in column with other windows.
    Mod+Shift+Minus { set-window-height "-10%"; }
    Mod+Shift+Equal { set-window-height "+10%"; }

    // Actions to switch layouts.
    // Note: if you uncomment these, make sure you do NOT have
    // a matching layout switch hotkey configured in xkb options above.
    // Having both at once on the same hotkey will break the switching,
    // since it will switch twice upon pressing the hotkey (once by xkb, once by niri).
    // Mod+Space       { switch-layout "next"; }
    // Mod+Shift+Space { switch-layout "prev"; }

    Print { screenshot; }
    Ctrl+Print { screenshot-screen; }
    Alt+Print { screenshot-window; }

    // Switch to the next keyboard layout
    Mod+Tab { spawn "wmutils" "next-keyboard-layout"; }

    // The quit action will show a confirmation dialog to avoid accidental exits.
    Mod+Shift+E { quit; }

    // Powers off the monitors. To turn them back on, do any input like
    // moving the mouse or pressing any other key.
    // Mod+Shift+P { power-off-monitors; }

    // Dynamic screencasting target
    Mod+Slash { clear-dynamic-cast-target; }
    Mod+Ctrl+Slash { set-dynamic-cast-window; }
    Mod+Shift+Slash { set-dynamic-cast-monitor; }

    // Debugging
    Mod+Shift+Ctrl+T { toggle-debug-tint; }
    Mod+Shift+Ctrl+O { debug-toggle-opaque-regions; }
    Mod+Shift+Ctrl+D { debug-toggle-damage; }
}

include "dms/alttab.kdl"
include "dms/binds.kdl"
include "dms/colors.kdl"
include "dms/cursor.kdl"
//include "dms/layout.kdl"
include "dms/outputs.kdl"
include "dms/wpblur.kdl"
