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 element-types

Nathan Sobo364e3e7de591
diff --cc crates/go_to_line2/src/go_to_line.rs
index abafe93cdd2,1d57be6fd0b..36bd7260305
--- a/crates/go_to_line2/src/go_to_line.rs
+++ b/crates/go_to_line2/src/go_to_line.rs
@@@ -148,8 -148,9 +148,9 @@@ impl Render for GoToLine
type Element = Div<Self>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
- modal(cx)
+ div()
+ .elevation_2(cx)
- .context("GoToLine")
+ .key_context("GoToLine")
.on_action(Self::cancel)
.on_action(Self::confirm)
.w_96()
diff --cc crates/gpui2/src/elements/div.rs
index c0f916ae966,95c44038ed1..aeda28256a8
--- a/crates/gpui2/src/elements/div.rs
+++ b/crates/gpui2/src/elements/div.rs
@@@ -1,295 -1,62 +1,323 @@@
-use std::fmt::Debug;
-
use crate::{
- point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, ElementInteractivity,
- FocusHandle, FocusListeners, Focusable, FocusableKeyDispatch, GlobalElementId, GroupBounds,
- InteractiveElementState, KeyContext, KeyDispatch, LayoutId, NonFocusableKeyDispatch, Overflow,
- ParentElement, Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity,
- StatelessInteractive, StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext,
- Visibility,
+ point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
+ BorrowWindow, Bounds, ClickEvent, Component, DispatchPhase, Element, ElementId, FocusEvent,
+ FocusHandle, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
+ MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, Point, Render, ScrollWheelEvent,
+ SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility,
};
+use collections::HashMap;
+use parking_lot::Mutex;
use refineable::Refineable;
use smallvec::SmallVec;
+use std::{
+ any::{Any, TypeId},
+ fmt::Debug,
+ marker::PhantomData,
+ mem,
+ sync::Arc,
+ time::Duration,
+};
+use taffy::style::Overflow;
use util::ResultExt;
-pub struct Div<
- V: 'static,
- I: ElementInteractivity<V> = StatelessInteractivity<V>,
- K: KeyDispatch<V> = NonFocusableKeyDispatch,
-> {
- interactivity: I,
- key_dispatch: K,
- children: SmallVec<[AnyElement<V>; 2]>,
- group: Option<SharedString>,
- base_style: StyleRefinement,
+const DRAG_THRESHOLD: f64 = 2.;
+const TOOLTIP_DELAY: Duration = Duration::from_millis(500);
+const TOOLTIP_OFFSET: Point<Pixels> = Point::new(px(10.0), px(8.0));
+
+pub struct GroupStyle {
+ pub group: SharedString,
+ pub style: StyleRefinement,
}
-pub fn div<V: 'static>() -> Div<V, StatelessInteractivity<V>, NonFocusableKeyDispatch> {
- Div {
- interactivity: StatelessInteractivity::default(),
- key_dispatch: NonFocusableKeyDispatch::default(),
- children: SmallVec::new(),
- group: None,
- base_style: StyleRefinement::default(),
+pub trait InteractiveComponent<V: 'static>: Sized + Element<V> {
+ fn interactivity(&mut self) -> &mut Interactivity<V>;
+
+ fn group(mut self, group: impl Into<SharedString>) -> Self {
+ self.interactivity().group = Some(group.into());
+ self
}
-}
-impl<V, F> Div<V, StatelessInteractivity<V>, F>
-where
- V: 'static,
- F: KeyDispatch<V>,
-{
- pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteractivity<V>, F> {
- Div {
- interactivity: StatefulInteractivity::new(id.into(), self.interactivity),
- key_dispatch: self.key_dispatch,
- children: self.children,
- group: self.group,
- base_style: self.base_style,
+ fn id(mut self, id: impl Into<ElementId>) -> Stateful<V, Self> {
+ self.interactivity().element_id = Some(id.into());
+
+ Stateful {
+ element: self,
+ view_type: PhantomData,
+ }
+ }
+
+ fn track_focus(mut self, focus_handle: &FocusHandle) -> Focusable<V, Self> {
+ self.interactivity().focusable = true;
+ self.interactivity().tracked_focus_handle = Some(focus_handle.clone());
+ Focusable {
+ element: self,
+ view_type: PhantomData,
}
}
+
+ fn key_context<C, E>(mut self, key_context: C) -> Self
+ where
+ C: TryInto<KeyContext, Error = E>,
+ E: Debug,
+ {
+ if let Some(key_context) = key_context.try_into().log_err() {
+ self.interactivity().key_context = key_context;
+ }
+ self
+ }
+
+ fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
+ self.interactivity().hover_style = f(StyleRefinement::default());
+ self
+ }
+
+ fn group_hover(
+ mut self,
+ group_name: impl Into<SharedString>,
+ f: impl FnOnce(StyleRefinement) -> StyleRefinement,
+ ) -> Self {
+ self.interactivity().group_hover_style = Some(GroupStyle {
+ group: group_name.into(),
+ style: f(StyleRefinement::default()),
+ });
+ self
+ }
+
+ fn on_mouse_down(
+ mut self,
+ button: MouseButton,
+ handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().mouse_down_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble
+ && event.button == button
+ && bounds.contains_point(&event.position)
+ {
+ handler(view, event, cx)
+ }
+ },
+ ));
+ self
+ }
+
++ fn on_any_mouse_down(
++ mut self,
++ handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
++ ) -> Self {
++ self.interactivity().mouse_down_listeners.push(Box::new(
++ move |view, event, bounds, phase, cx| {
++ if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
++ handler(view, event, cx)
++ }
++ },
++ ));
++ self
++ }
++
+ fn on_mouse_up(
+ mut self,
+ button: MouseButton,
+ handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().mouse_up_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble
+ && event.button == button
+ && bounds.contains_point(&event.position)
+ {
+ handler(view, event, cx)
+ }
+ },
+ ));
+ self
+ }
+
++ fn on_any_mouse_up(
++ mut self,
++ handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
++ ) -> Self {
++ self.interactivity().mouse_up_listeners.push(Box::new(
++ move |view, event, bounds, phase, cx| {
++ if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
++ handler(view, event, cx)
++ }
++ },
++ ));
++ self
++ }
++
+ fn on_mouse_down_out(
+ mut self,
+ handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().mouse_down_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
+ handler(view, event, cx)
+ }
+ },
+ ));
+ self
+ }
+
+ fn on_mouse_up_out(
+ mut self,
+ button: MouseButton,
+ handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().mouse_up_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Capture
+ && event.button == button
+ && !bounds.contains_point(&event.position)
+ {
+ handler(view, event, cx);
+ }
+ },
+ ));
+ self
+ }
+
+ fn on_mouse_move(
+ mut self,
+ handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().mouse_move_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
+ handler(view, event, cx);
+ }
+ },
+ ));
+ self
+ }
+
+ fn on_scroll_wheel(
+ mut self,
+ handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().scroll_wheel_listeners.push(Box::new(
+ move |view, event, bounds, phase, cx| {
+ if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
+ handler(view, event, cx);
+ }
+ },
+ ));
+ self
+ }
+
+ /// Capture the given action, fires during the capture phase
+ fn capture_action<A: Action>(
+ mut self,
+ listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().action_listeners.push((
+ TypeId::of::<A>(),
+ Box::new(move |view, action, phase, cx| {
+ let action = action.downcast_ref().unwrap();
+ if phase == DispatchPhase::Capture {
+ listener(view, action, cx)
+ }
+ }),
+ ));
+ self
+ }
+
+ /// Add a listener for the given action, fires during the bubble event phase
+ fn on_action<A: Action>(
+ mut self,
+ listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().action_listeners.push((
+ TypeId::of::<A>(),
+ Box::new(move |view, action, phase, cx| {
+ let action = action.downcast_ref().unwrap();
+ if phase == DispatchPhase::Bubble {
+ listener(view, action, cx)
+ }
+ }),
+ ));
+ self
+ }
+
+ fn on_key_down(
+ mut self,
+ listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity()
+ .key_down_listeners
+ .push(Box::new(move |view, event, phase, cx| {
+ listener(view, event, phase, cx)
+ }));
+ self
+ }
+
+ fn on_key_up(
+ mut self,
+ listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity()
+ .key_up_listeners
+ .push(Box::new(move |view, event, phase, cx| {
+ listener(view, event, phase, cx)
+ }));
+ self
+ }
+
+ fn drag_over<S: 'static>(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
+ self.interactivity()
+ .drag_over_styles
+ .push((TypeId::of::<S>(), f(StyleRefinement::default())));
+ self
+ }
+
+ fn group_drag_over<S: 'static>(
+ mut self,
+ group_name: impl Into<SharedString>,
+ f: impl FnOnce(StyleRefinement) -> StyleRefinement,
+ ) -> Self {
+ self.interactivity().group_drag_over_styles.push((
+ TypeId::of::<S>(),
+ GroupStyle {
+ group: group_name.into(),
+ style: f(StyleRefinement::default()),
+ },
+ ));
+ self
+ }
+
+ fn on_drop<W: 'static>(
+ mut self,
+ listener: impl Fn(&mut V, View<W>, &mut ViewContext<V>) + 'static,
+ ) -> Self {
+ self.interactivity().drop_listeners.push((
+ TypeId::of::<W>(),
+ Box::new(move |view, dragged_view, cx| {
+ listener(view, dragged_view.downcast().unwrap(), cx);
+ }),
+ ));
+ self
+ }
}
-impl<V, I, F> Div<V, I, F>
-where
- I: ElementInteractivity<V>,
- F: KeyDispatch<V>,
-{
- pub fn group(mut self, group: impl Into<SharedString>) -> Self {
- self.group = Some(group.into());
+pub trait StatefulInteractiveComponent<V: 'static, E: Element<V>>: InteractiveComponent<V> {
+ fn focusable(mut self) -> Focusable<V, Self> {
+ self.interactivity().focusable = true;
+ Focusable {
+ element: self,
+ view_type: PhantomData,
+ }
+ }
+
+ fn overflow_scroll(mut self) -> Self {
+ self.interactivity().base_style.overflow.x = Some(Overflow::Scroll);
+ self.interactivity().base_style.overflow.y = Some(Overflow::Scroll);
+ self
+ }
+
+ fn overflow_x_scroll(mut self) -> Self {
+ self.interactivity().base_style.overflow.x = Some(Overflow::Scroll);
self
}
diff --cc crates/gpui2/src/interactive.rs
index 5ce78553fd6,312121c9541..1896957ac80
--- a/crates/gpui2/src/interactive.rs
+++ b/crates/gpui2/src/interactive.rs
@@@ -1,9 -1,944 +1,9 @@@
use crate::{
- div, point, Component, FocusHandle, Keystroke, Modifiers, Div, Pixels, Point, Render,
- div, point, px, Action, AnyDrag, AnyTooltip, AnyView, AppContext, Bounds, Component,
- DispatchPhase, Div, Element, ElementId, FocusHandle, KeyContext, Keystroke, Modifiers,
- Overflow, Pixels, Point, Render, SharedString, Size, Style, StyleRefinement, Task, View,
++ div, point, Component, Div, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render,
ViewContext,
};
-use collections::HashMap;
-use derive_more::{Deref, DerefMut};
-use parking_lot::Mutex;
-use refineable::Refineable;
use smallvec::SmallVec;
-use std::{
- any::{Any, TypeId},
- fmt::Debug,
- marker::PhantomData,
- mem,
- ops::Deref,
- path::PathBuf,
- sync::Arc,
- time::Duration,
-};
-
-const DRAG_THRESHOLD: f64 = 2.;
-const TOOLTIP_DELAY: Duration = Duration::from_millis(500);
-const TOOLTIP_OFFSET: Point<Pixels> = Point::new(px(10.0), px(8.0));
-
-pub trait StatelessInteractive<V: 'static>: Element<V> {
- fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V>;
-
- fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().hover_style = f(StyleRefinement::default());
- self
- }
-
- fn group_hover(
- mut self,
- group_name: impl Into<SharedString>,
- f: impl FnOnce(StyleRefinement) -> StyleRefinement,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().group_hover_style = Some(GroupStyle {
- group: group_name.into(),
- style: f(StyleRefinement::default()),
- });
- self
- }
-
- fn on_mouse_down(
- mut self,
- button: MouseButton,
- handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_down_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble
- && event.button == button
- && bounds.contains_point(&event.position)
- {
- handler(view, event, cx)
- }
- }));
- self
- }
-
- fn on_any_mouse_down(
- mut self,
- handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_down_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- handler(view, event, cx)
- }
- }));
- self
- }
-
- fn on_any_mouse_up(
- mut self,
- handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_up_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- handler(view, event, cx)
- }
- }));
- self
- }
-
- fn on_mouse_up(
- mut self,
- button: MouseButton,
- handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_up_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble
- && event.button == button
- && bounds.contains_point(&event.position)
- {
- handler(view, event, cx)
- }
- }));
- self
- }
-
- fn on_mouse_down_out(
- mut self,
- handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_down_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
- handler(view, event, cx)
- }
- }));
- self
- }
-
- fn on_mouse_up_out(
- mut self,
- handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_up_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
- handler(view, event, cx);
- }
- }));
- self
- }
-
- fn on_mouse_move(
- mut self,
- handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .mouse_move_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- handler(view, event, cx);
- }
- }));
- self
- }
-
- fn on_scroll_wheel(
- mut self,
- handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .scroll_wheel_listeners
- .push(Box::new(move |view, event, bounds, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- handler(view, event, cx);
- }
- }));
- self
- }
-
- /// Capture the given action, fires during the capture phase
- fn capture_action<A: Action>(
- mut self,
- listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().action_listeners.push((
- TypeId::of::<A>(),
- Box::new(move |view, action, phase, cx| {
- let action = action.downcast_ref().unwrap();
- if phase == DispatchPhase::Capture {
- listener(view, action, cx)
- }
- }),
- ));
- self
- }
-
- /// Add a listener for the given action, fires during the bubble event phase
- fn on_action<A: Action>(
- mut self,
- listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().action_listeners.push((
- TypeId::of::<A>(),
- Box::new(move |view, action, phase, cx| {
- let action = action.downcast_ref().unwrap();
- if phase == DispatchPhase::Bubble {
- listener(view, action, cx)
- }
- }),
- ));
- self
- }
-
- fn on_key_down(
- mut self,
- listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .key_down_listeners
- .push(Box::new(move |view, event, phase, cx| {
- listener(view, event, phase, cx)
- }));
- self
- }
-
- fn on_key_up(
- mut self,
- listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .key_up_listeners
- .push(Box::new(move |view, event, phase, cx| {
- listener(view, event, phase, cx)
- }));
- self
- }
-
- fn drag_over<S: 'static>(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity()
- .drag_over_styles
- .push((TypeId::of::<S>(), f(StyleRefinement::default())));
- self
- }
-
- fn group_drag_over<S: 'static>(
- mut self,
- group_name: impl Into<SharedString>,
- f: impl FnOnce(StyleRefinement) -> StyleRefinement,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().group_drag_over_styles.push((
- TypeId::of::<S>(),
- GroupStyle {
- group: group_name.into(),
- style: f(StyleRefinement::default()),
- },
- ));
- self
- }
-
- fn on_drop<W: 'static>(
- mut self,
- listener: impl Fn(&mut V, View<W>, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateless_interactivity().drop_listeners.push((
- TypeId::of::<W>(),
- Box::new(move |view, dragged_view, cx| {
- listener(view, dragged_view.downcast().unwrap(), cx);
- }),
- ));
- self
- }
-}
-
-pub trait StatefulInteractive<V: 'static>: StatelessInteractive<V> {
- fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V>;
-
- fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
- where
- Self: Sized,
- {
- self.stateful_interactivity().active_style = f(StyleRefinement::default());
- self
- }
-
- fn group_active(
- mut self,
- group_name: impl Into<SharedString>,
- f: impl FnOnce(StyleRefinement) -> StyleRefinement,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateful_interactivity().group_active_style = Some(GroupStyle {
- group: group_name.into(),
- style: f(StyleRefinement::default()),
- });
- self
- }
-
- fn on_click(
- mut self,
- listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + 'static,
- ) -> Self
- where
- Self: Sized,
- {
- self.stateful_interactivity()
- .click_listeners
- .push(Box::new(move |view, event, cx| listener(view, event, cx)));
- self
- }
-
- fn on_drag<W>(
- mut self,
- listener: impl Fn(&mut V, &mut ViewContext<V>) -> View<W> + 'static,
- ) -> Self
- where
- Self: Sized,
- W: 'static + Render,
- {
- debug_assert!(
- self.stateful_interactivity().drag_listener.is_none(),
- "calling on_drag more than once on the same element is not supported"
- );
- self.stateful_interactivity().drag_listener =
- Some(Box::new(move |view_state, cursor_offset, cx| AnyDrag {
- view: listener(view_state, cx).into(),
- cursor_offset,
- }));
- self
- }
-
- fn on_hover(mut self, listener: impl 'static + Fn(&mut V, bool, &mut ViewContext<V>)) -> Self
- where
- Self: Sized,
- {
- debug_assert!(
- self.stateful_interactivity().hover_listener.is_none(),
- "calling on_hover more than once on the same element is not supported"
- );
- self.stateful_interactivity().hover_listener = Some(Box::new(listener));
- self
- }
-
- fn tooltip<W>(
- mut self,
- build_tooltip: impl Fn(&mut V, &mut ViewContext<V>) -> View<W> + 'static,
- ) -> Self
- where
- Self: Sized,
- W: 'static + Render,
- {
- debug_assert!(
- self.stateful_interactivity().tooltip_builder.is_none(),
- "calling tooltip more than once on the same element is not supported"
- );
- self.stateful_interactivity().tooltip_builder = Some(Arc::new(move |view_state, cx| {
- build_tooltip(view_state, cx).into()
- }));
-
- self
- }
-}
-
-pub trait ElementInteractivity<V: 'static>: 'static {
- fn as_stateless(&self) -> &StatelessInteractivity<V>;
- fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity<V>;
- fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>;
- fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>>;
-
- fn refine_style(
- &self,
- style: &mut Style,
- bounds: Bounds<Pixels>,
- element_state: &InteractiveElementState,
- cx: &mut ViewContext<V>,
- ) {
- let mouse_position = cx.mouse_position();
- let stateless = self.as_stateless();
- if let Some(group_hover) = stateless.group_hover_style.as_ref() {
- if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
- if group_bounds.contains_point(&mouse_position) {
- style.refine(&group_hover.style);
- }
- }
- }
- if bounds.contains_point(&mouse_position) {
- style.refine(&stateless.hover_style);
- }
-
- if let Some(drag) = cx.active_drag.take() {
- for (state_type, group_drag_style) in &self.as_stateless().group_drag_over_styles {
- if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
- if *state_type == drag.view.entity_type()
- && group_bounds.contains_point(&mouse_position)
- {
- style.refine(&group_drag_style.style);
- }
- }
- }
-
- for (state_type, drag_over_style) in &self.as_stateless().drag_over_styles {
- if *state_type == drag.view.entity_type() && bounds.contains_point(&mouse_position)
- {
- style.refine(drag_over_style);
- }
- }
-
- cx.active_drag = Some(drag);
- }
-
- if let Some(stateful) = self.as_stateful() {
- let active_state = element_state.active_state.lock();
- if active_state.group {
- if let Some(group_style) = stateful.group_active_style.as_ref() {
- style.refine(&group_style.style);
- }
- }
- if active_state.element {
- style.refine(&stateful.active_style);
- }
- }
- }
-
- fn initialize(&mut self, cx: &mut ViewContext<V>) {
- let stateless = self.as_stateless_mut();
-
- for listener in stateless.key_down_listeners.drain(..) {
- cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| {
- listener(state, event, phase, cx);
- })
- }
-
- for listener in stateless.key_up_listeners.drain(..) {
- cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| {
- listener(state, event, phase, cx);
- })
- }
-
- for (action_type, listener) in stateless.action_listeners.drain(..) {
- cx.on_action(action_type, listener)
- }
- }
-
- fn paint(
- &mut self,
- bounds: Bounds<Pixels>,
- content_size: Size<Pixels>,
- overflow: Point<Overflow>,
- element_state: &mut InteractiveElementState,
- cx: &mut ViewContext<V>,
- ) {
- let stateless = self.as_stateless_mut();
- for listener in stateless.mouse_down_listeners.drain(..) {
- cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
- listener(state, event, &bounds, phase, cx);
- })
- }
-
- for listener in stateless.mouse_up_listeners.drain(..) {
- cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
- listener(state, event, &bounds, phase, cx);
- })
- }
-
- for listener in stateless.mouse_move_listeners.drain(..) {
- cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
- listener(state, event, &bounds, phase, cx);
- })
- }
-
- for listener in stateless.scroll_wheel_listeners.drain(..) {
- cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
- listener(state, event, &bounds, phase, cx);
- })
- }
-
- let hover_group_bounds = stateless
- .group_hover_style
- .as_ref()
- .and_then(|group_hover| GroupBounds::get(&group_hover.group, cx));
-
- if let Some(group_bounds) = hover_group_bounds {
- let hovered = group_bounds.contains_point(&cx.mouse_position());
- cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
- if phase == DispatchPhase::Capture {
- if group_bounds.contains_point(&event.position) != hovered {
- cx.notify();
- }
- }
- });
- }
-
- if stateless.hover_style.is_some()
- || (cx.active_drag.is_some() && !stateless.drag_over_styles.is_empty())
- {
- let hovered = bounds.contains_point(&cx.mouse_position());
- cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
- if phase == DispatchPhase::Capture {
- if bounds.contains_point(&event.position) != hovered {
- cx.notify();
- }
- }
- });
- }
-
- if cx.active_drag.is_some() {
- let drop_listeners = mem::take(&mut stateless.drop_listeners);
- cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- if let Some(drag_state_type) =
- cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
- {
- for (drop_state_type, listener) in &drop_listeners {
- if *drop_state_type == drag_state_type {
- let drag = cx
- .active_drag
- .take()
- .expect("checked for type drag state type above");
- listener(view, drag.view.clone(), cx);
- cx.notify();
- cx.stop_propagation();
- }
- }
- }
- }
- });
- }
-
- if let Some(stateful) = self.as_stateful_mut() {
- let click_listeners = mem::take(&mut stateful.click_listeners);
- let drag_listener = mem::take(&mut stateful.drag_listener);
-
- if !click_listeners.is_empty() || drag_listener.is_some() {
- let pending_mouse_down = element_state.pending_mouse_down.clone();
- let mouse_down = pending_mouse_down.lock().clone();
- if let Some(mouse_down) = mouse_down {
- if let Some(drag_listener) = drag_listener {
- let active_state = element_state.active_state.clone();
-
- cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
- if cx.active_drag.is_some() {
- if phase == DispatchPhase::Capture {
- cx.notify();
- }
- } else if phase == DispatchPhase::Bubble
- && bounds.contains_point(&event.position)
- && (event.position - mouse_down.position).magnitude()
- > DRAG_THRESHOLD
- {
- *active_state.lock() = ActiveState::default();
- let cursor_offset = event.position - bounds.origin;
- let drag = drag_listener(view_state, cursor_offset, cx);
- cx.active_drag = Some(drag);
- cx.notify();
- cx.stop_propagation();
- }
- });
- }
-
- cx.on_mouse_event(move |view_state, event: &MouseUpEvent, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position)
- {
- let mouse_click = ClickEvent {
- down: mouse_down.clone(),
- up: event.clone(),
- };
- for listener in &click_listeners {
- listener(view_state, &mouse_click, cx);
- }
- }
- *pending_mouse_down.lock() = None;
- });
- } else {
- cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position)
- {
- *pending_mouse_down.lock() = Some(event.clone());
- }
- });
- }
- }
-
- if let Some(hover_listener) = stateful.hover_listener.take() {
- let was_hovered = element_state.hover_state.clone();
- let has_mouse_down = element_state.pending_mouse_down.clone();
-
- cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
- if phase != DispatchPhase::Bubble {
- return;
- }
- let is_hovered =
- bounds.contains_point(&event.position) && has_mouse_down.lock().is_none();
- let mut was_hovered = was_hovered.lock();
-
- if is_hovered != was_hovered.clone() {
- *was_hovered = is_hovered;
- drop(was_hovered);
-
- hover_listener(view_state, is_hovered, cx);
- }
- });
- }
-
- if let Some(tooltip_builder) = stateful.tooltip_builder.take() {
- let active_tooltip = element_state.active_tooltip.clone();
- let pending_mouse_down = element_state.pending_mouse_down.clone();
-
- cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
- if phase != DispatchPhase::Bubble {
- return;
- }
-
- let is_hovered = bounds.contains_point(&event.position)
- && pending_mouse_down.lock().is_none();
- if !is_hovered {
- active_tooltip.lock().take();
- return;
- }
-
- if active_tooltip.lock().is_none() {
- let task = cx.spawn({
- let active_tooltip = active_tooltip.clone();
- let tooltip_builder = tooltip_builder.clone();
-
- move |view, mut cx| async move {
- cx.background_executor().timer(TOOLTIP_DELAY).await;
- view.update(&mut cx, move |view_state, cx| {
- active_tooltip.lock().replace(ActiveTooltip {
- waiting: None,
- tooltip: Some(AnyTooltip {
- view: tooltip_builder(view_state, cx),
- cursor_offset: cx.mouse_position() + TOOLTIP_OFFSET,
- }),
- });
- cx.notify();
- })
- .ok();
- }
- });
- active_tooltip.lock().replace(ActiveTooltip {
- waiting: Some(task),
- tooltip: None,
- });
- }
- });
-
- if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() {
- if active_tooltip.tooltip.is_some() {
- cx.active_tooltip = active_tooltip.tooltip.clone()
- }
- }
- }
-
- let active_state = element_state.active_state.clone();
- if active_state.lock().is_none() {
- let active_group_bounds = stateful
- .group_active_style
- .as_ref()
- .and_then(|group_active| GroupBounds::get(&group_active.group, cx));
- cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| {
- if phase == DispatchPhase::Bubble {
- let group = active_group_bounds
- .map_or(false, |bounds| bounds.contains_point(&down.position));
- let element = bounds.contains_point(&down.position);
- if group || element {
- *active_state.lock() = ActiveState { group, element };
- cx.notify();
- }
- }
- });
- } else {
- cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
- if phase == DispatchPhase::Capture {
- *active_state.lock() = ActiveState::default();
- cx.notify();
- }
- });
- }
-
- if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
- let scroll_offset = element_state
- .scroll_offset
- .get_or_insert_with(Arc::default)
- .clone();
- let line_height = cx.line_height();
- let scroll_max = (content_size - bounds.size).max(&Size::default());
-
- cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
- if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
- let mut scroll_offset = scroll_offset.lock();
- let old_scroll_offset = *scroll_offset;
- let delta = event.delta.pixel_delta(line_height);
-
- if overflow.x == Overflow::Scroll {
- scroll_offset.x =
- (scroll_offset.x + delta.x).clamp(-scroll_max.width, px(0.));
- }
-
- if overflow.y == Overflow::Scroll {
- scroll_offset.y =
- (scroll_offset.y + delta.y).clamp(-scroll_max.height, px(0.));
- }
-
- if *scroll_offset != old_scroll_offset {
- cx.notify();
- cx.stop_propagation();
- }
- }
- });
- }
- }
- }
-}
-
-#[derive(Deref, DerefMut)]
-pub struct StatefulInteractivity<V> {
- pub id: ElementId,
- #[deref]
- #[deref_mut]
- stateless: StatelessInteractivity<V>,
- click_listeners: SmallVec<[ClickListener<V>; 2]>,
- active_style: StyleRefinement,
- group_active_style: Option<GroupStyle>,
- drag_listener: Option<DragListener<V>>,
- hover_listener: Option<HoverListener<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)
- }
-
- fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>> {
- Some(self)
- }
-
- fn as_stateless(&self) -> &StatelessInteractivity<V> {
- &self.stateless
- }
-
- fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity<V> {
- &mut self.stateless
- }
-}
-
-type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
-
-pub struct StatelessInteractivity<V> {
- pub dispatch_context: KeyContext,
- pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>,
- pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>,
- pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
- pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener<V>; 2]>,
- pub key_down_listeners: SmallVec<[KeyDownListener<V>; 2]>,
- pub key_up_listeners: SmallVec<[KeyUpListener<V>; 2]>,
- pub action_listeners: SmallVec<[(TypeId, ActionListener<V>); 8]>,
- pub hover_style: StyleRefinement,
- pub group_hover_style: Option<GroupStyle>,
- drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>,
- group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>,
- drop_listeners: SmallVec<[(TypeId, Box<DropListener<V>>); 2]>,
-}
-
-impl<V> StatelessInteractivity<V> {
- pub fn into_stateful(self, id: impl Into<ElementId>) -> StatefulInteractivity<V> {
- StatefulInteractivity {
- id: id.into(),
- stateless: self,
- click_listeners: SmallVec::new(),
- drag_listener: None,
- hover_listener: None,
- tooltip_builder: None,
- active_style: StyleRefinement::default(),
- group_active_style: None,
- }
- }
-}
-
-pub struct GroupStyle {
- pub group: SharedString,
- pub style: StyleRefinement,
-}
-
-#[derive(Default)]
-pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
-
-impl GroupBounds {
- pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
- cx.default_global::<Self>()
- .0
- .get(name)
- .and_then(|bounds_stack| bounds_stack.last())
- .cloned()
- }
-
- pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
- cx.default_global::<Self>()
- .0
- .entry(name)
- .or_default()
- .push(bounds);
- }
-
- pub fn pop(name: &SharedString, cx: &mut AppContext) {
- cx.default_global::<Self>().0.get_mut(name).unwrap().pop();
- }
-}
-
-#[derive(Copy, Clone, Default, Eq, PartialEq)]
-struct ActiveState {
- pub group: bool,
- pub element: bool,
-}
-
-impl ActiveState {
- pub fn is_none(&self) -> bool {
- !self.group && !self.element
- }
-}
-
-#[derive(Default)]
-pub struct InteractiveElementState {
- active_state: Arc<Mutex<ActiveState>>,
- hover_state: Arc<Mutex<bool>>,
- pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>,
- scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>,
- active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>,
-}
-
-struct ActiveTooltip {
- #[allow(unused)] // used to drop the task
- waiting: Option<Task<()>>,
- tooltip: Option<AnyTooltip>,
-}
-
-impl InteractiveElementState {
- pub fn scroll_offset(&self) -> Option<Point<Pixels>> {
- self.scroll_offset
- .as_ref()
- .map(|offset| offset.lock().clone())
- }
-
- pub fn track_scroll_offset(&mut self) -> Arc<Mutex<Point<Pixels>>> {
- self.scroll_offset
- .get_or_insert_with(|| Arc::new(Mutex::new(Default::default())))
- .clone()
- }
-}
-
-impl<V> Default for StatelessInteractivity<V> {
- fn default() -> Self {
- Self {
- dispatch_context: KeyContext::default(),
- mouse_down_listeners: SmallVec::new(),
- mouse_up_listeners: SmallVec::new(),
- mouse_move_listeners: SmallVec::new(),
- scroll_wheel_listeners: SmallVec::new(),
- key_down_listeners: SmallVec::new(),
- key_up_listeners: SmallVec::new(),
- action_listeners: SmallVec::new(),
- hover_style: StyleRefinement::default(),
- group_hover_style: None,
- drag_over_styles: SmallVec::new(),
- group_drag_over_styles: SmallVec::new(),
- drop_listeners: SmallVec::new(),
- }
- }
-}
-
-impl<V: 'static> ElementInteractivity<V> for StatelessInteractivity<V> {
- fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
- None
- }
-
- fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>> {
- None
- }
-
- fn as_stateless(&self) -> &StatelessInteractivity<V> {
- self
- }
-
- fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity<V> {
- self
- }
-}
+use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent {
@@@ -286,8 -1225,42 +286,8 @@@ pub struct FocusEvent
#[cfg(test)]
mod test {
use crate::{
- self as gpui, div, FocusHandle, InteractiveComponent, KeyBinding, Keystroke, Div,
- self as gpui, div, Div, FocusHandle, KeyBinding, Keystroke, ParentElement, Render,
- StatefulInteractivity, StatelessInteractive, TestAppContext, VisualContext,
++ self as gpui, div, Div, FocusHandle, InteractiveComponent, KeyBinding, Keystroke,
+ ParentComponent, Render, Stateful, TestAppContext, VisualContext,
};
struct TestView {
diff --cc crates/gpui2/src/key_dispatch.rs
index b06acae43d8,323fd7d2ff3..f737c6e30b2
--- a/crates/gpui2/src/key_dispatch.rs
+++ b/crates/gpui2/src/key_dispatch.rs
@@@ -1,6 -1,7 +1,6 @@@
use crate::{
- build_action_from_type, Action, DispatchPhase, FocusId, KeyContext, KeyMatch, Keymap,
- Keystroke, KeystrokeMatcher, WindowContext,
- build_action_from_type, Action, Bounds, DispatchPhase, Element, FocusEvent, FocusHandle,
- FocusId, KeyBinding, KeyContext, KeyMatch, Keymap, Keystroke, KeystrokeMatcher, MouseDownEvent,
- Pixels, Style, StyleRefinement, ViewContext, WindowContext,
++ build_action_from_type, Action, DispatchPhase, FocusId, KeyBinding, KeyContext, KeyMatch,
++ Keymap, Keystroke, KeystrokeMatcher, WindowContext,
};
use collections::HashMap;
use parking_lot::Mutex;
diff --cc crates/picker2/src/picker2.rs
index 0a2a50deb6a,0cfe5c8992a..199941ef3b8
--- a/crates/picker2/src/picker2.rs
+++ b/crates/picker2/src/picker2.rs
@@@ -1,7 -1,7 +1,7 @@@
use editor::Editor;
use gpui::{
- div, uniform_list, Component, Div, ParentComponent, Render, Styled, Task,
- UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
- div, uniform_list, Component, Div, MouseButton, ParentElement, Render, StatelessInteractive,
- Styled, Task, UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
++ div, prelude::*, uniform_list, Component, Div, MouseButton, Render, Task,
++ UniformListScrollHandle, View, ViewContext, WindowContext,
};
use std::{cmp, sync::Arc};
use ui::{prelude::*, v_stack, Divider, Label, LabelColor};
diff --cc crates/ui2/src/components/palette.rs
index 249e577ff10,4e1034595db..c72722dc086
--- a/crates/ui2/src/components/palette.rs
+++ b/crates/ui2/src/components/palette.rs
@@@ -1,5 -1,5 +1,5 @@@
- use crate::{h_stack, prelude::*, v_stack, Keybinding, Label, LabelColor};
-use crate::prelude::*;
-use crate::{h_stack, v_stack, KeyBinding, Label, LabelColor};
++use crate::{h_stack, prelude::*, v_stack, KeyBinding, Label, LabelColor};
+use gpui::prelude::*;
#[derive(Component)]
pub struct Palette {
diff --cc crates/ui2/src/components/tooltip.rs
index 80c6863f681,e6c0e3f44da..10316c609a1
--- a/crates/ui2/src/components/tooltip.rs
+++ b/crates/ui2/src/components/tooltip.rs
@@@ -1,6 -1,8 +1,8 @@@
-use gpui::{div, Div, ParentElement, Render, SharedString, Styled, ViewContext};
+use gpui::{div, Div, ParentComponent, Render, SharedString, Styled, ViewContext};
use theme2::ActiveTheme;
+ use crate::StyledExt;
+
#[derive(Clone, Debug)]
pub struct TextTooltip {
title: SharedString,
diff --cc crates/workspace2/src/modal_layer.rs
index 3ebb2749692,b3a5de8fb26..0703d0dc563
--- a/crates/workspace2/src/modal_layer.rs
+++ b/crates/workspace2/src/modal_layer.rs
@@@ -1,8 -1,8 +1,8 @@@
use gpui::{
- div, px, AnyView, Div, EventEmitter, FocusHandle, ParentElement, Render, StatelessInteractive,
- Styled, Subscription, View, ViewContext, VisualContext, WindowContext,
+ div, prelude::*, px, AnyView, EventEmitter, FocusHandle, Div, Render, Subscription, View,
+ ViewContext, WindowContext,
};
- use ui::v_stack;
+ use ui::{h_stack, v_stack};
pub struct ActiveModal {
modal: AnyView,