Checkpoint

This commit is contained in:
Antonio Scandurra
2023-10-21 16:01:47 +02:00
parent f3979a9f28
commit e4fe9538d7
107 changed files with 81 additions and 81 deletions
+370
View File
@@ -0,0 +1,370 @@
use crate::{
point, AnyElement, BorrowWindow, Bounds, Element, ElementFocus, ElementId, ElementInteraction,
FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable, GlobalElementId,
GroupBounds, InteractiveElementState, IntoAnyElement, LayoutId, Overflow, ParentElement,
Pixels, Point, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, Style, StyleRefinement, Styled, ViewContext,
};
use refineable::Refineable;
use smallvec::SmallVec;
pub struct Div<
V: 'static + Send + Sync,
I: ElementInteraction<V> = StatelessInteraction<V>,
F: ElementFocus<V> = FocusDisabled,
> {
interaction: I,
focus: F,
children: SmallVec<[AnyElement<V>; 2]>,
group: Option<SharedString>,
base_style: StyleRefinement,
}
pub fn div<V>() -> Div<V, StatelessInteraction<V>, FocusDisabled>
where
V: 'static + Send + Sync,
{
Div {
interaction: StatelessInteraction::default(),
focus: FocusDisabled,
children: SmallVec::new(),
group: None,
base_style: StyleRefinement::default(),
}
}
impl<V, F> Div<V, StatelessInteraction<V>, F>
where
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteraction<V>, F> {
Div {
interaction: id.into().into(),
focus: self.focus,
children: self.children,
group: self.group,
base_style: self.base_style,
}
}
}
impl<V, I, F> Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
pub fn group(mut self, group: impl Into<SharedString>) -> Self {
self.group = Some(group.into());
self
}
pub fn z_index(mut self, z_index: u32) -> Self {
self.base_style.z_index = Some(z_index);
self
}
pub fn overflow_hidden(mut self) -> Self {
self.base_style.overflow.x = Some(Overflow::Hidden);
self.base_style.overflow.y = Some(Overflow::Hidden);
self
}
pub fn overflow_hidden_x(mut self) -> Self {
self.base_style.overflow.x = Some(Overflow::Hidden);
self
}
pub fn overflow_hidden_y(mut self) -> Self {
self.base_style.overflow.y = Some(Overflow::Hidden);
self
}
fn with_element_id<R>(
&mut self,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut Self, Option<GlobalElementId>, &mut ViewContext<V>) -> R,
) -> R {
if let Some(id) = self.id() {
cx.with_element_id(id, |global_id, cx| f(self, Some(global_id), cx))
} else {
f(self, None, cx)
}
}
pub fn compute_style(
&self,
bounds: Bounds<Pixels>,
element_state: &DivState,
cx: &mut ViewContext<V>,
) -> Style {
let mut computed_style = Style::default();
computed_style.refine(&self.base_style);
self.focus.refine_style(&mut computed_style, cx);
self.interaction
.refine_style(&mut computed_style, bounds, &element_state.interactive, cx);
computed_style
}
}
impl<V> Div<V, StatefulInteraction<V>, FocusDisabled>
where
V: 'static + Send + Sync,
{
pub fn focusable(self) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
Div {
interaction: self.interaction,
focus: FocusEnabled::new(),
children: self.children,
group: self.group,
base_style: self.base_style,
}
}
pub fn track_focus(
self,
handle: &FocusHandle,
) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
Div {
interaction: self.interaction,
focus: FocusEnabled::tracked(handle),
children: self.children,
group: self.group,
base_style: self.base_style,
}
}
pub fn overflow_scroll(mut self) -> Self {
self.base_style.overflow.x = Some(Overflow::Scroll);
self.base_style.overflow.y = Some(Overflow::Scroll);
self
}
pub fn overflow_x_scroll(mut self) -> Self {
self.base_style.overflow.x = Some(Overflow::Scroll);
self
}
pub fn overflow_y_scroll(mut self) -> Self {
self.base_style.overflow.y = Some(Overflow::Scroll);
self
}
}
impl<V> Div<V, StatelessInteraction<V>, FocusDisabled>
where
V: 'static + Send + Sync,
{
pub fn track_focus(
self,
handle: &FocusHandle,
) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
Div {
interaction: self.interaction.into_stateful(handle),
focus: handle.clone().into(),
children: self.children,
group: self.group,
base_style: self.base_style,
}
}
}
impl<V, I> Focusable for Div<V, I, FocusEnabled<V>>
where
I: ElementInteraction<V>,
V: 'static + Send + Sync,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
&mut self.focus.focus_listeners
}
fn set_focus_style(&mut self, style: StyleRefinement) {
self.focus.focus_style = style;
}
fn set_focus_in_style(&mut self, style: StyleRefinement) {
self.focus.focus_in_style = style;
}
fn set_in_focus_style(&mut self, style: StyleRefinement) {
self.focus.in_focus_style = style;
}
}
#[derive(Default)]
pub struct DivState {
interactive: InteractiveElementState,
focus_handle: Option<FocusHandle>,
child_layout_ids: SmallVec<[LayoutId; 4]>,
}
impl<V, I, F> Element for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<ElementId> {
self.interaction
.as_stateful()
.map(|identified| identified.id.clone())
}
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> Self::ElementState {
let mut element_state = element_state.unwrap_or_default();
self.focus
.initialize(element_state.focus_handle.take(), cx, |focus_handle, cx| {
element_state.focus_handle = focus_handle;
self.interaction.initialize(cx, |cx| {
for child in &mut self.children {
child.initialize(view_state, cx);
}
})
});
element_state
}
fn layout(
&mut self,
view_state: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId {
let style = self.compute_style(Bounds::default(), element_state, cx);
style.apply_text_style(cx, |cx| {
self.with_element_id(cx, |this, _global_id, cx| {
let layout_ids = this
.children
.iter_mut()
.map(|child| child.layout(view_state, cx))
.collect::<SmallVec<_>>();
element_state.child_layout_ids = layout_ids.clone();
cx.request_layout(&style, layout_ids)
})
})
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) {
self.with_element_id(cx, |this, _global_id, cx| {
if let Some(group) = this.group.clone() {
GroupBounds::push(group, bounds, cx);
}
let style = this.compute_style(bounds, element_state, cx);
let z_index = style.z_index.unwrap_or(0);
let mut child_min = point(Pixels::MAX, Pixels::MAX);
let mut child_max = Point::default();
let content_size = if element_state.child_layout_ids.is_empty() {
bounds.size
} else {
for child_layout_id in &element_state.child_layout_ids {
let child_bounds = cx.layout_bounds(*child_layout_id);
child_min = child_min.min(&child_bounds.origin);
child_max = child_max.max(&child_bounds.lower_right());
}
(child_max - child_min).into()
};
cx.stack(z_index, |cx| {
cx.stack(0, |cx| {
style.paint(bounds, cx);
this.focus.paint(bounds, cx);
this.interaction.paint(
bounds,
content_size,
style.overflow,
&mut element_state.interactive,
cx,
);
});
cx.stack(1, |cx| {
style.apply_text_style(cx, |cx| {
style.apply_overflow(bounds, cx, |cx| {
let scroll_offset = element_state.interactive.scroll_offset();
cx.with_scroll_offset(scroll_offset, |cx| {
for child in &mut this.children {
child.paint(view_state, cx);
}
});
})
})
});
});
if let Some(group) = this.group.as_ref() {
GroupBounds::pop(group, cx);
}
})
}
}
impl<V, I, F> IntoAnyElement<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
fn into_any(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> ParentElement for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
&mut self.children
}
}
impl<V, I, F> Styled for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
fn style(&mut self) -> &mut StyleRefinement {
&mut self.base_style
}
}
impl<V, I, F> StatelessInteractive for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
fn stateless_interactivity(&mut self) -> &mut StatelessInteraction<V> {
self.interaction.as_stateless_mut()
}
}
impl<V, F> StatefulInteractive for Div<V, StatefulInteraction<V>, F>
where
F: ElementFocus<V>,
V: 'static + Send + Sync,
{
fn stateful_interactivity(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
&mut self.interaction
}
}
+194
View File
@@ -0,0 +1,194 @@
use crate::{
div, AnyElement, BorrowWindow, Bounds, Div, DivState, Element, ElementFocus, ElementId,
ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, IntoAnyElement,
LayoutId, Pixels, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, StyleRefinement, Styled, ViewContext,
};
use futures::FutureExt;
use util::ResultExt;
pub struct Img<
V: 'static + Send + Sync,
I: ElementInteraction<V> = StatelessInteraction<V>,
F: ElementFocus<V> = FocusDisabled,
> {
base: Div<V, I, F>,
uri: Option<SharedString>,
grayscale: bool,
}
pub fn img<V>() -> Img<V, StatelessInteraction<V>, FocusDisabled>
where
V: 'static + Send + Sync,
{
Img {
base: div(),
uri: None,
grayscale: false,
}
}
impl<V, I, F> Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
self.uri = Some(uri.into());
self
}
pub fn grayscale(mut self, grayscale: bool) -> Self {
self.grayscale = grayscale;
self
}
}
impl<V, F> Img<V, StatelessInteraction<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocus<V>,
{
pub fn id(self, id: impl Into<ElementId>) -> Img<V, StatefulInteraction<V>, F> {
Img {
base: self.base.id(id),
uri: self.uri,
grayscale: self.grayscale,
}
}
}
impl<V, I, F> IntoAnyElement<V> for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn into_any(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> Element for Img<V, I, F>
where
V: Send + Sync + 'static,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
self.base.id()
}
fn initialize(
&mut self,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
self.base.initialize(view_state, element_state, cx)
}
fn layout(
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) {
cx.stack(0, |cx| {
self.base.paint(bounds, view, element_state, cx);
});
let style = self.base.compute_style(bounds, element_state, cx);
let corner_radii = style.corner_radii;
if let Some(uri) = self.uri.clone() {
let image_future = cx.image_cache.get(uri);
if let Some(data) = image_future
.clone()
.now_or_never()
.and_then(ResultExt::log_err)
{
let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size());
cx.stack(1, |cx| {
cx.paint_image(bounds, corner_radii, data, self.grayscale)
.log_err()
});
} else {
cx.spawn(|_, mut cx| async move {
if image_future.await.log_err().is_some() {
cx.on_next_frame(|cx| cx.notify());
}
})
.detach()
}
}
}
}
impl<V, I, F> Styled for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn style(&mut self) -> &mut StyleRefinement {
self.base.style()
}
}
impl<V, I, F> StatelessInteractive for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn stateless_interactivity(&mut self) -> &mut StatelessInteraction<V> {
self.base.stateless_interactivity()
}
}
impl<V, F> StatefulInteractive for Img<V, StatefulInteraction<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocus<V>,
{
fn stateful_interactivity(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
self.base.stateful_interactivity()
}
}
impl<V, I> Focusable for Img<V, I, FocusEnabled<V>>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
self.base.focus_listeners()
}
fn set_focus_style(&mut self, style: StyleRefinement) {
self.base.set_focus_style(style)
}
fn set_focus_in_style(&mut self, style: StyleRefinement) {
self.base.set_focus_in_style(style)
}
fn set_in_focus_style(&mut self, style: StyleRefinement) {
self.base.set_in_focus_style(style)
}
}
+168
View File
@@ -0,0 +1,168 @@
use crate::{
div, AnyElement, Bounds, Div, DivState, Element, ElementFocus, ElementId, ElementInteraction,
FocusDisabled, FocusEnabled, FocusListeners, Focusable, IntoAnyElement, LayoutId, Pixels,
SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
StatelessInteractive, StyleRefinement, Styled, ViewContext,
};
use util::ResultExt;
pub struct Svg<
V: 'static + Send + Sync,
I: ElementInteraction<V> = StatelessInteraction<V>,
F: ElementFocus<V> = FocusDisabled,
> {
base: Div<V, I, F>,
path: Option<SharedString>,
}
pub fn svg<V>() -> Svg<V, StatelessInteraction<V>, FocusDisabled>
where
V: 'static + Send + Sync,
{
Svg {
base: div(),
path: None,
}
}
impl<V, I, F> Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
pub fn path(mut self, path: impl Into<SharedString>) -> Self {
self.path = Some(path.into());
self
}
}
impl<V, F> Svg<V, StatelessInteraction<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocus<V>,
{
pub fn id(self, id: impl Into<ElementId>) -> Svg<V, StatefulInteraction<V>, F> {
Svg {
base: self.base.id(id),
path: self.path,
}
}
}
impl<V, I, F> IntoAnyElement<V> for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn into_any(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V, I, F> Element for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
self.base.id()
}
fn initialize(
&mut self,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
self.base.initialize(view_state, element_state, cx)
}
fn layout(
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) where
Self: Sized,
{
self.base.paint(bounds, view, element_state, cx);
let color = self
.base
.compute_style(bounds, element_state, cx)
.text
.color;
if let Some((path, color)) = self.path.as_ref().zip(color) {
cx.paint_svg(bounds, path.clone(), color).log_err();
}
}
}
impl<V, I, F> Styled for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn style(&mut self) -> &mut StyleRefinement {
self.base.style()
}
}
impl<V, I, F> StatelessInteractive for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn stateless_interactivity(&mut self) -> &mut StatelessInteraction<V> {
self.base.stateless_interactivity()
}
}
impl<V, F> StatefulInteractive for Svg<V, StatefulInteraction<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocus<V>,
{
fn stateful_interactivity(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
self.base.stateful_interactivity()
}
}
impl<V, I> Focusable for Svg<V, I, FocusEnabled<V>>
where
V: 'static + Send + Sync,
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
self.base.focus_listeners()
}
fn set_focus_style(&mut self, style: StyleRefinement) {
self.base.set_focus_style(style)
}
fn set_focus_in_style(&mut self, style: StyleRefinement) {
self.base.set_focus_in_style(style)
}
fn set_in_focus_style(&mut self, style: StyleRefinement) {
self.base.set_in_focus_style(style)
}
}
+143
View File
@@ -0,0 +1,143 @@
use crate::{
AnyElement, BorrowWindow, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels,
SharedString, Size, ViewContext,
};
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::{marker::PhantomData, sync::Arc};
use util::ResultExt;
impl<S: 'static + Send + Sync> IntoAnyElement<S> for SharedString {
fn into_any(self) -> AnyElement<S> {
Text {
text: self,
state_type: PhantomData,
}
.into_any()
}
}
impl<V: 'static + Send + Sync> IntoAnyElement<V> for &'static str {
fn into_any(self) -> AnyElement<V> {
Text {
text: self.into(),
state_type: PhantomData,
}
.into_any()
}
}
// TODO: Figure out how to pass `String` to `child` without this.
// This impl doesn't exist in the `gpui2` crate.
impl<S: 'static + Send + Sync> IntoAnyElement<S> for String {
fn into_any(self) -> AnyElement<S> {
Text {
text: self.into(),
state_type: PhantomData,
}
.into_any()
}
}
pub struct Text<V> {
text: SharedString,
state_type: PhantomData<V>,
}
impl<V: 'static + Send + Sync> IntoAnyElement<V> for Text<V> {
fn into_any(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<V: 'static + Send + Sync> Element for Text<V> {
type ViewState = V;
type ElementState = Arc<Mutex<Option<TextElementState>>>;
fn id(&self) -> Option<crate::ElementId> {
None
}
fn initialize(
&mut self,
_view_state: &mut V,
element_state: Option<Self::ElementState>,
_cx: &mut ViewContext<V>,
) -> Self::ElementState {
element_state.unwrap_or_default()
}
fn layout(
&mut self,
_view: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) -> LayoutId {
let text_system = cx.text_system().clone();
let text_style = cx.text_style();
let font_size = text_style.font_size * 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 layout_id = cx.request_measured_layout(Default::default(), rem_size, {
let element_state = element_state.clone();
move |known_dimensions, _| {
let Some(lines) = text_system
.layout_text(
&text,
font_size,
&[text_style.to_run(text.len())],
known_dimensions.width, // Wrap if we know the width.
)
.log_err()
else {
return Size::default();
};
let line_count = lines
.iter()
.map(|line| line.wrap_count() + 1)
.sum::<usize>();
let size = Size {
width: lines.iter().map(|line| line.layout.width).max().unwrap(),
height: line_height * line_count,
};
element_state
.lock()
.replace(TextElementState { lines, line_height });
size
}
});
layout_id
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) {
let element_state = element_state.lock();
let element_state = element_state
.as_ref()
.expect("measurement has not been performed");
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;
}
}
}
pub struct TextElementState {
lines: SmallVec<[Line; 1]>,
line_height: Pixels,
}