Merge remote-tracking branch 'origin/callback-handles' into search2

This commit is contained in:
Piotr Osiewicz
2023-11-21 00:40:20 +01:00
118 changed files with 2175 additions and 6954 deletions
+1 -1
View File
@@ -424,7 +424,7 @@ impl AppContext {
/// Opens a new window with the given option and the root view returned by the given function.
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
/// functionality.
pub fn open_window<V: 'static + Render<V>>(
pub fn open_window<V: 'static + Render>(
&mut self,
options: crate::WindowOptions,
build_root_view: impl FnOnce(&mut WindowContext) -> View<V>,
+3 -3
View File
@@ -115,7 +115,7 @@ impl AsyncAppContext {
build_root_view: impl FnOnce(&mut WindowContext) -> View<V>,
) -> Result<WindowHandle<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
let app = self
.app
@@ -286,7 +286,7 @@ impl VisualContext for AsyncWindowContext {
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
self.window
.update(self, |_, cx| cx.build_view(build_view_state))
@@ -306,7 +306,7 @@ impl VisualContext for AsyncWindowContext {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
self.window
.update(self, |_, cx| cx.replace_root_view(build_view))
+7 -7
View File
@@ -127,7 +127,7 @@ impl TestAppContext {
pub fn add_window<F, V>(&mut self, build_window: F) -> WindowHandle<V>
where
F: FnOnce(&mut ViewContext<V>) -> V,
V: 'static + Render<V>,
V: 'static + Render,
{
let mut cx = self.app.borrow_mut();
cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window))
@@ -144,7 +144,7 @@ impl TestAppContext {
pub fn add_window_view<F, V>(&mut self, build_window: F) -> (View<V>, &mut VisualTestContext)
where
F: FnOnce(&mut ViewContext<V>) -> V,
V: 'static + Render<V>,
V: 'static + Render,
{
let mut cx = self.app.borrow_mut();
let window = cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window));
@@ -568,7 +568,7 @@ impl<'a> VisualContext for VisualTestContext<'a> {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
self.window
.update(self.cx, |_, cx| cx.build_view(build_view))
@@ -590,7 +590,7 @@ impl<'a> VisualContext for VisualTestContext<'a> {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
self.window
.update(self.cx, |_, cx| cx.replace_root_view(build_view))
@@ -618,7 +618,7 @@ impl<'a> VisualContext for VisualTestContext<'a> {
}
impl AnyWindowHandle {
pub fn build_view<V: Render<V> + 'static>(
pub fn build_view<V: Render + 'static>(
&self,
cx: &mut TestAppContext,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
@@ -629,8 +629,8 @@ impl AnyWindowHandle {
pub struct EmptyView {}
impl Render<Self> for EmptyView {
type Element = Div<Self>;
impl Render for EmptyView {
type Element = Div;
fn render(&mut self, _cx: &mut crate::ViewContext<Self>) -> Self::Element {
div()
+111 -164
View File
@@ -1,24 +1,25 @@
use crate::{
AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext,
WindowContext,
};
use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
use std::{any::Any, fmt::Debug, marker::PhantomData};
use std::{any::Any, fmt::Debug};
pub trait Render<V: 'static>: 'static + Sized {
type Element: Element<V> + 'static;
pub trait Render: 'static + Sized {
type Element: Element + 'static;
fn render(&mut self, cx: &mut ViewContext<V>) -> Self::Element;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
}
pub trait RenderOnce<V: 'static>: Sized {
type Element: Element<V> + 'static;
pub trait RenderOnce: Sized {
type Element: Element + 'static;
fn element_id(&self) -> Option<ElementId>;
fn render_once(self) -> Self::Element;
fn render_into_any(self) -> AnyElement<V> {
fn render_into_any(self) -> AnyElement {
self.render_once().into_any()
}
@@ -26,9 +27,8 @@ pub trait RenderOnce<V: 'static>: Sized {
self,
origin: Point<Pixels>,
available_space: Size<T>,
view_state: &mut V,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut <Self::Element as Element<V>>::State, &mut ViewContext<V>) -> R,
cx: &mut WindowContext,
f: impl FnOnce(&mut <Self::Element as Element>::State, &mut WindowContext) -> R,
) -> R
where
T: Clone + Default + Debug + Into<AvailableSpace>,
@@ -39,13 +39,9 @@ pub trait RenderOnce<V: 'static>: Sized {
element: Some(element),
phase: ElementDrawPhase::Start,
};
let frame_state = DrawableElement::draw(
element,
origin,
available_space.map(Into::into),
view_state,
cx,
);
let frame_state =
DrawableElement::draw(element, origin, available_space.map(Into::into), cx);
if let Some(mut frame_state) = frame_state {
f(&mut frame_state, cx)
@@ -61,7 +57,7 @@ pub trait RenderOnce<V: 'static>: Sized {
fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
where
Self: Sized,
U: RenderOnce<V>,
U: RenderOnce,
{
f(self)
}
@@ -87,70 +83,55 @@ pub trait RenderOnce<V: 'static>: Sized {
}
}
pub trait Element<V: 'static>: 'static + RenderOnce<V> {
pub trait Element: 'static + RenderOnce {
type State: 'static;
fn layout(
&mut self,
view_state: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State);
fn paint(
self,
bounds: Bounds<Pixels>,
view_state: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
);
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext);
fn into_any(self) -> AnyElement<V> {
fn into_any(self) -> AnyElement {
AnyElement::new(self)
}
}
pub trait Component<V: 'static>: 'static {
type Rendered: RenderOnce<V>;
pub trait Component: 'static {
type Rendered: RenderOnce;
fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered;
fn render(self, cx: &mut WindowContext) -> Self::Rendered;
}
pub struct CompositeElement<V, C> {
pub struct CompositeElement<C> {
component: Option<C>,
view_type: PhantomData<V>,
}
pub struct CompositeElementState<V: 'static, C: Component<V>> {
rendered_element: Option<<C::Rendered as RenderOnce<V>>::Element>,
rendered_element_state: <<C::Rendered as RenderOnce<V>>::Element as Element<V>>::State,
pub struct CompositeElementState<C: Component> {
rendered_element: Option<<C::Rendered as RenderOnce>::Element>,
rendered_element_state: <<C::Rendered as RenderOnce>::Element as Element>::State,
}
impl<V, C> CompositeElement<V, C> {
impl<C> CompositeElement<C> {
pub fn new(component: C) -> Self {
CompositeElement {
component: Some(component),
view_type: PhantomData,
}
}
}
impl<V: 'static, C: Component<V>> Element<V> for CompositeElement<V, C> {
type State = CompositeElementState<V, C>;
impl<C: Component> Element for CompositeElement<C> {
type State = CompositeElementState<C>;
fn layout(
&mut self,
view: &mut V,
state: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut element = self
.component
.take()
.unwrap()
.render(view, cx)
.render_once();
let (layout_id, state) = element.layout(view, state.map(|s| s.rendered_element_state), cx);
let mut element = self.component.take().unwrap().render(cx).render_once();
let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx);
let state = CompositeElementState {
rendered_element: Some(element),
rendered_element_state: state,
@@ -158,23 +139,16 @@ impl<V: 'static, C: Component<V>> Element<V> for CompositeElement<V, C> {
(layout_id, state)
}
fn paint(
self,
bounds: Bounds<Pixels>,
view: &mut V,
state: &mut Self::State,
cx: &mut ViewContext<V>,
) {
state.rendered_element.take().unwrap().paint(
bounds,
view,
&mut state.rendered_element_state,
cx,
);
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
state
.rendered_element
.take()
.unwrap()
.paint(bounds, &mut state.rendered_element_state, cx);
}
}
impl<V: 'static, C: Component<V>> RenderOnce<V> for CompositeElement<V, C> {
impl<C: Component> RenderOnce for CompositeElement<C> {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
@@ -189,10 +163,10 @@ impl<V: 'static, C: Component<V>> RenderOnce<V> for CompositeElement<V, C> {
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ParentElement<V: 'static> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
pub trait ParentElement {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>;
fn child(mut self, child: impl RenderOnce<V>) -> Self
fn child(mut self, child: impl RenderOnce) -> Self
where
Self: Sized,
{
@@ -200,7 +174,7 @@ pub trait ParentElement<V: 'static> {
self
}
fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce<V>>) -> Self
fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce>) -> Self
where
Self: Sized,
{
@@ -213,26 +187,28 @@ pub trait ParentElement<V: 'static> {
}
}
trait ElementObject<V> {
trait ElementObject {
fn element_id(&self) -> Option<ElementId>;
fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId;
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId;
fn paint(&mut self, cx: &mut WindowContext);
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> Size<Pixels>;
fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
);
}
pub struct DrawableElement<V: 'static, E: Element<V>> {
pub struct DrawableElement<E: Element> {
element: Option<E>,
phase: ElementDrawPhase<E::State>,
}
@@ -253,7 +229,7 @@ enum ElementDrawPhase<S> {
}
/// A wrapper around an implementer of [Element] that allows it to be drawn in a window.
impl<V, E: Element<V>> DrawableElement<V, E> {
impl<E: Element> DrawableElement<E> {
fn new(element: E) -> Self {
DrawableElement {
element: Some(element),
@@ -265,18 +241,15 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
self.element.as_ref()?.element_id()
}
fn layout(&mut self, state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
let (layout_id, frame_state) = if let Some(id) = self.element.as_ref().unwrap().element_id()
{
let layout_id = cx.with_element_state(id, |element_state, cx| {
self.element
.as_mut()
.unwrap()
.layout(state, element_state, cx)
self.element.as_mut().unwrap().layout(element_state, cx)
});
(layout_id, None)
} else {
let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(state, None, cx);
let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(None, cx);
(layout_id, Some(frame_state))
};
@@ -287,7 +260,7 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
layout_id
}
fn paint(mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> Option<E::State> {
fn paint(mut self, cx: &mut WindowContext) -> Option<E::State> {
match self.phase {
ElementDrawPhase::LayoutRequested {
layout_id,
@@ -304,7 +277,7 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
self.element
.take()
.unwrap()
.paint(bounds, view_state, &mut frame_state, cx);
.paint(bounds, &mut frame_state, cx);
Some(frame_state)
} else {
let element_id = self
@@ -315,12 +288,10 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
.expect("if we don't have frame state, we should have element state");
cx.with_element_state(element_id, |element_state, cx| {
let mut element_state = element_state.unwrap();
self.element.take().unwrap().paint(
bounds,
view_state,
&mut element_state,
cx,
);
self.element
.take()
.unwrap()
.paint(bounds, &mut element_state, cx);
((), element_state)
});
None
@@ -334,11 +305,10 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> Size<Pixels> {
if matches!(&self.phase, ElementDrawPhase::Start) {
self.layout(view_state, cx);
self.layout(cx);
}
let layout_id = match &mut self.phase {
@@ -376,22 +346,20 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> Option<E::State> {
self.measure(available_space, view_state, cx);
cx.with_absolute_element_offset(origin, |cx| self.paint(view_state, cx))
self.measure(available_space, cx);
cx.with_absolute_element_offset(origin, |cx| self.paint(cx))
}
}
// impl<V: 'static, E: Element<V>> Element<V> for DrawableElement<V, E> {
// type State = <E::Element as Element<V>>::State;
// impl<V: 'static, E: Element> Element for DrawableElement<V, E> {
// type State = <E::Element as Element>::State;
// fn layout(
// &mut self,
// view_state: &mut V,
// element_state: Option<Self::State>,
// cx: &mut ViewContext<V>,
// cx: &mut WindowContext,
// ) -> (LayoutId, Self::State) {
// }
@@ -399,15 +367,14 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
// fn paint(
// self,
// bounds: Bounds<Pixels>,
// view_state: &mut V,
// element_state: &mut Self::State,
// cx: &mut ViewContext<V>,
// cx: &mut WindowContext,
// ) {
// todo!()
// }
// }
// impl<V: 'static, E: 'static + Element<V>> RenderOnce<V> for DrawableElement<V, E> {
// impl<V: 'static, E: 'static + Element> RenderOnce for DrawableElement<V, E> {
// type Element = Self;
// fn element_id(&self) -> Option<ElementId> {
@@ -419,81 +386,71 @@ impl<V, E: Element<V>> DrawableElement<V, E> {
// }
// }
impl<V, E> ElementObject<V> for Option<DrawableElement<V, E>>
impl<E> ElementObject for Option<DrawableElement<E>>
where
E: Element<V>,
E: Element,
E::State: 'static,
{
fn element_id(&self) -> Option<ElementId> {
self.as_ref().unwrap().element_id()
}
fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
DrawableElement::layout(self.as_mut().unwrap(), view_state, cx)
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
DrawableElement::layout(self.as_mut().unwrap(), cx)
}
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
DrawableElement::paint(self.take().unwrap(), view_state, cx);
fn paint(&mut self, cx: &mut WindowContext) {
DrawableElement::paint(self.take().unwrap(), cx);
}
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> Size<Pixels> {
DrawableElement::measure(self.as_mut().unwrap(), available_space, view_state, cx)
DrawableElement::measure(self.as_mut().unwrap(), available_space, cx)
}
fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) {
DrawableElement::draw(
self.take().unwrap(),
origin,
available_space,
view_state,
cx,
);
DrawableElement::draw(self.take().unwrap(), origin, available_space, cx);
}
}
pub struct AnyElement<V>(Box<dyn ElementObject<V>>);
pub struct AnyElement(Box<dyn ElementObject>);
impl<V: 'static> AnyElement<V> {
impl AnyElement {
pub fn new<E>(element: E) -> Self
where
V: 'static,
E: 'static + Element<V>,
E: 'static + Element,
E::State: Any,
{
AnyElement(Box::new(Some(DrawableElement::new(element))) as Box<dyn ElementObject<V>>)
AnyElement(Box::new(Some(DrawableElement::new(element))) as Box<dyn ElementObject>)
}
pub fn element_id(&self) -> Option<ElementId> {
self.0.element_id()
}
pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
self.0.layout(view_state, cx)
pub fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
self.0.layout(cx)
}
pub fn paint(mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.0.paint(view_state, cx)
pub fn paint(mut self, cx: &mut WindowContext) {
self.0.paint(cx)
}
/// Initializes this element and performs layout within the given available space to determine its size.
pub fn measure(
&mut self,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> Size<Pixels> {
self.0.measure(available_space, view_state, cx)
self.0.measure(available_space, cx)
}
/// Initializes this element and performs layout in the available space, then paints it at the given origin.
@@ -501,43 +458,35 @@ impl<V: 'static> AnyElement<V> {
mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) {
self.0.draw(origin, available_space, view_state, cx)
self.0.draw(origin, available_space, cx)
}
/// Converts this `AnyElement` into a trait object that can be stored and manipulated.
pub fn into_any(self) -> AnyElement<V> {
pub fn into_any(self) -> AnyElement {
AnyElement::new(self)
}
}
impl<V: 'static> Element<V> for AnyElement<V> {
impl Element for AnyElement {
type State = ();
fn layout(
&mut self,
view_state: &mut V,
_: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let layout_id = self.layout(view_state, cx);
let layout_id = self.layout(cx);
(layout_id, ())
}
fn paint(
self,
_bounds: Bounds<Pixels>,
view_state: &mut V,
_: &mut Self::State,
cx: &mut ViewContext<V>,
) {
self.paint(view_state, cx);
fn paint(self, _: Bounds<Pixels>, _: &mut Self::State, cx: &mut WindowContext) {
self.paint(cx);
}
}
impl<V: 'static> RenderOnce<V> for AnyElement<V> {
impl RenderOnce for AnyElement {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
@@ -549,13 +498,13 @@ impl<V: 'static> RenderOnce<V> for AnyElement<V> {
}
}
// impl<V, E, F> Element<V> for Option<F>
// impl<V, E, F> Element for Option<F>
// where
// V: 'static,
// E: Element<V>,
// F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static,
// E: Element,
// F: FnOnce(&mut V, &mut WindowContext<'_, V>) -> E + 'static,
// {
// type State = Option<AnyElement<V>>;
// type State = Option<AnyElement>;
// fn element_id(&self) -> Option<ElementId> {
// None
@@ -563,9 +512,8 @@ impl<V: 'static> RenderOnce<V> for AnyElement<V> {
// fn layout(
// &mut self,
// view_state: &mut V,
// _: Option<Self::State>,
// cx: &mut ViewContext<V>,
// cx: &mut WindowContext,
// ) -> (LayoutId, Self::State) {
// let render = self.take().unwrap();
// let mut element = (render)(view_state, cx).into_any();
@@ -576,19 +524,18 @@ impl<V: 'static> RenderOnce<V> for AnyElement<V> {
// fn paint(
// self,
// _bounds: Bounds<Pixels>,
// view_state: &mut V,
// rendered_element: &mut Self::State,
// cx: &mut ViewContext<V>,
// cx: &mut WindowContext,
// ) {
// rendered_element.take().unwrap().paint(view_state, cx);
// }
// }
// impl<V, E, F> RenderOnce<V> for Option<F>
// impl<V, E, F> RenderOnce for Option<F>
// where
// V: 'static,
// E: Element<V>,
// F: FnOnce(&mut V, &mut ViewContext<V>) -> E + 'static,
// E: Element,
// F: FnOnce(&mut V, &mut WindowContext) -> E + 'static,
// {
// type Element = Self;
File diff suppressed because it is too large Load Diff
+14 -19
View File
@@ -1,17 +1,17 @@
use crate::{
BorrowWindow, Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity,
LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, ViewContext,
Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity, LayoutId, Pixels,
RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
};
use futures::FutureExt;
use util::ResultExt;
pub struct Img<V: 'static> {
interactivity: Interactivity<V>,
pub struct Img {
interactivity: Interactivity,
uri: Option<SharedString>,
grayscale: bool,
}
pub fn img<V: 'static>() -> Img<V> {
pub fn img() -> Img {
Img {
interactivity: Interactivity::default(),
uri: None,
@@ -19,10 +19,7 @@ pub fn img<V: 'static>() -> Img<V> {
}
}
impl<V> Img<V>
where
V: 'static,
{
impl Img {
pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
self.uri = Some(uri.into());
self
@@ -34,14 +31,13 @@ where
}
}
impl<V> Element<V> for Img<V> {
impl Element for Img {
type State = InteractiveElementState;
fn layout(
&mut self,
_view_state: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
self.interactivity.layout(element_state, cx, |style, cx| {
cx.request_layout(&style, None)
@@ -51,9 +47,8 @@ impl<V> Element<V> for Img<V> {
fn paint(
self,
bounds: Bounds<Pixels>,
_view_state: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) {
self.interactivity.paint(
bounds,
@@ -78,7 +73,7 @@ impl<V> Element<V> for Img<V> {
.log_err()
});
} else {
cx.spawn(|_, mut cx| async move {
cx.spawn(|mut cx| async move {
if image_future.await.ok().is_some() {
cx.on_next_frame(|cx| cx.notify());
}
@@ -91,7 +86,7 @@ impl<V> Element<V> for Img<V> {
}
}
impl<V: 'static> RenderOnce<V> for Img<V> {
impl RenderOnce for Img {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
@@ -103,14 +98,14 @@ impl<V: 'static> RenderOnce<V> for Img<V> {
}
}
impl<V> Styled for Img<V> {
impl Styled for Img {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.interactivity.base_style
}
}
impl<V> InteractiveElement<V> for Img<V> {
fn interactivity(&mut self) -> &mut Interactivity<V> {
impl InteractiveElement for Img {
fn interactivity(&mut self) -> &mut Interactivity {
&mut self.interactivity
}
}
+13 -15
View File
@@ -3,15 +3,15 @@ use taffy::style::{Display, Position};
use crate::{
point, AnyElement, BorrowWindow, Bounds, Element, LayoutId, ParentElement, Pixels, Point,
RenderOnce, Size, Style,
RenderOnce, Size, Style, WindowContext,
};
pub struct OverlayState {
child_layout_ids: SmallVec<[LayoutId; 4]>,
}
pub struct Overlay<V> {
children: SmallVec<[AnyElement<V>; 2]>,
pub struct Overlay {
children: SmallVec<[AnyElement; 2]>,
anchor_corner: AnchorCorner,
fit_mode: OverlayFitMode,
// todo!();
@@ -21,7 +21,7 @@ pub struct Overlay<V> {
/// overlay gives you a floating element that will avoid overflowing the window bounds.
/// Its children should have no margin to avoid measurement issues.
pub fn overlay<V: 'static>() -> Overlay<V> {
pub fn overlay() -> Overlay {
Overlay {
children: SmallVec::new(),
anchor_corner: AnchorCorner::TopLeft,
@@ -30,7 +30,7 @@ pub fn overlay<V: 'static>() -> Overlay<V> {
}
}
impl<V> Overlay<V> {
impl Overlay {
/// Sets which corner of the overlay should be anchored to the current position.
pub fn anchor(mut self, anchor: AnchorCorner) -> Self {
self.anchor_corner = anchor;
@@ -51,25 +51,24 @@ impl<V> Overlay<V> {
}
}
impl<V: 'static> ParentElement<V> for Overlay<V> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
impl ParentElement for Overlay {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}
impl<V: 'static> Element<V> for Overlay<V> {
impl Element for Overlay {
type State = OverlayState;
fn layout(
&mut self,
view_state: &mut V,
_: Option<Self::State>,
cx: &mut crate::ViewContext<V>,
cx: &mut WindowContext,
) -> (crate::LayoutId, Self::State) {
let child_layout_ids = self
.children
.iter_mut()
.map(|child| child.layout(view_state, cx))
.map(|child| child.layout(cx))
.collect::<SmallVec<_>>();
let mut overlay_style = Style::default();
@@ -84,9 +83,8 @@ impl<V: 'static> Element<V> for Overlay<V> {
fn paint(
self,
bounds: crate::Bounds<crate::Pixels>,
view_state: &mut V,
element_state: &mut Self::State,
cx: &mut crate::ViewContext<V>,
cx: &mut WindowContext,
) {
if element_state.child_layout_ids.is_empty() {
return;
@@ -147,13 +145,13 @@ impl<V: 'static> Element<V> for Overlay<V> {
cx.with_element_offset(desired.origin - bounds.origin, |cx| {
for child in self.children {
child.paint(view_state, cx);
child.paint(cx);
}
})
}
}
impl<V: 'static> RenderOnce<V> for Overlay<V> {
impl RenderOnce for Overlay {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
+13 -19
View File
@@ -1,49 +1,43 @@
use crate::{
Bounds, Element, ElementId, InteractiveElement, InteractiveElementState, Interactivity,
LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, ViewContext,
LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
};
use util::ResultExt;
pub struct Svg<V: 'static> {
interactivity: Interactivity<V>,
pub struct Svg {
interactivity: Interactivity,
path: Option<SharedString>,
}
pub fn svg<V: 'static>() -> Svg<V> {
pub fn svg() -> Svg {
Svg {
interactivity: Interactivity::default(),
path: None,
}
}
impl<V> Svg<V> {
impl Svg {
pub fn path(mut self, path: impl Into<SharedString>) -> Self {
self.path = Some(path.into());
self
}
}
impl<V> Element<V> for Svg<V> {
impl Element for Svg {
type State = InteractiveElementState;
fn layout(
&mut self,
_view_state: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
self.interactivity.layout(element_state, cx, |style, cx| {
cx.request_layout(&style, None)
})
}
fn paint(
self,
bounds: Bounds<Pixels>,
_view_state: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
) where
fn paint(self, bounds: Bounds<Pixels>, element_state: &mut Self::State, cx: &mut WindowContext)
where
Self: Sized,
{
self.interactivity
@@ -55,7 +49,7 @@ impl<V> Element<V> for Svg<V> {
}
}
impl<V: 'static> RenderOnce<V> for Svg<V> {
impl RenderOnce for Svg {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
@@ -67,14 +61,14 @@ impl<V: 'static> RenderOnce<V> for Svg<V> {
}
}
impl<V> Styled for Svg<V> {
impl Styled for Svg {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.interactivity.base_style
}
}
impl<V> InteractiveElement<V> for Svg<V> {
fn interactivity(&mut self) -> &mut Interactivity<V> {
impl InteractiveElement for Svg {
fn interactivity(&mut self) -> &mut Interactivity {
&mut self.interactivity
}
}
+31 -138
View File
@@ -1,6 +1,6 @@
use crate::{
BorrowWindow, Bounds, Element, ElementId, LayoutId, Pixels, RenderOnce, SharedString, Size,
TextRun, ViewContext, WindowContext, WrappedLine,
Bounds, Element, ElementId, LayoutId, Pixels, RenderOnce, SharedString, Size, TextRun,
WindowContext, WrappedLine,
};
use anyhow::anyhow;
use parking_lot::{Mutex, MutexGuard};
@@ -8,32 +8,25 @@ use smallvec::SmallVec;
use std::{cell::Cell, rc::Rc, sync::Arc};
use util::ResultExt;
impl<V: 'static> Element<V> for &'static str {
impl Element for &'static str {
type State = TextState;
fn layout(
&mut self,
_: &mut V,
_: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut state = TextState::default();
let layout_id = state.layout(SharedString::from(*self), None, cx);
(layout_id, state)
}
fn paint(
self,
bounds: Bounds<Pixels>,
_: &mut V,
state: &mut TextState,
cx: &mut ViewContext<V>,
) {
fn paint(self, bounds: Bounds<Pixels>, state: &mut TextState, cx: &mut WindowContext) {
state.paint(bounds, self, cx)
}
}
impl<V: 'static> RenderOnce<V> for &'static str {
impl RenderOnce for &'static str {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
@@ -45,37 +38,30 @@ impl<V: 'static> RenderOnce<V> for &'static str {
}
}
impl<V: 'static> Element<V> for SharedString {
impl Element for SharedString {
type State = TextState;
fn layout(
&mut self,
_: &mut V,
_: Option<Self::State>,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut state = TextState::default();
let layout_id = state.layout(self.clone(), None, cx);
(layout_id, state)
}
fn paint(
self,
bounds: Bounds<Pixels>,
_: &mut V,
state: &mut TextState,
cx: &mut ViewContext<V>,
) {
fn paint(self, bounds: Bounds<Pixels>, state: &mut TextState, cx: &mut WindowContext) {
let text_str: &str = self.as_ref();
state.paint(bounds, text_str, cx)
}
}
impl<V: 'static> RenderOnce<V> for SharedString {
impl RenderOnce for SharedString {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
Some(self.clone().into())
None
}
fn render_once(self) -> Self::Element {
@@ -102,110 +88,25 @@ impl StyledText {
}
}
impl<V: 'static> Element<V> for StyledText {
impl Element for StyledText {
type State = TextState;
fn layout(
&mut self,
_view: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
_: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let element_state = element_state.unwrap_or_default();
let text_system = cx.text_system().clone();
let text_style = cx.text_style();
let font_size = text_style.font_size.to_pixels(cx.rem_size());
let line_height = text_style
.line_height
.to_pixels(font_size.into(), cx.rem_size());
let text = self.text.clone();
let rem_size = cx.rem_size();
let runs = if let Some(runs) = self.runs.take() {
runs
} else {
vec![text_style.to_run(text.len())]
};
let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
let element_state = element_state.clone();
move |known_dimensions, available_space| {
let wrap_width = known_dimensions.width.or(match available_space.width {
crate::AvailableSpace::Definite(x) => Some(x),
_ => None,
});
if let Some(text_state) = element_state.0.lock().as_ref() {
if text_state.size.is_some()
&& (wrap_width.is_none() || wrap_width == text_state.wrap_width)
{
return text_state.size.unwrap();
}
}
let Some(lines) = text_system
.shape_text(
&text,
font_size,
&runs[..],
wrap_width, // Wrap if we know the width.
)
.log_err()
else {
element_state.lock().replace(TextStateInner {
lines: Default::default(),
line_height,
wrap_width,
size: Some(Size::default()),
});
return Size::default();
};
let mut size: Size<Pixels> = Size::default();
for line in &lines {
let line_size = line.size(line_height);
size.height += line_size.height;
size.width = size.width.max(line_size.width);
}
element_state.lock().replace(TextStateInner {
lines,
line_height,
wrap_width,
size: Some(size),
});
size
}
});
(layout_id, element_state)
let mut state = TextState::default();
let layout_id = state.layout(self.text.clone(), self.runs.take(), cx);
(layout_id, state)
}
fn paint(
self,
bounds: Bounds<Pixels>,
_: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
) {
let element_state = element_state.lock();
let element_state = element_state
.as_ref()
.ok_or_else(|| anyhow!("measurement has not been performed on {}", &self.text))
.unwrap();
let line_height = element_state.line_height;
let mut line_origin = bounds.origin;
for line in &element_state.lines {
line.paint(line_origin, line_height, cx).log_err();
line_origin.y += line.size(line_height).height;
}
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
state.paint(bounds, &self.text, cx)
}
}
impl<V: 'static> RenderOnce<V> for StyledText {
impl RenderOnce for StyledText {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
@@ -327,7 +228,7 @@ impl TextState {
}
struct InteractiveText {
id: ElementId,
element_id: ElementId,
text: StyledText,
}
@@ -336,28 +237,27 @@ struct InteractiveTextState {
clicked_range_ixs: Rc<Cell<SmallVec<[usize; 1]>>>,
}
impl<V: 'static> Element<V> for InteractiveText {
impl Element for InteractiveText {
type State = InteractiveTextState;
fn layout(
&mut self,
view_state: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
if let Some(InteractiveTextState {
text_state,
clicked_range_ixs,
}) = element_state
}) = state
{
let (layout_id, text_state) = self.text.layout(view_state, Some(text_state), cx);
let (layout_id, text_state) = self.text.layout(Some(text_state), cx);
let element_state = InteractiveTextState {
text_state,
clicked_range_ixs,
};
(layout_id, element_state)
} else {
let (layout_id, text_state) = self.text.layout(view_state, None, cx);
let (layout_id, text_state) = self.text.layout(None, cx);
let element_state = InteractiveTextState {
text_state,
clicked_range_ixs: Rc::default(),
@@ -366,23 +266,16 @@ impl<V: 'static> Element<V> for InteractiveText {
}
}
fn paint(
self,
bounds: Bounds<Pixels>,
view_state: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
) {
self.text
.paint(bounds, view_state, &mut element_state.text_state, cx)
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
self.text.paint(bounds, &mut state.text_state, cx)
}
}
impl<V: 'static> RenderOnce<V> for InteractiveText {
impl RenderOnce for InteractiveText {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
Some(self.id.clone())
Some(self.element_id.clone())
}
fn render_once(self) -> Self::Element {
+41 -50
View File
@@ -1,7 +1,7 @@
use crate::{
point, px, size, AnyElement, AvailableSpace, BorrowWindow, Bounds, Element, ElementId,
InteractiveElement, InteractiveElementState, Interactivity, LayoutId, Pixels, Point,
RenderOnce, Size, StyleRefinement, Styled, ViewContext,
point, px, size, AnyElement, AvailableSpace, Bounds, Element, ElementId, InteractiveElement,
InteractiveElementState, Interactivity, LayoutId, Pixels, Point, Render, RenderOnce, Size,
StyleRefinement, Styled, View, ViewContext, WindowContext,
};
use smallvec::SmallVec;
use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
@@ -10,31 +10,36 @@ use taffy::style::Overflow;
/// uniform_list provides lazy rendering for a set of items that are of uniform height.
/// When rendered into a container with overflow-y: hidden and a fixed (or max) height,
/// uniform_list will only render the visibile subset of items.
pub fn uniform_list<I, V, E>(
pub fn uniform_list<I, R, V>(
view: View<V>,
id: I,
item_count: usize,
f: impl 'static + Fn(&mut V, Range<usize>, &mut ViewContext<V>) -> Vec<E>,
) -> UniformList<V>
f: impl 'static + Fn(&mut V, Range<usize>, &mut ViewContext<V>) -> Vec<R>,
) -> UniformList
where
I: Into<ElementId>,
V: 'static,
E: Element<V>,
R: RenderOnce,
V: Render,
{
let id = id.into();
let mut style = StyleRefinement::default();
style.overflow.y = Some(Overflow::Hidden);
let render_range = move |range, cx: &mut WindowContext| {
view.update(cx, |this, cx| {
f(this, range, cx)
.into_iter()
.map(|component| component.render_into_any())
.collect()
})
};
UniformList {
id: id.clone(),
style,
item_count,
item_to_measure_index: 0,
render_items: Box::new(move |view, visible_range, cx| {
f(view, visible_range, cx)
.into_iter()
.map(|component| component.into_any())
.collect()
}),
render_items: Box::new(render_range),
interactivity: Interactivity {
element_id: Some(id.into()),
..Default::default()
@@ -43,19 +48,14 @@ where
}
}
pub struct UniformList<V: 'static> {
pub struct UniformList {
id: ElementId,
style: StyleRefinement,
item_count: usize,
item_to_measure_index: usize,
render_items: Box<
dyn for<'a> Fn(
&'a mut V,
Range<usize>,
&'a mut ViewContext<V>,
) -> SmallVec<[AnyElement<V>; 64]>,
>,
interactivity: Interactivity<V>,
render_items:
Box<dyn for<'a> Fn(Range<usize>, &'a mut WindowContext) -> SmallVec<[AnyElement; 64]>>,
interactivity: Interactivity,
scroll_handle: Option<UniformListScrollHandle>,
}
@@ -89,7 +89,7 @@ impl UniformListScrollHandle {
}
}
impl<V: 'static> Styled for UniformList<V> {
impl Styled for UniformList {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
@@ -101,25 +101,24 @@ pub struct UniformListState {
item_size: Size<Pixels>,
}
impl<V: 'static> Element<V> for UniformList<V> {
impl Element for UniformList {
type State = UniformListState;
fn layout(
&mut self,
view_state: &mut V,
element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let max_items = self.item_count;
let rem_size = cx.rem_size();
let item_size = element_state
let item_size = state
.as_ref()
.map(|s| s.item_size)
.unwrap_or_else(|| self.measure_item(view_state, None, cx));
.unwrap_or_else(|| self.measure_item(None, cx));
let (layout_id, interactive) =
self.interactivity
.layout(element_state.map(|s| s.interactive), cx, |style, cx| {
.layout(state.map(|s| s.interactive), cx, |style, cx| {
cx.request_measured_layout(
style,
rem_size,
@@ -157,9 +156,8 @@ impl<V: 'static> Element<V> for UniformList<V> {
fn paint(
self,
bounds: Bounds<crate::Pixels>,
view_state: &mut V,
element_state: &mut Self::State,
cx: &mut ViewContext<V>,
cx: &mut WindowContext,
) {
let style =
self.interactivity
@@ -185,9 +183,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
.get_or_insert_with(Rc::default)
.clone();
let item_height = self
.measure_item(view_state, Some(padded_bounds.size.width), cx)
.height;
let item_height = self.measure_item(Some(padded_bounds.size.width), cx).height;
self.interactivity.paint(
bounds,
@@ -229,7 +225,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
self.item_count,
);
let items = (self.render_items)(view_state, visible_range.clone(), cx);
let items = (self.render_items)(visible_range.clone(), cx);
cx.with_z_index(1, |cx| {
for (item, ix) in items.into_iter().zip(visible_range) {
let item_origin = padded_bounds.origin
@@ -238,7 +234,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
AvailableSpace::Definite(padded_bounds.size.width),
AvailableSpace::Definite(item_height),
);
item.draw(item_origin, available_space, view_state, cx);
item.draw(item_origin, available_space, cx);
}
});
}
@@ -248,7 +244,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
}
}
impl<V> RenderOnce<V> for UniformList<V> {
impl RenderOnce for UniformList {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
@@ -260,24 +256,19 @@ impl<V> RenderOnce<V> for UniformList<V> {
}
}
impl<V> UniformList<V> {
impl UniformList {
pub fn with_width_from_item(mut self, item_index: Option<usize>) -> Self {
self.item_to_measure_index = item_index.unwrap_or(0);
self
}
fn measure_item(
&self,
view_state: &mut V,
list_width: Option<Pixels>,
cx: &mut ViewContext<V>,
) -> Size<Pixels> {
fn measure_item(&self, list_width: Option<Pixels>, cx: &mut WindowContext) -> Size<Pixels> {
if self.item_count == 0 {
return Size::default();
}
let item_ix = cmp::min(self.item_to_measure_index, self.item_count - 1);
let mut items = (self.render_items)(view_state, item_ix..item_ix + 1, cx);
let mut items = (self.render_items)(item_ix..item_ix + 1, cx);
let mut item_to_measure = items.pop().unwrap();
let available_space = size(
list_width.map_or(AvailableSpace::MinContent, |width| {
@@ -285,7 +276,7 @@ impl<V> UniformList<V> {
}),
AvailableSpace::MinContent,
);
item_to_measure.measure(available_space, view_state, cx)
item_to_measure.measure(available_space, cx)
}
pub fn track_scroll(mut self, handle: UniformListScrollHandle) -> Self {
@@ -294,8 +285,8 @@ impl<V> UniformList<V> {
}
}
impl<V> InteractiveElement<V> for UniformList<V> {
fn interactivity(&mut self) -> &mut crate::Interactivity<V> {
impl InteractiveElement for UniformList {
fn interactivity(&mut self) -> &mut crate::Interactivity {
&mut self.interactivity
}
}
+2 -2
View File
@@ -121,7 +121,7 @@ pub trait VisualContext: Context {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>;
V: 'static + Render;
fn update_view<V: 'static, R>(
&mut self,
@@ -134,7 +134,7 @@ pub trait VisualContext: Context {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>;
V: 'static + Render;
fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
+5 -3
View File
@@ -1,4 +1,6 @@
use crate::{AsyncWindowContext, Bounds, Pixels, PlatformInputHandler, View, ViewContext};
use crate::{
AsyncWindowContext, Bounds, Pixels, PlatformInputHandler, View, ViewContext, WindowContext,
};
use std::ops::Range;
/// Implement this trait to allow views to handle textual input when implementing an editor, field, etc.
@@ -43,9 +45,9 @@ pub struct ElementInputHandler<V> {
impl<V: 'static> ElementInputHandler<V> {
/// Used in [Element::paint] with the element's bounds and a view context for its
/// containing view.
pub fn new(element_bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) -> Self {
pub fn new(element_bounds: Bounds<Pixels>, view: View<V>, cx: &mut WindowContext) -> Self {
ElementInputHandler {
view: cx.view().clone(),
view,
element_bounds,
cx: cx.to_async(),
}
+24 -14
View File
@@ -1,5 +1,6 @@
use crate::{
div, point, Div, Element, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, ViewContext,
div, point, Div, Element, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, RenderOnce,
ViewContext,
};
use smallvec::SmallVec;
use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf};
@@ -63,24 +64,24 @@ pub struct Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
V: 'static,
E: Element<()>,
E: RenderOnce,
{
pub state: S,
pub render_drag_handle: R,
view_type: PhantomData<V>,
view_element_types: PhantomData<(V, E)>,
}
impl<S, R, V, E> Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
V: 'static,
E: Element<()>,
E: Element,
{
pub fn new(state: S, render_drag_handle: R) -> Self {
Drag {
state,
render_drag_handle,
view_type: PhantomData,
view_element_types: Default::default(),
}
}
}
@@ -192,8 +193,8 @@ impl Deref for MouseExitEvent {
#[derive(Debug, Clone, Default)]
pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
impl Render<Self> for ExternalPaths {
type Element = Div<Self>;
impl Render for ExternalPaths {
type Element = Div;
fn render(&mut self, _: &mut ViewContext<Self>) -> Self::Element {
div() // Intentionally left empty because the platform will render icons for the dragged files
@@ -286,7 +287,7 @@ pub struct FocusEvent {
mod test {
use crate::{
self as gpui, div, Div, FocusHandle, InteractiveElement, KeyBinding, Keystroke,
ParentElement, Render, Stateful, TestAppContext, VisualContext,
ParentElement, Render, RenderOnce, Stateful, TestAppContext, VisualContext,
};
struct TestView {
@@ -297,16 +298,25 @@ mod test {
actions!(TestAction);
impl Render<Self> for TestView {
type Element = Stateful<Self, Div<Self>>;
impl Render for TestView {
type Element = Stateful<Div>;
fn render(&mut self, _: &mut gpui::ViewContext<Self>) -> Self::Element {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
div().id("testview").child(
div()
.key_context("parent")
.on_key_down(|this: &mut TestView, _, _, _| this.saw_key_down = true)
.on_action(|this: &mut TestView, _: &TestAction, _| this.saw_action = true)
.child(div().key_context("nested").track_focus(&self.focus_handle)),
.on_key_down(cx.listener(|this, _, _| this.saw_key_down = true))
.on_action(
cx.listener(|this: &mut TestView, _: &TestAction, _| {
this.saw_action = true
}),
)
.child(
div()
.key_context("nested")
.track_focus(&self.focus_handle)
.render_once(),
),
)
}
}
+2 -2
View File
@@ -2,7 +2,7 @@ use crate::{
black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask,
Corners, CornersRefinement, CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font,
FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rgba,
SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext,
SharedString, Size, SizeRefinement, Styled, TextRun, WindowContext,
};
use refineable::{Cascade, Refineable};
use smallvec::SmallVec;
@@ -313,7 +313,7 @@ impl Style {
}
/// Paints the background of an element styled with this style.
pub fn paint<V: 'static>(&self, bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
pub fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) {
let rem_size = cx.rem_size();
cx.with_z_index(0, |cx| {
+58 -124
View File
@@ -6,7 +6,7 @@ use crate::{
};
use anyhow::{Context, Result};
use std::{
any::{Any, TypeId},
any::TypeId,
hash::{Hash, Hasher},
};
@@ -59,15 +59,15 @@ impl<V: 'static> View<V> {
self.model.read(cx)
}
pub fn render_with<E>(&self, component: E) -> RenderViewWith<E, V>
where
E: 'static + Element<V>,
{
RenderViewWith {
view: self.clone(),
element: Some(component),
}
}
// pub fn render_with<E>(&self, component: E) -> RenderViewWith<E, V>
// where
// E: 'static + Element,
// {
// RenderViewWith {
// view: self.clone(),
// element: Some(component),
// }
// }
pub fn focus_handle(&self, cx: &AppContext) -> FocusHandle
where
@@ -77,6 +77,24 @@ impl<V: 'static> View<V> {
}
}
impl<V: Render> Element for View<V> {
type State = Option<AnyElement>;
fn layout(
&mut self,
_state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut element = self.update(cx, |view, cx| view.render(cx).into_any());
let layout_id = element.layout(cx);
(layout_id, Some(element))
}
fn paint(self, _: Bounds<Pixels>, element: &mut Self::State, cx: &mut WindowContext) {
element.take().unwrap().paint(cx);
}
}
impl<V> Clone for View<V> {
fn clone(&self) -> Self {
Self {
@@ -151,8 +169,8 @@ impl<V> Eq for WeakView<V> {}
#[derive(Clone, Debug)]
pub struct AnyView {
model: AnyModel,
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, Box<dyn Any>),
paint: fn(&AnyView, Box<dyn Any>, &mut WindowContext),
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
paint: fn(&AnyView, AnyElement, &mut WindowContext),
}
impl AnyView {
@@ -199,7 +217,7 @@ impl AnyView {
}
}
impl<V: 'static + Render<V>> From<View<V>> for AnyView {
impl<V: Render> From<View<V>> for AnyView {
fn from(value: View<V>) -> Self {
AnyView {
model: value.model.into_any(),
@@ -209,36 +227,24 @@ impl<V: 'static + Render<V>> From<View<V>> for AnyView {
}
}
impl<V: 'static + Render<V>, ParentV: 'static> Element<ParentV> for View<V> {
type State = Option<AnyElement<V>>;
impl Element for AnyView {
type State = Option<AnyElement>;
fn layout(
&mut self,
_parent_view: &mut ParentV,
_state: Option<Self::State>,
cx: &mut ViewContext<ParentV>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
self.update(cx, |view, cx| {
let mut element = view.render(cx).into_any();
let layout_id = element.layout(view, cx);
(layout_id, Some(element))
})
let (layout_id, state) = (self.layout)(self, cx);
(layout_id, Some(state))
}
fn paint(
self,
_: Bounds<Pixels>,
_parent: &mut ParentV,
element: &mut Self::State,
cx: &mut ViewContext<ParentV>,
) {
self.update(cx, |view, cx| {
element.take().unwrap().paint(view, cx);
});
fn paint(self, _: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
(self.paint)(&self, state.take().unwrap(), cx)
}
}
impl<V: 'static + Render<V>, ParentV: 'static> RenderOnce<ParentV> for View<V> {
impl<V: 'static + Render> RenderOnce for View<V> {
type Element = View<V>;
fn element_id(&self) -> Option<ElementId> {
@@ -250,31 +256,7 @@ impl<V: 'static + Render<V>, ParentV: 'static> RenderOnce<ParentV> for View<V> {
}
}
impl<V: 'static> Element<V> for AnyView {
type State = Option<Box<dyn Any>>;
fn layout(
&mut self,
_view_state: &mut V,
_element_state: Option<Self::State>,
cx: &mut ViewContext<V>,
) -> (LayoutId, Self::State) {
let (layout_id, rendered_element) = (self.layout)(self, cx);
(layout_id, Some(rendered_element))
}
fn paint(
mut self,
_bounds: Bounds<Pixels>,
_view_state: &mut V,
rendered_element: &mut Self::State,
cx: &mut ViewContext<V>,
) {
(self.paint)(&mut self, rendered_element.take().unwrap(), cx)
}
}
impl<ParentV: 'static> RenderOnce<ParentV> for AnyView {
impl RenderOnce for AnyView {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
@@ -288,8 +270,8 @@ impl<ParentV: 'static> RenderOnce<ParentV> for AnyView {
pub struct AnyWeakView {
model: AnyWeakModel,
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, Box<dyn Any>),
paint: fn(&AnyView, Box<dyn Any>, &mut WindowContext),
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
paint: fn(&AnyView, AnyElement, &mut WindowContext),
}
impl AnyWeakView {
@@ -303,7 +285,7 @@ impl AnyWeakView {
}
}
impl<V: 'static + Render<V>> From<WeakView<V>> for AnyWeakView {
impl<V: 'static + Render> From<WeakView<V>> for AnyWeakView {
fn from(view: WeakView<V>) -> Self {
Self {
model: view.model.into(),
@@ -313,88 +295,40 @@ impl<V: 'static + Render<V>> From<WeakView<V>> for AnyWeakView {
}
}
pub struct RenderViewWith<E, V> {
view: View<V>,
element: Option<E>,
}
impl<E, ParentV, V> Element<ParentV> for RenderViewWith<E, V>
impl<T, E> Render for T
where
E: 'static + Element<V>,
ParentV: 'static,
V: 'static,
T: 'static + FnMut(&mut WindowContext) -> E,
E: 'static + Send + Element,
{
type State = Option<AnyElement<V>>;
type Element = E;
fn layout(
&mut self,
_: &mut ParentV,
_: Option<Self::State>,
cx: &mut ViewContext<ParentV>,
) -> (LayoutId, Self::State) {
self.view.update(cx, |view, cx| {
let mut element = self.element.take().unwrap().into_any();
let layout_id = element.layout(view, cx);
(layout_id, Some(element))
})
}
fn paint(
self,
_: Bounds<Pixels>,
_: &mut ParentV,
element: &mut Self::State,
cx: &mut ViewContext<ParentV>,
) {
self.view
.update(cx, |view, cx| element.take().unwrap().paint(view, cx))
}
}
impl<E, V, ParentV> RenderOnce<ParentV> for RenderViewWith<E, V>
where
E: 'static + Element<V>,
V: 'static,
ParentV: 'static,
{
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
self.element.as_ref().unwrap().element_id()
}
fn render_once(self) -> Self::Element {
self
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
(self)(cx)
}
}
mod any_view {
use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext};
use std::any::Any;
use crate::{AnyElement, AnyView, BorrowWindow, Element, LayoutId, Render, WindowContext};
pub(crate) fn layout<V: 'static + Render<V>>(
pub(crate) fn layout<V: 'static + Render>(
view: &AnyView,
cx: &mut WindowContext,
) -> (LayoutId, Box<dyn Any>) {
) -> (LayoutId, AnyElement) {
cx.with_element_id(Some(view.model.entity_id), |cx| {
let view = view.clone().downcast::<V>().unwrap();
view.update(cx, |view, cx| {
let mut element = AnyElement::new(view.render(cx));
let layout_id = element.layout(view, cx);
(layout_id, Box::new(element) as Box<dyn Any>)
})
let mut element = view.update(cx, |view, cx| view.render(cx).into_any());
let layout_id = element.layout(cx);
(layout_id, element)
})
}
pub(crate) fn paint<V: 'static + Render<V>>(
pub(crate) fn paint<V: 'static + Render>(
view: &AnyView,
element: Box<dyn Any>,
element: AnyElement,
cx: &mut WindowContext,
) {
cx.with_element_id(Some(view.model.entity_id), |cx| {
let view = view.clone().downcast::<V>().unwrap();
let element = element.downcast::<AnyElement<V>>().unwrap();
view.update(cx, |view, cx| element.paint(view, cx))
element.paint(cx);
})
}
}
+96 -64
View File
@@ -187,7 +187,7 @@ impl Drop for FocusHandle {
/// FocusableView allows users of your view to easily
/// focus it (using cx.focus_view(view))
pub trait FocusableView: 'static + Render<Self> {
pub trait FocusableView: 'static + Render {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle;
}
@@ -1436,6 +1436,82 @@ impl<'a> WindowContext<'a> {
.dispatch_tree
.bindings_for_action(action)
}
pub fn listener_for<V: Render, E>(
&self,
view: &View<V>,
f: impl Fn(&mut V, &E, &mut ViewContext<V>) + 'static,
) -> impl Fn(&E, &mut WindowContext) + 'static {
let view = view.downgrade();
move |e: &E, cx: &mut WindowContext| {
view.update(cx, |view, cx| f(view, e, cx)).ok();
}
}
pub fn constructor_for<V: Render, R>(
&self,
view: &View<V>,
f: impl Fn(&mut V, &mut ViewContext<V>) -> R + 'static,
) -> impl Fn(&mut WindowContext) -> R + 'static {
let view = view.clone();
move |cx: &mut WindowContext| view.update(cx, |view, cx| f(view, cx))
}
//========== ELEMENT RELATED FUNCTIONS ===========
pub fn with_key_dispatch<R>(
&mut self,
context: KeyContext,
focus_handle: Option<FocusHandle>,
f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R,
) -> R {
let window = &mut self.window;
window
.current_frame
.dispatch_tree
.push_node(context.clone());
if let Some(focus_handle) = focus_handle.as_ref() {
window
.current_frame
.dispatch_tree
.make_focusable(focus_handle.id);
}
let result = f(focus_handle, self);
self.window.current_frame.dispatch_tree.pop_node();
result
}
/// Register a focus listener for the current frame only. It will be cleared
/// on the next frame render. You should use this method only from within elements,
/// and we may want to enforce that better via a different context type.
// todo!() Move this to `FrameContext` to emphasize its individuality?
pub fn on_focus_changed(
&mut self,
listener: impl Fn(&FocusEvent, &mut WindowContext) + 'static,
) {
self.window
.current_frame
.focus_listeners
.push(Box::new(move |event, cx| {
listener(event, cx);
}));
}
/// Set an input handler, such as [ElementInputHandler], which interfaces with the
/// platform to receive textual input with proper integration with concerns such
/// as IME interactions.
pub fn handle_input(
&mut self,
focus_handle: &FocusHandle,
input_handler: impl PlatformInputHandler,
) {
if focus_handle.is_focused(self) {
self.window
.platform_window
.set_input_handler(Box::new(input_handler));
}
}
}
impl Context for WindowContext<'_> {
@@ -1520,7 +1596,7 @@ impl VisualContext for WindowContext<'_> {
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
let slot = self.app.entities.reserve();
let view = View {
@@ -1559,7 +1635,7 @@ impl VisualContext for WindowContext<'_> {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render<V>,
V: 'static + Render,
{
let slot = self.app.entities.reserve();
let view = View {
@@ -1617,6 +1693,10 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
self.borrow_mut()
}
fn app(&self) -> &AppContext {
self.borrow()
}
fn window(&self) -> &Window {
self.borrow()
}
@@ -2124,49 +2204,6 @@ impl<'a, V: 'static> ViewContext<'a, V> {
)
}
/// Register a focus listener for the current frame only. It will be cleared
/// on the next frame render. You should use this method only from within elements,
/// and we may want to enforce that better via a different context type.
// todo!() Move this to `FrameContext` to emphasize its individuality?
pub fn on_focus_changed(
&mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) {
let handle = self.view().downgrade();
self.window
.current_frame
.focus_listeners
.push(Box::new(move |event, cx| {
handle
.update(cx, |view, cx| listener(view, event, cx))
.log_err();
}));
}
pub fn with_key_dispatch<R>(
&mut self,
context: KeyContext,
focus_handle: Option<FocusHandle>,
f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R,
) -> R {
let window = &mut self.window;
window
.current_frame
.dispatch_tree
.push_node(context.clone());
if let Some(focus_handle) = focus_handle.as_ref() {
window
.current_frame
.dispatch_tree
.make_focusable(focus_handle.id);
}
let result = f(focus_handle, self);
self.window.current_frame.dispatch_tree.pop_node();
result
}
pub fn spawn<Fut, R>(
&mut self,
f: impl FnOnce(WeakView<V>, AsyncWindowContext) -> Fut,
@@ -2243,21 +2280,6 @@ impl<'a, V: 'static> ViewContext<'a, V> {
});
}
/// Set an input handler, such as [ElementInputHandler], which interfaces with the
/// platform to receive textual input with proper integration with concerns such
/// as IME interactions.
pub fn handle_input(
&mut self,
focus_handle: &FocusHandle,
input_handler: impl PlatformInputHandler,
) {
if focus_handle.is_focused(self) {
self.window
.platform_window
.set_input_handler(Box::new(input_handler));
}
}
pub fn emit<Evt>(&mut self, event: Evt)
where
Evt: 'static,
@@ -2284,6 +2306,16 @@ impl<'a, V: 'static> ViewContext<'a, V> {
{
self.defer(|_, cx| cx.emit(Manager::Dismiss))
}
pub fn listener<E>(
&self,
f: impl Fn(&mut V, &E, &mut ViewContext<V>) + 'static,
) -> impl Fn(&E, &mut WindowContext) + 'static {
let view = self.view().downgrade();
move |e: &E, cx: &mut WindowContext| {
view.update(cx, |view, cx| f(view, e, cx)).ok();
}
}
}
impl<V> Context for ViewContext<'_, V> {
@@ -2335,7 +2367,7 @@ impl<V> Context for ViewContext<'_, V> {
}
impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn build_view<W: Render<W> + 'static>(
fn build_view<W: Render + 'static>(
&mut self,
build_view_state: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>> {
@@ -2355,7 +2387,7 @@ impl<V: 'static> VisualContext for ViewContext<'_, V> {
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>>
where
W: 'static + Render<W>,
W: 'static + Render,
{
self.window_cx.replace_root_view(build_view)
}
@@ -2400,7 +2432,7 @@ pub struct WindowHandle<V> {
state_type: PhantomData<V>,
}
impl<V: 'static + Render<V>> WindowHandle<V> {
impl<V: 'static + Render> WindowHandle<V> {
pub fn new(id: WindowId) -> Self {
WindowHandle {
any_handle: AnyWindowHandle {