Add support for a measure function to the layout engine facade

This commit is contained in:
Nathan Sobo 2023-08-15 22:02:56 -06:00
parent 4efc46c763
commit a8ecc1a643
5 changed files with 261 additions and 181 deletions

View file

@ -2,13 +2,14 @@ use std::{any::Any, rc::Rc};
use crate::{
adapter::Adapter,
style::{DefinedLength, Display, ElementStyle, Fill, Length, Overflow, Position},
style::{Display, ElementStyle, Fill, Overflow, Position},
};
use anyhow::Result;
use derive_more::{Deref, DerefMut};
use gpui::{
scene::MouseClick, EngineLayout, LayoutContext as LegacyLayoutContext,
PaintContext as LegacyPaintContext,
geometry::{DefinedLength, Length},
scene::MouseClick,
EngineLayout, LayoutContext as LegacyLayoutContext, PaintContext as LegacyPaintContext,
};
use playground_macros::tailwind_lengths;
pub use taffy::tree::NodeId;

View file

@ -3,13 +3,12 @@ use components::button;
use element::Element;
use frame::frame;
use gpui::{
geometry::{rect::RectF, vector::vec2f},
geometry::{percent, rect::RectF, vector::vec2f},
platform::WindowOptions,
};
use log::LevelFilter;
use simplelog::SimpleLogger;
use style::percent;
use themes::{rose_pine, ThemeColors};
use view::view;

View file

@ -1,4 +1,5 @@
use crate::color::Hsla;
use gpui::geometry::{DefinedLength, Edges, Length, Point, Size};
pub use taffy::style::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
Overflow, Position,
@ -143,180 +144,6 @@ impl Default for ElementStyle {
}
}
#[derive(Clone)]
pub struct Point<T> {
pub x: T,
pub y: T,
}
impl<T> Into<taffy::geometry::Point<T>> for Point<T> {
fn into(self) -> taffy::geometry::Point<T> {
taffy::geometry::Point {
x: self.x,
y: self.y,
}
}
}
#[derive(Clone)]
pub struct Size<T> {
pub width: T,
pub height: T,
}
impl Size<DefinedLength> {
pub const fn zero() -> Self {
Self {
width: DefinedLength::Pixels(0.),
height: DefinedLength::Pixels(0.),
}
}
pub fn to_taffy(&self, rem_size: f32) -> taffy::geometry::Size<taffy::style::LengthPercentage> {
taffy::geometry::Size {
width: self.width.to_taffy(rem_size),
height: self.height.to_taffy(rem_size),
}
}
}
impl Size<Length> {
pub const fn auto() -> Self {
Self {
width: Length::Auto,
height: Length::Auto,
}
}
pub fn to_taffy<T: From<taffy::prelude::LengthPercentageAuto>>(
&self,
rem_size: f32,
) -> taffy::geometry::Size<T> {
taffy::geometry::Size {
width: self.width.to_taffy(rem_size).into(),
height: self.height.to_taffy(rem_size).into(),
}
}
}
#[derive(Clone)]
pub struct Edges<T> {
pub top: T,
pub right: T,
pub bottom: T,
pub left: T,
}
impl Edges<DefinedLength> {
pub const fn zero() -> Self {
Self {
top: DefinedLength::Pixels(0.0),
right: DefinedLength::Pixels(0.0),
bottom: DefinedLength::Pixels(0.0),
left: DefinedLength::Pixels(0.0),
}
}
pub fn to_taffy(&self, rem_size: f32) -> taffy::geometry::Rect<taffy::style::LengthPercentage> {
taffy::geometry::Rect {
top: self.top.to_taffy(rem_size),
right: self.right.to_taffy(rem_size),
bottom: self.bottom.to_taffy(rem_size),
left: self.left.to_taffy(rem_size),
}
}
}
impl Edges<Length> {
pub const fn auto() -> Self {
Self {
top: Length::Auto,
right: Length::Auto,
bottom: Length::Auto,
left: Length::Auto,
}
}
pub const fn zero() -> Self {
Self {
top: Length::Defined(DefinedLength::Pixels(0.0)),
right: Length::Defined(DefinedLength::Pixels(0.0)),
bottom: Length::Defined(DefinedLength::Pixels(0.0)),
left: Length::Defined(DefinedLength::Pixels(0.0)),
}
}
pub fn to_taffy(
&self,
rem_size: f32,
) -> taffy::geometry::Rect<taffy::style::LengthPercentageAuto> {
taffy::geometry::Rect {
top: self.top.to_taffy(rem_size),
right: self.right.to_taffy(rem_size),
bottom: self.bottom.to_taffy(rem_size),
left: self.left.to_taffy(rem_size),
}
}
}
/// A non-auto length that can be defined in pixels, rems, or percent of parent.
#[derive(Clone, Copy)]
pub enum DefinedLength {
Pixels(f32),
Rems(f32),
Percent(f32), // 0. - 100.
}
impl DefinedLength {
fn to_taffy(&self, rem_size: f32) -> taffy::style::LengthPercentage {
match self {
DefinedLength::Pixels(pixels) => taffy::style::LengthPercentage::Length(*pixels),
DefinedLength::Rems(rems) => taffy::style::LengthPercentage::Length(rems * rem_size),
DefinedLength::Percent(percent) => {
taffy::style::LengthPercentage::Percent(*percent / 100.)
}
}
}
}
/// A length that can be defined in pixels, rems, percent of parent, or auto.
#[derive(Clone, Copy)]
pub enum Length {
Defined(DefinedLength),
Auto,
}
pub fn auto() -> Length {
Length::Auto
}
pub fn percent(percent: f32) -> DefinedLength {
DefinedLength::Percent(percent)
}
pub fn rems(rems: f32) -> DefinedLength {
DefinedLength::Rems(rems)
}
pub fn pixels(pixels: f32) -> DefinedLength {
DefinedLength::Pixels(pixels)
}
impl Length {
fn to_taffy(&self, rem_size: f32) -> taffy::prelude::LengthPercentageAuto {
match self {
Length::Defined(length) => length.to_taffy(rem_size).into(),
Length::Auto => taffy::prelude::LengthPercentageAuto::Auto,
}
}
}
impl From<DefinedLength> for Length {
fn from(value: DefinedLength) -> Self {
Length::Defined(value)
}
}
#[derive(Clone)]
pub enum Fill {
Color(Hsla),