From bf74e6e7d47a012a9c592a9da044f8d3bf9efbce Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 19:16:46 +0100 Subject: Implement `Default` for `MouseCursor` --- native/src/mouse_cursor.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'native') diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs index a4740a27..c7297e0e 100644 --- a/native/src/mouse_cursor.rs +++ b/native/src/mouse_cursor.rs @@ -22,3 +22,9 @@ pub enum MouseCursor { /// The cursor is over a text widget. Text, } + +impl Default for MouseCursor { + fn default() -> MouseCursor { + MouseCursor::OutOfBounds + } +} -- cgit From 8426bf953cb50f3b7fcb1e0ec8c2fdf22d2b01af Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 19:20:59 +0100 Subject: Implement `Empty` widget It can be useful if you want to fill some space with nothing. --- native/src/widget.rs | 3 ++ native/src/widget/empty.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 native/src/widget/empty.rs (limited to 'native') diff --git a/native/src/widget.rs b/native/src/widget.rs index 26889280..b73f229e 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -24,6 +24,7 @@ pub mod button; pub mod checkbox; pub mod column; pub mod container; +pub mod empty; pub mod image; pub mod radio; pub mod row; @@ -42,6 +43,8 @@ pub use column::Column; #[doc(no_inline)] pub use container::Container; #[doc(no_inline)] +pub use empty::Empty; +#[doc(no_inline)] pub use image::Image; #[doc(no_inline)] pub use radio::Radio; diff --git a/native/src/widget/empty.rs b/native/src/widget/empty.rs new file mode 100644 index 00000000..f670048f --- /dev/null +++ b/native/src/widget/empty.rs @@ -0,0 +1,103 @@ +//! Distribute content vertically. +use std::hash::Hash; + +use crate::{ + layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, +}; + +/// An amount of empty space. +/// +/// It can be useful if you want to fill some space with nothing. +/// +/// [`Empty`]: struct.Empty.html +#[derive(Debug)] +pub struct Empty { + width: Length, + height: Length, +} + +impl Empty { + /// Creates an amount of [`Empty`] space. + /// + /// [`Empty`]: struct.Empty.html + pub fn new() -> Self { + Empty { + width: Length::Shrink, + height: Length::Shrink, + } + } + + /// Sets the width of the [`Empty`] space. + /// + /// [`Empty`]: struct..html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Empty`] space. + /// + /// [`Empty`]: struct.Column.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } +} + +impl<'a, Message, Renderer> Widget for Empty +where + Renderer: self::Renderer, +{ + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + + layout::Node::new(limits.resolve(Size::ZERO)) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> Renderer::Output { + renderer.draw(layout.bounds()) + } + + fn hash_layout(&self, state: &mut Hasher) { + std::any::TypeId::of::().hash(state); + self.width.hash(state); + self.height.hash(state); + } +} + +/// The renderer of an amount of [`Empty`] space. +/// +/// [`Empty`]: struct.Empty.html +pub trait Renderer: crate::Renderer { + /// Draws an amount of [`Empty`] space. + /// + /// You should most likely return an empty primitive here. + fn draw(&mut self, bounds: Rectangle) -> Self::Output; +} + +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(empty: Empty) -> Element<'a, Message, Renderer> { + Element::new(empty) + } +} -- cgit From aecc3ad1955b8433d1634ab1b975d2af20e772a1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 18:37:13 +0100 Subject: Add `Length::FillPortion` variant It allows to specify the amount of available space an element should take relative to other elements. --- native/src/layout/limits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native') diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index 2705a47d..a35f7ff7 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -52,7 +52,7 @@ impl Limits { Length::Shrink => { self.fill.width = self.min.width; } - Length::Fill => { + Length::Fill | Length::FillPortion(_) => { self.fill.width = self.fill.width.min(self.max.width); } Length::Units(units) => { @@ -76,7 +76,7 @@ impl Limits { Length::Shrink => { self.fill.height = self.min.height; } - Length::Fill => { + Length::Fill | Length::FillPortion(_) => { self.fill.height = self.fill.height.min(self.max.height); } Length::Units(units) => { -- cgit From 74d01a69577065ce4152fc80b297e8816ab3deff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 20:50:52 +0100 Subject: Subtract size of previous elements in flex layout --- native/src/layout/flex.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 0ed17d41..03b13e38 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -73,10 +73,12 @@ where Renderer: crate::Renderer, { let limits = limits.pad(padding); + let total_spacing = spacing * items.len().saturating_sub(1) as f32; + let max_cross = axis.cross(limits.max()); - let mut total_non_fill = spacing * items.len().saturating_sub(1) as f32; let mut fill_sum = 0; let mut cross = axis.cross(limits.min()); + let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); @@ -89,12 +91,15 @@ where .fill_factor(); if fill_factor == 0 { - let child_limits = Limits::new(Size::ZERO, limits.max()); + let (max_width, max_height) = axis.pack(available, max_cross); + + let child_limits = + Limits::new(Size::ZERO, Size::new(max_width, max_height)); let layout = child.layout(renderer, &child_limits); let size = layout.size(); - total_non_fill += axis.main(size); + available -= axis.main(size); cross = cross.max(axis.cross(size)); nodes[i] = layout; @@ -103,8 +108,7 @@ where } } - let available = axis.main(limits.max()); - let remaining = (available - total_non_fill).max(0.0); + let remaining = available.max(0.0); for (i, child) in items.iter().enumerate() { let fill_factor = match axis { -- cgit From 3a327e08e96d9588d145c32afe4f04f37a8f0f0f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 21:32:21 +0100 Subject: Rename `Empty` widget to `Space` --- native/src/widget.rs | 6 +-- native/src/widget/empty.rs | 103 -------------------------------------------- native/src/widget/space.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 106 deletions(-) delete mode 100644 native/src/widget/empty.rs create mode 100644 native/src/widget/space.rs (limited to 'native') diff --git a/native/src/widget.rs b/native/src/widget.rs index b73f229e..dcc99645 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -24,12 +24,12 @@ pub mod button; pub mod checkbox; pub mod column; pub mod container; -pub mod empty; pub mod image; pub mod radio; pub mod row; pub mod scrollable; pub mod slider; +pub mod space; pub mod svg; pub mod text; pub mod text_input; @@ -43,8 +43,6 @@ pub use column::Column; #[doc(no_inline)] pub use container::Container; #[doc(no_inline)] -pub use empty::Empty; -#[doc(no_inline)] pub use image::Image; #[doc(no_inline)] pub use radio::Radio; @@ -55,6 +53,8 @@ pub use scrollable::Scrollable; #[doc(no_inline)] pub use slider::Slider; #[doc(no_inline)] +pub use space::Space; +#[doc(no_inline)] pub use svg::Svg; #[doc(no_inline)] pub use text::Text; diff --git a/native/src/widget/empty.rs b/native/src/widget/empty.rs deleted file mode 100644 index f670048f..00000000 --- a/native/src/widget/empty.rs +++ /dev/null @@ -1,103 +0,0 @@ -//! Distribute content vertically. -use std::hash::Hash; - -use crate::{ - layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, -}; - -/// An amount of empty space. -/// -/// It can be useful if you want to fill some space with nothing. -/// -/// [`Empty`]: struct.Empty.html -#[derive(Debug)] -pub struct Empty { - width: Length, - height: Length, -} - -impl Empty { - /// Creates an amount of [`Empty`] space. - /// - /// [`Empty`]: struct.Empty.html - pub fn new() -> Self { - Empty { - width: Length::Shrink, - height: Length::Shrink, - } - } - - /// Sets the width of the [`Empty`] space. - /// - /// [`Empty`]: struct..html - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`Empty`] space. - /// - /// [`Empty`]: struct.Column.html - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } -} - -impl<'a, Message, Renderer> Widget for Empty -where - Renderer: self::Renderer, -{ - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - - layout::Node::new(limits.resolve(Size::ZERO)) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> Renderer::Output { - renderer.draw(layout.bounds()) - } - - fn hash_layout(&self, state: &mut Hasher) { - std::any::TypeId::of::().hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -/// The renderer of an amount of [`Empty`] space. -/// -/// [`Empty`]: struct.Empty.html -pub trait Renderer: crate::Renderer { - /// Draws an amount of [`Empty`] space. - /// - /// You should most likely return an empty primitive here. - fn draw(&mut self, bounds: Rectangle) -> Self::Output; -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static, -{ - fn from(empty: Empty) -> Element<'a, Message, Renderer> { - Element::new(empty) - } -} diff --git a/native/src/widget/space.rs b/native/src/widget/space.rs new file mode 100644 index 00000000..2029c52f --- /dev/null +++ b/native/src/widget/space.rs @@ -0,0 +1,104 @@ +//! Distribute content vertically. +use std::hash::Hash; + +use crate::{ + layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, +}; + +/// An amount of empty space. +/// +/// It can be useful if you want to fill some space with nothing. +#[derive(Debug)] +pub struct Space { + width: Length, + height: Length, +} + +impl Space { + /// Creates an amount of empty [`Space`] with the given width and height. + /// + /// [`Space`]: struct.Space.html + pub fn new(width: Length, height: Length) -> Self { + Space { width, height } + } + + /// Creates an amount of horizontal [`Space`]. + /// + /// [`Space`]: struct.Space.html + pub fn with_width(width: Length) -> Self { + Space { + width, + height: Length::Shrink, + } + } + + /// Creates an amount of vertical [`Space`]. + /// + /// [`Space`]: struct.Space.html + pub fn with_height(height: Length) -> Self { + Space { + width: Length::Shrink, + height, + } + } +} + +impl<'a, Message, Renderer> Widget for Space +where + Renderer: self::Renderer, +{ + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + + layout::Node::new(limits.resolve(Size::ZERO)) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> Renderer::Output { + renderer.draw(layout.bounds()) + } + + fn hash_layout(&self, state: &mut Hasher) { + std::any::TypeId::of::().hash(state); + self.width.hash(state); + self.height.hash(state); + } +} + +/// The renderer of an amount of [`Space`]. +/// +/// [`Space`]: struct.Space.html +pub trait Renderer: crate::Renderer { + /// Draws an amount of empty [`Space`]. + /// + /// You should most likely return an empty primitive here. + /// + /// [`Space`]: struct.Space.html + fn draw(&mut self, bounds: Rectangle) -> Self::Output; +} + +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(space: Space) -> Element<'a, Message, Renderer> { + Element::new(space) + } +} -- cgit