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 {
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 {

View File

@ -179,7 +179,7 @@ impl<'a> FlowLayouter<'a> {
// the region expands.
let size = Size::new(
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
} else {
self.used.h
@ -196,11 +196,8 @@ impl<'a> FlowLayouter<'a> {
match item {
FlowItem::Absolute(v) => before += v,
FlowItem::Fractional(v) => {
let ratio = v / self.fr;
let remaining = self.full.h - self.used.h;
if remaining.is_finite() && ratio.is_finite() {
before += ratio * remaining;
}
before += v.resolve(self.fr, remaining);
}
FlowItem::Frame(frame, 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) {
for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) {
if let TrackSizing::Fractional(v) = col {
let ratio = v / fr;
if ratio.is_finite() {
*rcol = ratio * remaining;
}
*rcol = v.resolve(fr, remaining);
}
}
}
@ -547,7 +544,7 @@ impl<'a> GridLayouter<'a> {
// Determine the size of the grid in this region, expanding fully if
// there are fr rows.
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;
self.cts.exact.y = Some(self.full);
} else {
@ -563,14 +560,9 @@ impl<'a> GridLayouter<'a> {
let frame = match row {
Row::Frame(frame) => frame,
Row::Fr(v, y) => {
let ratio = v / self.fr;
let remaining = self.full - self.used.h;
if remaining.is_finite() && ratio.is_finite() {
let resolved = ratio * remaining;
self.layout_single_row(ctx, resolved, y)
} else {
continue;
}
let height = v.resolve(self.fr, remaining);
self.layout_single_row(ctx, height, y)
}
};

View File

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

View File

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

View File

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