From 2303111e09d806ef2a652bddc2b73be6dccf6ae2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Nov 2019 01:55:32 +0100 Subject: Draft new layout API --- native/src/layout/limits.rs | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 native/src/layout/limits.rs (limited to 'native/src/layout/limits.rs') diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs new file mode 100644 index 00000000..ad105119 --- /dev/null +++ b/native/src/layout/limits.rs @@ -0,0 +1,6 @@ +#[derive(Debug)] +pub struct Limits {} + +impl Limits { + pub const NONE: Limits = Limits {}; +} -- cgit From 0240c3981b716c82ecb3364945815335b420a63e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Nov 2019 06:05:20 +0100 Subject: Draft custom layout engine based on `druid` --- native/src/layout/limits.rs | 131 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 3 deletions(-) (limited to 'native/src/layout/limits.rs') diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index ad105119..e5cee5df 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -1,6 +1,131 @@ -#[derive(Debug)] -pub struct Limits {} +use crate::{Length, Size}; + +#[derive(Debug, Clone, Copy)] +pub struct Limits { + min: Size, + max: Size, + fill: Size, +} impl Limits { - pub const NONE: Limits = Limits {}; + pub const NONE: Limits = Limits { + min: Size::ZERO, + max: Size::INFINITY, + fill: Size::INFINITY, + }; + + pub fn new(min: Size, max: Size) -> Limits { + Limits { + min, + max, + fill: Size::INFINITY, + } + } + + pub fn width(mut self, width: Length) -> Limits { + match width { + Length::Shrink => { + self.fill.width = self.min.width; + } + Length::Fill => { + self.fill.width = self.fill.width.min(self.max.width); + } + Length::Units(units) => { + let new_width = + (units as f32).min(self.max.width).max(self.min.width); + + self.min.width = new_width; + self.max.width = new_width; + self.fill.width = new_width; + } + } + + self + } + + pub fn height(mut self, height: Length) -> Limits { + match height { + Length::Shrink => { + self.fill.height = self.min.height; + } + Length::Fill => { + self.fill.height = self.fill.height.min(self.max.height); + } + Length::Units(units) => { + let new_height = + (units as f32).min(self.max.height).max(self.min.height); + + self.min.height = new_height; + self.max.height = new_height; + self.fill.height = new_height; + } + } + + self + } + + pub fn min_width(mut self, min_width: u32) -> Limits { + self.min.width = + self.min.width.max(min_width as f32).min(self.max.width); + + self + } + + pub fn max_width(mut self, max_width: u32) -> Limits { + self.max.width = + self.max.width.min(max_width as f32).max(self.min.width); + + self + } + + pub fn max_height(mut self, max_height: u32) -> Limits { + self.max.height = + self.max.height.min(max_height as f32).max(self.min.height); + + self + } + + pub fn resolve(&self, intrinsic_size: Size) -> Size { + Size::new( + intrinsic_size + .width + .min(self.max.width) + .max(self.fill.width), + intrinsic_size + .height + .min(self.max.height) + .max(self.fill.height), + ) + } + + pub fn min(&self) -> Size { + self.min + } + + pub fn max(&self) -> Size { + self.max + } + + pub fn pad(&self, padding: f32) -> Limits { + self.shrink(Size::new(padding * 2.0, padding * 2.0)) + } + + pub fn shrink(&self, size: Size) -> Limits { + let min = Size::new( + (self.min().width - size.width).max(0.0), + (self.min().height - size.height).max(0.0), + ); + + let max = Size::new( + (self.max().width - size.width).max(0.0), + (self.max().height - size.height).max(0.0), + ); + + let fill = Size::new( + (self.fill.width - size.width).max(0.0), + (self.fill.height - size.height).max(0.0), + ); + + Limits { min, max, fill } + } } -- cgit From ceb02f4a36769c488c2525db2fb73f092a6c2706 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 11 Nov 2019 05:26:08 +0100 Subject: Implement `Container` widget Remove `align_self` and `justify_content` methods --- native/src/layout/limits.rs | 50 ++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'native/src/layout/limits.rs') diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index e5cee5df..af269acd 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -22,6 +22,14 @@ impl Limits { } } + pub fn min(&self) -> Size { + self.min + } + + pub fn max(&self) -> Size { + self.max + } + pub fn width(mut self, width: Length) -> Limits { match width { Length::Shrink => { @@ -85,27 +93,6 @@ impl Limits { self } - pub fn resolve(&self, intrinsic_size: Size) -> Size { - Size::new( - intrinsic_size - .width - .min(self.max.width) - .max(self.fill.width), - intrinsic_size - .height - .min(self.max.height) - .max(self.fill.height), - ) - } - - pub fn min(&self) -> Size { - self.min - } - - pub fn max(&self) -> Size { - self.max - } - pub fn pad(&self, padding: f32) -> Limits { self.shrink(Size::new(padding * 2.0, padding * 2.0)) } @@ -128,4 +115,25 @@ impl Limits { Limits { min, max, fill } } + + pub fn loose(&self) -> Limits { + Limits { + min: Size::ZERO, + max: self.max, + fill: self.fill, + } + } + + pub fn resolve(&self, intrinsic_size: Size) -> Size { + Size::new( + intrinsic_size + .width + .min(self.max.width) + .max(self.fill.width), + intrinsic_size + .height + .min(self.max.height) + .max(self.fill.height), + ) + } } -- cgit