-- #######################################################################################
-- AUTOGENERATED HYPR CONFIG.
-- PLEASE USE THE CONFIG PROVIDED IN THE GIT REPO /example/hyprland.lua AND EDIT IT,
-- OR EDIT THIS ONE ACCORDING TO THE WIKI INSTRUCTIONS.
-- #######################################################################################

--
-- Please note not all available settings / options are set here.
-- For a full list, see the wiki
--

-- autogenerated = 1 -- remove this line to remove the warning

-- See https://wiki.hyprland.org/Configuring/Monitors/
hl.monitor({
    output   = "",
    mode     = "preferred",
    position = "auto",
    scale    = 1,
})

-- See https://wiki.hyprland.org/Configuring/Keywords/ for more

-------------------
---- AUTOSTART ----
-------------------

hl.on("hyprland.start", function()
    -- Execute your favorite apps at launch
    hl.exec_cmd("garuda-welcome")

    -- Add networkmanager applet to tray in waybar
    hl.exec_cmd("nm-applet --indicator")

    -- Set keyboard layout
    hl.exec_cmd(".local/bin/garuda-locale.sh")

    -- Set monitor values. For own values, please comment with #
    hl.exec_cmd(".local/bin/mon.sh")

    -- background
    hl.exec_cmd("wpaperd")

    -- status bar
    hl.exec_cmd("waybar")

    hl.exec_cmd("mako")
    hl.exec_cmd("/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1")

    -- experimental(might work might won't)
    hl.exec_cmd("xrdb -load ~/.Xresources")

    -- Clipboard Manager
    hl.exec_cmd("wl-paste --type text --watch cliphist store")  -- Stores only text data
    hl.exec_cmd("wl-paste --type image --watch cliphist store") -- Stores only image data

    -- Use gtk-settings
    hl.exec_cmd("apply-gsettings")

    -- low battery warning script
    hl.exec_cmd(".local/bin/battery_warn.sh")

    -- Select Kitty font size once from monitor resolution.
    hl.exec_cmd(".local/bin/kitty-font-autosize.sh")

    -- Hypr Ecosystem
    -- hypridle
    hl.exec_cmd("hypridle")
end)

-- Source a Lua file (multi-file configs)
-- require("myColors")

-------------------------------
---- ENVIRONMENT VARIABLES ----
-------------------------------

-- some env variables
hl.env("XDG_CURRENT_DESKTOP", "Hyprland")
-- we usually prefer things to be set up through script
-- have a look at hyprstart file


-- For all categories, see https://wiki.hyprland.org/Configuring/Variables/
-- For kb_model check /usr/share/X11/xkb/rules/base.lst
hl.config({
    input = {
        kb_layout  = "us",
        kb_variant = "nodeadkeys",
        kb_model   = "pc105",
        kb_options = "",
        kb_rules   = "",

        numlock_by_default = true,
        follow_mouse       = 1,

        touchpad = {
            natural_scroll       = true,
            tap_to_click         = true,
            disable_while_typing = true,
        },

        sensitivity = 0, -- -1.0 - 1.0, 0 means no modification.
    },
})

hl.config({
    general = {
        -- See https://wiki.hyprland.org/Configuring/Variables/ for more

        gaps_in     = 5,
        gaps_out    = 10,
        border_size = 2,

        col = {
            active_border   = { colors = { "rgba(33ccffee)", "rgba(8f00ffee)" }, angle = 45 },
            inactive_border = "rgba(595959aa)",
        },

        layout = "dwindle",
    },
})

hl.config({
    decoration = {
        -- See https://wiki.hyprland.org/Configuring/Variables/ for more

        rounding = 10,

        blur = {
            enabled = true,
            size    = 5,
            passes  = 3,
        },

        shadow = {
            enabled      = true,
            range        = 4,
            render_power = 3,
            color        = "rgba(1a1a1aee)",
        },
    },
})

-- Apply blur universally to all layer-shell surfaces
hl.layer_rule({
    name = "blur-all-layers",

    match = {
        namespace = ".*",
    },

    blur         = true,
    blur_popups  = true,
    ignore_alpha = 0.10,
})

hl.config({
    animations = {
        enabled = true,
    },
})

-- Some default animations, see https://wiki.hyprland.org/Configuring/Animations/ for more
hl.curve("myBezier", { type = "bezier", points = { { 0.05, 0.9 }, { 0.1, 1.05 } } })

hl.animation({ leaf = "windows",    enabled = true, speed = 7,  bezier = "myBezier" })
hl.animation({ leaf = "windowsOut", enabled = true, speed = 7,  bezier = "default",  style = "popin 80%" })
hl.animation({ leaf = "border",     enabled = true, speed = 10, bezier = "default" })
hl.animation({ leaf = "fade",       enabled = true, speed = 7,  bezier = "default" })
hl.animation({ leaf = "workspaces", enabled = true, speed = 6,  bezier = "default" })

hl.config({
    dwindle = {
        -- See https://wiki.hyprland.org/Configuring/Dwindle-Layout/ for more
        -- pseudotile was removed in newer Hyprland versions.
        -- Pseudotiling is still toggled with mainMod + P below.
        preserve_split = true, -- you probably want this
    },
})

hl.config({
    master = {
        -- See https://wiki.hyprland.org/Configuring/Master-Layout/ for more
        new_status = "master",
    },
})

hl.gesture({
    fingers   = 3,
    direction = "up",
    action    = function()
        hl.exec_cmd("garuda-toolbox")
    end,
})

hl.gesture({
    fingers   = 3,
    direction = "down",
    action    = function()
        hl.exec_cmd("nwg-drawer -mb 10 -mr 10 -ml 10 -mt 10")
    end,
})

hl.gesture({
    fingers   = 3,
    direction = "horizontal",
    action    = "workspace",
})

hl.config({
    misc = {
        disable_hyprland_logo = true,
    },
})

-- Example per-device config
-- See https://wiki.hyprland.org/Configuring/Keywords/
-- executing for more

-- hl.device({
--     name        = "epic mouse V1",
--     sensitivity = -0.5,
-- })


--------------------------------
---- WINDOWS AND WORKSPACES ----
--------------------------------

-- See https://wiki.hyprland.org/Configuring/Window-Rules/ for more
-- Example windowrule v1
hl.window_rule({ match = { class = "garuda-assistant" },         float = true })
hl.window_rule({ match = { class = "garuda-boot-options" },      float = true })
hl.window_rule({ match = { class = "garuda-boot-repair" },       float = true })
hl.window_rule({ match = { class = "garuda-gamer" },             float = true })
hl.window_rule({ match = { class = "garuda-network-assistant" }, float = true })
hl.window_rule({ match = { class = "garuda-settings-manager" },  float = true })
hl.window_rule({ match = { class = "garuda-welcome" },           float = true })
hl.window_rule({ match = { class = "calamares" },                float = true })

-- Example windowrule v2
-- windowrulev2 = float,class:^(kitty)$,title:^(kitty)$
-- Lua equivalent:
-- hl.window_rule({ match = { class = "^(kitty)$", title = "^(kitty)$" }, float = true })


---------------------
---- KEYBINDINGS ----
---------------------

-- See https://wiki.hyprland.org/Configuring/Keywords/ for more
local mainMod = "SUPER"

local function bind_exec(keys, cmd, opts)
    hl.bind(keys, hl.dsp.exec_cmd(cmd), opts)
end

-- Example binds, see https://wiki.hyprland.org/Configuring/Binds/ for more
bind_exec(mainMod .. " + SHIFT + R", "hyprctl reload")
bind_exec(mainMod .. " + code:36", "kitty") -- 36 = Enter
bind_exec(mainMod .. " + T", "kitty")
hl.bind(mainMod .. " + Q", hl.dsp.window.close())
bind_exec(mainMod .. " + SHIFT + E", "nwgbar")
bind_exec(mainMod .. " + N", "thunar")
hl.bind(mainMod .. " + SHIFT + code:65", hl.dsp.window.float({ action = "toggle" }))
bind_exec("SUPER + D", "pkill wofi || wofi --normal-window --show drun --allow-images", { release = true })
bind_exec(mainMod .. " + E", "emote")
bind_exec(mainMod .. " + SHIFT + D", "nwg-drawer -mb 10 -mr 10 -ml 10 -mt 10")
hl.bind(mainMod .. " + P", hl.dsp.window.pseudo())       -- dwindle
hl.bind(mainMod .. " + SHIFT + P", hl.dsp.layout("togglesplit")) -- dwindle
bind_exec(mainMod .. " + V", "cliphist list | wofi --dmenu | cliphist decode | wl-copy")
bind_exec(mainMod .. " + B", "brightnessctl -c backlight set 1%-")
bind_exec(mainMod .. " + SHIFT + B", "brightnessctl -c backlight set +1%")


-- Mainmod + Function keys
bind_exec(mainMod .. " + F1",  "firedragon")
bind_exec(mainMod .. " + F2",  "thunderbird")
bind_exec(mainMod .. " + F3",  "thunar")
bind_exec(mainMod .. " + F4",  "geany")
bind_exec(mainMod .. " + F5",  "github-desktop")
bind_exec(mainMod .. " + F6",  "gparted")
bind_exec(mainMod .. " + F7",  "inkscape")
bind_exec(mainMod .. " + F8",  "blender")
bind_exec(mainMod .. " + F9",  "meld")
bind_exec(mainMod .. " + F10", "joplin-desktop")
bind_exec(mainMod .. " + F11", "snapper-tools")
bind_exec(mainMod .. " + F12", "galculator")

-- Move focus with mainMod + arrow keys
hl.bind(mainMod .. " + left",  hl.dsp.focus({ direction = "l" }))
hl.bind(mainMod .. " + H",     hl.dsp.focus({ direction = "l" }))
hl.bind(mainMod .. " + right", hl.dsp.focus({ direction = "r" }))
hl.bind(mainMod .. " + L",     hl.dsp.focus({ direction = "r" }))
hl.bind(mainMod .. " + up",    hl.dsp.focus({ direction = "u" }))
hl.bind(mainMod .. " + K",     hl.dsp.focus({ direction = "u" }))
hl.bind(mainMod .. " + down",  hl.dsp.focus({ direction = "d" }))
hl.bind(mainMod .. " + J",     hl.dsp.focus({ direction = "d" }))

-- Switch workspaces with mainMod + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind(mainMod .. " + " .. key, hl.dsp.focus({ workspace = i }))
end

-- Move to workspace with focused container with ALT + SHIFT + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind("ALT + SHIFT + " .. key, hl.dsp.window.move({ workspace = i, follow = true }))
end

-- Move active window to a workspace with mainMod + SHIFT + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind(mainMod .. " + SHIFT + " .. key, hl.dsp.window.move({ workspace = i, follow = false }))
end

-- Scroll through existing workspaces with mainMod + scroll
hl.bind(mainMod .. " + mouse_down", hl.dsp.focus({ workspace = "e+1" }))
hl.bind(mainMod .. " + mouse_up",   hl.dsp.focus({ workspace = "e-1" }))

-- Move/resize windows with mainMod + LMB/RMB and dragging
hl.bind(mainMod .. " + mouse:272", hl.dsp.window.drag(),   { mouse = true })
hl.bind(mainMod .. " + mouse:273", hl.dsp.window.resize(), { mouse = true })

-- status bar
-- Converted from old line:
-- layerrule = match:class waybar, match:float yes
-- NOTE: layer rules match namespace, not class/float. Kept the useful waybar rule below.
hl.layer_rule({
    match        = { namespace = "waybar" },
    ignore_alpha = 0.5,
})

-- set volume (laptops only and may or may not work on PCs)
bind_exec('code:122', 'pamixer --decrease 5; notify-send " Volume: "$(pamixer --get-volume) -t 500')
bind_exec('code:123', 'pamixer --increase 5; notify-send " Volume: "$(pamixer --get-volume) -t 500')
bind_exec('code:121', 'pamixer --toggle-mute; notify-send " Volume: Toggle-mute" -t 500')
bind_exec('XF86AudioMicMute', 'pactl set-source-mute @DEFAULT_SOURCE@ toggle; notify-send "System Mic: Toggle-mute" -t 500')

-- other bindings
bind_exec(mainMod .. " + O", "firedragon")
hl.bind(mainMod .. " + M", hl.dsp.window.fullscreen({ mode = "maximized" }))
hl.bind(mainMod .. " + F", hl.dsp.window.fullscreen({ mode = "fullscreen" }))
hl.bind(mainMod .. " + SHIFT + F", hl.dsp.window.fullscreen_state({ internal = 0, client = 2 }))
bind_exec("code:232", "brightnessctl -c backlight set 5%-")
bind_exec("code:233", "brightnessctl -c backlight set +5%")
bind_exec(mainMod .. " + SHIFT + C", "killall -9 wpaperd && wpaperd")

-- https://github.com/hyprwm/contrib/blob/main/grimblast/grimblast.1.scd
-- Print: All outputs
-- SHIFT+Print: Select area
-- $mainMod+Print: Current window
-- $mainMod+Shift+Print: Current output

hl.bind("Print", hl.dsp.exec_cmd("grimblast save screen && notify-send Screenshot captured"))
hl.bind("SHIFT + Print", hl.dsp.exec_cmd("grimblast save area && notify-send Selected\\ area captured"))
hl.bind(mainMod .. " + Print", hl.dsp.exec_cmd("grimblast save active && notify-send Active\\ window captured"))
hl.bind(mainMod .. " + SHIFT + Print", hl.dsp.exec_cmd("grimblast output active && notify-send Output captured"))

-- for resizing window
-- will switch to a submap called resize
hl.bind(mainMod .. " + R", hl.dsp.submap("resize"))

-- will start a submap called "resize"
hl.define_submap("resize", function()
    -- sets repeatable binds for resizing the active window
    hl.bind("right", hl.dsp.window.resize({ x = 50,  y = 0,   relative = true }), { repeating = true })
    hl.bind("L",     hl.dsp.window.resize({ x = 50,  y = 0,   relative = true }), { repeating = true })
    hl.bind("left",  hl.dsp.window.resize({ x = -50, y = 0,   relative = true }), { repeating = true })
    hl.bind("H",     hl.dsp.window.resize({ x = -50, y = 0,   relative = true }), { repeating = true })
    hl.bind("up",    hl.dsp.window.resize({ x = 0,   y = -50, relative = true }), { repeating = true })
    hl.bind("K",     hl.dsp.window.resize({ x = 0,   y = -50, relative = true }), { repeating = true })
    hl.bind("down",  hl.dsp.window.resize({ x = 0,   y = 50,  relative = true }), { repeating = true })
    hl.bind("J",     hl.dsp.window.resize({ x = 0,   y = 50,  relative = true }), { repeating = true })

    -- use reset to go back to the global submap
    hl.bind("escape", hl.dsp.submap("reset"))
end)

-- will reset the submap, meaning end the current one and return to the global one
-- submap=reset

-- to move window
hl.bind(mainMod .. " + SHIFT + up",    hl.dsp.window.move({ direction = "u" }))
hl.bind(mainMod .. " + SHIFT + K",     hl.dsp.window.move({ direction = "u" }))
hl.bind(mainMod .. " + SHIFT + down",  hl.dsp.window.move({ direction = "d" }))
hl.bind(mainMod .. " + SHIFT + J",     hl.dsp.window.move({ direction = "d" }))
hl.bind(mainMod .. " + SHIFT + left",  hl.dsp.window.move({ direction = "l" }))
hl.bind(mainMod .. " + SHIFT + H",     hl.dsp.window.move({ direction = "l" }))
hl.bind(mainMod .. " + SHIFT + right", hl.dsp.window.move({ direction = "r" }))
hl.bind(mainMod .. " + SHIFT + L",     hl.dsp.window.move({ direction = "r" }))

-- special workspace

hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.window.move({ workspace = "+0", follow = true }))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.window.move({ workspace = "special:magic", follow = true }))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))

