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 })
|
(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 {
|
Self {
|
||||||
description: super::localize_action(&action),
|
description: localized_description,
|
||||||
modified: defaults.0.iter().filter(|(_, a)| **a == action).fold(
|
modified: defaults.0.iter().filter(|(_, a)| **a == action).fold(
|
||||||
modified,
|
modified,
|
||||||
|modified, (binding, _)| {
|
|modified, (binding, _)| {
|
||||||
|
|
@ -182,7 +191,6 @@ impl Model {
|
||||||
|
|
||||||
pub(super) fn on_enter(&mut self) {
|
pub(super) fn on_enter(&mut self) {
|
||||||
let mut shortcuts = self.config.get::<Shortcuts>("defaults").unwrap_or_default();
|
let mut shortcuts = self.config.get::<Shortcuts>("defaults").unwrap_or_default();
|
||||||
|
|
||||||
self.defaults = shortcuts.clone();
|
self.defaults = shortcuts.clone();
|
||||||
|
|
||||||
if let Ok(custom) = self.config.get::<Shortcuts>("custom") {
|
if let Ok(custom) = self.config.get::<Shortcuts>("custom") {
|
||||||
|
|
@ -405,9 +413,12 @@ impl Model {
|
||||||
shortcut.input.clear();
|
shortcut.input.clear();
|
||||||
return Command::none();
|
return Command::none();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(action) = self.config_contains(&new_binding) {
|
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 =
|
self.replace_dialog =
|
||||||
Some((id, new_binding, action, action_str));
|
Some((id, new_binding, action, action_str));
|
||||||
return Command::none();
|
return Command::none();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
|
pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
|
||||||
|
|
||||||
pub mod custom;
|
pub mod custom;
|
||||||
|
|
@ -16,11 +17,14 @@ use cosmic_settings_config::shortcuts::action::{
|
||||||
Direction, FocusDirection, Orientation, ResizeDirection,
|
Direction, FocusDirection, Orientation, ResizeDirection,
|
||||||
};
|
};
|
||||||
use cosmic_settings_config::shortcuts::{self, Action, Shortcuts};
|
use cosmic_settings_config::shortcuts::{self, Action, Shortcuts};
|
||||||
|
use cosmic_settings_config::Binding;
|
||||||
use cosmic_settings_page::Section;
|
use cosmic_settings_page::Section;
|
||||||
use cosmic_settings_page::{self as page, section};
|
use cosmic_settings_page::{self as page, section};
|
||||||
|
use itertools::Itertools;
|
||||||
use shortcuts::action::System as SystemAction;
|
use shortcuts::action::System as SystemAction;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use slotmap::{DefaultKey, Key, SecondaryMap, SlotMap};
|
use slotmap::{DefaultKey, Key, SecondaryMap, SlotMap};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
pub struct Page {
|
pub struct Page {
|
||||||
modified: Modified,
|
modified: Modified,
|
||||||
|
|
@ -49,11 +53,11 @@ struct SubPages {
|
||||||
window_tiling: page::Entity,
|
window_tiling: page::Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct Search {
|
struct Search {
|
||||||
input: String,
|
input: String,
|
||||||
actions: SlotMap<DefaultKey, Action>,
|
actions: SlotMap<DefaultKey, Action>,
|
||||||
localized: SecondaryMap<DefaultKey, String>,
|
localized: SecondaryMap<DefaultKey, String>,
|
||||||
|
config: cosmic_config::Config,
|
||||||
shortcuts: Shortcuts,
|
shortcuts: Shortcuts,
|
||||||
defaults: Shortcuts,
|
defaults: Shortcuts,
|
||||||
}
|
}
|
||||||
|
|
@ -248,7 +252,6 @@ impl Page {
|
||||||
self.search_model.on_clear();
|
self.search_model.on_clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.search.actions.is_empty() {
|
if self.search.actions.is_empty() {
|
||||||
self.search.cache_localized_actions();
|
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 {
|
impl Search {
|
||||||
fn cache_localized_actions(&mut self) {
|
fn cache_localized_actions(&mut self) {
|
||||||
self.actions.clear();
|
self.actions.clear();
|
||||||
self.localized.clear();
|
self.localized.clear();
|
||||||
|
let custom_actions = self.retrieve_custom_actions();
|
||||||
for action in all_actions() {
|
for action in all_system_actions() {
|
||||||
let localized = localize_action(action);
|
let localized = localize_action(action);
|
||||||
let id = self.actions.insert(action.clone());
|
let id = self.actions.insert(action.clone());
|
||||||
self.localized.insert(id, localized);
|
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> {
|
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::Close,
|
||||||
Action::Debug,
|
Action::Debug,
|
||||||
|
|
@ -625,3 +664,11 @@ fn localize_action(action: &Action) -> String {
|
||||||
Action::Spawn(command) => command.clone(),
|
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