Abstraction for fr resolving

This commit is contained in:
Laurenz 2021-11-16 18:15:47 +01:00
parent 79638d4bbd
commit 0cdf17216f
6 changed files with 21 additions and 34 deletions

View File

@ -34,6 +34,16 @@ impl Fractional {
pub fn abs(self) -> Self { pub fn abs(self) -> Self {
Self::new(self.get().abs()) Self::new(self.get().abs())
} }
/// Resolve this fractionals share in the remaining space.
pub fn resolve(self, total: Self, remaining: Length) -> Length {
let ratio = self / total;
if ratio.is_finite() && remaining.is_finite() {
ratio * remaining
} else {
Length::zero()
}
}
} }
impl Debug for Fractional { impl Debug for Fractional {

View File

@ -179,7 +179,7 @@ impl<'a> FlowLayouter<'a> {
// the region expands. // the region expands.
let size = Size::new( let size = Size::new(
if self.expand.x { self.full.w } else { self.used.w }, if self.expand.x { self.full.w } else { self.used.w },
if self.expand.y || (!self.fr.is_zero() && self.full.h.is_finite()) { if self.expand.y || (self.fr.get() > 0.0 && self.full.h.is_finite()) {
self.full.h self.full.h
} else { } else {
self.used.h self.used.h
@ -196,11 +196,8 @@ impl<'a> FlowLayouter<'a> {
match item { match item {
FlowItem::Absolute(v) => before += v, FlowItem::Absolute(v) => before += v,
FlowItem::Fractional(v) => { FlowItem::Fractional(v) => {
let ratio = v / self.fr;
let remaining = self.full.h - self.used.h; let remaining = self.full.h - self.used.h;
if remaining.is_finite() && ratio.is_finite() { before += v.resolve(self.fr, remaining);
before += ratio * remaining;
}
} }
FlowItem::Frame(frame, align) => { FlowItem::Frame(frame, align) => {
ruler = ruler.max(align); ruler = ruler.max(align);

View File

@ -314,10 +314,7 @@ impl<'a> GridLayouter<'a> {
fn grow_fractional_columns(&mut self, remaining: Length, fr: Fractional) { fn grow_fractional_columns(&mut self, remaining: Length, fr: Fractional) {
for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) { for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) {
if let TrackSizing::Fractional(v) = col { if let TrackSizing::Fractional(v) = col {
let ratio = v / fr; *rcol = v.resolve(fr, remaining);
if ratio.is_finite() {
*rcol = ratio * remaining;
}
} }
} }
} }
@ -547,7 +544,7 @@ impl<'a> GridLayouter<'a> {
// Determine the size of the grid in this region, expanding fully if // Determine the size of the grid in this region, expanding fully if
// there are fr rows. // there are fr rows.
let mut size = self.used; let mut size = self.used;
if !self.fr.is_zero() && self.full.is_finite() { if self.fr.get() > 0.0 && self.full.is_finite() {
size.h = self.full; size.h = self.full;
self.cts.exact.y = Some(self.full); self.cts.exact.y = Some(self.full);
} else { } else {
@ -563,14 +560,9 @@ impl<'a> GridLayouter<'a> {
let frame = match row { let frame = match row {
Row::Frame(frame) => frame, Row::Frame(frame) => frame,
Row::Fr(v, y) => { Row::Fr(v, y) => {
let ratio = v / self.fr;
let remaining = self.full - self.used.h; let remaining = self.full - self.used.h;
if remaining.is_finite() && ratio.is_finite() { let height = v.resolve(self.fr, remaining);
let resolved = ratio * remaining; self.layout_single_row(ctx, height, y)
self.layout_single_row(ctx, resolved, y)
} else {
continue;
}
} }
}; };

View File

@ -498,12 +498,7 @@ impl<'a> LineLayout<'a> {
match *item { match *item {
ParItem::Absolute(v) => offset += v, ParItem::Absolute(v) => offset += v,
ParItem::Fractional(v) => { ParItem::Fractional(v) => offset += v.resolve(self.fr, remaining),
let ratio = v / self.fr;
if remaining.is_finite() && ratio.is_finite() {
offset += ratio * remaining;
}
}
ParItem::Text(ref shaped, align) => position(shaped.build(), align), ParItem::Text(ref shaped, align) => position(shaped.build(), align),
ParItem::Frame(ref frame, align) => position(frame.clone(), align), ParItem::Frame(ref frame, align) => position(frame.clone(), align),
} }

View File

@ -147,7 +147,7 @@ impl Layout for ShapeNode {
} else { } else {
let default = Length::pt(30.0); let default = Length::pt(30.0);
let size = Size::new( let size = Size::new(
if regions.expand.x && regions.current.w.is_finite() { if regions.expand.x {
regions.current.w regions.current.w
} else { } else {
match self.kind { match self.kind {
@ -155,11 +155,7 @@ impl Layout for ShapeNode {
ShapeKind::Rect | ShapeKind::Ellipse => 1.5 * default, ShapeKind::Rect | ShapeKind::Ellipse => 1.5 * default,
} }
}, },
if regions.expand.y && regions.current.h.is_finite() { if regions.expand.y { regions.current.h } else { default },
regions.current.h
} else {
default
},
); );
Frame::new(size, size.h) Frame::new(size, size.h)

View File

@ -204,7 +204,7 @@ impl<'a> StackLayouter<'a> {
// Expand fully if there are fr spacings. // Expand fully if there are fr spacings.
let full = self.full.get(self.axis); let full = self.full.get(self.axis);
if !self.fr.is_zero() && full.is_finite() { if self.fr.get() > 0.0 && full.is_finite() {
size.set(self.axis, full); size.set(self.axis, full);
} }
@ -216,11 +216,8 @@ impl<'a> StackLayouter<'a> {
match item { match item {
StackItem::Absolute(v) => before += v, StackItem::Absolute(v) => before += v,
StackItem::Fractional(v) => { StackItem::Fractional(v) => {
let ratio = v / self.fr;
let remaining = self.full.get(self.axis) - self.used.block; let remaining = self.full.get(self.axis) - self.used.block;
if remaining.is_finite() && ratio.is_finite() { before += v.resolve(self.fr, remaining)
before += ratio * remaining;
}
} }
StackItem::Frame(frame) => { StackItem::Frame(frame) => {
let parent = size.get(self.axis); let parent = size.get(self.axis);