refactor(hyprland): simplify keybind action schema
This commit is contained in:
@@ -1,12 +1,20 @@
|
|||||||
{ lib, ... }:
|
{ lib, ... }:
|
||||||
let
|
let
|
||||||
actionMatchesType =
|
actionType = lib.types.addCheck (lib.types.attrsOf lib.types.anything) isAction;
|
||||||
|
|
||||||
|
isAction =
|
||||||
action:
|
action:
|
||||||
let
|
builtins.hasAttr "type" action
|
||||||
required = actionFields.${action.type};
|
&& builtins.hasAttr action.type actionFields
|
||||||
in
|
&& (
|
||||||
lib.all (field: action.${field} != null) required
|
let
|
||||||
&& lib.all (field: lib.elem field required || action.${field} == null) actionOptions;
|
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 = {
|
actionFields = {
|
||||||
exec = [ "command" ];
|
exec = [ "command" ];
|
||||||
@@ -25,14 +33,21 @@ let
|
|||||||
"move-to-workspace" = [ "workspace" ];
|
"move-to-workspace" = [ "workspace" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
actionOptions = [
|
actionFieldChecks = {
|
||||||
"command"
|
command = builtins.isString;
|
||||||
"direction"
|
direction =
|
||||||
"x"
|
direction:
|
||||||
"y"
|
lib.elem direction [
|
||||||
"workspace"
|
"left"
|
||||||
"message"
|
"down"
|
||||||
];
|
"up"
|
||||||
|
"right"
|
||||||
|
];
|
||||||
|
x = builtins.isInt;
|
||||||
|
y = builtins.isInt;
|
||||||
|
workspace = builtins.isInt;
|
||||||
|
message = builtins.isString;
|
||||||
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
checks = [
|
checks = [
|
||||||
@@ -40,10 +55,6 @@ in
|
|||||||
assertion = entries: lib.all (entry: entry.value.keys != [ ]) entries;
|
assertion = entries: lib.all (entry: entry.value.keys != [ ]) entries;
|
||||||
message = "Desktop keybinds must set at least one key.";
|
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 {
|
type = lib.types.submodule {
|
||||||
@@ -58,74 +69,7 @@ in
|
|||||||
};
|
};
|
||||||
|
|
||||||
action = lib.mkOption {
|
action = lib.mkOption {
|
||||||
type = lib.types.submodule {
|
type = actionType;
|
||||||
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.";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
description = "Declares the desktop action for this keybind.";
|
description = "Declares the desktop action for this keybind.";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user