Refactor alignment types into an alignment module

This commit is contained in:
Héctor Ramón Jiménez 2021-09-20 15:09:55 +07:00
parent 5fae6e59ff
commit a0ad399622
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
54 changed files with 402 additions and 377 deletions

View file

@ -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<Message, Renderer>(
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<Node> = 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),
);
}

View file

@ -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;
}
}

View file

@ -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};

View file

@ -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<Color>,
_horizontal_alignment: HorizontalAlignment,
_vertical_alignment: VerticalAlignment,
_horizontal_alignment: alignment::Horizontal,
_vertical_alignment: alignment::Vertical,
) {
}
}

View file

@ -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);

View file

@ -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<Element<'a, Message, Renderer>>,
}
@ -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
}

View file

@ -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,
);

View file

@ -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);

View file

@ -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<Element<'a, Message, Renderer>>,
}
@ -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
}

View file

@ -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
}

View file

@ -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<Renderer: self::Renderer> {
font: Renderer::Font,
width: Length,
height: Length,
horizontal_alignment: HorizontalAlignment,
vertical_alignment: VerticalAlignment,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
}
impl<Renderer: self::Renderer> Text<Renderer> {
@ -43,8 +44,8 @@ impl<Renderer: self::Renderer> Text<Renderer> {
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<Renderer: self::Renderer> Text<Renderer> {
/// 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<Color>,
horizontal_alignment: HorizontalAlignment,
vertical_alignment: VerticalAlignment,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
) -> Self::Output;
}

View file

@ -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<Message, Renderer: self::Renderer + text::Renderer> {
width: Length,
size: u16,
text_size: Option<u16>,
text_alignment: HorizontalAlignment,
text_alignment: alignment::Horizontal,
spacing: u16,
font: Renderer::Font,
style: Renderer::Style,
@ -62,7 +67,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
width: Length::Fill,
size: <Renderer as self::Renderer>::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<Message, Renderer: self::Renderer + text::Renderer>
}
/// 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,
))
}