diff options
author | 2019-11-14 06:46:50 +0100 | |
---|---|---|
committer | 2019-11-14 06:46:50 +0100 | |
commit | bc8d347736ec997ec0e0c401289e2bc09e212b8a (patch) | |
tree | b98798c09a3aa914b7d0869fba0cfd3efff7754f /core/src/widget | |
parent | 839e039dbf2fb89dcb8c141503740777d2af2eb3 (diff) | |
parent | 73f3c900071f950ea914652ca3f0002c1e173f61 (diff) | |
download | iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.gz iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.bz2 iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.zip |
Merge pull request #52 from hecrj/custom-layout-engine
Custom layout engine
Diffstat (limited to 'core/src/widget')
-rw-r--r-- | core/src/widget/button.rs | 16 | ||||
-rw-r--r-- | core/src/widget/column.rs | 40 | ||||
-rw-r--r-- | core/src/widget/container.rs | 84 | ||||
-rw-r--r-- | core/src/widget/image.rs | 16 | ||||
-rw-r--r-- | core/src/widget/row.rs | 40 | ||||
-rw-r--r-- | core/src/widget/scrollable.rs | 27 |
6 files changed, 112 insertions, 111 deletions
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<Background>, pub border_radius: u16, - - pub align_self: Option<Align>, } 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 2df327a0..4a97ad98 100644 --- a/core/src/widget/column.rs +++ b/core/src/widget/column.rs @@ -1,4 +1,6 @@ -use crate::{Align, Justify, Length}; +use crate::{Align, Length}; + +use std::u32; /// A container that distributes its contents vertically. /// @@ -10,11 +12,9 @@ pub struct Column<Element> { pub padding: u16, pub width: Length, pub height: Length, - pub max_width: Length, - pub max_height: Length, - pub align_self: Option<Align>, + pub max_width: u32, + pub max_height: u32, pub align_items: Align, - pub justify_content: Justify, pub children: Vec<Element>, } @@ -28,11 +28,9 @@ impl<Element> Column<Element> { padding: 0, width: Length::Fill, height: Length::Shrink, - max_width: Length::Shrink, - max_height: Length::Shrink, - align_self: None, + max_width: u32::MAX, + max_height: u32::MAX, align_items: Align::Start, - justify_content: Justify::Start, children: Vec::new(), } } @@ -74,7 +72,7 @@ impl<Element> Column<Element> { /// 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,22 +80,11 @@ impl<Element> Column<Element> { /// 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 } - /// 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 @@ -106,15 +93,6 @@ impl<Element> Column<Element> { 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..9bc92fe0 --- /dev/null +++ b/core/src/widget/container.rs @@ -0,0 +1,84 @@ +use crate::{Align, Length}; + +use std::u32; + +#[derive(Debug)] +pub struct Container<Element> { + pub width: Length, + pub height: Length, + pub max_width: u32, + pub max_height: u32, + pub horizontal_alignment: Align, + pub vertical_alignment: Align, + pub content: Element, +} + +impl<Element> Container<Element> { + /// Creates an empty [`Container`]. + /// + /// [`Container`]: struct.Container.html + pub fn new<T>(content: T) -> Self + where + T: Into<Element>, + { + Container { + width: Length::Shrink, + height: Length::Shrink, + max_width: u32::MAX, + max_height: u32::MAX, + horizontal_alignment: Align::Start, + vertical_alignment: Align::Start, + 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 + } + + /// 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..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. /// @@ -24,8 +24,6 @@ pub struct Image { /// The height of the image pub height: Length, - - pub align_self: Option<Align>, } 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 6bdb4ed2..3d882b47 100644 --- a/core/src/widget/row.rs +++ b/core/src/widget/row.rs @@ -1,4 +1,6 @@ -use crate::{Align, Justify, Length}; +use crate::{Align, Length}; + +use std::u32; /// A container that distributes its contents horizontally. /// @@ -10,11 +12,9 @@ pub struct Row<Element> { pub padding: u16, pub width: Length, pub height: Length, - pub max_width: Length, - pub max_height: Length, - pub align_self: Option<Align>, + pub max_width: u32, + pub max_height: u32, pub align_items: Align, - pub justify_content: Justify, pub children: Vec<Element>, } @@ -28,11 +28,9 @@ impl<Element> Row<Element> { padding: 0, width: Length::Fill, height: Length::Shrink, - max_width: Length::Shrink, - max_height: Length::Shrink, - align_self: None, + max_width: u32::MAX, + max_height: u32::MAX, align_items: Align::Start, - justify_content: Justify::Start, children: Vec::new(), } } @@ -74,7 +72,7 @@ impl<Element> Row<Element> { /// 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,22 +80,11 @@ impl<Element> Row<Element> { /// 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 } - /// 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 @@ -106,15 +93,6 @@ impl<Element> Row<Element> { 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 31a5abed..7f2f0e99 100644 --- a/core/src/widget/scrollable.rs +++ b/core/src/widget/scrollable.rs @@ -1,11 +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 align_self: Option<Align>, + pub max_height: u32, pub content: Column<Element>, } @@ -14,8 +15,7 @@ impl<'a, Element> Scrollable<'a, Element> { Scrollable { state, height: Length::Shrink, - max_height: Length::Shrink, - align_self: None, + max_height: u32::MAX, content: Column::new(), } } @@ -57,7 +57,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,22 +65,11 @@ 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 } - /// 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 @@ -140,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 { |