refactor(hyprland): simplify keybind action schema
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
{ lib, ... }:
|
||||
let
|
||||
actionMatchesType =
|
||||
actionType = lib.types.addCheck (lib.types.attrsOf lib.types.anything) isAction;
|
||||
|
||||
isAction =
|
||||
action:
|
||||
let
|
||||
required = actionFields.${action.type};
|
||||
in
|
||||
lib.all (field: action.${field} != null) required
|
||||
&& lib.all (field: lib.elem field required || action.${field} == null) actionOptions;
|
||||
builtins.hasAttr "type" action
|
||||
&& builtins.hasAttr action.type actionFields
|
||||
&& (
|
||||
let
|
||||
fields = builtins.getAttr action.type actionFields;
|
||||
allowedFields = [ "type" ] ++ fields;
|
||||
in
|
||||
lib.all (field: builtins.hasAttr field action) fields
|
||||
&& lib.all (field: lib.elem field allowedFields) (builtins.attrNames action)
|
||||
&& lib.all (field: actionFieldChecks.${field} action.${field}) fields
|
||||
);
|
||||
|
||||
actionFields = {
|
||||
exec = [ "command" ];
|
||||
@@ -25,14 +33,21 @@ let
|
||||
"move-to-workspace" = [ "workspace" ];
|
||||
};
|
||||
|
||||
actionOptions = [
|
||||
"command"
|
||||
"direction"
|
||||
"x"
|
||||
"y"
|
||||
"workspace"
|
||||
"message"
|
||||
];
|
||||
actionFieldChecks = {
|
||||
command = builtins.isString;
|
||||
direction =
|
||||
direction:
|
||||
lib.elem direction [
|
||||
"left"
|
||||
"down"
|
||||
"up"
|
||||
"right"
|
||||
];
|
||||
x = builtins.isInt;
|
||||
y = builtins.isInt;
|
||||
workspace = builtins.isInt;
|
||||
message = builtins.isString;
|
||||
};
|
||||
in
|
||||
{
|
||||
checks = [
|
||||
@@ -40,10 +55,6 @@ in
|
||||
assertion = entries: lib.all (entry: entry.value.keys != [ ]) entries;
|
||||
message = "Desktop keybinds must set at least one key.";
|
||||
}
|
||||
{
|
||||
assertion = entries: lib.all (entry: actionMatchesType entry.value.action) entries;
|
||||
message = "Desktop keybind action fields must match their type.";
|
||||
}
|
||||
];
|
||||
|
||||
type = lib.types.submodule {
|
||||
@@ -58,74 +69,7 @@ in
|
||||
};
|
||||
|
||||
action = lib.mkOption {
|
||||
type = lib.types.submodule {
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [
|
||||
"exec"
|
||||
"focus"
|
||||
"move-window"
|
||||
"resize-window"
|
||||
"toggle-floating"
|
||||
"fullscreen"
|
||||
"layout"
|
||||
"close-window"
|
||||
"kill-window"
|
||||
"focus-workspace"
|
||||
"move-to-workspace"
|
||||
];
|
||||
description = "Selects the desktop action this keybind triggers.";
|
||||
};
|
||||
|
||||
command = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
example = "ghostty";
|
||||
description = "Command executed by an exec action.";
|
||||
};
|
||||
|
||||
direction = lib.mkOption {
|
||||
type = lib.types.nullOr (
|
||||
lib.types.enum [
|
||||
"left"
|
||||
"down"
|
||||
"up"
|
||||
"right"
|
||||
]
|
||||
);
|
||||
default = null;
|
||||
description = "Direction used by focus and move-window actions.";
|
||||
};
|
||||
|
||||
x = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.int;
|
||||
default = null;
|
||||
example = 40;
|
||||
description = "Horizontal delta used by resize-window actions.";
|
||||
};
|
||||
|
||||
y = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.int;
|
||||
default = null;
|
||||
example = 40;
|
||||
description = "Vertical delta used by resize-window actions.";
|
||||
};
|
||||
|
||||
workspace = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.int;
|
||||
default = null;
|
||||
example = 1;
|
||||
description = "Workspace number used by workspace actions.";
|
||||
};
|
||||
|
||||
message = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
example = "togglesplit";
|
||||
description = "Layout message sent by layout actions.";
|
||||
};
|
||||
};
|
||||
};
|
||||
type = actionType;
|
||||
description = "Declares the desktop action for this keybind.";
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user