Skip to content

Commit

Permalink
Making unsized Spacers similar to unsized Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoods committed Nov 14, 2024
1 parent 7be6e7b commit bf7c841
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 10 additions & 9 deletions composable-views/src/layout/spacing.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use std::cell::OnceCell;
use std::cell::Cell;

use crate::{Bounds, Output, Size, View};

pub struct Spacer(pub(crate) OnceCell<Size>);
pub struct Spacer(Cell<Size>);

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)]
Expand All @@ -37,19 +35,22 @@ 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)]
fn draw(&self, bounds: Bounds, onto: &mut impl Output) {}

#[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);
}
}
2 changes: 2 additions & 0 deletions composable-views/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit bf7c841

Please sign in to comment.