Checkpoint

This commit is contained in:
Nathan Sobo
2023-10-10 22:14:47 -06:00
parent 61b8ad38bd
commit f53b63eaf6
17 changed files with 567 additions and 472 deletions
+21 -23
View File
@@ -1,11 +1,11 @@
use crate::{
AnyElement, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels, Result, Size, ViewContext,
AnyElement, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels, Size, ViewContext,
};
use parking_lot::Mutex;
use std::{marker::PhantomData, sync::Arc};
use util::{arc_cow::ArcCow, ResultExt};
impl<S: 'static> IntoAnyElement<S> for ArcCow<'static, str> {
impl<S: 'static + Send + Sync> IntoAnyElement<S> for ArcCow<'static, str> {
fn into_any(self) -> AnyElement<S> {
Text {
text: self,
@@ -15,7 +15,7 @@ impl<S: 'static> IntoAnyElement<S> for ArcCow<'static, str> {
}
}
impl<V: 'static> IntoAnyElement<V> for &'static str {
impl<V: 'static + Send + Sync> IntoAnyElement<V> for &'static str {
fn into_any(self) -> AnyElement<V> {
Text {
text: ArcCow::from(self),
@@ -30,15 +30,16 @@ pub struct Text<S> {
state_type: PhantomData<S>,
}
impl<S: 'static> Element for Text<S> {
type State = S;
type FrameState = Arc<Mutex<Option<TextFrameState>>>;
impl<S: 'static + Send + Sync> Element for Text<S> {
type ViewState = S;
type ElementState = Arc<Mutex<Option<TextElementState>>>;
fn layout(
&mut self,
_view: &mut S,
_element_state: Option<Self::ElementState>,
cx: &mut ViewContext<S>,
) -> Result<(LayoutId, Self::FrameState)> {
) -> (LayoutId, Self::ElementState) {
let text_system = cx.text_system().clone();
let text_style = cx.text_style();
let font_size = text_style.font_size * cx.rem_size();
@@ -46,11 +47,11 @@ impl<S: 'static> Element for Text<S> {
.line_height
.to_pixels(font_size.into(), cx.rem_size());
let text = self.text.clone();
let frame_state = Arc::new(Mutex::new(None));
let element_state = Arc::new(Mutex::new(None));
let rem_size = cx.rem_size();
let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
let frame_state = frame_state.clone();
let element_state = element_state.clone();
move |_, _| {
let Some(line_layout) = text_system
.layout_line(
@@ -68,7 +69,7 @@ impl<S: 'static> Element for Text<S> {
height: line_height,
};
frame_state.lock().replace(TextFrameState {
element_state.lock().replace(TextElementState {
line: Arc::new(line_layout),
line_height,
});
@@ -77,35 +78,32 @@ impl<S: 'static> Element for Text<S> {
}
});
Ok((layout_id?, frame_state))
(layout_id, element_state)
}
fn paint<'a>(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::State,
frame_state: &mut Self::FrameState,
_: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<S>,
) -> Result<()> {
) {
let line;
let line_height;
{
let frame_state = frame_state.lock();
let frame_state = frame_state
let element_state = element_state.lock();
let element_state = element_state
.as_ref()
.expect("measurement has not been performed");
line = frame_state.line.clone();
line_height = frame_state.line_height;
line = element_state.line.clone();
line_height = element_state.line_height;
}
// todo!("We haven't added visible bounds to the new element system yet, so this is a placeholder.");
line.paint(bounds, bounds, line_height, cx)?;
Ok(())
line.paint(bounds, bounds, line_height, cx).log_err();
}
}
pub struct TextFrameState {
pub struct TextElementState {
line: Arc<Line>,
line_height: Pixels,
}