Implement hotkeys using KGlobalAccel (#24)

This commit is contained in:
Krzysztof Nazarewski 2024-03-27 15:07:15 +01:00 committed by GitHub
parent 3399f8b3b0
commit 92b259050d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 66 deletions

View File

@ -14,7 +14,8 @@
wallpaper = "${pkgs.libsForQt5.plasma-workspace-wallpapers}/share/wallpapers/Patak/contents/images/1080x1920.png";
};
hotkeys.commands."Launch Konsole" = {
hotkeys.commands."launch-konsole" = {
name = "Launch Konsole";
key = "Meta+Alt+K";
command = "konsole";
};

View File

@ -1,8 +1,14 @@
# Global hotkeys (user-defined keyboard shortcuts):
{ config, lib, ... }:
{ pkgs, config, lib, ... }:
let
cfg = config.programs.plasma;
group = rec {
name = "plasma-manager-commands";
desktop = "${name}.desktop";
description = "Plasma Manager";
};
commandType = { name, ... }: {
options = {
name = lib.mkOption {
@ -19,79 +25,44 @@ let
key = lib.mkOption {
type = lib.types.str;
description = "The key that triggers the action.";
description = "The key combination that triggers the action.";
default = "";
};
keys = lib.mkOption {
type = with lib.types; listOf str;
description = "The key combinations that trigger the action.";
default = [ ];
};
command = lib.mkOption {
type = lib.types.str;
description = "The command to execute.";
};
};
};
# Create a hotkey attribute set from the given command. The idx
# parameter is the index within the hotkey list for this command.
commandToHotkey = cmd: idx: {
inherit (cmd) name comment;
triggers = [{
Key = cmd.key;
Type = "SHORTCUT";
Uuid = "{" + builtins.hashString "sha256" (builtins.toString idx + cmd.name) + "}";
}];
actions = [{
CommandURL = cmd.command;
Type = "COMMAND_URL";
}];
conditions = [ ];
};
# Convert a hotkey to an attribute set that can be used with
# programs.plasma.files:
hotkeyToSettings = hotkey: idx:
let
prefix = "Data_${toString idx}";
toSection = name: items:
builtins.listToAttrs
(lib.imap0
(jdx: item: {
name = "${prefix}${name}${toString jdx}";
value = item;
})
items);
in
{
${prefix} = {
Comment = hotkey.comment;
Enabled = true;
Name = hotkey.name;
Type = "SIMPLE_ACTION_DATA";
logs.enabled = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Connect command's stdin and stdout to systemd journal with systemd-cat.";
};
"${prefix}Conditions".ConditionsCount =
builtins.length (hotkey.conditions);
logs.identifier = lib.mkOption {
type = lib.types.str;
default = lib.trivial.pipe name [
lib.strings.toLower
(builtins.replaceStrings [ " " ] [ "-" ])
(n: "${group.name}-${n}")
];
description = "Identifier passed down to systemd-cat.";
};
"${prefix}Actions".ActionsCount =
builtins.length (hotkey.actions);
"${prefix}Triggers".TriggersCount =
builtins.length (hotkey.triggers);
}
// toSection "Conditions" hotkey.conditions
// toSection "Actions" hotkey.actions
// toSection "Triggers" hotkey.triggers;
# Turn all options in this module into an attribute sets for
# programs.plasma.files.
hotkeys =
let items =
(map commandToHotkey (builtins.attrValues cfg.hotkeys.commands));
in
lib.foldr (a: b: a // b) { Data.DataCount = builtins.length items; }
(lib.imap1 (idx: hotkey: hotkeyToSettings (hotkey idx) idx) items);
logs.extraArgs = lib.mkOption {
type = lib.types.str;
default = "";
description = "Additional arguments provided to systemd-cat.";
};
};
};
in
{
options.programs.plasma.hotkeys = {
@ -105,6 +76,35 @@ in
config = lib.mkIf
(cfg.enable && builtins.length (builtins.attrNames cfg.hotkeys.commands) != 0)
{
programs.plasma.configFile.khotkeysrc = hotkeys;
xdg.desktopEntries."${group.name}" = {
name = group.description;
noDisplay = true;
type = "Application";
actions = lib.mapAttrs
(_: command: {
name = command.name;
exec =
if command.logs.enabled then
"${pkgs.systemd}/bin/systemd-cat --identifier=${command.logs.identifier} ${command.logs.extraArgs} ${command.command}"
else command.command;
})
cfg.hotkeys.commands;
};
programs.plasma.configFile."kglobalshortcutsrc"."${group.desktop}" = {
_k_friendly_name.value = group.description;
} // lib.attrsets.mapAttrs
(_: command:
let
keys = command.keys ++ lib.optionals (command.key != "") [ command.key ];
in
{
value = lib.concatStringsSep "," [
(lib.concatStringsSep "\t" (map (lib.escape [ "," ]) keys))
"" # List of default keys, not needed.
command.comment
];
})
cfg.hotkeys.commands;
};
}