From d360cd3434865052dacbbe4329b0dac5bdf515eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Mon, 25 Feb 2019 19:03:35 +0100 Subject: [PATCH] Support exact ratios for layout constraints --- src/layout.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/layout.rs b/src/layout.rs index fe1d63f..9e63722 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -24,6 +24,7 @@ pub enum Direction { pub enum Constraint { // TODO: enforce range 0 - 100 Percentage(u16), + Ratio(u32, u32), Length(u16), Max(u16), Min(u16), @@ -159,6 +160,11 @@ fn split(area: Rect, layout: &Layout) -> Vec { Constraint::Percentage(v) => { elements[i].width | EQ(WEAK) | (f64::from(v * dest_area.width) / 100.0) } + Constraint::Ratio(n, d) => { + elements[i].width + | EQ(WEAK) + | (f64::from(dest_area.width) * f64::from(n) / f64::from(d)) + } Constraint::Min(v) => elements[i].width | GE(WEAK) | f64::from(v), Constraint::Max(v) => elements[i].width | LE(WEAK) | f64::from(v), }); @@ -176,6 +182,11 @@ fn split(area: Rect, layout: &Layout) -> Vec { Constraint::Percentage(v) => { elements[i].height | EQ(WEAK) | (f64::from(v * dest_area.height) / 100.0) } + Constraint::Ratio(n, d) => { + elements[i].height + | EQ(WEAK) + | (f64::from(dest_area.height) * f64::from(n) / f64::from(d)) + } Constraint::Min(v) => elements[i].height | GE(WEAK) | f64::from(v), Constraint::Max(v) => elements[i].height | LE(WEAK) | f64::from(v), });