Skip to content

Commit a4ae159

Browse files
refactor(shortcuts): clean up imports and remove deprecated actions
- Extract and explicitly import needed functions from lib and builtins - Filter out deprecated actions from available shortcuts - Simplify code structure by removing unnecessary nested let bindings - Maintain the same functionality while improving code readability Related: pop-os/cosmic-settings-daemon#73
1 parent f159b8d commit a4ae159

File tree

1 file changed

+88
-58
lines changed

1 file changed

+88
-58
lines changed

modules/shortcuts.nix

+88-58
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
{ config, lib, ... }:
2+
let
3+
inherit (builtins)
4+
all
5+
elem
6+
filter
7+
getAttr
8+
groupBy
9+
hasAttr
10+
mapAttrs
11+
stringLength
12+
;
13+
14+
inherit (lib)
15+
importJSON
16+
init
17+
last
18+
mapAttrsToList
19+
mkIf
20+
mkOption
21+
pipe
22+
splitString
23+
toLower
24+
types
25+
unique
26+
;
27+
28+
inherit (lib.cosmic)
29+
cleanNullsExceptOptional
30+
defaultNullOpts
31+
mkRonExpression
32+
rustToNixType
33+
;
34+
in
235
{
336
options.wayland.desktopManager.cosmic.shortcuts =
437
let
5-
inherit (lib.cosmic) defaultNullOpts;
6-
738
shortcutSubmodule =
839
let
9-
generatedActions = lib.importJSON ../generated/actions-for-shortcuts.json;
40+
generatedActions = importJSON ../generated/actions-for-shortcuts.json;
1041
in
11-
lib.types.submodule {
42+
types.submodule {
1243
options = {
1344
description =
14-
defaultNullOpts.mkRonOptionalOf lib.types.str
45+
defaultNullOpts.mkRonOptionalOf types.str
1546
{
1647
__type = "optional";
1748
value = "Open Terminal";
@@ -21,65 +52,73 @@
2152
Used by COSMIC Settings to display the name of a custom shortcut.
2253
This field is optional, and should only be used when defining custom shortcuts.
2354
'';
24-
key = lib.mkOption {
25-
type = lib.types.str;
55+
key = mkOption {
56+
type = types.str;
2657
example = "Super+Q";
2758
description = ''
2859
The key combination that triggers the action.
2960
For example, "Super+Q" would trigger the action when the Super and Q keys are pressed together.
3061
'';
3162
};
32-
action = lib.mkOption {
63+
action = mkOption {
3364
type =
34-
with lib.types;
65+
with types;
3566
maybeRonRaw (
3667
oneOf (
3768
[
3869
(ronEnum (
39-
lib.pipe generatedActions [
40-
(builtins.getAttr "Actions")
41-
(builtins.filter (action: !(builtins.hasAttr "type" action)))
70+
pipe generatedActions [
71+
(getAttr "Actions")
72+
(filter (action: !(hasAttr "type" action)))
4273
(map (action: action.name))
74+
# Remove deprecated actions from the list
75+
# TODO: Remove it when it gets removed from actions
76+
(filter (
77+
action:
78+
!(elem action [
79+
"MigrateWorkspaceToNextOutput"
80+
"MigrateWorkspaceToPreviousOutput"
81+
"MoveToNextOutput"
82+
"MoveToPreviousOutput"
83+
"NextOutput"
84+
"PreviousOutput"
85+
"SendToNextOutput"
86+
"SendToPreviousOutput"
87+
])
88+
))
4389
]
4490
))
4591
]
4692
++
47-
lib.mapAttrsToList
93+
mapAttrsToList
4894
(
4995
type: names:
5096
let
5197
actionDependencies = generatedActions.Dependencies;
5298

5399
elemType =
54-
let
55-
inherit (lib.cosmic) rustToNixType;
56-
in
57-
if builtins.hasAttr type actionDependencies then
100+
if hasAttr type actionDependencies then
58101
ronEnum (map (action: action.name) actionDependencies.${type})
59102
else
60103
rustToNixType type;
61104
in
62105
ronTupleEnumOf elemType names 1
63106
)
64107
(
65-
lib.pipe generatedActions [
66-
(builtins.getAttr "Actions")
67-
(builtins.filter (action: builtins.hasAttr "type" action))
68-
(builtins.groupBy (action: action.type))
69-
(builtins.mapAttrs (_: actions: map (action: action.name) actions))
108+
pipe generatedActions [
109+
(getAttr "Actions")
110+
(filter (action: hasAttr "type" action))
111+
(groupBy (action: action.type))
112+
(mapAttrs (_: actions: map (action: action.name) actions))
70113
]
71114
)
72115
)
73116
);
74-
example =
75-
let
76-
inherit (lib.cosmic) mkRonExpression;
77-
in
78-
mkRonExpression 0 {
79-
__type = "enum";
80-
variant = "Spawn";
81-
value = [ "firefox" ];
82-
} null;
117+
example = mkRonExpression 0 {
118+
__type = "enum";
119+
variant = "Spawn";
120+
value = [ "firefox" ];
121+
} null;
83122
description = ''
84123
The action triggered by the shortcut.
85124
Actions can include running a command, moving windows, system actions, and more.
@@ -88,7 +127,7 @@
88127
};
89128
};
90129
in
91-
defaultNullOpts.mkNullable (lib.types.listOf shortcutSubmodule)
130+
defaultNullOpts.mkNullable (types.listOf shortcutSubmodule)
92131
[
93132
{
94133
description = {
@@ -162,49 +201,40 @@
162201
"Super"
163202
];
164203

165-
isModifier = part: builtins.elem part validModifiers;
204+
isModifier = part: elem part validModifiers;
166205

167-
parts = lib.pipe key [
168-
(lib.splitString "+")
169-
(builtins.filter (x: x != ""))
206+
parts = pipe key [
207+
(splitString "+")
208+
(filter (x: x != ""))
170209
];
171-
172-
init = lib.init parts;
173-
last = lib.last parts;
174210
in
175211
{
176212
key =
177-
if builtins.all isModifier parts then
213+
if all isModifier parts then
178214
null
179-
else if builtins.stringLength last == 1 then
180-
lib.toLower last
215+
else if stringLength last == 1 then
216+
toLower (last parts)
181217
else
182218
last;
183219

184220
modifiers = map (modifier: {
185221
__type = "enum";
186222
variant = modifier;
187-
}) (lib.unique (if builtins.all isModifier parts then parts else init));
223+
}) (unique (if all isModifier parts then parts else init parts));
188224
};
189225
in
190-
lib.mkIf (cfg.shortcuts != null) {
226+
mkIf (cfg.shortcuts != null) {
191227
wayland.desktopManager.cosmic.configFile."com.system76.CosmicSettings.Shortcuts" = {
192228
entries.custom = {
193229
__type = "map";
194-
value = lib.pipe cfg.shortcuts [
195-
(map (shortcut: {
196-
key =
197-
let
198-
inherit (lib.cosmic) cleanNullsExceptOptional;
199-
in
200-
lib.pipe shortcut.key [
201-
parseShortcuts
202-
(parsed: parsed // { inherit (shortcut) description; })
203-
cleanNullsExceptOptional
204-
];
205-
value = shortcut.action;
206-
}))
207-
];
230+
value = map (shortcut: {
231+
key = pipe shortcut.key [
232+
parseShortcuts
233+
(parsed: parsed // { inherit (shortcut) description; })
234+
cleanNullsExceptOptional
235+
];
236+
value = shortcut.action;
237+
}) cfg.shortcuts;
208238
};
209239
version = 1;
210240
};

0 commit comments

Comments
 (0)