fix(shortcuts): searchable custom shortcuts
This commit is contained in:
parent
f8cdc7af2f
commit
2705479ccb
2 changed files with 67 additions and 9 deletions
|
|
@ -61,8 +61,17 @@ impl ShortcutModel {
|
|||
(slab, if is_default { modified } else { modified + 1 })
|
||||
});
|
||||
|
||||
let mut localized_description = super::localize_action(&action);
|
||||
if let Action::Spawn(_) = &action {
|
||||
localized_description = bindings
|
||||
.iter()
|
||||
.map(|(_, shortcut)| super::localize_custom_action(&action, &shortcut.binding))
|
||||
.take(1)
|
||||
.collect();
|
||||
}
|
||||
|
||||
Self {
|
||||
description: super::localize_action(&action),
|
||||
description: localized_description,
|
||||
modified: defaults.0.iter().filter(|(_, a)| **a == action).fold(
|
||||
modified,
|
||||
|modified, (binding, _)| {
|
||||
|
|
@ -182,7 +191,6 @@ impl Model {
|
|||
|
||||
pub(super) fn on_enter(&mut self) {
|
||||
let mut shortcuts = self.config.get::<Shortcuts>("defaults").unwrap_or_default();
|
||||
|
||||
self.defaults = shortcuts.clone();
|
||||
|
||||
if let Ok(custom) = self.config.get::<Shortcuts>("custom") {
|
||||
|
|
@ -405,9 +413,12 @@ impl Model {
|
|||
shortcut.input.clear();
|
||||
return Command::none();
|
||||
}
|
||||
|
||||
if let Some(action) = self.config_contains(&new_binding) {
|
||||
let action_str = super::localize_action(&action);
|
||||
let action_str = if let Action::Spawn(_) = &action {
|
||||
super::localize_custom_action(&action, &new_binding)
|
||||
} else {
|
||||
super::localize_action(&action)
|
||||
};
|
||||
self.replace_dialog =
|
||||
Some((id, new_binding, action, action_str));
|
||||
return Command::none();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
mod common;
|
||||
|
||||
pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
|
||||
|
||||
pub mod custom;
|
||||
|
|
@ -16,11 +17,14 @@ use cosmic_settings_config::shortcuts::action::{
|
|||
Direction, FocusDirection, Orientation, ResizeDirection,
|
||||
};
|
||||
use cosmic_settings_config::shortcuts::{self, Action, Shortcuts};
|
||||
use cosmic_settings_config::Binding;
|
||||
use cosmic_settings_page::Section;
|
||||
use cosmic_settings_page::{self as page, section};
|
||||
use itertools::Itertools;
|
||||
use shortcuts::action::System as SystemAction;
|
||||
use slab::Slab;
|
||||
use slotmap::{DefaultKey, Key, SecondaryMap, SlotMap};
|
||||
use std::io;
|
||||
|
||||
pub struct Page {
|
||||
modified: Modified,
|
||||
|
|
@ -49,11 +53,11 @@ struct SubPages {
|
|||
window_tiling: page::Entity,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Search {
|
||||
input: String,
|
||||
actions: SlotMap<DefaultKey, Action>,
|
||||
localized: SecondaryMap<DefaultKey, String>,
|
||||
config: cosmic_config::Config,
|
||||
shortcuts: Shortcuts,
|
||||
defaults: Shortcuts,
|
||||
}
|
||||
|
|
@ -248,7 +252,6 @@ impl Page {
|
|||
self.search_model.on_clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if self.search.actions.is_empty() {
|
||||
self.search.cache_localized_actions();
|
||||
}
|
||||
|
|
@ -280,16 +283,52 @@ impl page::AutoBind<crate::pages::Message> for Page {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Search {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
input: String::default(),
|
||||
defaults: Shortcuts::default(),
|
||||
config: shortcuts::context().unwrap(),
|
||||
localized: SecondaryMap::default(),
|
||||
actions: SlotMap::new(),
|
||||
shortcuts: Shortcuts::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Search {
|
||||
fn cache_localized_actions(&mut self) {
|
||||
self.actions.clear();
|
||||
self.localized.clear();
|
||||
|
||||
for action in all_actions() {
|
||||
let custom_actions = self.retrieve_custom_actions();
|
||||
for action in all_system_actions() {
|
||||
let localized = localize_action(action);
|
||||
let id = self.actions.insert(action.clone());
|
||||
self.localized.insert(id, localized);
|
||||
}
|
||||
for (binding, action) in custom_actions {
|
||||
let localized = localize_custom_action(&action, &binding);
|
||||
let id = self.actions.insert(action.clone());
|
||||
self.localized.insert(id, localized);
|
||||
}
|
||||
}
|
||||
|
||||
fn retrieve_custom_actions(&self) -> Vec<(Binding, Action)> {
|
||||
let custom_shortcusts = match self.config.get::<Shortcuts>("custom") {
|
||||
Ok(shortcuts) => shortcuts,
|
||||
Err(cosmic_config::Error::GetKey(_, why)) if why.kind() == io::ErrorKind::NotFound => {
|
||||
Shortcuts::default()
|
||||
}
|
||||
Err(why) => {
|
||||
tracing::error!(?why, "unable to get the current shortcuts config");
|
||||
Shortcuts::default()
|
||||
}
|
||||
};
|
||||
custom_shortcusts
|
||||
.0
|
||||
.into_iter()
|
||||
.unique_by(|(_, action)| localize_action(action))
|
||||
.collect::<Vec<(Binding, Action)>>()
|
||||
}
|
||||
|
||||
fn shortcut_models(&mut self) -> Slab<ShortcutModel> {
|
||||
|
|
@ -418,7 +457,7 @@ fn action_category(action: &Action) -> Option<Category> {
|
|||
})
|
||||
}
|
||||
|
||||
fn all_actions() -> &'static [Action] {
|
||||
fn all_system_actions() -> &'static [Action] {
|
||||
&[
|
||||
Action::Close,
|
||||
Action::Debug,
|
||||
|
|
@ -625,3 +664,11 @@ fn localize_action(action: &Action) -> String {
|
|||
Action::Spawn(command) => command.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn localize_custom_action(action: &Action, binding: &Binding) -> String {
|
||||
if let Some(description) = &binding.description {
|
||||
description.to_string()
|
||||
} else {
|
||||
localize_action(&action)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue