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` --- core/src/length.rs | 12 +++++++++++- core/src/rectangle.rs | 2 +- core/src/widget/column.rs | 14 ++++++++------ core/src/widget/row.rs | 14 ++++++++------ core/src/widget/scrollable.rs | 10 ++++++---- 5 files changed, 34 insertions(+), 18 deletions(-) (limited to 'core') diff --git a/core/src/length.rs b/core/src/length.rs index 0e670038..73c227d8 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -1,7 +1,17 @@ /// The strategy used to fill space in a specific dimension. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Hash)] pub enum Length { Fill, Shrink, Units(u16), } + +impl Length { + pub fn fill_factor(&self) -> u16 { + match self { + Length::Fill => 1, + Length::Shrink => 0, + Length::Units(_) => 0, + } + } +} diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index c3191677..ee1e3807 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -1,7 +1,7 @@ use crate::Point; /// A rectangle. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct Rectangle { /// X coordinate of the top-left corner. pub x: T, diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs index 2df327a0..f0c47f50 100644 --- a/core/src/widget/column.rs +++ b/core/src/widget/column.rs @@ -1,5 +1,7 @@ use crate::{Align, Justify, Length}; +use std::u32; + /// A container that distributes its contents vertically. /// /// A [`Column`] will try to fill the horizontal space of its container. @@ -10,8 +12,8 @@ pub struct Column { pub padding: u16, pub width: Length, pub height: Length, - pub max_width: Length, - pub max_height: Length, + pub max_width: u32, + pub max_height: u32, pub align_self: Option, pub align_items: Align, pub justify_content: Justify, @@ -28,8 +30,8 @@ impl Column { padding: 0, width: Length::Fill, height: Length::Shrink, - max_width: Length::Shrink, - max_height: Length::Shrink, + max_width: u32::MAX, + max_height: u32::MAX, align_self: None, align_items: Align::Start, justify_content: Justify::Start, @@ -74,7 +76,7 @@ impl Column { /// Sets the maximum width of the [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: Length) -> Self { + pub fn max_width(mut self, max_width: u32) -> Self { self.max_width = max_width; self } @@ -82,7 +84,7 @@ impl Column { /// Sets the maximum height of the [`Column`] in pixels. /// /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: Length) -> Self { + pub fn max_height(mut self, max_height: u32) -> Self { self.max_height = max_height; self } diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs index 6bdb4ed2..10716a7a 100644 --- a/core/src/widget/row.rs +++ b/core/src/widget/row.rs @@ -1,5 +1,7 @@ use crate::{Align, Justify, Length}; +use std::u32; + /// A container that distributes its contents horizontally. /// /// A [`Row`] will try to fill the horizontal space of its container. @@ -10,8 +12,8 @@ pub struct Row { pub padding: u16, pub width: Length, pub height: Length, - pub max_width: Length, - pub max_height: Length, + pub max_width: u32, + pub max_height: u32, pub align_self: Option, pub align_items: Align, pub justify_content: Justify, @@ -28,8 +30,8 @@ impl Row { padding: 0, width: Length::Fill, height: Length::Shrink, - max_width: Length::Shrink, - max_height: Length::Shrink, + max_width: u32::MAX, + max_height: u32::MAX, align_self: None, align_items: Align::Start, justify_content: Justify::Start, @@ -74,7 +76,7 @@ impl Row { /// Sets the maximum width of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: Length) -> Self { + pub fn max_width(mut self, max_width: u32) -> Self { self.max_width = max_width; self } @@ -82,7 +84,7 @@ impl Row { /// Sets the maximum height of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: Length) -> Self { + pub fn max_height(mut self, max_height: u32) -> Self { self.max_height = max_height; self } diff --git a/core/src/widget/scrollable.rs b/core/src/widget/scrollable.rs index 31a5abed..c5a2fc59 100644 --- a/core/src/widget/scrollable.rs +++ b/core/src/widget/scrollable.rs @@ -1,10 +1,12 @@ use crate::{Align, Column, Length, Point, Rectangle}; +use std::u32; + #[derive(Debug)] pub struct Scrollable<'a, Element> { pub state: &'a mut State, pub height: Length, - pub max_height: Length, + pub max_height: u32, pub align_self: Option, pub content: Column, } @@ -14,7 +16,7 @@ impl<'a, Element> Scrollable<'a, Element> { Scrollable { state, height: Length::Shrink, - max_height: Length::Shrink, + max_height: u32::MAX, align_self: None, content: Column::new(), } @@ -57,7 +59,7 @@ impl<'a, Element> Scrollable<'a, Element> { /// Sets the maximum width of the [`Scrollable`]. /// /// [`Scrollable`]: struct.Scrollable.html - pub fn max_width(mut self, max_width: Length) -> Self { + pub fn max_width(mut self, max_width: u32) -> Self { self.content = self.content.max_width(max_width); self } @@ -65,7 +67,7 @@ impl<'a, Element> Scrollable<'a, Element> { /// Sets the maximum height of the [`Scrollable`] in pixels. /// /// [`Scrollable`]: struct.Scrollable.html - pub fn max_height(mut self, max_height: Length) -> Self { + pub fn max_height(mut self, max_height: u32) -> Self { self.max_height = max_height; self } -- 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 --- core/src/align.rs | 3 -- core/src/justify.rs | 27 ------------- core/src/lib.rs | 2 - core/src/widget.rs | 2 + core/src/widget/button.rs | 16 +------- core/src/widget/column.rs | 26 +----------- core/src/widget/container.rs | 94 +++++++++++++++++++++++++++++++++++++++++++ core/src/widget/image.rs | 14 ------- core/src/widget/row.rs | 26 +----------- core/src/widget/scrollable.rs | 17 +------- 10 files changed, 101 insertions(+), 126 deletions(-) delete mode 100644 core/src/justify.rs create mode 100644 core/src/widget/container.rs (limited to 'core') diff --git a/core/src/align.rs b/core/src/align.rs index 5dbd658d..d6915086 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -15,7 +15,4 @@ pub enum Align { /// Align at the end of the cross axis. End, - - /// Stretch over the cross axis. - Stretch, } diff --git a/core/src/justify.rs b/core/src/justify.rs deleted file mode 100644 index 53aa7319..00000000 --- a/core/src/justify.rs +++ /dev/null @@ -1,27 +0,0 @@ -/// Distribution on the main axis of a container. -/// -/// * On a [`Column`], it describes __vertical__ distribution. -/// * On a [`Row`], it describes __horizontal__ distribution. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum Justify { - /// Place items at the start of the main axis. - Start, - - /// Place items at the center of the main axis. - Center, - - /// Place items at the end of the main axis. - End, - - /// Place items with space between. - SpaceBetween, - - /// Place items with space around. - SpaceAround, - - /// Place items with evenly distributed space. - SpaceEvenly, -} diff --git a/core/src/lib.rs b/core/src/lib.rs index 877a8f85..ab43ab94 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -3,7 +3,6 @@ pub mod widget; mod align; mod background; mod color; -mod justify; mod length; mod point; mod rectangle; @@ -12,7 +11,6 @@ mod vector; pub use align::Align; pub use background::Background; pub use color::Color; -pub use justify::Justify; pub use length::Length; pub use point::Point; pub use rectangle::Rectangle; diff --git a/core/src/widget.rs b/core/src/widget.rs index 41c4c6a8..9e629e4f 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -9,6 +9,7 @@ //! ``` mod checkbox; mod column; +mod container; mod image; mod radio; mod row; @@ -32,6 +33,7 @@ pub use text_input::TextInput; pub use checkbox::Checkbox; pub use column::Column; +pub use container::Container; pub use image::Image; pub use radio::Radio; pub use row::Row; diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs index a57f2dd8..9cf20071 100644 --- a/core/src/widget/button.rs +++ b/core/src/widget/button.rs @@ -5,7 +5,7 @@ //! [`Button`]: struct.Button.html //! [`State`]: struct.State.html -use crate::{Align, Background, Length}; +use crate::{Background, Length}; /// A generic widget that produces a message when clicked. pub struct Button<'a, Message, Element> { @@ -24,8 +24,6 @@ pub struct Button<'a, Message, Element> { pub background: Option, pub border_radius: u16, - - pub align_self: Option, } impl<'a, Message, Element> std::fmt::Debug for Button<'a, Message, Element> @@ -57,7 +55,6 @@ impl<'a, Message, Element> Button<'a, Message, Element> { padding: 0, background: None, border_radius: 0, - align_self: None, } } @@ -84,17 +81,6 @@ impl<'a, Message, Element> Button<'a, Message, Element> { self } - /// Sets the alignment of the [`Button`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Button`]: struct.Button.html - pub fn align_self(mut self, align: Align) -> Self { - self.align_self = Some(align); - self - } - /// Sets the message that will be produced when the [`Button`] is pressed. /// /// [`Button`]: struct.Button.html diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs index f0c47f50..4a97ad98 100644 --- a/core/src/widget/column.rs +++ b/core/src/widget/column.rs @@ -1,4 +1,4 @@ -use crate::{Align, Justify, Length}; +use crate::{Align, Length}; use std::u32; @@ -14,9 +14,7 @@ pub struct Column { pub height: Length, pub max_width: u32, pub max_height: u32, - pub align_self: Option, pub align_items: Align, - pub justify_content: Justify, pub children: Vec, } @@ -32,9 +30,7 @@ impl Column { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_self: None, align_items: Align::Start, - justify_content: Justify::Start, children: Vec::new(), } } @@ -89,17 +85,6 @@ impl Column { self } - /// Sets the alignment of the [`Column`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Column`]: struct.Column.html - pub fn align_self(mut self, align: Align) -> Self { - self.align_self = Some(align); - self - } - /// Sets the horizontal alignment of the contents of the [`Column`] . /// /// [`Column`]: struct.Column.html @@ -108,15 +93,6 @@ impl Column { self } - /// Sets the vertical distribution strategy for the contents of the - /// [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.justify_content = justify; - self - } - /// Adds an element to the [`Column`]. /// /// [`Column`]: struct.Column.html diff --git a/core/src/widget/container.rs b/core/src/widget/container.rs new file mode 100644 index 00000000..dc1aab08 --- /dev/null +++ b/core/src/widget/container.rs @@ -0,0 +1,94 @@ +use crate::{Align, Length}; + +use std::u32; + +#[derive(Debug)] +pub struct Container { + pub width: Length, + pub height: Length, + pub max_width: u32, + pub max_height: u32, + pub padding: u16, + pub horizontal_alignment: Align, + pub vertical_alignment: Align, + pub content: Element, +} + +impl Container { + /// Creates an empty [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn new(content: T) -> Self + where + T: Into, + { + Container { + width: Length::Shrink, + height: Length::Shrink, + max_width: u32::MAX, + max_height: u32::MAX, + horizontal_alignment: Align::Start, + vertical_alignment: Align::Start, + padding: 0, + content: content.into(), + } + } + + /// Sets the width of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the maximum width of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn max_width(mut self, max_width: u32) -> Self { + self.max_width = max_width; + self + } + + /// Sets the maximum height of the [`Container`] in pixels. + /// + /// [`Container`]: struct.Container.html + pub fn max_height(mut self, max_height: u32) -> Self { + self.max_height = max_height; + self + } + + /// Sets the padding of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } + + /// Centers the contents in the horizontal axis of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn center_x(mut self) -> Self { + self.horizontal_alignment = Align::Center; + + self + } + + /// Centers the contents in the vertical axis of the [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn center_y(mut self) -> Self { + self.vertical_alignment = Align::Center; + + self + } +} diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs index 6e410dce..7e1b1085 100644 --- a/core/src/widget/image.rs +++ b/core/src/widget/image.rs @@ -24,8 +24,6 @@ pub struct Image { /// The height of the image pub height: Length, - - pub align_self: Option, } impl Image { @@ -38,7 +36,6 @@ impl Image { clip: None, width: Length::Shrink, height: Length::Shrink, - align_self: None, } } @@ -65,15 +62,4 @@ impl Image { self.height = height; self } - - /// Sets the alignment of the [`Image`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Image`]: struct.Image.html - pub fn align_self(mut self, align: Align) -> Self { - self.align_self = Some(align); - self - } } diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs index 10716a7a..3d882b47 100644 --- a/core/src/widget/row.rs +++ b/core/src/widget/row.rs @@ -1,4 +1,4 @@ -use crate::{Align, Justify, Length}; +use crate::{Align, Length}; use std::u32; @@ -14,9 +14,7 @@ pub struct Row { pub height: Length, pub max_width: u32, pub max_height: u32, - pub align_self: Option, pub align_items: Align, - pub justify_content: Justify, pub children: Vec, } @@ -32,9 +30,7 @@ impl Row { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_self: None, align_items: Align::Start, - justify_content: Justify::Start, children: Vec::new(), } } @@ -89,17 +85,6 @@ impl Row { self } - /// Sets the alignment of the [`Row`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Row`]: struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.align_self = Some(align); - self - } - /// Sets the vertical alignment of the contents of the [`Row`] . /// /// [`Row`]: struct.Row.html @@ -108,15 +93,6 @@ impl Row { self } - /// Sets the horizontal distribution strategy for the contents of the - /// [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.justify_content = justify; - self - } - /// Adds an [`Element`] to the [`Row`]. /// /// [`Element`]: ../struct.Element.html diff --git a/core/src/widget/scrollable.rs b/core/src/widget/scrollable.rs index c5a2fc59..7f2f0e99 100644 --- a/core/src/widget/scrollable.rs +++ b/core/src/widget/scrollable.rs @@ -7,7 +7,6 @@ pub struct Scrollable<'a, Element> { pub state: &'a mut State, pub height: Length, pub max_height: u32, - pub align_self: Option, pub content: Column, } @@ -17,7 +16,6 @@ impl<'a, Element> Scrollable<'a, Element> { state, height: Length::Shrink, max_height: u32::MAX, - align_self: None, content: Column::new(), } } @@ -72,17 +70,6 @@ impl<'a, Element> Scrollable<'a, Element> { self } - /// Sets the alignment of the [`Scrollable`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn align_self(mut self, align: Align) -> Self { - self.align_self = Some(align); - self - } - /// Sets the horizontal alignment of the contents of the [`Scrollable`] . /// /// [`Scrollable`]: struct.Scrollable.html @@ -142,9 +129,9 @@ impl State { pub fn offset(&self, bounds: Rectangle, content_bounds: Rectangle) -> u32 { let hidden_content = - (content_bounds.height - bounds.height).round() as u32; + (content_bounds.height - bounds.height).max(0.0).round() as u32; - self.offset.min(hidden_content).max(0) + self.offset.min(hidden_content) } pub fn is_scrollbar_grabbed(&self) -> bool { -- cgit From d4d14b68f47e9527554a728ebbba9b840832626a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 11 Nov 2019 05:37:51 +0100 Subject: Remove `padding` from `Container` for now --- core/src/widget/container.rs | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'core') diff --git a/core/src/widget/container.rs b/core/src/widget/container.rs index dc1aab08..9bc92fe0 100644 --- a/core/src/widget/container.rs +++ b/core/src/widget/container.rs @@ -8,7 +8,6 @@ pub struct Container { pub height: Length, pub max_width: u32, pub max_height: u32, - pub padding: u16, pub horizontal_alignment: Align, pub vertical_alignment: Align, pub content: Element, @@ -29,7 +28,6 @@ impl Container { max_height: u32::MAX, horizontal_alignment: Align::Start, vertical_alignment: Align::Start, - padding: 0, content: content.into(), } } @@ -66,14 +64,6 @@ impl Container { self } - /// Sets the padding of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn padding(mut self, units: u16) -> Self { - self.padding = units; - self - } - /// Centers the contents in the horizontal axis of the [`Container`]. /// /// [`Container`]: struct.Container.html -- cgit From 860a6923bbed57a21ce4b2cae331f6a3a51ca3fe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 11 Nov 2019 06:07:31 +0100 Subject: Split text measurements cache from rendering cache This speeds up layouting in the most common scenario considerably! :tada: --- core/src/widget/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs index 7e1b1085..996ab5e1 100644 --- a/core/src/widget/image.rs +++ b/core/src/widget/image.rs @@ -1,6 +1,6 @@ //! Display images in your user interface. -use crate::{Align, Length, Rectangle}; +use crate::{Length, Rectangle}; /// A frame that displays an image while keeping aspect ratio. /// -- cgit