Merge branch 'main' into disclosable-component

This commit is contained in:
Mikayla
2023-08-23 16:30:27 -07:00
139 changed files with 13495 additions and 1061 deletions
+37 -23
View File
@@ -34,7 +34,7 @@ use crate::{
rect::RectF,
vector::{vec2f, Vector2F},
},
json, Action, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, TypeTag, View,
json, Action, Entity, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, TypeTag, View,
ViewContext, WeakViewHandle, WindowContext,
};
use anyhow::{anyhow, Result};
@@ -42,14 +42,19 @@ use collections::HashMap;
use core::panic;
use json::ToJson;
use smallvec::SmallVec;
use std::{any::Any, borrow::Cow, mem, ops::Range};
use std::{
any::{type_name, Any},
borrow::Cow,
mem,
ops::Range,
};
pub trait Element<V: View>: 'static {
pub trait Element<V: 'static>: 'static {
type LayoutState;
type PaintState;
fn view_name(&self) -> &'static str {
V::ui_name()
type_name::<V>()
}
fn layout(
@@ -252,11 +257,7 @@ pub trait Element<V: View>: 'static {
}
}
pub trait RenderElement {
fn render<V: View>(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;
}
trait AnyElementState<V: View> {
trait AnyElementState<V> {
fn layout(
&mut self,
constraint: SizeConstraint,
@@ -270,7 +271,7 @@ trait AnyElementState<V: View> {
origin: Vector2F,
visible_bounds: RectF,
view: &mut V,
cx: &mut ViewContext<V>,
cx: &mut PaintContext<V>,
);
fn rect_for_text_range(
@@ -287,7 +288,7 @@ trait AnyElementState<V: View> {
fn metadata(&self) -> Option<&dyn Any>;
}
enum ElementState<V: View, E: Element<V>> {
enum ElementState<V: 'static, E: Element<V>> {
Empty,
Init {
element: E,
@@ -308,7 +309,7 @@ enum ElementState<V: View, E: Element<V>> {
},
}
impl<V: View, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
impl<V, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
fn layout(
&mut self,
constraint: SizeConstraint,
@@ -351,7 +352,7 @@ impl<V: View, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
origin: Vector2F,
visible_bounds: RectF,
view: &mut V,
cx: &mut ViewContext<V>,
cx: &mut PaintContext<V>,
) {
*self = match mem::take(self) {
ElementState::PostLayout {
@@ -490,18 +491,18 @@ impl<V: View, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
}
}
impl<V: View, E: Element<V>> Default for ElementState<V, E> {
impl<V, E: Element<V>> Default for ElementState<V, E> {
fn default() -> Self {
Self::Empty
}
}
pub struct AnyElement<V: View> {
pub struct AnyElement<V> {
state: Box<dyn AnyElementState<V>>,
name: Option<Cow<'static, str>>,
}
impl<V: View> AnyElement<V> {
impl<V> AnyElement<V> {
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
@@ -527,7 +528,7 @@ impl<V: View> AnyElement<V> {
origin: Vector2F,
visible_bounds: RectF,
view: &mut V,
cx: &mut ViewContext<V>,
cx: &mut PaintContext<V>,
) {
self.state.paint(scene, origin, visible_bounds, view, cx);
}
@@ -569,7 +570,7 @@ impl<V: View> AnyElement<V> {
}
}
impl<V: View> Element<V> for AnyElement<V> {
impl<V: 'static> Element<V> for AnyElement<V> {
type LayoutState = ();
type PaintState = ();
@@ -627,12 +628,18 @@ impl<V: View> Element<V> for AnyElement<V> {
}
}
pub struct RootElement<V: View> {
impl Entity for AnyElement<()> {
type Event = ();
}
// impl View for AnyElement<()> {}
pub struct RootElement<V> {
element: AnyElement<V>,
view: WeakViewHandle<V>,
}
impl<V: View> RootElement<V> {
impl<V> RootElement<V> {
pub fn new(element: AnyElement<V>, view: WeakViewHandle<V>) -> Self {
Self { element, view }
}
@@ -700,7 +707,9 @@ impl<V: View> AnyRootElement for RootElement<V> {
.ok_or_else(|| anyhow!("paint called on a root element for a dropped view"))?;
view.update(cx, |view, cx| {
self.element.paint(scene, origin, visible_bounds, view, cx);
let mut cx = PaintContext::new(cx);
self.element
.paint(scene, origin, visible_bounds, view, &mut cx);
Ok(())
})
}
@@ -740,7 +749,7 @@ impl<V: View> AnyRootElement for RootElement<V> {
}
}
pub trait ParentElement<'a, V: View>: Extend<AnyElement<V>> + Sized {
pub trait ParentElement<'a, V: 'static>: Extend<AnyElement<V>> + Sized {
fn add_children<E: Element<V>>(&mut self, children: impl IntoIterator<Item = E>) {
self.extend(children.into_iter().map(|child| child.into_any()));
}
@@ -760,7 +769,12 @@ pub trait ParentElement<'a, V: View>: Extend<AnyElement<V>> + Sized {
}
}
impl<'a, V: View, T> ParentElement<'a, V> for T where T: Extend<AnyElement<V>> {}
impl<'a, V, T> ParentElement<'a, V> for T
where
V: 'static,
T: Extend<AnyElement<V>>,
{
}
pub fn constrain_size_preserving_aspect_ratio(max_size: Vector2F, size: Vector2F) -> Vector2F {
if max_size.x().is_infinite() && max_size.y().is_infinite() {