diff --git a/crates/gpui3/src/action.rs b/crates/gpui3/src/action.rs index fb8ce592a4ef6a678e807d1e37d538a45861af2f..0532790e60a8961c795bae63595bd0115da27734 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 418303bcff06b4474cef5fcd7e1f528164cceaa7..829f7a3b2cfa7a816b76ebc7c1acd3229b57aa18 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 3013d948cfa8aa1a0d699a8b7645c8d8b43d3828..f142663cbd4a97bdbe4f75cde6718a3aaaf34c65 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<()>, }