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/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 +++++++++++++-------- 8 files changed, 63 insertions(+), 51 deletions(-) (limited to 'native/src/widget') 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