mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-15 00:59:31 +03:00
Preserve stateless interactivity when assigning elements an id (#3274)
This commit is contained in:
commit
b33ea3c700
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -4987,8 +4987,8 @@ dependencies = [
|
||||
name = "menu2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"gpui2",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -27,7 +27,6 @@ impl Render for GoToLine {
|
||||
type Element = Div<Self>;
|
||||
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
dbg!("rendering GoToLine");
|
||||
div().bg(red()).w(px(100.0)).h(px(100.0))
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ where
|
||||
{
|
||||
pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteractivity<V>, F> {
|
||||
Div {
|
||||
interactivity: id.into().into(),
|
||||
interactivity: StatefulInteractivity::new(id.into(), self.interactivity),
|
||||
focus: self.focus,
|
||||
children: self.children,
|
||||
group: self.group,
|
||||
|
@ -30,7 +30,7 @@ where
|
||||
.map(|component| component.render())
|
||||
.collect()
|
||||
}),
|
||||
interactivity: id.into(),
|
||||
interactivity: StatefulInteractivity::new(id, StatelessInteractivity::default()),
|
||||
scroll_handle: None,
|
||||
}
|
||||
}
|
||||
|
@ -189,10 +189,10 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
|
||||
{
|
||||
self.stateless_interactivity().key_listeners.push((
|
||||
TypeId::of::<A>(),
|
||||
Box::new(move |view, event, _, phase, cx| {
|
||||
let event = event.downcast_ref().unwrap();
|
||||
Box::new(move |view, action, _dipatch_context, phase, cx| {
|
||||
let action = action.downcast_ref().unwrap();
|
||||
if phase == DispatchPhase::Capture {
|
||||
listener(view, event, cx)
|
||||
listener(view, action, cx)
|
||||
}
|
||||
None
|
||||
}),
|
||||
@ -210,10 +210,10 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
|
||||
{
|
||||
self.stateless_interactivity().key_listeners.push((
|
||||
TypeId::of::<A>(),
|
||||
Box::new(move |view, event, _, phase, cx| {
|
||||
let event = event.downcast_ref().unwrap();
|
||||
Box::new(move |view, action, _dispatch_context, phase, cx| {
|
||||
let action = action.downcast_ref().unwrap();
|
||||
if phase == DispatchPhase::Bubble {
|
||||
listener(view, event, cx)
|
||||
listener(view, action, cx)
|
||||
}
|
||||
|
||||
None
|
||||
@ -407,6 +407,8 @@ pub trait ElementInteractivity<V: 'static>: 'static {
|
||||
) -> R {
|
||||
if let Some(stateful) = self.as_stateful_mut() {
|
||||
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
|
||||
// In addition to any key down/up listeners registered directly on the element,
|
||||
// we also add a key listener to match actions from the keymap.
|
||||
stateful.key_listeners.push((
|
||||
TypeId::of::<KeyDownEvent>(),
|
||||
Box::new(move |_, key_down, context, phase, cx| {
|
||||
@ -774,6 +776,21 @@ pub struct StatefulInteractivity<V> {
|
||||
tooltip_builder: Option<TooltipBuilder<V>>,
|
||||
}
|
||||
|
||||
impl<V: 'static> StatefulInteractivity<V> {
|
||||
pub fn new(id: ElementId, stateless: StatelessInteractivity<V>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
stateless,
|
||||
click_listeners: SmallVec::new(),
|
||||
active_style: StyleRefinement::default(),
|
||||
group_active_style: None,
|
||||
drag_listener: None,
|
||||
hover_listener: None,
|
||||
tooltip_builder: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> ElementInteractivity<V> for StatefulInteractivity<V> {
|
||||
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
|
||||
Some(self)
|
||||
@ -792,21 +809,6 @@ impl<V: 'static> ElementInteractivity<V> for StatefulInteractivity<V> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> From<ElementId> for StatefulInteractivity<V> {
|
||||
fn from(id: ElementId) -> Self {
|
||||
Self {
|
||||
id,
|
||||
stateless: StatelessInteractivity::default(),
|
||||
click_listeners: SmallVec::new(),
|
||||
drag_listener: None,
|
||||
hover_listener: None,
|
||||
tooltip_builder: None,
|
||||
active_style: StyleRefinement::default(),
|
||||
group_active_style: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
|
||||
|
||||
pub struct StatelessInteractivity<V> {
|
||||
@ -1284,14 +1286,8 @@ mod test {
|
||||
fn render(&mut self, _: &mut gpui::ViewContext<Self>) -> Self::Element {
|
||||
div().id("testview").child(
|
||||
div()
|
||||
.on_key_down(|this: &mut TestView, _, _, _| {
|
||||
dbg!("ola!");
|
||||
this.saw_key_down = true
|
||||
})
|
||||
.on_action(|this: &mut TestView, _: &TestAction, _| {
|
||||
dbg!("ola!");
|
||||
this.saw_action = true
|
||||
})
|
||||
.on_key_down(|this: &mut TestView, _, _, _| this.saw_key_down = true)
|
||||
.on_action(|this: &mut TestView, _: &TestAction, _| this.saw_action = true)
|
||||
.track_focus(&self.focus_handle),
|
||||
)
|
||||
}
|
||||
|
@ -1154,7 +1154,6 @@ impl<'a> WindowContext<'a> {
|
||||
.insert(any_mouse_event.type_id(), handlers);
|
||||
}
|
||||
} else if let Some(any_key_event) = event.keyboard_event() {
|
||||
let mut did_handle_action = false;
|
||||
let key_dispatch_stack = mem::take(&mut self.window.current_frame.key_dispatch_stack);
|
||||
let key_event_type = any_key_event.type_id();
|
||||
let mut context_stack = SmallVec::<[&DispatchContext; 16]>::new();
|
||||
@ -1175,7 +1174,6 @@ impl<'a> WindowContext<'a> {
|
||||
self.dispatch_action(action, &key_dispatch_stack[..ix]);
|
||||
}
|
||||
if !self.app.propagate_event {
|
||||
did_handle_action = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -1204,7 +1202,6 @@ impl<'a> WindowContext<'a> {
|
||||
}
|
||||
|
||||
if !self.app.propagate_event {
|
||||
did_handle_action = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -1218,10 +1215,9 @@ impl<'a> WindowContext<'a> {
|
||||
|
||||
drop(context_stack);
|
||||
self.window.current_frame.key_dispatch_stack = key_dispatch_stack;
|
||||
return did_handle_action;
|
||||
}
|
||||
|
||||
true
|
||||
!self.app.propagate_event
|
||||
}
|
||||
|
||||
/// Attempt to map a keystroke to an action based on the keymap.
|
||||
|
@ -9,5 +9,5 @@ path = "src/menu2.rs"
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_derive.workspace = true
|
||||
gpui = { package = "gpui2", path = "../gpui2" }
|
||||
serde = { workspace = true }
|
||||
|
@ -1,25 +1,12 @@
|
||||
use serde_derive::Deserialize;
|
||||
use gpui::actions;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct Cancel;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct Confirm;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct SecondaryConfirm;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct SelectPrev;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct SelectNext;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct SelectFirst;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct SelectLast;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||
pub struct ShowContextMenu;
|
||||
actions!(
|
||||
Cancel,
|
||||
Confirm,
|
||||
SecondaryConfirm,
|
||||
SelectPrev,
|
||||
SelectNext,
|
||||
SelectFirst,
|
||||
SelectLast,
|
||||
ShowContextMenu
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user