Skip to content

Commit 2705479

Browse files
authored
fix(shortcuts): searchable custom shortcuts
1 parent f8cdc7a commit 2705479

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,17 @@ impl ShortcutModel {
6161
(slab, if is_default { modified } else { modified + 1 })
6262
});
6363

64+
let mut localized_description = super::localize_action(&action);
65+
if let Action::Spawn(_) = &action {
66+
localized_description = bindings
67+
.iter()
68+
.map(|(_, shortcut)| super::localize_custom_action(&action, &shortcut.binding))
69+
.take(1)
70+
.collect();
71+
}
72+
6473
Self {
65-
description: super::localize_action(&action),
74+
description: localized_description,
6675
modified: defaults.0.iter().filter(|(_, a)| **a == action).fold(
6776
modified,
6877
|modified, (binding, _)| {
@@ -182,7 +191,6 @@ impl Model {
182191

183192
pub(super) fn on_enter(&mut self) {
184193
let mut shortcuts = self.config.get::<Shortcuts>("defaults").unwrap_or_default();
185-
186194
self.defaults = shortcuts.clone();
187195

188196
if let Ok(custom) = self.config.get::<Shortcuts>("custom") {
@@ -405,9 +413,12 @@ impl Model {
405413
shortcut.input.clear();
406414
return Command::none();
407415
}
408-
409416
if let Some(action) = self.config_contains(&new_binding) {
410-
let action_str = super::localize_action(&action);
417+
let action_str = if let Action::Spawn(_) = &action {
418+
super::localize_custom_action(&action, &new_binding)
419+
} else {
420+
super::localize_action(&action)
421+
};
411422
self.replace_dialog =
412423
Some((id, new_binding, action, action_str));
413424
return Command::none();

cosmic-settings/src/pages/input/keyboard/shortcuts/mod.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod common;
2+
23
pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
34

45
pub mod custom;
@@ -16,11 +17,14 @@ use cosmic_settings_config::shortcuts::action::{
1617
Direction, FocusDirection, Orientation, ResizeDirection,
1718
};
1819
use cosmic_settings_config::shortcuts::{self, Action, Shortcuts};
20+
use cosmic_settings_config::Binding;
1921
use cosmic_settings_page::Section;
2022
use cosmic_settings_page::{self as page, section};
23+
use itertools::Itertools;
2124
use shortcuts::action::System as SystemAction;
2225
use slab::Slab;
2326
use slotmap::{DefaultKey, Key, SecondaryMap, SlotMap};
27+
use std::io;
2428

2529
pub struct Page {
2630
modified: Modified,
@@ -49,11 +53,11 @@ struct SubPages {
4953
window_tiling: page::Entity,
5054
}
5155

52-
#[derive(Default)]
5356
struct Search {
5457
input: String,
5558
actions: SlotMap<DefaultKey, Action>,
5659
localized: SecondaryMap<DefaultKey, String>,
60+
config: cosmic_config::Config,
5761
shortcuts: Shortcuts,
5862
defaults: Shortcuts,
5963
}
@@ -248,7 +252,6 @@ impl Page {
248252
self.search_model.on_clear();
249253
return;
250254
}
251-
252255
if self.search.actions.is_empty() {
253256
self.search.cache_localized_actions();
254257
}
@@ -280,16 +283,52 @@ impl page::AutoBind<crate::pages::Message> for Page {
280283
}
281284
}
282285

286+
impl Default for Search {
287+
fn default() -> Self {
288+
Self {
289+
input: String::default(),
290+
defaults: Shortcuts::default(),
291+
config: shortcuts::context().unwrap(),
292+
localized: SecondaryMap::default(),
293+
actions: SlotMap::new(),
294+
shortcuts: Shortcuts::default(),
295+
}
296+
}
297+
}
298+
283299
impl Search {
284300
fn cache_localized_actions(&mut self) {
285301
self.actions.clear();
286302
self.localized.clear();
287-
288-
for action in all_actions() {
303+
let custom_actions = self.retrieve_custom_actions();
304+
for action in all_system_actions() {
289305
let localized = localize_action(action);
290306
let id = self.actions.insert(action.clone());
291307
self.localized.insert(id, localized);
292308
}
309+
for (binding, action) in custom_actions {
310+
let localized = localize_custom_action(&action, &binding);
311+
let id = self.actions.insert(action.clone());
312+
self.localized.insert(id, localized);
313+
}
314+
}
315+
316+
fn retrieve_custom_actions(&self) -> Vec<(Binding, Action)> {
317+
let custom_shortcusts = match self.config.get::<Shortcuts>("custom") {
318+
Ok(shortcuts) => shortcuts,
319+
Err(cosmic_config::Error::GetKey(_, why)) if why.kind() == io::ErrorKind::NotFound => {
320+
Shortcuts::default()
321+
}
322+
Err(why) => {
323+
tracing::error!(?why, "unable to get the current shortcuts config");
324+
Shortcuts::default()
325+
}
326+
};
327+
custom_shortcusts
328+
.0
329+
.into_iter()
330+
.unique_by(|(_, action)| localize_action(action))
331+
.collect::<Vec<(Binding, Action)>>()
293332
}
294333

295334
fn shortcut_models(&mut self) -> Slab<ShortcutModel> {
@@ -418,7 +457,7 @@ fn action_category(action: &Action) -> Option<Category> {
418457
})
419458
}
420459

421-
fn all_actions() -> &'static [Action] {
460+
fn all_system_actions() -> &'static [Action] {
422461
&[
423462
Action::Close,
424463
Action::Debug,
@@ -625,3 +664,11 @@ fn localize_action(action: &Action) -> String {
625664
Action::Spawn(command) => command.clone(),
626665
}
627666
}
667+
668+
fn localize_custom_action(action: &Action, binding: &Binding) -> String {
669+
if let Some(description) = &binding.description {
670+
description.to_string()
671+
} else {
672+
localize_action(&action)
673+
}
674+
}

0 commit comments

Comments
 (0)