Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge remote-tracking branch 'origin/main' into perf-2

Nathan Sobo02606d1fb95c
diff --cc crates/gpui2/src/elements/div.rs
index ff2e87e8e7f,395afdcc884..61501d7a690
--- a/crates/gpui2/src/elements/div.rs
+++ b/crates/gpui2/src/elements/div.rs
@@@ -30,286 -31,20 +31,299 @@@ pub struct GroupStyle
pub style: Box<StyleRefinement>,
}
- pub struct DragMoveEvent<W: Render> {
+ pub struct DragMoveEvent<T> {
pub event: MouseMoveEvent,
- pub drag: View<W>,
+ drag: PhantomData<T>,
+ }
+
+ impl<T: 'static> DragMoveEvent<T> {
+ pub fn drag<'b>(&self, cx: &'b AppContext) -> &'b T {
+ cx.active_drag
+ .as_ref()
+ .and_then(|drag| drag.value.downcast_ref::<T>())
+ .expect("DragMoveEvent is only valid when the stored active drag is of the same type.")
+ }
}
+impl Interactivity {
+ pub fn on_mouse_down(
+ &mut self,
+ button: MouseButton,
+ listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_down_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble
+ && event.button == button
+ && bounds.visibly_contains(&event.position, cx)
+ {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_any_mouse_down(
+ &mut self,
+ listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_down_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_mouse_up(
+ &mut self,
+ button: MouseButton,
+ listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_up_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble
+ && event.button == button
+ && bounds.visibly_contains(&event.position, cx)
+ {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_any_mouse_up(
+ &mut self,
+ listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_up_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_mouse_down_out(
+ &mut self,
+ listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_down_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Capture && !bounds.visibly_contains(&event.position, cx)
+ {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_mouse_up_out(
+ &mut self,
+ button: MouseButton,
+ listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_up_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Capture
+ && event.button == button
+ && !bounds.visibly_contains(&event.position, cx)
+ {
+ (listener)(event, cx);
+ }
+ }));
+ }
+
+ pub fn on_mouse_move(
+ &mut self,
+ listener: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
+ ) {
+ self.mouse_move_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
+ (listener)(event, cx);
+ }
+ }));
+ }
+
- pub fn on_drag_move<W>(
++ pub fn on_drag_move<T>(
+ &mut self,
- listener: impl Fn(&DragMoveEvent<W>, &mut WindowContext) + 'static,
++ listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
+ ) where
- W: Render,
++ T: 'static,
+ {
+ self.mouse_move_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Capture
+ && bounds.drag_target_contains(&event.position, cx)
+ {
- if let Some(view) = cx.active_drag().and_then(|view| view.downcast::<W>().ok())
++ if cx
++ .active_drag
++ .as_ref()
++ .is_some_and(|drag| drag.value.type_id() == TypeId::of::<T>())
+ {
+ (listener)(
+ &DragMoveEvent {
+ event: event.clone(),
- drag: view,
++ drag: PhantomData,
+ },
+ cx,
+ );
+ }
+ }
+ }));
+ }
+
+ pub fn on_scroll_wheel(
+ &mut self,
+ listener: impl Fn(&ScrollWheelEvent, &mut WindowContext) + 'static,
+ ) {
+ self.scroll_wheel_listeners
+ .push(Box::new(move |event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
+ (listener)(event, cx);
+ }
+ }));
+ }
+
+ pub fn capture_action<A: Action>(
+ &mut self,
+ listener: impl Fn(&A, &mut WindowContext) + 'static,
+ ) {
+ self.action_listeners.push((
+ TypeId::of::<A>(),
+ Box::new(move |action, phase, cx| {
+ let action = action.downcast_ref().unwrap();
+ if phase == DispatchPhase::Capture {
+ (listener)(action, cx)
+ }
+ }),
+ ));
+ }
+
+ pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut WindowContext) + 'static) {
+ self.action_listeners.push((
+ TypeId::of::<A>(),
+ Box::new(move |action, phase, cx| {
+ let action = action.downcast_ref().unwrap();
+ if phase == DispatchPhase::Bubble {
+ (listener)(action, cx)
+ }
+ }),
+ ));
+ }
+
+ pub fn on_boxed_action(
+ &mut self,
+ action: &Box<dyn Action>,
+ listener: impl Fn(&Box<dyn Action>, &mut WindowContext) + 'static,
+ ) {
+ let action = action.boxed_clone();
+ self.action_listeners.push((
+ (*action).type_id(),
+ Box::new(move |_, phase, cx| {
+ if phase == DispatchPhase::Bubble {
+ (listener)(&action, cx)
+ }
+ }),
+ ));
+ }
+
+ pub fn on_key_down(&mut self, listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static) {
+ self.key_down_listeners
+ .push(Box::new(move |event, phase, cx| {
+ if phase == DispatchPhase::Bubble {
+ (listener)(event, cx)
+ }
+ }));
+ }
+
+ pub fn capture_key_down(
+ &mut self,
+ listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static,
+ ) {
+ self.key_down_listeners
+ .push(Box::new(move |event, phase, cx| {
+ if phase == DispatchPhase::Capture {
+ listener(event, cx)
+ }
+ }));
+ }
+
+ pub fn on_key_up(&mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static) {
+ self.key_up_listeners
+ .push(Box::new(move |event, phase, cx| {
+ if phase == DispatchPhase::Bubble {
+ listener(event, cx)
+ }
+ }));
+ }
+
+ pub fn capture_key_up(&mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static) {
+ self.key_up_listeners
+ .push(Box::new(move |event, phase, cx| {
+ if phase == DispatchPhase::Capture {
+ listener(event, cx)
+ }
+ }));
+ }
+
- pub fn on_drop<W: 'static>(
- &mut self,
- listener: impl Fn(&View<W>, &mut WindowContext) + 'static,
- ) {
++ pub fn on_drop<T: 'static>(&mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) {
+ self.drop_listeners.push((
- TypeId::of::<W>(),
- Box::new(move |dragged_view, cx| {
- listener(&dragged_view.downcast().unwrap(), cx);
++ TypeId::of::<T>(),
++ Box::new(move |dragged_value, cx| {
++ listener(dragged_value.downcast_ref().unwrap(), cx);
+ }),
+ ));
+ }
+
+ pub fn on_click(&mut self, listener: impl Fn(&ClickEvent, &mut WindowContext) + 'static)
+ where
+ Self: Sized,
+ {
+ self.click_listeners
+ .push(Box::new(move |event, cx| listener(event, cx)));
+ }
+
- pub fn on_drag<W>(&mut self, constructor: impl Fn(&mut WindowContext) -> View<W> + 'static)
- where
++ pub fn on_drag<T, W>(
++ &mut self,
++ value: T,
++ constructor: impl Fn(&T, &mut WindowContext) -> View<W> + 'static,
++ ) where
+ Self: Sized,
++ T: 'static,
+ W: 'static + Render,
+ {
+ debug_assert!(
+ self.drag_listener.is_none(),
+ "calling on_drag more than once on the same element is not supported"
+ );
- self.drag_listener = Some(Box::new(move |cursor_offset, cx| AnyDrag {
- view: constructor(cx).into(),
- cursor_offset,
- }));
++ self.drag_listener = Some((
++ Box::new(value),
++ Box::new(move |value, cx| constructor(value.downcast_ref().unwrap(), cx).into()),
++ ));
+ }
+
+ pub fn on_hover(&mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static)
+ where
+ Self: Sized,
+ {
+ debug_assert!(
+ self.hover_listener.is_none(),
+ "calling on_hover more than once on the same element is not supported"
+ );
+ self.hover_listener = Some(Box::new(listener));
+ }
+
+ pub fn tooltip(&mut self, build_tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static)
+ where
+ Self: Sized,
+ {
+ debug_assert!(
+ self.tooltip_builder.is_none(),
+ "calling tooltip more than once on the same element is not supported"
+ );
+ self.tooltip_builder = Some(Rc::new(build_tooltip));
+ }
+}
+
pub trait InteractiveElement: Sized {
fn interactivity(&mut self) -> &mut Interactivity;
@@@ -413,14 -208,34 +427,14 @@@
self
}
- fn on_drag_move<W>(
+ fn on_drag_move<T>(
mut self,
- listener: impl Fn(&DragMoveEvent<W>, &mut WindowContext) + 'static,
+ listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
) -> Self
where
- W: Render,
+ T: Render,
{
- self.interactivity().mouse_move_listeners.push(Box::new(
- move |event, bounds, phase, cx| {
- if phase == DispatchPhase::Capture
- && bounds.drag_target_contains(&event.position, cx)
- {
- if cx
- .active_drag
- .as_ref()
- .is_some_and(|drag| drag.value.type_id() == TypeId::of::<T>())
- {
- (listener)(
- &DragMoveEvent {
- event: event.clone(),
- drag: PhantomData,
- },
- cx,
- );
- }
- }
- },
- ));
+ self.interactivity().on_drag_move(listener);
self
}
@@@ -514,6 -375,16 +520,11 @@@
));
self
}
+
+ fn on_drop<T: 'static>(mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) -> Self {
- self.interactivity().drop_listeners.push((
- TypeId::of::<T>(),
- Box::new(move |dragged_value, cx| {
- listener(dragged_value.downcast_ref().unwrap(), cx);
- }),
- ));
++ self.interactivity().on_drop(listener);
+ self
+ }
}
pub trait StatefulInteractiveElement: InteractiveElement {
@@@ -574,12 -447,24 +585,17 @@@
self
}
- fn on_drag<W>(mut self, constructor: impl Fn(&mut WindowContext) -> View<W> + 'static) -> Self
+ fn on_drag<T, W>(
+ mut self,
+ value: T,
+ constructor: impl Fn(&T, &mut WindowContext) -> View<W> + 'static,
+ ) -> Self
where
Self: Sized,
+ T: 'static,
W: 'static + Render,
{
- self.interactivity().on_drag(constructor);
- debug_assert!(
- self.interactivity().drag_listener.is_none(),
- "calling on_drag more than once on the same element is not supported"
- );
- self.interactivity().drag_listener = Some((
- Box::new(value),
- Box::new(move |value, cx| constructor(value.downcast_ref().unwrap(), cx).into()),
- ));
++ self.interactivity().on_drag(value, constructor);
self
}
@@@ -1148,20 -1039,14 +1166,20 @@@ impl Interactivity
}
let click_listeners = mem::take(&mut self.click_listeners);
- let drag_listener = mem::take(&mut self.drag_listener);
+ let mut drag_listener = mem::take(&mut self.drag_listener);
if !click_listeners.is_empty() || drag_listener.is_some() {
- let pending_mouse_down = element_state.pending_mouse_down.clone();
+ let pending_mouse_down = element_state
+ .pending_mouse_down
+ .get_or_insert_with(Default::default)
+ .clone();
let mouse_down = pending_mouse_down.borrow().clone();
if let Some(mouse_down) = mouse_down {
- if let Some(drag_listener) = drag_listener {
+ if drag_listener.is_some() {
- let active_state = element_state.clicked_state.clone();
+ let active_state = element_state
+ .clicked_state
+ .get_or_insert_with(Default::default)
+ .clone();
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
diff --cc crates/terminal_view2/src/terminal_element.rs
index 7f221129f03,bced03e7eab..e9335a42203
--- a/crates/terminal_view2/src/terminal_element.rs
+++ b/crates/terminal_view2/src/terminal_element.rs
@@@ -677,36 -687,7 +677,35 @@@ impl TerminalElement
cx.notify();
})
}
- });
+ }
+ });
+ self.interactivity.on_scroll_wheel({
+ let terminal = terminal.clone();
+ move |e, cx| {
+ terminal.update(cx, |terminal, cx| {
+ terminal.scroll_wheel(e, origin);
+ cx.notify();
+ })
+ }
+ });
+
+ self.interactivity.on_drop::<ExternalPaths>({
+ let focus = focus.clone();
+ let terminal = terminal.clone();
+ move |external_paths, cx| {
+ cx.focus(&focus);
+ let mut new_text = external_paths
- .read(cx)
+ .paths()
+ .iter()
+ .map(|path| format!(" {path:?}"))
+ .join("");
+ new_text.push(' ');
+ terminal.update(cx, |terminal, _| {
+ // todo!() long paths are not displayed properly albeit the text is there
+ terminal.paste(&new_text);
+ });
+ }
+ });
// Mouse mode handlers:
// All mouse modes need the extra click handlers
diff --cc crates/workspace2/src/pane_group.rs
index 5d79109dee8,72466d9ce41..966e2c6341a
--- a/crates/workspace2/src/pane_group.rs
+++ b/crates/workspace2/src/pane_group.rs
@@@ -912,7 -906,7 +906,7 @@@ mod element
let mut bounding_boxes = self.bounding_boxes.lock();
bounding_boxes.clear();
- for (ix, mut child) in self.children.iter_mut().enumerate() {
- for (ix, child) in self.children.into_iter().enumerate() {
++ for (ix, child) in self.children.iter_mut().enumerate() {
//todo!(active_pane_magnification)
// If usign active pane magnification, need to switch to using
// 1 for all non-active panes, and then the magnification for the