From bf7c84180ed5d72489e27f9e98911ec9fb5f79a1 Mon Sep 17 00:00:00 2001 From: Bryan Woods Date: Thu, 14 Nov 2024 00:53:14 -0800 Subject: [PATCH] Making unsized `Spacer`s similar to unsized `Path`s --- composable-views/src/layout/spacing.rs | 19 ++++++++++--------- composable-views/src/text/font.rs | 2 ++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/composable-views/src/layout/spacing.rs b/composable-views/src/layout/spacing.rs index 0b0a249f..8a1c8c6a 100644 --- a/composable-views/src/layout/spacing.rs +++ b/composable-views/src/layout/spacing.rs @@ -1,20 +1,18 @@ -use std::cell::OnceCell; +use std::cell::Cell; use crate::{Bounds, Output, Size, View}; -pub struct Spacer(pub(crate) OnceCell); +pub struct Spacer(Cell); impl Spacer { #[inline(always)] pub fn fill() -> Self { - Spacer(OnceCell::new()) // a flexible spacer has no size (yet) + Spacer(Cell::new(Size::new(f32::INFINITY, f32::INFINITY))) // a flexible spacer has no size (yet) } #[inline(always)] pub fn fixed(width: f32, height: f32) -> Self { - let spacer = Self::fill(); - spacer.0.set(Size::new(width, height)).ok(); - spacer + Spacer(Cell::new(Size::new(width, height))) } #[inline(always)] @@ -37,7 +35,10 @@ impl Spacer { impl View for Spacer { #[inline] fn size(&self) -> Size { - self.0.get().cloned().unwrap_or_default() + match self.0.get() { + size if size.is_finite() => size, + _ => Size::zero(), + } } #[inline(always)] @@ -45,11 +46,11 @@ impl View for Spacer { #[inline(always)] fn needs_layout(&self) -> bool { - self.0.get().is_none() + self.0.get().is_finite() } #[inline] fn update_layout(&self, size: Size, _bounds: Bounds) { - self.0.set(size).ok(); + self.0.set(size); } } diff --git a/composable-views/src/text/font.rs b/composable-views/src/text/font.rs index 22aed368..fd7d501c 100644 --- a/composable-views/src/text/font.rs +++ b/composable-views/src/text/font.rs @@ -60,11 +60,13 @@ impl<'a> Font<'a> { } /// Horizontal face ascender. + #[inline] pub fn ascender(&self) -> f32 { self.face.ascender() as f32 } /// Horizontal face descender, + #[inline] pub fn descender(&self) -> f32 { self.face.descender() as f32 }