Checkpoint

This commit is contained in:
Nathan Sobo 2023-09-05 12:16:21 -06:00
parent 0307cb8a88
commit 9ec7569e09
10 changed files with 217 additions and 130 deletions

View file

@ -6,9 +6,10 @@ use crate::{
InteractionHandlers, Interactive,
};
use anyhow::Result;
use gpui::{LayoutId, RenderContext};
use gpui::LayoutId;
use refineable::{Refineable, RefinementCascade};
use smallvec::SmallVec;
use util::ResultExt;
pub struct Div<V: 'static> {
styles: RefinementCascade<Style>,
@ -36,9 +37,8 @@ impl<V: 'static> Element<V> for Div<V> {
Self: Sized,
{
let style = self.computed_style();
let pop_text_style = style.text_style().map_or(false, |style| {
cx.push_text_style(cx.text_style().clone().refined(&style));
true
let pop_text_style = style.text_style(cx).map_or(false, |style| {
cx.push_text_style(&style).log_err().is_some()
});
let children = self
@ -64,10 +64,8 @@ impl<V: 'static> Element<V> for Div<V> {
Self: Sized,
{
let style = &self.computed_style();
let pop_text_style = style.text_style().map_or(false, |style| {
let style = cx.text_style().clone().refined(&style);
cx.push_text_style(style);
true
let pop_text_style = style.text_style(cx).map_or(false, |style| {
cx.push_text_style(&style).log_err().is_some()
});
style.paint_background(layout.bounds, cx);
self.interaction_handlers()

View file

@ -4,7 +4,7 @@ use crate::{
paint_context::PaintContext,
};
use anyhow::Result;
use gpui::{geometry::Size, text_layout::LineLayout, LayoutId, RenderContext};
use gpui::{geometry::Size, text_layout::LineLayout, LayoutId};
use parking_lot::Mutex;
use std::sync::Arc;

View file

@ -18,5 +18,5 @@ pub use gpui::*;
pub use gpui2_macros::{Element, *};
pub use interactive::*;
pub use layout_context::LayoutContext;
pub use platform::{WindowBounds, WindowOptions};
pub use platform::{Platform, WindowBounds, WindowOptions};
pub use view::*;

View file

@ -1,7 +1,7 @@
use crate::{element::LayoutId, style::Style};
use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut};
use gpui::{geometry::Size, MeasureParams, RenderContext, ViewContext};
use gpui::{geometry::Size, MeasureParams};
pub use gpui::{taffy::tree::NodeId, LayoutContext as LegacyLayoutContext};
#[derive(Deref, DerefMut)]
@ -11,24 +11,6 @@ pub struct LayoutContext<'a, 'b, 'c, 'd, V> {
pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>,
}
impl<'a, 'b, V> RenderContext<'a, 'b, V> for LayoutContext<'a, 'b, '_, '_, V> {
fn text_style(&self) -> gpui::fonts::TextStyle {
self.legacy_cx.text_style()
}
fn push_text_style(&mut self, style: gpui::fonts::TextStyle) {
self.legacy_cx.push_text_style(style)
}
fn pop_text_style(&mut self) {
self.legacy_cx.pop_text_style()
}
fn as_view_context(&mut self) -> &mut ViewContext<'a, 'b, V> {
&mut self.view_context
}
}
impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> {
pub fn new(legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>) -> Self {
Self { legacy_cx }
@ -39,7 +21,7 @@ impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> {
style: Style,
children: impl IntoIterator<Item = NodeId>,
) -> Result<LayoutId> {
let rem_size = self.rem_pixels();
let rem_size = self.rem_size();
let id = self
.legacy_cx
.layout_engine()
@ -53,7 +35,7 @@ impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> {
where
F: Fn(MeasureParams) -> Size<f32> + Sync + Send + 'static,
{
let rem_size = self.rem_pixels();
let rem_size = self.rem_size();
let layout_id = self
.layout_engine()
.ok_or_else(|| anyhow!("no layout engine"))?

View file

@ -4,20 +4,23 @@ use crate::{
elements::pressable::{pressable, Pressable},
paint_context::PaintContext,
};
pub use fonts::Style as FontStyle;
pub use fonts::Weight as FontWeight;
pub use gpui::taffy::style::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
Overflow, Position,
};
use gpui::{
fonts::TextStyleRefinement,
fonts::{self, TextStyleRefinement},
geometry::{
rect::RectF, relative, AbsoluteLength, DefiniteLength, Edges, EdgesRefinement, Length,
Point, PointRefinement, Size, SizeRefinement,
},
taffy,
taffy, WindowContext,
};
use gpui2_macros::styleable_helpers;
use refineable::{Refineable, RefinementCascade};
use std::sync::Arc;
#[derive(Clone, Refineable)]
pub struct Style {
@ -92,11 +95,41 @@ pub struct Style {
/// The radius of the corners of this element
#[refineable]
pub corner_radii: CornerRadii,
/// The color of text within this element. Cascades to children unless overridden.
pub text_color: Option<Hsla>,
/// The font size in rems.
pub font_size: Option<f32>,
pub font_family: Option<Arc<str>>,
pub font_weight: Option<FontWeight>,
pub font_style: Option<FontStyle>,
}
impl Style {
pub fn text_style(&self, cx: &WindowContext) -> Option<TextStyleRefinement> {
if self.text_color.is_none()
&& self.font_size.is_none()
&& self.font_family.is_none()
&& self.font_weight.is_none()
&& self.font_style.is_none()
{
return None;
}
Some(TextStyleRefinement {
color: self.text_color.map(Into::into),
font_family: self.font_family.clone(),
font_size: self.font_size.map(|size| size * cx.rem_size()),
font_weight: self.font_weight,
font_style: self.font_style,
underline: None,
})
}
pub fn to_taffy(&self, rem_size: f32) -> taffy::style::Style {
taffy::style::Style {
display: self.display,
@ -128,7 +161,7 @@ impl Style {
/// Paints the background of an element styled with this style.
/// Return the bounds in which to paint the content.
pub fn paint_background<V: 'static>(&self, bounds: RectF, cx: &mut PaintContext<V>) {
let rem_size = cx.rem_pixels();
let rem_size = cx.rem_size();
if let Some(color) = self.fill.as_ref().and_then(Fill::color) {
cx.scene.push_quad(gpui::Quad {
bounds,
@ -138,17 +171,6 @@ impl Style {
});
}
}
pub fn text_style(&self) -> Option<TextStyleRefinement> {
if let Some(color) = self.text_color {
Some(TextStyleRefinement {
color: Some(color.into()),
..Default::default()
})
} else {
None
}
}
}
impl Default for Style {
@ -182,29 +204,12 @@ impl Default for Style {
flex_shrink: 1.0,
flex_basis: Length::Auto,
fill: None,
text_color: None,
corner_radii: CornerRadii::default(),
}
}
}
impl StyleRefinement {
pub fn text_style(&self) -> Option<TextStyleRefinement> {
self.text_color.map(|color| TextStyleRefinement {
color: Some(color.into()),
..Default::default()
})
}
}
pub struct OptionalTextStyle {
color: Option<Hsla>,
}
impl OptionalTextStyle {
pub fn apply(&self, style: &mut gpui::fonts::TextStyle) {
if let Some(color) = self.color {
style.color = color.into();
text_color: None,
font_size: Some(1.),
font_family: None,
font_weight: None,
font_style: None,
}
}
}
@ -416,4 +421,68 @@ pub trait StyleHelpers: Styleable<Style = Style> {
self.declared_style().text_color = Some(color.into());
self
}
fn text_xs(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(0.75);
self
}
fn text_sm(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(0.875);
self
}
fn text_base(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(1.0);
self
}
fn text_lg(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(1.125);
self
}
fn text_xl(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(1.25);
self
}
fn text_2xl(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(1.5);
self
}
fn text_3xl(mut self) -> Self
where
Self: Sized,
{
self.declared_style().font_size = Some(1.875);
self
}
fn font(mut self, family_name: impl Into<Arc<str>>) -> Self
where
Self: Sized,
{
self.declared_style().font_family = Some(family_name.into());
self
}
}