Skip to content

Commit

Permalink
feat: add COSMIC desktop shortcuts configuration
Browse files Browse the repository at this point in the history
Add support for configuring COSMIC desktop keyboard shortcuts using home-manager.
The changes include:
- New capitalizeWord utility function for string manipulation
- New shortcuts module with comprehensive keyboard shortcut configuration options
- Integration of shortcuts module into default modules
  • Loading branch information
HeitorAugustoLN committed Dec 26, 2024
1 parent 6da040e commit daf6783
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/utils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
);
in
cleanNullsExceptOptional';

capitalizeWord =
word:
with lib.strings;
concatImapStrings (index: char: if index == 1 then toUpper char else toLower char) (
stringToCharacters word
);
}
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[
./files.nix
./panels.nix
./shortcuts.nix
]
++ lib.foldlAttrs (
prev: name: type:
Expand Down
232 changes: 232 additions & 0 deletions modules/shortcuts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{ config, lib, ... }:
let
cfg = config.wayland.desktopManager.cosmic;

inherit (lib.cosmic.options) mkNullOrOption;
inherit (lib.cosmic.utils) capitalizeWord;
in
{
options.wayland.desktopManager.cosmic.shortcuts =
let
shortcutSubmodule =
let
# NOTE: Last updated: 26/12/2024 at 4:15 UTC-3 with the following commit:
# https://github.com/pop-os/cosmic-settings-daemon/commit/747e482ca197497ee3bc5f6e9dcd23c73e592e47
direction = [
"Down"
"Left"
"Right"
"Up"
];

enumVariants = [
"Close"
"Debug"
"Disable"
"LastWorkspace"
"Maximize"
"MigrateWorkspaceToNextOutput"
"MigrateWorkspaceToPreviousOutput"
"Minimize"
"MoveToLastWorkspace"
"MoveToNextOutput"
"MoveToNextWorkspace"
"MoveToPreviousOutput"
"MoveToPreviousWorkspace"
"NextOutput"
"NextWorkspace"
"PreviousOutput"
"PreviousWorkspace"
"SendToLastWorkspace"
"SendToNextOutput"
"SendToNextWorkspace"
"SendToPreviousOutput"
"SendToPreviousWorkspace"
"SwapWindow"
"ToggleOrientation"
"ToggleStacking"
"ToggleSticky"
"ToggleTiling"
"ToggleWindowFloating"
];

focusDirection = [
"Down"
"In"
"Left"
"Right"
"Out"
"Up"
];

orientation = [
"Horizontal"
"Vertical"
];

resizeDirection = [
"Inwards"
"Outwards"
];

# NOTE: Unused but kept since it might be useful in the future
# deadnix: skip
resizeEdge = [
"Bottom"
"BottomLeft"
"BottomRight"
"Left"
"Right"
"Top"
"TopLeft"
"TopRight"
];

system = [
"AppLibrary"
"BrightnessDown"
"BrightnessUp"
"HomeFolder"
"KeyboardBrightnessDown"
"KeyboardBrightnessUp"
"Launcher"
"LockScreen"
"Mute"
"MuteMic"
"PlayPause"
"PlayNext"
"PlayPrev"
"Screenshot"
"Terminal"
"VolumeLower"
"VolumeRaise"
"WebBrowser"
"WindowSwitcher"
"WindowSwitcherPrevious"
"WorkspaceOverview"
];
in
lib.types.submodule {
options = {
description = mkNullOrOption {
type = with lib.types; ronOptionalOf str;
example = {
__type = "optional";
value = "Open Terminal";
};
description = "Description of the shortcut";
};
key = lib.mkOption {
type = lib.types.str;
example = "Super+Q";
description = "The key combination for the shortcut";
};
value = lib.mkOption {
type =
with lib.types;
oneOf [
(ronEnum enumVariants)
(ronTupleEnumOf (ronEnum direction) [
"MigrateWorkspaceToNextOutput"
"Move"
"MoveToOutput"
"SendToOutput"
"SwitchOutput"
])
(ronTupleEnumOf ints.u8 [
"MoveToWorkspace"
"SendToWorkspace"
"Workspace"
])
(ronTupleEnumOf (ronEnum focusDirection) [ "Focus" ])
(ronTupleEnumOf (ronEnum orientation) [ "Orientation" ])
(ronTupleEnumOf (ronEnum resizeDirection) [ "Resizing" ])
(ronTupleEnumOf (ronEnum system) [ "System" ])
(ronTupleEnumOf str [ "Spawn" ])
];
example = {
__type = "enum";
variant = "Close";
};
description = "The action to be performed by the shortcut";
};
};
};
in
mkNullOrOption {
type = lib.types.listOf shortcutSubmodule;
example = [
{
description = "Open Firefox";
key = "Super+Alt+B";
value = {
__type = "enum";
variant = "Spawn";
value = "firefox";
};
}
{
key = "Super+Q";
value = {
__type = "enum";
variant = "Close";
};
}
];
description = ''
List of shortcuts to be configured for COSMIC.
'';
};

config =
let
parseShortcuts =
key:
let
parts = builtins.filter (x: x != "") (lib.splitString "+" key);

validModifiers = [
"Alt"
"Ctrl"
"Shift"
"Super"
];
in
{
key =
let
last = lib.last parts;
in
if builtins.stringLength last == 1 then
lib.toLower last
else if builtins.elem (capitalizeWord last) validModifiers then
throw "Last part of the key cannot be a modifier"
else
last;
modifiers = map (
modifier:
if builtins.elem modifier validModifiers then
modifier
else
throw "Invalid modifier: ${modifier}. Valid modifiers are: ${builtins.concatStringsSep ", " validModifiers}"
) (lib.init parts);
};
in
lib.mkIf (cfg.enable && cfg.shortcuts != null) {
wayland.desktopManager.cosmic.configFile."com.system76.CosmicSettings.Shortcuts" = {
entries.custom = {
__type = "map";
value = map (shortcut: {
key = lib.cosmic.utils.cleanNullsExceptOptional (
parseShortcuts shortcut.key
// {
inherit (shortcut) description;
}
);
inherit (shortcut) value;
}) cfg.shortcuts;
};
version = 1;
};
};
}

0 comments on commit daf6783

Please sign in to comment.