From be51cac3d71d5eb49e266d0d2aae6ab945caf560 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 14 Sep 2021 09:10:37 -0700 Subject: Add Align::Fill variant --- native/src/layout/flex.rs | 62 +++++++++++++++++++++++++++++++++++++---------- native/src/layout/node.rs | 6 +++++ 2 files changed, 55 insertions(+), 13 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 3d3ff82c..17045e69 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -76,7 +76,11 @@ where let max_cross = axis.cross(limits.max()); let mut fill_sum = 0; - let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); + let mut cross = if align_items == Align::Fill { + axis.cross(limits.min()) + } else { + axis.cross(limits.min()).max(axis.cross(limits.fill())) + }; let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); @@ -89,17 +93,34 @@ where } .fill_factor(); - if fill_factor == 0 { - let (max_width, max_height) = axis.pack(available, max_cross); + let cross_fill_factor = match axis { + Axis::Horizontal => child.height(), + Axis::Vertical => child.width(), + } + .fill_factor(); - let child_limits = - Limits::new(Size::ZERO, Size::new(max_width, max_height)); + if align_items != Align::Fill && fill_factor != 0 { + fill_sum += fill_factor; - let layout = child.layout(renderer, &child_limits); - let size = layout.size(); + continue; + } - available -= axis.main(size); + 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(); + + if align_items != Align::Fill + || cross_fill_factor == 0 && align_items == Align::Fill + { cross = cross.max(axis.cross(size)); + } + + if fill_factor == 0 { + available -= axis.main(size); nodes[i] = layout; } else { @@ -124,11 +145,23 @@ where max_main }; - let (min_main, min_cross) = - axis.pack(min_main, axis.cross(limits.min())); + let (min_main, min_cross) = axis.pack( + min_main, + if align_items == Align::Fill { + cross + } else { + axis.cross(limits.min()) + }, + ); - let (max_main, max_cross) = - axis.pack(max_main, axis.cross(limits.max())); + let (max_main, max_cross) = axis.pack( + max_main, + if align_items == Align::Fill { + cross + } else { + axis.cross(limits.max()) + }, + ); let child_limits = Limits::new( Size::new(min_main, min_cross), @@ -136,7 +169,10 @@ where ); let layout = child.layout(renderer, &child_limits); - cross = cross.max(axis.cross(layout.size())); + + if align_items != Align::Fill { + cross = cross.max(axis.cross(layout.size())); + } nodes[i] = layout; } diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs index d7666f31..bee5e64e 100644 --- a/native/src/layout/node.rs +++ b/native/src/layout/node.rs @@ -56,6 +56,9 @@ impl Node { Align::End => { self.bounds.x += space.width - self.bounds.width; } + Align::Fill => { + self.bounds.width = space.width; + } } match vertical_alignment { @@ -66,6 +69,9 @@ impl Node { Align::End => { self.bounds.y += space.height - self.bounds.height; } + Align::Fill => { + self.bounds.height = space.height; + } } } -- cgit From e89bbe8a79537c650149154cb54038819e0efad7 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 14 Sep 2021 10:38:02 -0700 Subject: Calc fill cross and use for all children --- native/src/layout/flex.rs | 84 +++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 29 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 17045e69..2f75fee1 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -76,16 +76,38 @@ where let max_cross = axis.cross(limits.max()); let mut fill_sum = 0; - let mut cross = if align_items == Align::Fill { - axis.cross(limits.min()) - } else { - axis.cross(limits.min()).max(axis.cross(limits.fill())) - }; + let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); + if align_items == Align::Fill { + let mut fill_cross = axis.cross(limits.min()); + + items.iter().for_each(|child| { + let cross_fill_factor = match axis { + Axis::Horizontal => child.height(), + Axis::Vertical => child.width(), + } + .fill_factor(); + + if cross_fill_factor == 0 { + 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(); + + fill_cross = fill_cross.max(axis.cross(size)); + } + }); + + cross = fill_cross; + } + for (i, child) in items.iter().enumerate() { let fill_factor = match axis { Axis::Horizontal => child.width(), @@ -93,35 +115,39 @@ where } .fill_factor(); - let cross_fill_factor = match axis { - Axis::Horizontal => child.height(), - Axis::Vertical => child.width(), - } - .fill_factor(); - - if align_items != Align::Fill && fill_factor != 0 { - fill_sum += fill_factor; - - continue; - } - - let (max_width, max_height) = axis.pack(available, max_cross); + if fill_factor == 0 { + let (min_width, min_height) = axis.pack( + 0.0, + if align_items == Align::Fill { + cross + } else { + 0.0 + }, + ); - let child_limits = - Limits::new(Size::ZERO, Size::new(max_width, max_height)); + let (max_width, max_height) = axis.pack( + available, + if align_items == Align::Fill { + cross + } else { + max_cross + }, + ); - let layout = child.layout(renderer, &child_limits); - let size = layout.size(); + let child_limits = Limits::new( + Size::new(min_width, min_height), + Size::new(max_width, max_height), + ); - if align_items != Align::Fill - || cross_fill_factor == 0 && align_items == Align::Fill - { - cross = cross.max(axis.cross(size)); - } + let layout = child.layout(renderer, &child_limits); + let size = layout.size(); - if fill_factor == 0 { available -= axis.main(size); + if align_items != Align::Fill { + cross = cross.max(axis.cross(size)); + } + nodes[i] = layout; } else { fill_sum += fill_factor; @@ -159,7 +185,7 @@ where if align_items == Align::Fill { cross } else { - axis.cross(limits.max()) + max_cross }, ); -- cgit From 95e4791a1e4611f0db703ac2911f56b391469b5f Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 15 Sep 2021 11:18:11 -0700 Subject: Improve readability of Align::Fill branching --- native/src/layout/flex.rs | 56 +++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 2f75fee1..52b48fec 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -116,23 +116,17 @@ where .fill_factor(); if fill_factor == 0 { - let (min_width, min_height) = axis.pack( - 0.0, - if align_items == Align::Fill { - cross - } else { - 0.0 - }, - ); + let (min_width, min_height) = if align_items == Align::Fill { + axis.pack(0.0, cross) + } else { + axis.pack(0.0, 0.0) + }; - let (max_width, max_height) = axis.pack( - available, - if align_items == Align::Fill { - cross - } else { - max_cross - }, - ); + let (max_width, max_height) = if align_items == Align::Fill { + axis.pack(available, cross) + } else { + axis.pack(available, max_cross) + }; let child_limits = Limits::new( Size::new(min_width, min_height), @@ -171,27 +165,21 @@ where max_main }; - let (min_main, min_cross) = axis.pack( - min_main, - if align_items == Align::Fill { - cross - } else { - axis.cross(limits.min()) - }, - ); + let (min_width, min_height) = if align_items == Align::Fill { + axis.pack(min_main, cross) + } else { + axis.pack(min_main, axis.cross(limits.min())) + }; - let (max_main, max_cross) = axis.pack( - max_main, - if align_items == Align::Fill { - cross - } else { - max_cross - }, - ); + let (max_width, max_height) = if align_items == Align::Fill { + axis.pack(max_main, cross) + } else { + axis.pack(max_main, max_cross) + }; let child_limits = Limits::new( - Size::new(min_main, min_cross), - Size::new(max_main, max_cross), + Size::new(min_width, min_height), + Size::new(max_width, max_height), ); let layout = child.layout(renderer, &child_limits); -- cgit From 5fae6e59ffbc5913761df638dc7f0c35b7f43bc9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 20 Sep 2021 14:33:02 +0700 Subject: Introduce and use `CrossAlign` enum for `Column` and `Row` --- native/src/layout/flex.rs | 30 +++++++++++++++++++----------- native/src/layout/node.rs | 22 +++++++++++----------- native/src/lib.rs | 4 ++-- native/src/widget/checkbox.rs | 4 ++-- native/src/widget/column.rs | 8 ++++---- native/src/widget/container.rs | 10 +++++++--- native/src/widget/radio.rs | 6 +++--- native/src/widget/row.rs | 8 ++++---- native/src/widget/scrollable.rs | 6 +++--- native/src/widget/toggler.rs | 6 +++--- 10 files changed, 58 insertions(+), 46 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 52b48fec..dfb5288b 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -19,7 +19,7 @@ use crate::{ layout::{Limits, Node}, - Align, Element, Padding, Point, Size, + CrossAlign, Element, Padding, Point, Size, }; /// The main axis of a flex layout. @@ -65,7 +65,7 @@ pub fn resolve( limits: &Limits, padding: Padding, spacing: f32, - align_items: Align, + align_items: CrossAlign, items: &[Element<'_, Message, Renderer>], ) -> Node where @@ -82,7 +82,7 @@ where let mut nodes: Vec = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); - if align_items == Align::Fill { + if align_items == CrossAlign::Fill { let mut fill_cross = axis.cross(limits.min()); items.iter().for_each(|child| { @@ -116,13 +116,13 @@ where .fill_factor(); if fill_factor == 0 { - let (min_width, min_height) = if align_items == Align::Fill { + let (min_width, min_height) = if align_items == CrossAlign::Fill { axis.pack(0.0, cross) } else { axis.pack(0.0, 0.0) }; - let (max_width, max_height) = if align_items == Align::Fill { + let (max_width, max_height) = if align_items == CrossAlign::Fill { axis.pack(available, cross) } else { axis.pack(available, max_cross) @@ -138,7 +138,7 @@ where available -= axis.main(size); - if align_items != Align::Fill { + if align_items != CrossAlign::Fill { cross = cross.max(axis.cross(size)); } @@ -165,13 +165,13 @@ where max_main }; - let (min_width, min_height) = if align_items == Align::Fill { + let (min_width, min_height) = if align_items == CrossAlign::Fill { axis.pack(min_main, cross) } else { axis.pack(min_main, axis.cross(limits.min())) }; - let (max_width, max_height) = if align_items == Align::Fill { + let (max_width, max_height) = if align_items == CrossAlign::Fill { axis.pack(max_main, cross) } else { axis.pack(max_main, max_cross) @@ -184,7 +184,7 @@ where let layout = child.layout(renderer, &child_limits); - if align_items != Align::Fill { + if align_items != CrossAlign::Fill { cross = cross.max(axis.cross(layout.size())); } @@ -206,10 +206,18 @@ where match axis { Axis::Horizontal => { - node.align(Align::Start, align_items, Size::new(0.0, cross)); + node.align( + CrossAlign::Start, + align_items, + Size::new(0.0, cross), + ); } Axis::Vertical => { - node.align(align_items, Align::Start, Size::new(cross, 0.0)); + node.align( + align_items, + CrossAlign::Start, + Size::new(cross, 0.0), + ); } } diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs index bee5e64e..2239a654 100644 --- a/native/src/layout/node.rs +++ b/native/src/layout/node.rs @@ -1,4 +1,4 @@ -use crate::{Align, Point, Rectangle, Size}; +use crate::{CrossAlign, Point, Rectangle, Size}; /// The bounds of an element and its children. #[derive(Debug, Clone, Default)] @@ -44,32 +44,32 @@ impl Node { /// Aligns the [`Node`] in the given space. pub fn align( &mut self, - horizontal_alignment: Align, - vertical_alignment: Align, + horizontal_alignment: CrossAlign, + vertical_alignment: CrossAlign, space: Size, ) { match horizontal_alignment { - Align::Start => {} - Align::Center => { + CrossAlign::Start => {} + CrossAlign::Center => { self.bounds.x += (space.width - self.bounds.width) / 2.0; } - Align::End => { + CrossAlign::End => { self.bounds.x += space.width - self.bounds.width; } - Align::Fill => { + CrossAlign::Fill => { self.bounds.width = space.width; } } match vertical_alignment { - Align::Start => {} - Align::Center => { + CrossAlign::Start => {} + CrossAlign::Center => { self.bounds.y += (space.height - self.bounds.height) / 2.0; } - Align::End => { + CrossAlign::End => { self.bounds.y += space.height - self.bounds.height; } - Align::Fill => { + CrossAlign::Fill => { self.bounds.height = space.height; } } diff --git a/native/src/lib.rs b/native/src/lib.rs index cb0600e2..cf8b0da5 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -62,8 +62,8 @@ mod debug; mod debug; pub use iced_core::{ - Align, Background, Color, Font, HorizontalAlignment, Length, Padding, - Point, Rectangle, Size, Vector, VerticalAlignment, + Align, Background, Color, CrossAlign, Font, HorizontalAlignment, Length, + Padding, Point, Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures}; diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 0f21c873..e74515c6 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -8,7 +8,7 @@ use crate::row; use crate::text; use crate::touch; use crate::{ - Align, Clipboard, Color, Element, Hasher, HorizontalAlignment, Layout, + Clipboard, Color, CrossAlign, Element, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -138,7 +138,7 @@ where Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push( Row::new() .width(Length::Units(self.size)) diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 52a2e80c..83e5760a 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -5,7 +5,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Align, Clipboard, Element, Hasher, Layout, Length, Padding, Point, + Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, }; @@ -20,7 +20,7 @@ pub struct Column<'a, Message, Renderer> { height: Length, max_width: u32, max_height: u32, - align_items: Align, + align_items: CrossAlign, children: Vec>, } @@ -41,7 +41,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: Align::Start, + align_items: CrossAlign::Start, children, } } @@ -87,7 +87,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } /// Sets the horizontal alignment of the contents of the [`Column`] . - pub fn align_items(mut self, align: Align) -> Self { + pub fn align_items(mut self, align: CrossAlign) -> Self { self.align_items = align; self } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 69aee64d..ae18db25 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,8 +5,8 @@ use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Align, Clipboard, Element, Hasher, Layout, Length, Padding, Point, - Rectangle, Widget, + Align, Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, + Point, Rectangle, Widget, }; use std::u32; @@ -143,7 +143,11 @@ where self.padding.left.into(), self.padding.top.into(), )); - content.align(self.horizontal_alignment, self.vertical_alignment, size); + content.align( + CrossAlign::from(self.horizontal_alignment), + CrossAlign::from(self.vertical_alignment), + size, + ); layout::Node::with_children(size.pad(self.padding), vec![content]) } diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index dee82d1f..1ab51051 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -8,8 +8,8 @@ use crate::text; use crate::touch; use crate::{layout, Color}; use crate::{ - Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length, - Point, Rectangle, Row, Text, VerticalAlignment, Widget, + Clipboard, CrossAlign, Element, Hasher, HorizontalAlignment, Layout, + Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; /// A circular button representing a choice. @@ -153,7 +153,7 @@ where Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push( Row::new() .width(Length::Units(self.size)) diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 9ebc9145..e200e697 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -3,7 +3,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Align, Clipboard, Element, Hasher, Layout, Length, Padding, Point, + Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, }; @@ -19,7 +19,7 @@ pub struct Row<'a, Message, Renderer> { height: Length, max_width: u32, max_height: u32, - align_items: Align, + align_items: CrossAlign, children: Vec>, } @@ -40,7 +40,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: Align::Start, + align_items: CrossAlign::Start, children, } } @@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } /// Sets the vertical alignment of the contents of the [`Row`] . - pub fn align_items(mut self, align: Align) -> Self { + pub fn align_items(mut self, align: CrossAlign) -> Self { self.align_items = align; self } diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 68da2e67..0a2155cc 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -6,8 +6,8 @@ use crate::mouse; use crate::overlay; use crate::touch; use crate::{ - Align, Clipboard, Column, Element, Hasher, Layout, Length, Padding, Point, - Rectangle, Size, Vector, Widget, + Clipboard, Column, CrossAlign, Element, Hasher, Layout, Length, Padding, + Point, Rectangle, Size, Vector, Widget, }; use std::{f32, hash::Hash, u32}; @@ -84,7 +84,7 @@ impl<'a, Message, Renderer: self::Renderer> Scrollable<'a, Message, Renderer> { } /// Sets the horizontal alignment of the contents of the [`Scrollable`] . - pub fn align_items(mut self, align_items: Align) -> Self { + pub fn align_items(mut self, align_items: CrossAlign) -> Self { self.content = self.content.align_items(align_items); self } diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index 4035276c..34dc52a0 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -2,8 +2,8 @@ use std::hash::Hash; use crate::{ - event, layout, mouse, row, text, Align, Clipboard, Element, Event, Hasher, - HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, + event, layout, mouse, row, text, Clipboard, CrossAlign, Element, Event, + Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -132,7 +132,7 @@ where let mut row = Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(Align::Center); + .align_items(CrossAlign::Center); if let Some(label) = &self.label { row = row.push( -- cgit From a0ad3996225601aaa1ebe051cba115374b55c80e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 20 Sep 2021 15:09:55 +0700 Subject: Refactor alignment types into an `alignment` module --- native/src/layout/flex.rs | 27 ++++++++++++--------------- native/src/layout/node.rs | 22 +++++++++++----------- native/src/lib.rs | 5 +++-- native/src/renderer/null.rs | 23 +++++++++++++++++------ native/src/widget/checkbox.rs | 11 ++++++----- native/src/widget/column.rs | 8 ++++---- native/src/widget/container.rs | 25 +++++++++++++------------ native/src/widget/radio.rs | 13 +++++++------ native/src/widget/row.rs | 8 ++++---- native/src/widget/scrollable.rs | 4 ++-- native/src/widget/text.rs | 24 ++++++++++++++---------- native/src/widget/toggler.rs | 21 +++++++++++++-------- 12 files changed, 106 insertions(+), 85 deletions(-) (limited to 'native') diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index dfb5288b..5fbcbca0 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -16,11 +16,8 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - -use crate::{ - layout::{Limits, Node}, - CrossAlign, Element, Padding, Point, Size, -}; +use crate::layout::{Limits, Node}; +use crate::{Alignment, Element, Padding, Point, Size}; /// The main axis of a flex layout. #[derive(Debug)] @@ -65,7 +62,7 @@ pub fn resolve( limits: &Limits, padding: Padding, spacing: f32, - align_items: CrossAlign, + align_items: Alignment, items: &[Element<'_, Message, Renderer>], ) -> Node where @@ -82,7 +79,7 @@ where let mut nodes: Vec = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); - if align_items == CrossAlign::Fill { + if align_items == Alignment::Fill { let mut fill_cross = axis.cross(limits.min()); items.iter().for_each(|child| { @@ -116,13 +113,13 @@ where .fill_factor(); if fill_factor == 0 { - let (min_width, min_height) = if align_items == CrossAlign::Fill { + let (min_width, min_height) = if align_items == Alignment::Fill { axis.pack(0.0, cross) } else { axis.pack(0.0, 0.0) }; - let (max_width, max_height) = if align_items == CrossAlign::Fill { + let (max_width, max_height) = if align_items == Alignment::Fill { axis.pack(available, cross) } else { axis.pack(available, max_cross) @@ -138,7 +135,7 @@ where available -= axis.main(size); - if align_items != CrossAlign::Fill { + if align_items != Alignment::Fill { cross = cross.max(axis.cross(size)); } @@ -165,13 +162,13 @@ where max_main }; - let (min_width, min_height) = if align_items == CrossAlign::Fill { + let (min_width, min_height) = if align_items == Alignment::Fill { axis.pack(min_main, cross) } else { axis.pack(min_main, axis.cross(limits.min())) }; - let (max_width, max_height) = if align_items == CrossAlign::Fill { + let (max_width, max_height) = if align_items == Alignment::Fill { axis.pack(max_main, cross) } else { axis.pack(max_main, max_cross) @@ -184,7 +181,7 @@ where let layout = child.layout(renderer, &child_limits); - if align_items != CrossAlign::Fill { + if align_items != Alignment::Fill { cross = cross.max(axis.cross(layout.size())); } @@ -207,7 +204,7 @@ where match axis { Axis::Horizontal => { node.align( - CrossAlign::Start, + Alignment::Start, align_items, Size::new(0.0, cross), ); @@ -215,7 +212,7 @@ where Axis::Vertical => { node.align( align_items, - CrossAlign::Start, + Alignment::Start, Size::new(cross, 0.0), ); } diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs index 2239a654..e9e6058e 100644 --- a/native/src/layout/node.rs +++ b/native/src/layout/node.rs @@ -1,4 +1,4 @@ -use crate::{CrossAlign, Point, Rectangle, Size}; +use crate::{Alignment, Point, Rectangle, Size}; /// The bounds of an element and its children. #[derive(Debug, Clone, Default)] @@ -44,32 +44,32 @@ impl Node { /// Aligns the [`Node`] in the given space. pub fn align( &mut self, - horizontal_alignment: CrossAlign, - vertical_alignment: CrossAlign, + horizontal_alignment: Alignment, + vertical_alignment: Alignment, space: Size, ) { match horizontal_alignment { - CrossAlign::Start => {} - CrossAlign::Center => { + Alignment::Start => {} + Alignment::Center => { self.bounds.x += (space.width - self.bounds.width) / 2.0; } - CrossAlign::End => { + Alignment::End => { self.bounds.x += space.width - self.bounds.width; } - CrossAlign::Fill => { + Alignment::Fill => { self.bounds.width = space.width; } } match vertical_alignment { - CrossAlign::Start => {} - CrossAlign::Center => { + Alignment::Start => {} + Alignment::Center => { self.bounds.y += (space.height - self.bounds.height) / 2.0; } - CrossAlign::End => { + Alignment::End => { self.bounds.y += space.height - self.bounds.height; } - CrossAlign::Fill => { + Alignment::Fill => { self.bounds.height = space.height; } } diff --git a/native/src/lib.rs b/native/src/lib.rs index cf8b0da5..170a588b 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -61,9 +61,10 @@ mod debug; #[path = "debug/null.rs"] mod debug; +pub use iced_core::alignment; pub use iced_core::{ - Align, Background, Color, CrossAlign, Font, HorizontalAlignment, Length, - Padding, Point, Rectangle, Size, Vector, VerticalAlignment, + Alignment, Background, Color, Font, Length, Padding, Point, Rectangle, + Size, Vector, }; pub use iced_futures::{executor, futures}; diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 2c47ddf2..b5921582 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -1,8 +1,19 @@ +use crate::alignment; +use crate::button; +use crate::checkbox; +use crate::column; +use crate::container; +use crate::pane_grid; +use crate::progress_bar; +use crate::radio; +use crate::row; +use crate::scrollable; +use crate::slider; +use crate::text; +use crate::text_input; +use crate::toggler; use crate::{ - button, checkbox, column, container, pane_grid, progress_bar, radio, row, - scrollable, slider, text, text_input, toggler, Color, Element, Font, - HorizontalAlignment, Layout, Padding, Point, Rectangle, Renderer, Size, - VerticalAlignment, + Color, Element, Font, Layout, Padding, Point, Rectangle, Renderer, Size, }; /// A renderer that does nothing. @@ -87,8 +98,8 @@ impl text::Renderer for Null { _size: u16, _font: Font, _color: Option, - _horizontal_alignment: HorizontalAlignment, - _vertical_alignment: VerticalAlignment, + _horizontal_alignment: alignment::Horizontal, + _vertical_alignment: alignment::Vertical, ) { } } diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index e74515c6..8bdb6b78 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -1,6 +1,7 @@ //! Show toggle controls using checkboxes. use std::hash::Hash; +use crate::alignment::{self, Alignment}; use crate::event::{self, Event}; use crate::layout; use crate::mouse; @@ -8,8 +9,8 @@ use crate::row; use crate::text; use crate::touch; use crate::{ - Clipboard, Color, CrossAlign, Element, Hasher, HorizontalAlignment, Layout, - Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, + Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Row, + Text, Widget, }; /// A box that can be checked. @@ -138,7 +139,7 @@ where Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push( Row::new() .width(Length::Units(self.size)) @@ -202,8 +203,8 @@ where self.text_size.unwrap_or(renderer.default_size()), self.font, self.text_color, - HorizontalAlignment::Left, - VerticalAlignment::Center, + alignment::Horizontal::Left, + alignment::Vertical::Center, ); let is_mouse_over = bounds.contains(cursor_position); diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 83e5760a..30cf0781 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -5,7 +5,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, Point, + Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, }; @@ -20,7 +20,7 @@ pub struct Column<'a, Message, Renderer> { height: Length, max_width: u32, max_height: u32, - align_items: CrossAlign, + align_items: Alignment, children: Vec>, } @@ -41,7 +41,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: CrossAlign::Start, + align_items: Alignment::Start, children, } } @@ -87,7 +87,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } /// Sets the horizontal alignment of the contents of the [`Column`] . - pub fn align_items(mut self, align: CrossAlign) -> Self { + pub fn align_items(mut self, align: Alignment) -> Self { self.align_items = align; self } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index ae18db25..0e86ab62 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -1,12 +1,13 @@ //! Decorate content and apply alignment. use std::hash::Hash; +use crate::alignment::{self, Alignment}; use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Align, Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, - Point, Rectangle, Widget, + Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, + Widget, }; use std::u32; @@ -21,8 +22,8 @@ pub struct Container<'a, Message, Renderer: self::Renderer> { height: Length, max_width: u32, max_height: u32, - horizontal_alignment: Align, - vertical_alignment: Align, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, style: Renderer::Style, content: Element<'a, Message, Renderer>, } @@ -42,8 +43,8 @@ where height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - horizontal_alignment: Align::Start, - vertical_alignment: Align::Start, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, style: Renderer::Style::default(), content: content.into(), } @@ -80,26 +81,26 @@ where } /// Sets the content alignment for the horizontal axis of the [`Container`]. - pub fn align_x(mut self, alignment: Align) -> Self { + pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self { self.horizontal_alignment = alignment; self } /// Sets the content alignment for the vertical axis of the [`Container`]. - pub fn align_y(mut self, alignment: Align) -> Self { + pub fn align_y(mut self, alignment: alignment::Vertical) -> Self { self.vertical_alignment = alignment; self } /// Centers the contents in the horizontal axis of the [`Container`]. pub fn center_x(mut self) -> Self { - self.horizontal_alignment = Align::Center; + self.horizontal_alignment = alignment::Horizontal::Center; self } /// Centers the contents in the vertical axis of the [`Container`]. pub fn center_y(mut self) -> Self { - self.vertical_alignment = Align::Center; + self.vertical_alignment = alignment::Vertical::Center; self } @@ -144,8 +145,8 @@ where self.padding.top.into(), )); content.align( - CrossAlign::from(self.horizontal_alignment), - CrossAlign::from(self.vertical_alignment), + Alignment::from(self.horizontal_alignment), + Alignment::from(self.vertical_alignment), size, ); diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 1ab51051..513b2fce 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,15 +1,16 @@ //! Create choices using radio buttons. use std::hash::Hash; +use crate::alignment::{self, Alignment}; use crate::event::{self, Event}; +use crate::layout; use crate::mouse; use crate::row; use crate::text; use crate::touch; -use crate::{layout, Color}; use crate::{ - Clipboard, CrossAlign, Element, Hasher, HorizontalAlignment, Layout, - Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, + Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Row, + Text, Widget, }; /// A circular button representing a choice. @@ -153,7 +154,7 @@ where Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push( Row::new() .width(Length::Units(self.size)) @@ -214,8 +215,8 @@ where self.text_size.unwrap_or(renderer.default_size()), self.font, self.text_color, - HorizontalAlignment::Left, - VerticalAlignment::Center, + alignment::Horizontal::Left, + alignment::Vertical::Center, ); let is_mouse_over = bounds.contains(cursor_position); diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index e200e697..1923f213 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -3,7 +3,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::overlay; use crate::{ - Clipboard, CrossAlign, Element, Hasher, Layout, Length, Padding, Point, + Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, }; @@ -19,7 +19,7 @@ pub struct Row<'a, Message, Renderer> { height: Length, max_width: u32, max_height: u32, - align_items: CrossAlign, + align_items: Alignment, children: Vec>, } @@ -40,7 +40,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: CrossAlign::Start, + align_items: Alignment::Start, children, } } @@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } /// Sets the vertical alignment of the contents of the [`Row`] . - pub fn align_items(mut self, align: CrossAlign) -> Self { + pub fn align_items(mut self, align: Alignment) -> Self { self.align_items = align; self } diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 0a2155cc..a8e467d3 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -6,7 +6,7 @@ use crate::mouse; use crate::overlay; use crate::touch; use crate::{ - Clipboard, Column, CrossAlign, Element, Hasher, Layout, Length, Padding, + Alignment, Clipboard, Column, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Size, Vector, Widget, }; @@ -84,7 +84,7 @@ impl<'a, Message, Renderer: self::Renderer> Scrollable<'a, Message, Renderer> { } /// Sets the horizontal alignment of the contents of the [`Scrollable`] . - pub fn align_items(mut self, align_items: CrossAlign) -> Self { + pub fn align_items(mut self, align_items: Alignment) -> Self { self.content = self.content.align_items(align_items); self } diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index d8bc0a00..168d49c2 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -1,7 +1,8 @@ //! Write some text for your users to read. +use crate::alignment; +use crate::layout; use crate::{ - layout, Color, Element, Hasher, HorizontalAlignment, Layout, Length, Point, - Rectangle, Size, VerticalAlignment, Widget, + Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; pub use iced_core::text::Hit; @@ -29,8 +30,8 @@ pub struct Text { font: Renderer::Font, width: Length, height: Length, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, } impl Text { @@ -43,8 +44,8 @@ impl Text { font: Default::default(), width: Length::Shrink, height: Length::Shrink, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, } } @@ -83,14 +84,17 @@ impl Text { /// Sets the [`HorizontalAlignment`] of the [`Text`]. pub fn horizontal_alignment( mut self, - alignment: HorizontalAlignment, + alignment: alignment::Horizontal, ) -> Self { self.horizontal_alignment = alignment; self } /// Sets the [`VerticalAlignment`] of the [`Text`]. - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { + pub fn vertical_alignment( + mut self, + alignment: alignment::Vertical, + ) -> Self { self.vertical_alignment = alignment; self } @@ -215,8 +219,8 @@ pub trait Renderer: crate::Renderer { size: u16, font: Self::Font, color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, ) -> Self::Output; } diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index 34dc52a0..c624be4c 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -1,10 +1,15 @@ //! Show toggle controls using togglers. use std::hash::Hash; +use crate::alignment; +use crate::event; +use crate::layout; +use crate::mouse; +use crate::row; +use crate::text; use crate::{ - event, layout, mouse, row, text, Clipboard, CrossAlign, Element, Event, - Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, - VerticalAlignment, Widget, + Alignment, Clipboard, Element, Event, Hasher, Layout, Length, Point, + Rectangle, Row, Text, Widget, }; /// A toggler widget @@ -30,7 +35,7 @@ pub struct Toggler { width: Length, size: u16, text_size: Option, - text_alignment: HorizontalAlignment, + text_alignment: alignment::Horizontal, spacing: u16, font: Renderer::Font, style: Renderer::Style, @@ -62,7 +67,7 @@ impl width: Length::Fill, size: ::DEFAULT_SIZE, text_size: None, - text_alignment: HorizontalAlignment::Left, + text_alignment: alignment::Horizontal::Left, spacing: 0, font: Renderer::Font::default(), style: Renderer::Style::default(), @@ -88,7 +93,7 @@ impl } /// Sets the horizontal alignment of the text of the [`Toggler`] - pub fn text_alignment(mut self, alignment: HorizontalAlignment) -> Self { + pub fn text_alignment(mut self, alignment: alignment::Horizontal) -> Self { self.text_alignment = alignment; self } @@ -132,7 +137,7 @@ where let mut row = Row::<(), Renderer>::new() .width(self.width) .spacing(self.spacing) - .align_items(CrossAlign::Center); + .align_items(Alignment::Center); if let Some(label) = &self.label { row = row.push( @@ -202,7 +207,7 @@ where self.font, None, self.text_alignment, - VerticalAlignment::Center, + alignment::Vertical::Center, )) } -- cgit