Merge branch 'gpui2-element-renderer' into zed2

This commit is contained in:
Marshall Bowers 2023-10-26 15:23:02 +02:00
commit d62c51a4b8
66 changed files with 897 additions and 1346 deletions

View file

@ -3,36 +3,37 @@ use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem};
pub trait Element: IntoAnyElement<Self::ViewState> {
type ViewState: 'static;
pub trait Element<V: 'static> {
type ElementState: 'static;
fn id(&self) -> Option<ElementId>;
/// Called to initialize this element for the current frame. If this
/// element had state in a previous frame, it will be passed in for the 3rd argument.
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState;
// where
// Self::ViewState: Any + Send + Sync;
// V: Any + Send + Sync;
fn layout(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId;
// where
// Self::ViewState: Any + Send + Sync;
// V: Any + Send + Sync;
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
);
// where
@ -42,26 +43,23 @@ pub trait Element: IntoAnyElement<Self::ViewState> {
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ParentElement: Element {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]>;
pub trait ParentElement<V: 'static> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
fn child(mut self, child: impl IntoAnyElement<Self::ViewState>) -> Self
fn child(mut self, child: impl Component<V>) -> Self
where
Self: Sized,
{
self.children_mut().push(child.into_any());
self.children_mut().push(child.render());
self
}
fn children(
mut self,
iter: impl IntoIterator<Item = impl IntoAnyElement<Self::ViewState>>,
) -> Self
fn children(mut self, iter: impl IntoIterator<Item = impl Component<V>>) -> Self
where
Self: Sized,
{
self.children_mut()
.extend(iter.into_iter().map(|item| item.into_any()));
.extend(iter.into_iter().map(|item| item.render()));
self
}
}
@ -72,7 +70,7 @@ trait ElementObject<V> {
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
}
struct RenderedElement<E: Element> {
struct RenderedElement<V: 'static, E: Element<V>> {
element: E,
phase: ElementRenderPhase<E::ElementState>,
}
@ -94,7 +92,7 @@ enum ElementRenderPhase<V> {
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
/// improved usability.
impl<E: Element> RenderedElement<E> {
impl<V, E: Element<V>> RenderedElement<V, E> {
fn new(element: E) -> Self {
RenderedElement {
element,
@ -103,13 +101,13 @@ impl<E: Element> RenderedElement<E> {
}
}
impl<E> ElementObject<E::ViewState> for RenderedElement<E>
impl<V, E> ElementObject<V> for RenderedElement<V, E>
where
E: Element,
E: Element<V>,
// E::ViewState: Any + Send + Sync,
E::ElementState: Any + Send + Sync,
{
fn initialize(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
let frame_state = if let Some(id) = self.element.id() {
cx.with_element_state(id, |element_state, cx| {
let element_state = self.element.initialize(view_state, element_state, cx);
@ -124,7 +122,7 @@ where
self.phase = ElementRenderPhase::Initialized { frame_state };
}
fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
fn layout(&mut self, state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
let layout_id;
let mut frame_state;
match mem::take(&mut self.phase) {
@ -154,7 +152,7 @@ where
layout_id
}
fn paint(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.phase = match mem::take(&mut self.phase) {
ElementRenderPhase::LayoutRequested {
layout_id,
@ -182,11 +180,15 @@ where
pub struct AnyElement<V>(Box<dyn ElementObject<V> + Send + Sync>);
unsafe impl<V> Send for AnyElement<V> {}
unsafe impl<V> Sync for AnyElement<V> {}
impl<V> AnyElement<V> {
pub fn new<E>(element: E) -> Self
where
V: 'static,
E: 'static + Send + Sync,
E: Element<ViewState = V>,
E: Element<V>,
E::ElementState: Any + Send + Sync,
{
AnyElement(Box::new(RenderedElement::new(element)))
@ -205,12 +207,88 @@ impl<V> AnyElement<V> {
}
}
pub trait IntoAnyElement<V> {
fn into_any(self) -> AnyElement<V>;
}
pub trait Component<V> {
fn render(self) -> AnyElement<V>;
impl<V> IntoAnyElement<V> for AnyElement<V> {
fn into_any(self) -> AnyElement<V> {
fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
where
Self: Sized,
{
if condition {
self = then(self);
}
self
}
}
impl<V> Component<V> for AnyElement<V> {
fn render(self) -> AnyElement<V> {
self
}
}
impl<V, E, F> Element<V> for Option<F>
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
{
type ElementState = AnyElement<V>;
fn id(&self) -> Option<ElementId> {
None
}
fn initialize(
&mut self,
view_state: &mut V,
_rendered_element: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
let render = self.take().unwrap();
let mut rendered_element = (render)(view_state, cx).render();
rendered_element.initialize(view_state, cx);
rendered_element
}
fn layout(
&mut self,
view_state: &mut V,
rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) -> LayoutId {
rendered_element.layout(view_state, cx)
}
fn paint(
&mut self,
_bounds: Bounds<Pixels>,
view_state: &mut V,
rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) {
rendered_element.paint(view_state, cx)
}
}
impl<V, E, F> Component<V> for Option<F>
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
{
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, E, F> Component<V> for F
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
{
fn render(self) -> AnyElement<V> {
AnyElement::new(Some(self))
}
}

View file

@ -1,7 +1,7 @@
use crate::{
point, AnyElement, BorrowWindow, Bounds, Element, ElementFocus, ElementId, ElementInteraction,
FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable, GlobalElementId,
GroupBounds, InteractiveElementState, IntoAnyElement, LayoutId, Overflow, ParentElement,
point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementFocus, ElementId,
ElementInteraction, FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable,
GlobalElementId, GroupBounds, InteractiveElementState, LayoutId, Overflow, ParentElement,
Pixels, Point, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, Style, StyleRefinement, Styled, ViewContext,
};
@ -160,7 +160,7 @@ impl<V: 'static> Div<V, StatelessInteraction<V>, FocusDisabled> {
}
}
impl<V, I> Focusable for Div<V, I, FocusEnabled<V>>
impl<V, I> Focusable<V> for Div<V, I, FocusEnabled<V>>
where
V: 'static,
I: ElementInteraction<V>,
@ -189,12 +189,11 @@ pub struct DivState {
child_layout_ids: SmallVec<[LayoutId; 4]>,
}
impl<V, I, F> Element for Div<V, I, F>
impl<V, I, F> Element<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<ElementId> {
@ -205,9 +204,9 @@ where
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
let mut element_state = element_state.unwrap_or_default();
self.focus
@ -224,9 +223,9 @@ where
fn layout(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
let style = self.compute_style(Bounds::default(), element_state, cx);
style.apply_text_style(cx, |cx| {
@ -245,9 +244,9 @@ where
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) {
self.with_element_id(cx, |this, _global_id, cx| {
if let Some(group) = this.group.clone() {
@ -304,23 +303,23 @@ where
}
}
impl<V, I, F> IntoAnyElement<V> for Div<V, I, F>
impl<V, I, F> Component<V> for Div<V, I, F>
where
// V: Any + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn into_any(self) -> AnyElement<V> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> ParentElement for Div<V, I, F>
impl<V, I, F> ParentElement<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
&mut self.children
}
}
@ -335,7 +334,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Div<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -345,11 +344,11 @@ where
}
}
impl<V, F> StatefulInteractive for Div<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Div<V, StatefulInteraction<V>, F>
where
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
&mut self.interaction
}
}

View file

@ -1,6 +1,6 @@
use crate::{
div, AnyElement, BorrowWindow, Bounds, Div, DivState, Element, ElementFocus, ElementId,
ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, IntoAnyElement,
div, AnyElement, BorrowWindow, Bounds, Component, Div, DivState, Element, ElementFocus,
ElementId, ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable,
LayoutId, Pixels, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, StyleRefinement, Styled, ViewContext,
};
@ -55,22 +55,21 @@ where
}
}
impl<V, I, F> IntoAnyElement<V> for Img<V, I, F>
impl<V, I, F> Component<V> for Img<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn into_any(self) -> AnyElement<V> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> Element for Img<V, I, F>
impl<V, I, F> Element<V> for Img<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
@ -90,7 +89,7 @@ where
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
@ -143,7 +142,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Img<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Img<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -153,21 +152,21 @@ where
}
}
impl<V, F> StatefulInteractive for Img<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Img<V, StatefulInteraction<V>, F>
where
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
self.base.stateful_interaction()
}
}
impl<V, I> Focusable for Img<V, I, FocusEnabled<V>>
impl<V, I> Focusable<V> for Img<V, I, FocusEnabled<V>>
where
V: 'static,
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
self.base.focus_listeners()
}

View file

@ -1,6 +1,6 @@
use crate::{
div, AnyElement, Bounds, Div, DivState, Element, ElementFocus, ElementId, ElementInteraction,
FocusDisabled, FocusEnabled, FocusListeners, Focusable, IntoAnyElement, LayoutId, Pixels,
div, AnyElement, Bounds, Component, Div, DivState, Element, ElementFocus, ElementId,
ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, LayoutId, Pixels,
SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, StyleRefinement, Styled, ViewContext,
};
@ -45,22 +45,21 @@ where
}
}
impl<V, I, F> IntoAnyElement<V> for Svg<V, I, F>
impl<V, I, F> Component<V> for Svg<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn into_any(self) -> AnyElement<V> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> Element for Svg<V, I, F>
impl<V, I, F> Element<V> for Svg<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
@ -80,7 +79,7 @@ where
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
@ -88,7 +87,7 @@ where
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view: &mut Self::ViewState,
view: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) where
@ -116,7 +115,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Svg<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Svg<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -126,21 +125,21 @@ where
}
}
impl<V, F> StatefulInteractive for Svg<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Svg<V, StatefulInteraction<V>, F>
where
V: 'static,
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
self.base.stateful_interaction()
}
}
impl<V: 'static, I> Focusable for Svg<V, I, FocusEnabled<V>>
impl<V: 'static, I> Focusable<V> for Svg<V, I, FocusEnabled<V>>
where
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
self.base.focus_listeners()
}

