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 branch 'main' into perf-2

Mikayla Maki8418f25d0a72
diff --cc crates/gpui2/src/elements/canvas.rs
index 56cfef45534,6a7d68e831b..d04c65811ff
--- a/crates/gpui2/src/elements/canvas.rs
+++ b/crates/gpui2/src/elements/canvas.rs
@@@ -37,11 -37,11 +37,13 @@@ impl Element for Canvas
let mut style = Style::default();
style.refine(&self.style);
let layout_id = cx.request_layout(&style, []);
- (layout_id, ())
+ (layout_id, style)
}
- fn paint(&mut self, bounds: Bounds<Pixels>, _: &mut (), cx: &mut WindowContext) {
- (self.paint_callback.take().unwrap())(&bounds, cx)
- fn paint(self, bounds: Bounds<Pixels>, style: &mut Style, cx: &mut WindowContext) {
- style.paint(bounds, cx, |cx| (self.paint_callback)(&bounds, cx));
++ fn paint(&mut self, bounds: Bounds<Pixels>, style: &mut Style, cx: &mut WindowContext) {
++ style.paint(bounds, cx, |cx| {
++ (self.paint_callback.take().unwrap())(&bounds, cx)
++ });
}
}
diff --cc crates/gpui2/src/elements/div.rs
index 2702ed72f43,aa77480944b..f38c8503851
--- a/crates/gpui2/src/elements/div.rs
+++ b/crates/gpui2/src/elements/div.rs
@@@ -45,285 -45,6 +45,285 @@@ impl<T: 'static> DragMoveEvent<T>
}
}
+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<T>(
+ &mut self,
+ listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
+ ) where
+ 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 cx
+ .active_drag
+ .as_ref()
- .is_some_and(|drag| drag.value.type_id() == TypeId::of::<T>())
++ .is_some_and(|drag| (*drag.value).type_id() == TypeId::of::<T>())
+ {
+ (listener)(
+ &DragMoveEvent {
+ event: event.clone(),
+ 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<T: 'static>(&mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) {
+ self.drop_listeners.push((
+ 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<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(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;
@@@ -432,9 -213,29 +432,9 @@@
listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
) -> Self
where
- T: Render,
+ T: 'static,
{
- 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
}