Use id instead of type_id for actions

Currently, both are the same thing, so the logic is not changed.
This commit is contained in:
Kirill Bulatov 2023-07-17 12:24:56 +03:00
parent 10a1df3faa
commit eaa8224076
5 changed files with 30 additions and 37 deletions

View file

@ -1073,7 +1073,7 @@ impl AppContext {
pub fn is_action_available(&self, action: &dyn Action) -> bool { pub fn is_action_available(&self, action: &dyn Action) -> bool {
let mut available_in_window = false; let mut available_in_window = false;
let action_type = action.as_any().type_id(); let action_id = action.id();
if let Some(window_id) = self.platform.main_window_id() { if let Some(window_id) = self.platform.main_window_id() {
available_in_window = self available_in_window = self
.read_window(window_id, |cx| { .read_window(window_id, |cx| {
@ -1083,7 +1083,7 @@ impl AppContext {
cx.views_metadata.get(&(window_id, view_id)) cx.views_metadata.get(&(window_id, view_id))
{ {
if let Some(actions) = cx.actions.get(&view_metadata.type_id) { if let Some(actions) = cx.actions.get(&view_metadata.type_id) {
if actions.contains_key(&action_type) { if actions.contains_key(&action_id) {
return true; return true;
} }
} }
@ -1094,7 +1094,7 @@ impl AppContext {
}) })
.unwrap_or(false); .unwrap_or(false);
} }
available_in_window || self.global_actions.contains_key(&action_type) available_in_window || self.global_actions.contains_key(&action_id)
} }
fn actions_mut( fn actions_mut(
@ -3399,7 +3399,7 @@ impl<'a, 'b, 'c, V: View> LayoutContext<'a, 'b, 'c, V> {
for (i, view_id) in self.ancestors(view_id).enumerate() { for (i, view_id) in self.ancestors(view_id).enumerate() {
if let Some(view_metadata) = self.views_metadata.get(&(window_id, view_id)) { if let Some(view_metadata) = self.views_metadata.get(&(window_id, view_id)) {
if let Some(actions) = self.actions.get(&view_metadata.type_id) { if let Some(actions) = self.actions.get(&view_metadata.type_id) {
if actions.contains_key(&action.as_any().type_id()) { if actions.contains_key(&action.id()) {
handler_depth = Some(i); handler_depth = Some(i);
} }
} }
@ -3407,12 +3407,12 @@ impl<'a, 'b, 'c, V: View> LayoutContext<'a, 'b, 'c, V> {
} }
} }
if self.global_actions.contains_key(&action.as_any().type_id()) { if self.global_actions.contains_key(&action.id()) {
handler_depth = Some(contexts.len()) handler_depth = Some(contexts.len())
} }
self.keystroke_matcher self.keystroke_matcher
.bindings_for_action_type(action.as_any().type_id()) .bindings_for_action(action.id())
.find_map(|b| { .find_map(|b| {
let highest_handler = handler_depth?; let highest_handler = handler_depth?;
if action.eq(b.action()) if action.eq(b.action())

View file

@ -363,17 +363,13 @@ impl<'a> WindowContext<'a> {
) -> Vec<(&'static str, Box<dyn Action>, SmallVec<[Binding; 1]>)> { ) -> Vec<(&'static str, Box<dyn Action>, SmallVec<[Binding; 1]>)> {
let window_id = self.window_id; let window_id = self.window_id;
let mut contexts = Vec::new(); let mut contexts = Vec::new();
let mut handler_depths_by_action_type = HashMap::<TypeId, usize>::default(); let mut handler_depths_by_action_id = HashMap::<TypeId, usize>::default();
for (depth, view_id) in self.ancestors(view_id).enumerate() { for (depth, view_id) in self.ancestors(view_id).enumerate() {
if let Some(view_metadata) = self.views_metadata.get(&(window_id, view_id)) { if let Some(view_metadata) = self.views_metadata.get(&(window_id, view_id)) {
contexts.push(view_metadata.keymap_context.clone()); contexts.push(view_metadata.keymap_context.clone());
if let Some(actions) = self.actions.get(&view_metadata.type_id) { if let Some(actions) = self.actions.get(&view_metadata.type_id) {
handler_depths_by_action_type.extend( handler_depths_by_action_id
actions .extend(actions.keys().copied().map(|action_id| (action_id, depth)));
.keys()
.copied()
.map(|action_type| (action_type, depth)),
);
} }
} else { } else {
log::error!( log::error!(
@ -383,21 +379,21 @@ impl<'a> WindowContext<'a> {
} }
} }
handler_depths_by_action_type.extend( handler_depths_by_action_id.extend(
self.global_actions self.global_actions
.keys() .keys()
.copied() .copied()
.map(|action_type| (action_type, contexts.len())), .map(|action_id| (action_id, contexts.len())),
); );
self.action_deserializers self.action_deserializers
.iter() .iter()
.filter_map(move |(name, (type_id, deserialize))| { .filter_map(move |(name, (action_id, deserialize))| {
if let Some(action_depth) = handler_depths_by_action_type.get(type_id).copied() { if let Some(action_depth) = handler_depths_by_action_id.get(action_id).copied() {
let action = deserialize(serde_json::Value::Object(Default::default())).ok()?; let action = deserialize(serde_json::Value::Object(Default::default())).ok()?;
let bindings = self let bindings = self
.keystroke_matcher .keystroke_matcher
.bindings_for_action_type(*type_id) .bindings_for_action(*action_id)
.filter(|b| { .filter(|b| {
action.eq(b.action()) action.eq(b.action())
&& (0..=action_depth) && (0..=action_depth)

View file

@ -47,8 +47,8 @@ impl KeymapMatcher {
self.keymap.clear(); self.keymap.clear();
} }
pub fn bindings_for_action_type(&self, action_type: TypeId) -> impl Iterator<Item = &Binding> { pub fn bindings_for_action(&self, action_id: TypeId) -> impl Iterator<Item = &Binding> {
self.keymap.bindings_for_action_type(action_type) self.keymap.bindings_for_action(action_id)
} }
pub fn clear_pending(&mut self) { pub fn clear_pending(&mut self) {

View file

@ -1,39 +1,36 @@
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{any::TypeId, collections::HashMap};
any::{Any, TypeId},
collections::HashMap,
};
use super::Binding; use super::Binding;
#[derive(Default)] #[derive(Default)]
pub struct Keymap { pub struct Keymap {
bindings: Vec<Binding>, bindings: Vec<Binding>,
binding_indices_by_action_type: HashMap<TypeId, SmallVec<[usize; 3]>>, binding_indices_by_action_id: HashMap<TypeId, SmallVec<[usize; 3]>>,
} }
impl Keymap { impl Keymap {
pub fn new(bindings: Vec<Binding>) -> Self { pub fn new(bindings: Vec<Binding>) -> Self {
let mut binding_indices_by_action_type = HashMap::new(); let mut binding_indices_by_action_id = HashMap::new();
for (ix, binding) in bindings.iter().enumerate() { for (ix, binding) in bindings.iter().enumerate() {
binding_indices_by_action_type binding_indices_by_action_id
.entry(binding.action().type_id()) .entry(binding.action().id())
.or_insert_with(SmallVec::new) .or_insert_with(SmallVec::new)
.push(ix); .push(ix);
} }
Self { Self {
binding_indices_by_action_type, binding_indices_by_action_id,
bindings, bindings,
} }
} }
pub(crate) fn bindings_for_action_type( pub(crate) fn bindings_for_action(
&self, &self,
action_type: TypeId, action_id: TypeId,
) -> impl Iterator<Item = &'_ Binding> { ) -> impl Iterator<Item = &'_ Binding> {
self.binding_indices_by_action_type self.binding_indices_by_action_id
.get(&action_type) .get(&action_id)
.map(SmallVec::as_slice) .map(SmallVec::as_slice)
.unwrap_or(&[]) .unwrap_or(&[])
.iter() .iter()
@ -42,8 +39,8 @@ impl Keymap {
pub(crate) fn add_bindings<T: IntoIterator<Item = Binding>>(&mut self, bindings: T) { pub(crate) fn add_bindings<T: IntoIterator<Item = Binding>>(&mut self, bindings: T) {
for binding in bindings { for binding in bindings {
self.binding_indices_by_action_type self.binding_indices_by_action_id
.entry(binding.action().as_any().type_id()) .entry(binding.action().id())
.or_default() .or_default()
.push(self.bindings.len()); .push(self.bindings.len());
self.bindings.push(binding); self.bindings.push(binding);
@ -52,7 +49,7 @@ impl Keymap {
pub(crate) fn clear(&mut self) { pub(crate) fn clear(&mut self) {
self.bindings.clear(); self.bindings.clear();
self.binding_indices_by_action_type.clear(); self.binding_indices_by_action_id.clear();
} }
pub fn bindings(&self) -> &Vec<Binding> { pub fn bindings(&self) -> &Vec<Binding> {

View file

@ -231,7 +231,7 @@ impl MacForegroundPlatform {
} => { } => {
// TODO // TODO
let keystrokes = keystroke_matcher let keystrokes = keystroke_matcher
.bindings_for_action_type(action.as_any().type_id()) .bindings_for_action(action.id())
.find(|binding| binding.action().eq(action.as_ref())) .find(|binding| binding.action().eq(action.as_ref()))
.map(|binding| binding.keystrokes()); .map(|binding| binding.keystrokes());
let selector = match os_action { let selector = match os_action {