diff --git a/crates/gpui3/src/action.rs b/crates/gpui3/src/action.rs index fb8ce592a4..0532790e60 100644 --- a/crates/gpui3/src/action.rs +++ b/crates/gpui3/src/action.rs @@ -4,11 +4,31 @@ use collections::{HashMap, HashSet}; use std::any::Any; pub trait Action: Any + Send + Sync { - fn eq(&self, action: &dyn Action) -> bool; + fn partial_eq(&self, action: &dyn Action) -> bool; fn boxed_clone(&self) -> Box; fn as_any(&self) -> &dyn Any; } +impl Action for T +where + T: Any + PartialEq + Clone + Send + Sync, +{ + fn partial_eq(&self, action: &dyn Action) -> bool { + action + .as_any() + .downcast_ref::() + .map_or(false, |a| self == a) + } + + fn boxed_clone(&self) -> Box { + Box::new(self.clone()) + } + + fn as_any(&self) -> &dyn Any { + self + } +} + #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct DispatchContext { set: HashSet, diff --git a/crates/gpui3/src/keymap/binding.rs b/crates/gpui3/src/keymap/binding.rs index 418303bcff..829f7a3b2c 100644 --- a/crates/gpui3/src/keymap/binding.rs +++ b/crates/gpui3/src/keymap/binding.rs @@ -63,7 +63,7 @@ impl KeyBinding { action: &dyn Action, contexts: &[&DispatchContext], ) -> Option> { - if self.action.eq(action) && self.matches_context(contexts) { + if self.action.partial_eq(action) && self.matches_context(contexts) { Some(self.keystrokes.clone()) } else { None diff --git a/crates/storybook2/src/stories/focus.rs b/crates/storybook2/src/stories/focus.rs index 3013d948cf..f142663cbd 100644 --- a/crates/storybook2/src/stories/focus.rs +++ b/crates/storybook2/src/stories/focus.rs @@ -1,61 +1,18 @@ use crate::themes::rose_pine; use gpui3::{ - div, view, Action, Context, Focusable, KeyBinding, ParentElement, StatelessInteractive, Styled, - View, WindowContext, + div, view, Context, Focusable, KeyBinding, ParentElement, StatelessInteractive, Styled, View, + WindowContext, }; -use std::any::Any; -#[derive(Clone)] +#[derive(Clone, PartialEq)] struct ActionA; -impl Action for ActionA { - fn eq(&self, action: &dyn Action) -> bool { - action.as_any().downcast_ref::().is_some() - } - - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - - fn as_any(&self) -> &dyn Any { - self - } -} - -#[derive(Clone)] +#[derive(Clone, PartialEq)] struct ActionB; -impl Action for ActionB { - fn eq(&self, action: &dyn Action) -> bool { - action.as_any().downcast_ref::().is_some() - } - - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - - fn as_any(&self) -> &dyn Any { - self - } -} - -#[derive(Clone)] +#[derive(Clone, PartialEq)] struct ActionC; -impl Action for ActionC { - fn eq(&self, action: &dyn Action) -> bool { - action.as_any().downcast_ref::().is_some() - } - - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) - } - - fn as_any(&self) -> &dyn Any { - self - } -} - pub struct FocusStory { text: View<()>, }