View file

@ -1,41 +1,41 @@
use crate::{
AnyElement, BorrowWindow, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels,
SharedString, Size, ViewContext,
AnyElement, BorrowWindow, Bounds, Component, Element, LayoutId, Line, Pixels, SharedString,
Size, ViewContext,
};
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::{marker::PhantomData, sync::Arc};
use util::ResultExt;
impl<V: 'static> IntoAnyElement<V> for SharedString {
fn into_any(self) -> AnyElement<V> {
impl<V: 'static> Component<V> for SharedString {
fn render(self) -> AnyElement<V> {
Text {
text: self,
state_type: PhantomData,
}
.into_any()
.render()
}
}
impl<V: 'static> IntoAnyElement<V> for &'static str {
fn into_any(self) -> AnyElement<V> {
impl<V: 'static> Component<V> for &'static str {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
state_type: PhantomData,
}
.into_any()
.render()
}
}
// TODO: Figure out how to pass `String` to `child` without this.
// This impl doesn't exist in the `gpui2` crate.
impl<V: 'static> IntoAnyElement<V> for String {
fn into_any(self) -> AnyElement<V> {
impl<V: 'static> Component<V> for String {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
state_type: PhantomData,
}
.into_any()
.render()
}
}
@ -47,14 +47,13 @@ pub struct Text<V> {
unsafe impl<V> Send for Text<V> {}
unsafe impl<V> Sync for Text<V> {}
impl<V: 'static> IntoAnyElement<V> for Text<V> {
fn into_any(self) -> AnyElement<V> {
impl<V: 'static> Component<V> for Text<V> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V: 'static> Element for Text<V> {
type ViewState = V;
impl<V: 'static> Element<V> for Text<V> {
type ElementState = Arc<Mutex<Option<TextElementState>>>;
fn id(&self) -> Option<crate::ElementId> {

View file

@ -11,8 +11,8 @@ pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Arc<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static>;
pub trait Focusable: Element {
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState>;
pub trait Focusable<V: 'static>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
fn set_focus_style(&mut self, style: StyleRefinement);
fn set_focus_in_style(&mut self, style: StyleRefinement);
fn set_in_focus_style(&mut self, style: StyleRefinement);
@ -43,10 +43,7 @@ pub trait Focusable: Element {
fn on_focus(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -62,10 +59,7 @@ pub trait Focusable: Element {
fn on_blur(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -81,10 +75,7 @@ pub trait Focusable: Element {
fn on_focus_in(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -109,10 +100,7 @@ pub trait Focusable: Element {
fn on_focus_out(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,

View file

@ -1,7 +1,7 @@
use crate::{
point, px, view, Action, AnyBox, AnyDrag, AppContext, BorrowWindow, Bounds, DispatchContext,
DispatchPhase, Element, ElementId, FocusHandle, KeyMatch, Keystroke, Modifiers, Overflow,
Pixels, Point, SharedString, Size, Style, StyleRefinement, ViewContext,
point, px, view, Action, AnyBox, AnyDrag, AppContext, BorrowWindow, Bounds, Component,
DispatchContext, DispatchPhase, Element, ElementId, FocusHandle, KeyMatch, Keystroke,
Modifiers, Overflow, Pixels, Point, SharedString, Size, Style, StyleRefinement, ViewContext,
};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
@ -19,8 +19,8 @@ use std::{
const DRAG_THRESHOLD: f64 = 2.;
pub trait StatelessInteractive: Element {
fn stateless_interaction(&mut self) -> &mut StatelessInteraction<Self::ViewState>;
pub trait StatelessInteractive<V: 'static>: Element<V> {
fn stateless_interaction(&mut self) -> &mut StatelessInteraction<V>;
fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
@ -48,10 +48,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_down(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseDownEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -72,10 +69,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_up(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseUpEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -96,10 +90,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_down_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseDownEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -120,10 +111,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_up_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseUpEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -143,10 +131,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_move(
mut self,
handler: impl Fn(&mut Self::ViewState, &MouseMoveEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -163,10 +148,7 @@ pub trait StatelessInteractive: Element {
fn on_scroll_wheel(
mut self,
handler: impl Fn(&mut Self::ViewState, &ScrollWheelEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -194,10 +176,7 @@ pub trait StatelessInteractive: Element {
fn on_action<A: 'static>(
mut self,
listener: impl Fn(&mut Self::ViewState, &A, DispatchPhase, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &A, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -215,12 +194,8 @@ pub trait StatelessInteractive: Element {
fn on_key_down(
mut self,
listener: impl Fn(
&mut Self::ViewState,
&KeyDownEvent,
DispatchPhase,
&mut ViewContext<Self::ViewState>,
) + Send
listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
) -> Self
@ -240,7 +215,7 @@ pub trait StatelessInteractive: Element {
fn on_key_up(
mut self,
listener: impl Fn(&mut Self::ViewState, &KeyUpEvent, DispatchPhase, &mut ViewContext<Self::ViewState>)
listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
@ -289,10 +264,7 @@ pub trait StatelessInteractive: Element {
fn on_drop<S: 'static>(
mut self,
listener: impl Fn(&mut Self::ViewState, S, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, S, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -307,8 +279,8 @@ pub trait StatelessInteractive: Element {
}
}
pub trait StatefulInteractive: StatelessInteractive {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState>;
pub trait StatefulInteractive<V: 'static>: StatelessInteractive<V> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V>;
fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
@ -335,10 +307,7 @@ pub trait StatefulInteractive: StatelessInteractive {
fn on_click(
mut self,
listener: impl Fn(&mut Self::ViewState, &ClickEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -351,20 +320,14 @@ pub trait StatefulInteractive: StatelessInteractive {
fn on_drag<S, R, E>(
mut self,
listener: impl Fn(
&mut Self::ViewState,
&mut ViewContext<Self::ViewState>,
) -> Drag<S, R, Self::ViewState, E>
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &mut ViewContext<V>) -> Drag<S, R, V, E> + Send + Sync + 'static,
) -> Self
where
Self: Sized,
S: Any + Send + Sync,
R: Fn(&mut Self::ViewState, &mut ViewContext<Self::ViewState>) -> E,
R: Fn(&mut V, &mut ViewContext<V>) -> E,
R: 'static + Send + Sync,
E: Element<ViewState = Self::ViewState>,
E: Component<V>,
{
debug_assert!(
self.stateful_interaction().drag_listener.is_none(),
@ -907,7 +870,8 @@ pub struct ClickEvent {
pub struct Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
E: Element<ViewState = V>,
V: 'static,
E: Component<V>,
{
pub state: S,
pub render_drag_handle: R,
@ -917,7 +881,8 @@ where
impl<S, R, V, E> Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
E: Element<ViewState = V>,
V: 'static,
E: Component<V>,
{
pub fn new(state: S, render_drag_handle: R) -> Self {
Drag {

View file

@ -1,7 +1,7 @@
use parking_lot::Mutex;
use crate::{
AnyBox, AnyElement, BorrowWindow, Bounds, Element, ElementId, EntityId, Handle, IntoAnyElement,
AnyBox, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, EntityId, Handle,
LayoutId, Pixels, ViewContext, WindowContext,
};
use std::{marker::PhantomData, sync::Arc};
@ -33,16 +33,16 @@ pub fn view<V, E>(
render: impl Fn(&mut V, &mut ViewContext<V>) -> E + Send + Sync + 'static,
) -> View<V>
where
E: IntoAnyElement<V>,
E: Component<V>,
{
View {
state,
render: Arc::new(move |state, cx| render(state, cx).into_any()),
render: Arc::new(move |state, cx| render(state, cx).render()),
}
}
impl<V: 'static, ParentViewState: 'static> IntoAnyElement<ParentViewState> for View<V> {
fn into_any(self) -> AnyElement<ParentViewState> {
impl<V: 'static, ParentViewState: 'static> Component<ParentViewState> for View<V> {
fn render(self) -> AnyElement<ParentViewState> {
AnyElement::new(EraseViewState {
view: self,
parent_view_state_type: PhantomData,
@ -50,8 +50,7 @@ impl<V: 'static, ParentViewState: 'static> IntoAnyElement<ParentViewState> for V
}
}
impl<V: 'static> Element for View<V> {
type ViewState = ();
impl<V: 'static> Element<()> for View<V> {
type ElementState = AnyElement<V>;
fn id(&self) -> Option<crate::ElementId> {
@ -99,14 +98,13 @@ struct EraseViewState<V, ParentV> {
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
unsafe impl<V, ParentV> Sync for EraseViewState<V, ParentV> {}
impl<V: 'static, ParentV: 'static> IntoAnyElement<ParentV> for EraseViewState<V, ParentV> {
fn into_any(self) -> AnyElement<ParentV> {
impl<V: 'static, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
type ViewState = ParentV;
impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -115,18 +113,18 @@ impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
fn initialize(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> Self::ElementState {
ViewObject::initialize(&mut self.view, cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> LayoutId {
ViewObject::layout(&mut self.view, element, cx)
}
@ -134,9 +132,9 @@ impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) {
ViewObject::paint(&mut self.view, bounds, element, cx)
}
@ -187,8 +185,8 @@ pub struct AnyView {
view: Arc<Mutex<dyn ViewObject>>,
}
impl<ParentV: 'static> IntoAnyElement<ParentV> for AnyView {
fn into_any(self) -> AnyElement<ParentV> {
impl<ParentV: 'static> Component<ParentV> for AnyView {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(EraseAnyViewState {
view: self,
parent_view_state_type: PhantomData,
@ -196,8 +194,7 @@ impl<ParentV: 'static> IntoAnyElement<ParentV> for AnyView {
}
}
impl Element for AnyView {
type ViewState = ();
impl Element<()> for AnyView {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -206,18 +203,18 @@ impl Element for AnyView {
fn initialize(
&mut self,
_: &mut Self::ViewState,
_: &mut (),
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<()>,
) -> Self::ElementState {
self.view.lock().initialize(cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: &mut (),
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<()>,
) -> LayoutId {
self.view.lock().layout(element, cx)
}
@ -227,7 +224,7 @@ impl Element for AnyView {
bounds: Bounds<Pixels>,
_: &mut (),
element: &mut AnyBox,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<()>,
) {
self.view.lock().paint(bounds, element, cx)
}
@ -241,14 +238,13 @@ struct EraseAnyViewState<ParentViewState> {
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
unsafe impl<ParentV> Sync for EraseAnyViewState<ParentV> {}
impl<ParentV: 'static> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV> {
fn into_any(self) -> AnyElement<ParentV> {
impl<ParentV: 'static> Component<ParentV> for EraseAnyViewState<ParentV> {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
type ViewState = ParentV;
impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -257,18 +253,18 @@ impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
fn initialize(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> Self::ElementState {
self.view.view.lock().initialize(cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> LayoutId {
self.view.view.lock().layout(element, cx)
}
@ -276,9 +272,9 @@ impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) {
self.view.view.lock().paint(bounds, element, cx)
}