Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement hotkeys using KGlobalAccel application tab #24

Merged
merged 6 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
Expand Down
130 changes: 65 additions & 65 deletions modules/hotkeys.nix
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);

"${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;
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.";
};

# 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 = {
Expand All @@ -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;
};
}
Loading