From 633eacfd83bd5de1fdc9f647e83c39929f20e6dd Mon Sep 17 00:00:00 2001 From: Krzysztof Nazarewski Date: Thu, 19 Oct 2023 15:32:42 +0200 Subject: [PATCH 1/4] implement hotkeys using KGlobalAccel application tab https://github.com/pjones/plasma-manager/issues/18 --- modules/hotkeys.nix | 106 +++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 66 deletions(-) diff --git a/modules/hotkeys.nix b/modules/hotkeys.nix index ead36745..b546b51c 100644 --- a/modules/hotkeys.nix +++ b/modules/hotkeys.nix @@ -3,6 +3,12 @@ let cfg = config.programs.plasma; + group = rec { + name = "plasma-manager-commands"; + desktop = "${name}.desktop"; + description = "Plasma (Home) Manager Commands"; + }; + commandType = { name, ... }: { options = { name = lib.mkOption { @@ -19,7 +25,14 @@ 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 { @@ -28,70 +41,6 @@ let }; }; }; - - # 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"; - }; - - "${prefix}Conditions".ConditionsCount = - builtins.length (hotkey.conditions); - - "${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); in { options.programs.plasma.hotkeys = { @@ -105,6 +54,31 @@ 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 = command.command; + }) + cfg.hotkeys.commands; + }; + + programs.plasma.configFile."kglobalshortcutsrc"."${group.desktop}" = { + configGroupNesting = [ group.desktop ]; + _k_friendly_name = group.description; + } // lib.attrsets.mapAttrs + (_: command: + let + keys = command.keys ++ lib.optionals (command.key != "") [ command.key ]; + in + lib.concatStringsSep "," [ + (lib.concatStringsSep "\t" (map (lib.escape [ "," ]) keys)) + "" # List of default keys, not needed. + command.comment + ]) + cfg.hotkeys.commands; }; } From 96fb3158751d05a3d99f0483b1a86694760ebcfa Mon Sep 17 00:00:00 2001 From: Krzysztof Nazarewski Date: Mon, 23 Oct 2023 12:59:23 +0200 Subject: [PATCH 2/4] add `systemd-cat` to hotkeys see https://github.com/bugaevc/wl-clipboard/issues/201 --- modules/hotkeys.nix | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/hotkeys.nix b/modules/hotkeys.nix index b546b51c..0a0897d5 100644 --- a/modules/hotkeys.nix +++ b/modules/hotkeys.nix @@ -1,12 +1,12 @@ # 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 (Home) Manager Commands"; + description = "Home (Plasma) Manager"; }; commandType = { name, ... }: { @@ -39,6 +39,28 @@ let type = lib.types.str; description = "The command to execute."; }; + + logs.enabled = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Connect command's stdin and stdout to systemd journal with systemd-cat."; + }; + + 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."; + }; + + logs.extraArgs = lib.mkOption { + type = lib.types.str; + default = ""; + description = "Additional arguments provided to systemd-cat."; + }; }; }; in @@ -61,7 +83,10 @@ in actions = lib.mapAttrs (_: command: { name = command.name; - exec = command.command; + 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; }; From 8304f6c00d1ad8d7bd467a4093a68cf12b7cf3ed Mon Sep 17 00:00:00 2001 From: Toast <39011842+toast003@users.noreply.github.com> Date: Mon, 13 Nov 2023 22:57:42 +0100 Subject: [PATCH 3/4] example: update example for new hotkeys module --- example/home.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/home.nix b/example/home.nix index 4cad35c0..5d75a018 100644 --- a/example/home.nix +++ b/example/home.nix @@ -6,7 +6,8 @@ # Some high-level settings: workspace.clickItemTo = "select"; - hotkeys.commands."Launch Konsole" = { + hotkeys.commands."launch-konsole" = { + name = "Launch Konsole"; key = "Meta+Alt+K"; command = "konsole"; }; From c43ff128365864cf8fa6d9a7bc7bce4483402e3e Mon Sep 17 00:00:00 2001 From: magnouvean Date: Wed, 27 Mar 2024 15:00:55 +0100 Subject: [PATCH 4/4] Make hotkeys functional with the new config format --- modules/hotkeys.nix | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/hotkeys.nix b/modules/hotkeys.nix index 0a0897d5..98c4a3d3 100644 --- a/modules/hotkeys.nix +++ b/modules/hotkeys.nix @@ -6,7 +6,7 @@ let group = rec { name = "plasma-manager-commands"; desktop = "${name}.desktop"; - description = "Home (Plasma) Manager"; + description = "Plasma Manager"; }; commandType = { name, ... }: { @@ -50,7 +50,7 @@ let type = lib.types.str; default = lib.trivial.pipe name [ lib.strings.toLower - (builtins.replaceStrings [" "] ["-"]) + (builtins.replaceStrings [ " " ] [ "-" ]) (n: "${group.name}-${n}") ]; description = "Identifier passed down to systemd-cat."; @@ -92,18 +92,19 @@ in }; programs.plasma.configFile."kglobalshortcutsrc"."${group.desktop}" = { - configGroupNesting = [ group.desktop ]; - _k_friendly_name = group.description; + _k_friendly_name.value = group.description; } // lib.attrsets.mapAttrs (_: command: let keys = command.keys ++ lib.optionals (command.key != "") [ command.key ]; in - lib.concatStringsSep "," [ - (lib.concatStringsSep "\t" (map (lib.escape [ "," ]) keys)) - "" # List of default keys, not needed. - command.comment - ]) + { + value = lib.concatStringsSep "," [ + (lib.concatStringsSep "\t" (map (lib.escape [ "," ]) keys)) + "" # List of default keys, not needed. + command.comment + ]; + }) cfg.hotkeys.commands; }; }