-- other blurings
-- blurls = wofi
-- blurls = thunar
-- blurls = gedit
-- blurls = gtk-layer-shell # for nwg-drawer
-- blurls = catfish
hl.layer_rule({ match = { namespace = "wofi" },            blur = true })
hl.layer_rule({ match = { namespace = "thunar" },          blur = true })
hl.layer_rule({ match = { namespace = "gedit" },           blur = true })
hl.layer_rule({ match = { namespace = "gtk-layer-shell" }, blur = true }) -- for nwg-drawer
hl.layer_rule({ match = { namespace = "catfish" },         blur = true })

-- window rules
-- nwgbar :
hl.layer_rule({
    match      = { namespace = ".*nwgbar.*" },
    blur       = true,
    dim_around = false,
})

hl.window_rule({ match = { title = "thunar" },  opacity = "0.85 override 0.85 override" })
hl.window_rule({ match = { title = "gedit" },   opacity = "0.85 override 0.85 override" })
hl.window_rule({ match = { title = "catfish" }, opacity = "0.85 override 0.85 override" })
hl.window_rule({ match = { title = "wofi" },    stay_focused = true })

-- window rules with evaluation
hl.window_rule({
    name    = "windowrule-1",
    opacity = "0.85 0.85",
    match   = { float = true },
})

-- video play paues bindings
bind_exec("code:172", "playerctl play-pause")
bind_exec("code:171", "playerctl next")
bind_exec("code:173", "playerctl previous")

-- Performance Tweaks (For less power consumption without sacrificing performance)
-- See : https://wiki.hyprland.org/Configuring/Performance/
-- Hyprland 0.55+ uses debug.vfr. It defaults to true; keep it explicit here.
hl.config({
    debug = {
        vfr = true,
    },
})

-- enable G-Hyprland
bind_exec(mainMod .. " + SHIFT + G", "kitty -e ~/.local/bin/bear/implement_gum.sh enable")

-- Install Garuda Hyprland
bind_exec(mainMod .. " + I", ".local/bin/calamares.sh")
