From be51cac3d71d5eb49e266d0d2aae6ab945caf560 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 14 Sep 2021 09:10:37 -0700 Subject: [PATCH 1/5] Add Align::Fill variant --- core/src/align.rs | 3 ++ native/src/layout/flex.rs | 62 +++++++++++++++++++++++++++++++-------- native/src/layout/node.rs | 6 ++++ web/src/css.rs | 1 + 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/core/src/align.rs b/core/src/align.rs index 8a59afa1c6..aa8838c65b 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -9,6 +9,9 @@ pub enum Align { /// Align at the end of the axis. End, + + /// Fill the entire axis. + Fill, } /// The horizontal alignment of some resource. diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 3d3ff82cdf..17045e69af 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 d7666f3172..bee5e64eb3 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; + } } } diff --git a/web/src/css.rs b/web/src/css.rs index 21f51f8552..23b21e2249 100644 --- a/web/src/css.rs +++ b/web/src/css.rs @@ -201,6 +201,7 @@ pub fn align(align: Align) -> &'static str { Align::Start => "flex-start", Align::Center => "center", Align::End => "flex-end", + Align::Fill => "stretch", } } From e89bbe8a79537c650149154cb54038819e0efad7 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 14 Sep 2021 10:38:02 -0700 Subject: [PATCH 2/5] Calc fill cross and use for all children --- native/src/layout/flex.rs | 84 +++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 17045e69af..2f75fee14c 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 }, ); From 95e4791a1e4611f0db703ac2911f56b391469b5f Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 15 Sep 2021 11:18:11 -0700 Subject: [PATCH 3/5] Improve readability of Align::Fill branching --- native/src/layout/flex.rs | 56 +++++++++++++++------------------------ 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 2f75fee14c..52b48fecb8 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); From 5fae6e59ffbc5913761df638dc7f0c35b7f43bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 20 Sep 2021 14:33:02 +0700 Subject: [PATCH 4/5] Introduce and use `CrossAlign` enum for `Column` and `Row` --- core/src/align.rs | 23 ++++++++++++++++ core/src/lib.rs | 2 +- examples/bezier_tool/src/main.rs | 5 ++-- examples/color_palette/src/main.rs | 8 +++--- examples/counter/src/main.rs | 6 +++-- examples/custom_widget/src/main.rs | 4 +-- examples/download_progress/src/main.rs | 12 ++++----- examples/events/src/main.rs | 8 +++--- examples/game_of_life/src/main.rs | 8 +++--- examples/geometry/src/main.rs | 6 ++--- examples/integration_opengl/src/controls.rs | 6 ++--- examples/integration_wgpu/src/controls.rs | 6 ++--- examples/pane_grid/src/main.rs | 9 ++++--- examples/pick_list/src/main.rs | 4 +-- examples/pokedex/src/main.rs | 12 ++++----- examples/qr_code/src/main.rs | 4 +-- examples/stopwatch/src/main.rs | 6 ++--- examples/styling/src/main.rs | 8 +++--- examples/todos/src/main.rs | 10 +++---- examples/tooltip/src/main.rs | 7 ++--- 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 ++--- src/lib.rs | 5 ++-- web/src/css.rs | 13 +++++++-- web/src/lib.rs | 5 ++-- 33 files changed, 166 insertions(+), 115 deletions(-) diff --git a/core/src/align.rs b/core/src/align.rs index aa8838c65b..ad0d8a2596 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -9,11 +9,34 @@ pub enum Align { /// Align at the end of the axis. End, +} + +/// Alignment on the cross axis of a container. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum CrossAlign { + /// Align at the start of the axis. + Start, + + /// Align at the center of the axis. + Center, + + /// Align at the end of the axis. + End, /// Fill the entire axis. Fill, } +impl From for CrossAlign { + fn from(align: Align) -> Self { + match align { + Align::Start => Self::Start, + Align::Center => Self::Center, + Align::End => Self::End, + } + } +} + /// The horizontal alignment of some resource. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum HorizontalAlignment { diff --git a/core/src/lib.rs b/core/src/lib.rs index e937264dbc..b0aa4e1252 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -29,7 +29,7 @@ mod rectangle; mod size; mod vector; -pub use align::{Align, HorizontalAlignment, VerticalAlignment}; +pub use align::{Align, CrossAlign, HorizontalAlignment, VerticalAlignment}; pub use background::Background; pub use color::Color; pub use font::Font; diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 97832e0157..b17f5726aa 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -1,6 +1,7 @@ //! This example showcases an interactive `Canvas` for drawing Bézier curves. use iced::{ - button, Align, Button, Column, Element, Length, Sandbox, Settings, Text, + button, Button, Column, CrossAlign, Element, Length, Sandbox, Settings, + Text, }; pub fn main() -> iced::Result { @@ -51,7 +52,7 @@ impl Sandbox for Example { Column::new() .padding(20) .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push( Text::new("Bezier tool example") .width(Length::Shrink) diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index bb2c61cb4e..90277186a4 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -1,8 +1,8 @@ use iced::canvas::{self, Cursor, Frame, Geometry, Path}; use iced::{ - slider, Align, Canvas, Color, Column, Element, HorizontalAlignment, Length, - Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, Vector, - VerticalAlignment, + slider, Canvas, Color, Column, CrossAlign, Element, HorizontalAlignment, + Length, Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, + Vector, VerticalAlignment, }; use palette::{self, Hsl, Limited, Srgb}; use std::marker::PhantomData; @@ -298,7 +298,7 @@ impl ColorPicker { Row::new() .spacing(10) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(Text::new(C::LABEL).width(Length::Units(50))) .push(slider(s1, cr1, c1, move |v| C::new(v, c2, c3))) .push(slider(s2, cr2, c2, move |v| C::new(c1, v, c3))) diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index e0b2ebd6e5..710849ea1e 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -1,4 +1,6 @@ -use iced::{button, Align, Button, Column, Element, Sandbox, Settings, Text}; +use iced::{ + button, Button, Column, CrossAlign, Element, Sandbox, Settings, Text, +}; pub fn main() -> iced::Result { Counter::run(Settings::default()) @@ -42,7 +44,7 @@ impl Sandbox for Counter { fn view(&mut self) -> Element { Column::new() .padding(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push( Button::new(&mut self.increment_button, Text::new("Increment")) .on_press(Message::IncrementPressed), diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index 36f468c74e..ce7fe9af0e 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -84,7 +84,7 @@ mod circle { use circle::Circle; use iced::{ - slider, Align, Column, Container, Element, Length, Sandbox, Settings, + slider, Column, Container, CrossAlign, Element, Length, Sandbox, Settings, Slider, Text, }; @@ -129,7 +129,7 @@ impl Sandbox for Example { .padding(20) .spacing(20) .max_width(500) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(Circle::new(self.radius)) .push(Text::new(format!("Radius: {:.2}", self.radius))) .push( diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index cd024926bb..a6dd54ba70 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, executor, Align, Application, Button, Column, Command, Container, - Element, Length, ProgressBar, Settings, Subscription, Text, + button, executor, Application, Button, Column, Command, Container, + CrossAlign, Element, Length, ProgressBar, Settings, Subscription, Text, }; mod download; @@ -83,7 +83,7 @@ impl Application for Example { .on_press(Message::Add) .padding(10), ) - .align_items(Align::End); + .align_items(CrossAlign::End); Container::new(downloads) .width(Length::Fill) @@ -182,7 +182,7 @@ impl Download { } State::Finished { button } => Column::new() .spacing(10) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(Text::new("Download finished!")) .push( Button::new(button, Text::new("Start again")) @@ -195,7 +195,7 @@ impl Download { } State::Errored { button } => Column::new() .spacing(10) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(Text::new("Something went wrong :(")) .push( Button::new(button, Text::new("Try again")) @@ -207,7 +207,7 @@ impl Download { Column::new() .spacing(10) .padding(10) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(progress_bar) .push(control) .into() diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 911ff425ac..398b321fa6 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -1,7 +1,7 @@ use iced::{ - button, executor, Align, Application, Button, Checkbox, Column, Command, - Container, Element, HorizontalAlignment, Length, Settings, Subscription, - Text, + button, executor, Application, Button, Checkbox, Column, Command, + Container, CrossAlign, Element, HorizontalAlignment, Length, Settings, + Subscription, Text, }; use iced_native::{window, Event}; @@ -98,7 +98,7 @@ impl Application for Events { .on_press(Message::Exit); let content = Column::new() - .align_items(Align::Center) + .align_items(CrossAlign::Center) .spacing(20) .push(events) .push(toggle) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index bc6c370803..38f4fcbd57 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -11,8 +11,8 @@ use iced::slider::{self, Slider}; use iced::time; use iced::window; use iced::{ - Align, Application, Checkbox, Column, Command, Container, Element, Length, - Row, Settings, Subscription, Text, + Application, Checkbox, Column, Command, Container, CrossAlign, Element, + Length, Row, Settings, Subscription, Text, }; use preset::Preset; use std::time::{Duration, Instant}; @@ -844,7 +844,7 @@ impl Controls { let speed_controls = Row::new() .width(Length::Fill) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .spacing(10) .push( Slider::new( @@ -860,7 +860,7 @@ impl Controls { Row::new() .padding(10) .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(playback_controls) .push(speed_controls) .push( diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index f650b2c185..733efca7a2 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -161,8 +161,8 @@ mod rainbow { } use iced::{ - scrollable, Align, Column, Container, Element, Length, Sandbox, Scrollable, - Settings, Text, + scrollable, Column, Container, CrossAlign, Element, Length, Sandbox, + Scrollable, Settings, Text, }; use rainbow::Rainbow; @@ -194,7 +194,7 @@ impl Sandbox for Example { .padding(20) .spacing(20) .max_width(500) - .align_items(Align::Start) + .align_items(CrossAlign::Start) .push(Rainbow::new()) .push(Text::new( "In this example we draw a custom widget Rainbow, using \ diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs index ddc6827c06..ebf64d4342 100644 --- a/examples/integration_opengl/src/controls.rs +++ b/examples/integration_opengl/src/controls.rs @@ -1,6 +1,6 @@ use iced_glow::Renderer; use iced_glutin::{ - slider, Align, Color, Column, Command, Element, Length, Program, Row, + slider, Color, Column, Command, CrossAlign, Element, Length, Program, Row, Slider, Text, }; @@ -79,11 +79,11 @@ impl Program for Controls { Row::new() .width(Length::Fill) .height(Length::Fill) - .align_items(Align::End) + .align_items(CrossAlign::End) .push( Column::new() .width(Length::Fill) - .align_items(Align::End) + .align_items(CrossAlign::End) .push( Column::new() .padding(10) diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 824f9f53f8..e765403897 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -1,6 +1,6 @@ use iced_wgpu::Renderer; use iced_winit::{ - slider, Align, Color, Column, Command, Element, Length, Program, Row, + slider, Color, Column, Command, CrossAlign, Element, Length, Program, Row, Slider, Text, }; @@ -79,11 +79,11 @@ impl Program for Controls { Row::new() .width(Length::Fill) .height(Length::Fill) - .align_items(Align::End) + .align_items(CrossAlign::End) .push( Column::new() .width(Length::Fill) - .align_items(Align::End) + .align_items(CrossAlign::End) .push( Column::new() .padding(10) diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 5a134756b9..737c9126fe 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,7 +1,8 @@ use iced::{ - button, executor, keyboard, pane_grid, scrollable, Align, Application, - Button, Color, Column, Command, Container, Element, HorizontalAlignment, - Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text, + button, executor, keyboard, pane_grid, scrollable, Application, Button, + Color, Column, Command, Container, CrossAlign, Element, + HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings, + Subscription, Text, }; use iced_native::{event, subscription, Event}; @@ -329,7 +330,7 @@ impl Content { let content = Scrollable::new(scroll) .width(Length::Fill) .spacing(10) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(controls); Container::new(content) diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 1eec97911a..363d322255 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -1,5 +1,5 @@ use iced::{ - pick_list, scrollable, Align, Container, Element, Length, PickList, + pick_list, scrollable, Container, CrossAlign, Element, Length, PickList, Sandbox, Scrollable, Settings, Space, Text, }; @@ -49,7 +49,7 @@ impl Sandbox for Example { let mut content = Scrollable::new(&mut self.scroll) .width(Length::Fill) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .spacing(10) .push(Space::with_height(Length::Units(600))) .push(Text::new("Which is your favorite language?")) diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index fdf667ccc7..aa57aae387 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, futures, image, Align, Application, Button, Column, Command, - Container, Element, Length, Row, Settings, Text, + button, futures, image, Application, Button, Column, Command, Container, + CrossAlign, Element, Length, Row, Settings, Text, }; pub fn main() -> iced::Result { @@ -85,14 +85,14 @@ impl Application for Pokedex { Pokedex::Loaded { pokemon, search } => Column::new() .max_width(500) .spacing(20) - .align_items(Align::End) + .align_items(CrossAlign::End) .push(pokemon.view()) .push( button(search, "Keep searching!").on_press(Message::Search), ), Pokedex::Errored { try_again, .. } => Column::new() .spacing(20) - .align_items(Align::End) + .align_items(CrossAlign::End) .push(Text::new("Whoops! Something went wrong...").size(40)) .push(button(try_again, "Try again").on_press(Message::Search)), }; @@ -121,7 +121,7 @@ impl Pokemon { fn view(&mut self) -> Element { Row::new() .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(image::Viewer::new( &mut self.image_viewer, self.image.clone(), @@ -131,7 +131,7 @@ impl Pokemon { .spacing(20) .push( Row::new() - .align_items(Align::Center) + .align_items(CrossAlign::Center) .spacing(20) .push( Text::new(&self.name) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 37b4855dae..73c2e0589f 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -1,7 +1,7 @@ use iced::qr_code::{self, QRCode}; use iced::text_input::{self, TextInput}; use iced::{ - Align, Column, Container, Element, Length, Sandbox, Settings, Text, + Column, Container, CrossAlign, Element, Length, Sandbox, Settings, Text, }; pub fn main() -> iced::Result { @@ -62,7 +62,7 @@ impl Sandbox for QRGenerator { let mut content = Column::new() .width(Length::Units(700)) .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(title) .push(input); diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 983cf3e6de..41b3eb375a 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, executor, time, Align, Application, Button, Column, Command, - Container, Element, HorizontalAlignment, Length, Row, Settings, + button, executor, time, Application, Button, Column, Command, Container, + CrossAlign, Element, HorizontalAlignment, Length, Row, Settings, Subscription, Text, }; use std::time::{Duration, Instant}; @@ -130,7 +130,7 @@ impl Application for Stopwatch { .push(reset_button); let content = Column::new() - .align_items(Align::Center) + .align_items(CrossAlign::Center) .spacing(20) .push(duration) .push(controls); diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 7bc49281a9..4a004f3fdb 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,7 +1,7 @@ use iced::{ - button, scrollable, slider, text_input, Align, Button, Checkbox, Column, - Container, Element, Length, ProgressBar, Radio, Row, Rule, Sandbox, - Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, + button, scrollable, slider, text_input, Button, Checkbox, Column, + Container, CrossAlign, Element, Length, ProgressBar, Radio, Row, Rule, + Sandbox, Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, }; pub fn main() -> iced::Result { @@ -132,7 +132,7 @@ impl Sandbox for Styling { Row::new() .spacing(10) .height(Length::Units(100)) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(scrollable) .push(Rule::vertical(38).style(self.theme)) .push( diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 7a8ecc1a78..6e798f3287 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, scrollable, text_input, Align, Application, Button, Checkbox, - Column, Command, Container, Element, Font, HorizontalAlignment, Length, + button, scrollable, text_input, Application, Button, Checkbox, Column, + Command, Container, CrossAlign, Element, Font, HorizontalAlignment, Length, Row, Scrollable, Settings, Text, TextInput, }; use serde::{Deserialize, Serialize}; @@ -295,7 +295,7 @@ impl Task { Row::new() .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(checkbox) .push( Button::new(edit_button, edit_icon()) @@ -320,7 +320,7 @@ impl Task { Row::new() .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push(text_input) .push( Button::new( @@ -369,7 +369,7 @@ impl Controls { Row::new() .spacing(20) - .align_items(Align::Center) + .align_items(CrossAlign::Center) .push( Text::new(&format!( "{} {} left", diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index d6c8b8e1bb..f6b5dc0ed1 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -1,7 +1,8 @@ use iced::tooltip::{self, Tooltip}; use iced::{ - button, Button, Column, Container, Element, HorizontalAlignment, Length, - Row, Sandbox, Settings, Text, VerticalAlignment, + button, Button, Column, Container, CrossAlign, Element, + HorizontalAlignment, Length, Row, Sandbox, Settings, Text, + VerticalAlignment, }; pub fn main() { @@ -60,7 +61,7 @@ impl Sandbox for Example { ]) .width(Length::Fill) .height(Length::Fill) - .align_items(iced::Align::Center) + .align_items(CrossAlign::Center) .spacing(50); let follow_cursor = tooltip( diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 52b48fecb8..dfb5288b1b 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 bee5e64eb3..2239a6541e 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 cb0600e2c3..cf8b0da5c2 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 0f21c873c9..e74515c6ee 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 52a2e80c10..83e5760ae4 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 69aee64de2..ae18db25c2 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 dee82d1fcc..1ab51051a9 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 9ebc914581..e200e69783 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 68da2e6793..0a2155cc0d 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 4035276c55..34dc52a094 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( diff --git a/src/lib.rs b/src/lib.rs index 3d4b96f173..5ef25d2e89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,6 +246,7 @@ pub use sandbox::Sandbox; pub use settings::Settings; pub use runtime::{ - futures, Align, Background, Color, Command, Font, HorizontalAlignment, - Length, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment, + futures, Align, Background, Color, Command, CrossAlign, Font, + HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector, + VerticalAlignment, }; diff --git a/web/src/css.rs b/web/src/css.rs index 23b21e2249..c132d04543 100644 --- a/web/src/css.rs +++ b/web/src/css.rs @@ -1,5 +1,5 @@ //! Style your widgets. -use crate::{bumpalo, Align, Background, Color, Length, Padding}; +use crate::{bumpalo, Align, Background, Color, CrossAlign, Length, Padding}; use std::collections::BTreeMap; @@ -201,7 +201,16 @@ pub fn align(align: Align) -> &'static str { Align::Start => "flex-start", Align::Center => "center", Align::End => "flex-end", - Align::Fill => "stretch", + } +} + +/// Returns the style value for the given [`CrossAlign`]. +pub fn cross_align(align: CrossAlign) -> &'static str { + match align { + CrossAlign::Start => "flex-start", + CrossAlign::Center => "center", + CrossAlign::End => "flex-end", + CrossAlign::Fill => "stretch", } } diff --git a/web/src/lib.rs b/web/src/lib.rs index 8cfe685b2e..b968c96fba 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -74,8 +74,9 @@ pub use dodrio; pub use element::Element; pub use hasher::Hasher; pub use iced_core::{ - keyboard, mouse, Align, Background, Color, Font, HorizontalAlignment, - Length, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, + keyboard, mouse, Align, Background, Color, CrossAlign, Font, + HorizontalAlignment, Length, Padding, Point, Rectangle, Size, Vector, + VerticalAlignment, }; pub use iced_futures::{executor, futures}; pub use subscription::Subscription; From a0ad3996225601aaa1ebe051cba115374b55c80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 20 Sep 2021 15:09:55 +0700 Subject: [PATCH 5/5] Refactor alignment types into an `alignment` module --- core/src/align.rs | 64 --------------------- core/src/alignment.rs | 63 ++++++++++++++++++++ core/src/lib.rs | 4 +- examples/bezier_tool/src/main.rs | 5 +- examples/color_palette/src/main.rs | 13 ++--- examples/counter/src/main.rs | 4 +- examples/custom_widget/src/main.rs | 4 +- examples/download_progress/src/main.rs | 12 ++-- examples/events/src/main.rs | 9 ++- examples/game_of_life/src/main.rs | 14 ++--- examples/geometry/src/main.rs | 4 +- examples/integration_opengl/src/controls.rs | 9 +-- examples/integration_wgpu/src/controls.rs | 6 +- examples/pane_grid/src/main.rs | 11 ++-- examples/pick_list/src/main.rs | 4 +- examples/pokedex/src/main.rs | 12 ++-- examples/qr_code/src/main.rs | 4 +- examples/stopwatch/src/main.rs | 9 ++- examples/styling/src/main.rs | 8 +-- examples/todos/src/main.rs | 23 ++++---- examples/tooltip/src/main.rs | 11 ++-- examples/tour/src/main.rs | 22 +++---- glow/src/backend.rs | 15 ++--- glow/src/lib.rs | 7 +-- graphics/src/layer.rs | 12 ++-- graphics/src/lib.rs | 4 +- graphics/src/overlay/menu.rs | 11 ++-- graphics/src/primitive.rs | 9 +-- graphics/src/widget/canvas/text.rs | 11 ++-- graphics/src/widget/checkbox.rs | 9 +-- graphics/src/widget/pick_list.rs | 15 +++-- graphics/src/widget/text.rs | 21 ++++--- graphics/src/widget/text_input.rs | 15 ++--- 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 ++++--- src/lib.rs | 7 ++- web/src/css.rs | 26 +++------ web/src/lib.rs | 15 +++-- web/src/widget/column.rs | 11 ++-- web/src/widget/container.rs | 21 ++++--- web/src/widget/row.rs | 11 ++-- web/src/widget/scrollable.rs | 8 +-- web/src/widget/text.rs | 28 ++++----- wgpu/src/backend.rs | 15 ++--- 54 files changed, 401 insertions(+), 376 deletions(-) delete mode 100644 core/src/align.rs create mode 100644 core/src/alignment.rs diff --git a/core/src/align.rs b/core/src/align.rs deleted file mode 100644 index ad0d8a2596..0000000000 --- a/core/src/align.rs +++ /dev/null @@ -1,64 +0,0 @@ -/// Alignment on an axis of a container. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum Align { - /// Align at the start of the axis. - Start, - - /// Align at the center of the axis. - Center, - - /// Align at the end of the axis. - End, -} - -/// Alignment on the cross axis of a container. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum CrossAlign { - /// Align at the start of the axis. - Start, - - /// Align at the center of the axis. - Center, - - /// Align at the end of the axis. - End, - - /// Fill the entire axis. - Fill, -} - -impl From for CrossAlign { - fn from(align: Align) -> Self { - match align { - Align::Start => Self::Start, - Align::Center => Self::Center, - Align::End => Self::End, - } - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} diff --git a/core/src/alignment.rs b/core/src/alignment.rs new file mode 100644 index 0000000000..8c561d9d55 --- /dev/null +++ b/core/src/alignment.rs @@ -0,0 +1,63 @@ +//! Align and position widgets. + +/// Alignment on the axis of a container. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Alignment { + /// Align at the start of the axis. + Start, + + /// Align at the center of the axis. + Center, + + /// Align at the end of the axis. + End, + + /// Fill the entire axis. + Fill, +} + +impl From for Alignment { + fn from(horizontal: Horizontal) -> Self { + match horizontal { + Horizontal::Left => Self::Start, + Horizontal::Center => Self::Center, + Horizontal::Right => Self::End, + } + } +} + +impl From for Alignment { + fn from(vertical: Vertical) -> Self { + match vertical { + Vertical::Top => Self::Start, + Vertical::Center => Self::Center, + Vertical::Bottom => Self::End, + } + } +} + +/// The horizontal [`Alignment`] of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Horizontal { + /// Align left + Left, + + /// Horizontally centered + Center, + + /// Align right + Right, +} + +/// The vertical [`Alignment`] of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Vertical { + /// Align top + Top, + + /// Vertically centered + Center, + + /// Align bottom + Bottom, +} diff --git a/core/src/lib.rs b/core/src/lib.rs index b0aa4e1252..1f58a8cde3 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -14,11 +14,11 @@ #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] +pub mod alignment; pub mod keyboard; pub mod mouse; pub mod text; -mod align; mod background; mod color; mod font; @@ -29,7 +29,7 @@ mod rectangle; mod size; mod vector; -pub use align::{Align, CrossAlign, HorizontalAlignment, VerticalAlignment}; +pub use alignment::Alignment; pub use background::Background; pub use color::Color; pub use font::Font; diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index b17f5726aa..35b5182c61 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -1,7 +1,6 @@ //! This example showcases an interactive `Canvas` for drawing Bézier curves. use iced::{ - button, Button, Column, CrossAlign, Element, Length, Sandbox, Settings, - Text, + button, Alignment, Button, Column, Element, Length, Sandbox, Settings, Text, }; pub fn main() -> iced::Result { @@ -52,7 +51,7 @@ impl Sandbox for Example { Column::new() .padding(20) .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push( Text::new("Bezier tool example") .width(Length::Shrink) diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 90277186a4..ad3004b031 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -1,8 +1,7 @@ use iced::canvas::{self, Cursor, Frame, Geometry, Path}; use iced::{ - slider, Canvas, Color, Column, CrossAlign, Element, HorizontalAlignment, - Length, Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, - Vector, VerticalAlignment, + alignment, slider, Alignment, Canvas, Color, Column, Element, Length, + Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, Vector, }; use palette::{self, Hsl, Limited, Srgb}; use std::marker::PhantomData; @@ -163,8 +162,8 @@ impl Theme { }); let mut text = canvas::Text { - horizontal_alignment: HorizontalAlignment::Center, - vertical_alignment: VerticalAlignment::Top, + horizontal_alignment: alignment::Horizontal::Center, + vertical_alignment: alignment::Vertical::Top, size: 15.0, ..canvas::Text::default() }; @@ -206,7 +205,7 @@ impl Theme { }); } - text.vertical_alignment = VerticalAlignment::Bottom; + text.vertical_alignment = alignment::Vertical::Bottom; let hsl = Hsl::from(Srgb::from(self.base)); for i in 0..self.len() { @@ -298,7 +297,7 @@ impl ColorPicker { Row::new() .spacing(10) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(Text::new(C::LABEL).width(Length::Units(50))) .push(slider(s1, cr1, c1, move |v| C::new(v, c2, c3))) .push(slider(s2, cr2, c2, move |v| C::new(c1, v, c3))) diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index 710849ea1e..931cf5e175 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -1,5 +1,5 @@ use iced::{ - button, Button, Column, CrossAlign, Element, Sandbox, Settings, Text, + button, Alignment, Button, Column, Element, Sandbox, Settings, Text, }; pub fn main() -> iced::Result { @@ -44,7 +44,7 @@ impl Sandbox for Counter { fn view(&mut self) -> Element { Column::new() .padding(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push( Button::new(&mut self.increment_button, Text::new("Increment")) .on_press(Message::IncrementPressed), diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index ce7fe9af0e..c9ad1905b1 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -84,7 +84,7 @@ mod circle { use circle::Circle; use iced::{ - slider, Column, Container, CrossAlign, Element, Length, Sandbox, Settings, + slider, Alignment, Column, Container, Element, Length, Sandbox, Settings, Slider, Text, }; @@ -129,7 +129,7 @@ impl Sandbox for Example { .padding(20) .spacing(20) .max_width(500) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(Circle::new(self.radius)) .push(Text::new(format!("Radius: {:.2}", self.radius))) .push( diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index a6dd54ba70..21804a0a2f 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, executor, Application, Button, Column, Command, Container, - CrossAlign, Element, Length, ProgressBar, Settings, Subscription, Text, + button, executor, Alignment, Application, Button, Column, Command, + Container, Element, Length, ProgressBar, Settings, Subscription, Text, }; mod download; @@ -83,7 +83,7 @@ impl Application for Example { .on_press(Message::Add) .padding(10), ) - .align_items(CrossAlign::End); + .align_items(Alignment::End); Container::new(downloads) .width(Length::Fill) @@ -182,7 +182,7 @@ impl Download { } State::Finished { button } => Column::new() .spacing(10) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(Text::new("Download finished!")) .push( Button::new(button, Text::new("Start again")) @@ -195,7 +195,7 @@ impl Download { } State::Errored { button } => Column::new() .spacing(10) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(Text::new("Something went wrong :(")) .push( Button::new(button, Text::new("Try again")) @@ -207,7 +207,7 @@ impl Download { Column::new() .spacing(10) .padding(10) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(progress_bar) .push(control) .into() diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 398b321fa6..7f024c56df 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -1,7 +1,6 @@ use iced::{ - button, executor, Application, Button, Checkbox, Column, Command, - Container, CrossAlign, Element, HorizontalAlignment, Length, Settings, - Subscription, Text, + alignment, button, executor, Alignment, Application, Button, Checkbox, + Column, Command, Container, Element, Length, Settings, Subscription, Text, }; use iced_native::{window, Event}; @@ -91,14 +90,14 @@ impl Application for Events { &mut self.exit, Text::new("Exit") .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) .width(Length::Units(100)) .padding(10) .on_press(Message::Exit); let content = Column::new() - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(20) .push(events) .push(toggle) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 38f4fcbd57..5011261801 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -11,7 +11,7 @@ use iced::slider::{self, Slider}; use iced::time; use iced::window; use iced::{ - Application, Checkbox, Column, Command, Container, CrossAlign, Element, + Alignment, Application, Checkbox, Column, Command, Container, Element, Length, Row, Settings, Subscription, Text, }; use preset::Preset; @@ -158,10 +158,10 @@ impl Application for GameOfLife { mod grid { use crate::Preset; use iced::{ + alignment, canvas::event::{self, Event}, canvas::{self, Cache, Canvas, Cursor, Frame, Geometry, Path, Text}, - mouse, Color, Element, HorizontalAlignment, Length, Point, Rectangle, - Size, Vector, VerticalAlignment, + mouse, Color, Element, Length, Point, Rectangle, Size, Vector, }; use rustc_hash::{FxHashMap, FxHashSet}; use std::future::Future; @@ -498,8 +498,8 @@ mod grid { color: Color::WHITE, size: 14.0, position: Point::new(frame.width(), frame.height()), - horizontal_alignment: HorizontalAlignment::Right, - vertical_alignment: VerticalAlignment::Bottom, + horizontal_alignment: alignment::Horizontal::Right, + vertical_alignment: alignment::Vertical::Bottom, ..Text::default() }; @@ -844,7 +844,7 @@ impl Controls { let speed_controls = Row::new() .width(Length::Fill) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(10) .push( Slider::new( @@ -860,7 +860,7 @@ impl Controls { Row::new() .padding(10) .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(playback_controls) .push(speed_controls) .push( diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 733efca7a2..e51154930b 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -161,7 +161,7 @@ mod rainbow { } use iced::{ - scrollable, Column, Container, CrossAlign, Element, Length, Sandbox, + scrollable, Alignment, Column, Container, Element, Length, Sandbox, Scrollable, Settings, Text, }; use rainbow::Rainbow; @@ -194,7 +194,7 @@ impl Sandbox for Example { .padding(20) .spacing(20) .max_width(500) - .align_items(CrossAlign::Start) + .align_items(Alignment::Start) .push(Rainbow::new()) .push(Text::new( "In this example we draw a custom widget Rainbow, using \ diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs index ebf64d4342..fa5aa91db6 100644 --- a/examples/integration_opengl/src/controls.rs +++ b/examples/integration_opengl/src/controls.rs @@ -1,7 +1,8 @@ use iced_glow::Renderer; +use iced_glutin::slider; use iced_glutin::{ - slider, Color, Column, Command, CrossAlign, Element, Length, Program, Row, - Slider, Text, + Alignment, Color, Column, Command, Element, Length, Program, Row, Slider, + Text, }; pub struct Controls { @@ -79,11 +80,11 @@ impl Program for Controls { Row::new() .width(Length::Fill) .height(Length::Fill) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push( Column::new() .width(Length::Fill) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push( Column::new() .padding(10) diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index e765403897..414eb9cee4 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -1,6 +1,6 @@ use iced_wgpu::Renderer; use iced_winit::{ - slider, Color, Column, Command, CrossAlign, Element, Length, Program, Row, + slider, Alignment, Color, Column, Command, Element, Length, Program, Row, Slider, Text, }; @@ -79,11 +79,11 @@ impl Program for Controls { Row::new() .width(Length::Fill) .height(Length::Fill) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push( Column::new() .width(Length::Fill) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push( Column::new() .padding(10) diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 737c9126fe..69872bad7d 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,8 +1,7 @@ use iced::{ - button, executor, keyboard, pane_grid, scrollable, Application, Button, - Color, Column, Command, Container, CrossAlign, Element, - HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings, - Subscription, Text, + alignment, button, executor, keyboard, pane_grid, scrollable, Alignment, + Application, Button, Color, Column, Command, Container, Element, Length, + PaneGrid, Row, Scrollable, Settings, Subscription, Text, }; use iced_native::{event, subscription, Event}; @@ -293,7 +292,7 @@ impl Content { state, Text::new(label) .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center) + .horizontal_alignment(alignment::Horizontal::Center) .size(16), ) .width(Length::Fill) @@ -330,7 +329,7 @@ impl Content { let content = Scrollable::new(scroll) .width(Length::Fill) .spacing(10) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(controls); Container::new(content) diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 363d322255..52303d705f 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -1,5 +1,5 @@ use iced::{ - pick_list, scrollable, Container, CrossAlign, Element, Length, PickList, + pick_list, scrollable, Alignment, Container, Element, Length, PickList, Sandbox, Scrollable, Settings, Space, Text, }; @@ -49,7 +49,7 @@ impl Sandbox for Example { let mut content = Scrollable::new(&mut self.scroll) .width(Length::Fill) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(10) .push(Space::with_height(Length::Units(600))) .push(Text::new("Which is your favorite language?")) diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index aa57aae387..c99240a143 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, futures, image, Application, Button, Column, Command, Container, - CrossAlign, Element, Length, Row, Settings, Text, + button, futures, image, Alignment, Application, Button, Column, Command, + Container, Element, Length, Row, Settings, Text, }; pub fn main() -> iced::Result { @@ -85,14 +85,14 @@ impl Application for Pokedex { Pokedex::Loaded { pokemon, search } => Column::new() .max_width(500) .spacing(20) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push(pokemon.view()) .push( button(search, "Keep searching!").on_press(Message::Search), ), Pokedex::Errored { try_again, .. } => Column::new() .spacing(20) - .align_items(CrossAlign::End) + .align_items(Alignment::End) .push(Text::new("Whoops! Something went wrong...").size(40)) .push(button(try_again, "Try again").on_press(Message::Search)), }; @@ -121,7 +121,7 @@ impl Pokemon { fn view(&mut self) -> Element { Row::new() .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(image::Viewer::new( &mut self.image_viewer, self.image.clone(), @@ -131,7 +131,7 @@ impl Pokemon { .spacing(20) .push( Row::new() - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(20) .push( Text::new(&self.name) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 73c2e0589f..92c82d45e9 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -1,7 +1,7 @@ use iced::qr_code::{self, QRCode}; use iced::text_input::{self, TextInput}; use iced::{ - Column, Container, CrossAlign, Element, Length, Sandbox, Settings, Text, + Alignment, Column, Container, Element, Length, Sandbox, Settings, Text, }; pub fn main() -> iced::Result { @@ -62,7 +62,7 @@ impl Sandbox for QRGenerator { let mut content = Column::new() .width(Length::Units(700)) .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(title) .push(input); diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 41b3eb375a..dc8a4de74d 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -1,7 +1,6 @@ use iced::{ - button, executor, time, Application, Button, Column, Command, Container, - CrossAlign, Element, HorizontalAlignment, Length, Row, Settings, - Subscription, Text, + alignment, button, executor, time, Alignment, Application, Button, Column, + Command, Container, Element, Length, Row, Settings, Subscription, Text, }; use std::time::{Duration, Instant}; @@ -104,7 +103,7 @@ impl Application for Stopwatch { Button::new( state, Text::new(label) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) .min_width(80) .padding(10) @@ -130,7 +129,7 @@ impl Application for Stopwatch { .push(reset_button); let content = Column::new() - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(20) .push(duration) .push(controls); diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 4a004f3fdb..81d33ad32d 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,7 +1,7 @@ use iced::{ - button, scrollable, slider, text_input, Button, Checkbox, Column, - Container, CrossAlign, Element, Length, ProgressBar, Radio, Row, Rule, - Sandbox, Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, + button, scrollable, slider, text_input, Alignment, Button, Checkbox, + Column, Container, Element, Length, ProgressBar, Radio, Row, Rule, Sandbox, + Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, }; pub fn main() -> iced::Result { @@ -132,7 +132,7 @@ impl Sandbox for Styling { Row::new() .spacing(10) .height(Length::Units(100)) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(scrollable) .push(Rule::vertical(38).style(self.theme)) .push( diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6e798f3287..11f23fd456 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,7 +1,10 @@ +use iced::alignment::{self, Alignment}; +use iced::button::{self, Button}; +use iced::scrollable::{self, Scrollable}; +use iced::text_input::{self, TextInput}; use iced::{ - button, scrollable, text_input, Application, Button, Checkbox, Column, - Command, Container, CrossAlign, Element, Font, HorizontalAlignment, Length, - Row, Scrollable, Settings, Text, TextInput, + Application, Checkbox, Column, Command, Container, Element, Font, Length, + Row, Settings, Text, }; use serde::{Deserialize, Serialize}; @@ -151,7 +154,7 @@ impl Application for Todos { .width(Length::Fill) .size(100) .color([0.5, 0.5, 0.5]) - .horizontal_alignment(HorizontalAlignment::Center); + .horizontal_alignment(alignment::Horizontal::Center); let input = TextInput::new( input, @@ -295,7 +298,7 @@ impl Task { Row::new() .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(checkbox) .push( Button::new(edit_button, edit_icon()) @@ -320,7 +323,7 @@ impl Task { Row::new() .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push(text_input) .push( Button::new( @@ -369,7 +372,7 @@ impl Controls { Row::new() .spacing(20) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .push( Text::new(&format!( "{} {} left", @@ -431,7 +434,7 @@ impl Filter { fn loading_message<'a>() -> Element<'a, Message> { Container::new( Text::new("Loading...") - .horizontal_alignment(HorizontalAlignment::Center) + .horizontal_alignment(alignment::Horizontal::Center) .size(50), ) .width(Length::Fill) @@ -445,7 +448,7 @@ fn empty_message<'a>(message: &str) -> Element<'a, Message> { Text::new(message) .width(Length::Fill) .size(25) - .horizontal_alignment(HorizontalAlignment::Center) + .horizontal_alignment(alignment::Horizontal::Center) .color([0.7, 0.7, 0.7]), ) .width(Length::Fill) @@ -464,7 +467,7 @@ fn icon(unicode: char) -> Text { Text::new(&unicode.to_string()) .font(ICONS) .width(Length::Units(20)) - .horizontal_alignment(HorizontalAlignment::Center) + .horizontal_alignment(alignment::Horizontal::Center) .size(20) } diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index f6b5dc0ed1..cfeaf6a697 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -1,8 +1,7 @@ use iced::tooltip::{self, Tooltip}; use iced::{ - button, Button, Column, Container, CrossAlign, Element, - HorizontalAlignment, Length, Row, Sandbox, Settings, Text, - VerticalAlignment, + alignment, button, Alignment, Button, Column, Container, Element, Length, + Row, Sandbox, Settings, Text, }; pub fn main() { @@ -61,7 +60,7 @@ impl Sandbox for Example { ]) .width(Length::Fill) .height(Length::Fill) - .align_items(CrossAlign::Center) + .align_items(Alignment::Center) .spacing(50); let follow_cursor = tooltip( @@ -105,8 +104,8 @@ fn tooltip<'a>( .size(40) .width(Length::Fill) .height(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center) - .vertical_alignment(VerticalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center) + .vertical_alignment(alignment::Vertical::Center), ) .on_press(Message) .width(Length::Fill) diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 1215f83de5..b5af48c73c 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,7 +1,7 @@ use iced::{ - button, scrollable, slider, text_input, Button, Checkbox, Color, Column, - Container, Element, HorizontalAlignment, Image, Length, Radio, Row, - Sandbox, Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, + alignment, button, scrollable, slider, text_input, Button, Checkbox, Color, + Column, Container, Element, Image, Length, Radio, Row, Sandbox, Scrollable, + Settings, Slider, Space, Text, TextInput, Toggler, }; pub fn main() -> iced::Result { @@ -419,7 +419,7 @@ impl<'a> Step { .push( Text::new(&value.to_string()) .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) } @@ -466,7 +466,7 @@ impl<'a> Step { .push( Text::new(&format!("{} px", spacing)) .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ); Self::container("Rows and columns") @@ -591,7 +591,7 @@ impl<'a> Step { .push( Text::new(&format!("Width: {} px", width.to_string())) .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) } @@ -612,7 +612,7 @@ impl<'a> Step { Text::new("You are halfway there!") .width(Length::Fill) .size(30) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) .push(Column::new().height(Length::Units(4096))) .push(ferris(300)) @@ -620,7 +620,7 @@ impl<'a> Step { Text::new("You made it!") .width(Length::Fill) .size(50) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) } @@ -662,7 +662,7 @@ impl<'a> Step { value }) .width(Length::Fill) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) } @@ -680,7 +680,7 @@ impl<'a> Step { Element::new( Text::new("Not available on web yet!") .color([0.7, 0.7, 0.7]) - .horizontal_alignment(HorizontalAlignment::Center), + .horizontal_alignment(alignment::Horizontal::Center), ) } else { Element::new(Checkbox::new( @@ -725,7 +725,7 @@ fn button<'a, Message: Clone>( ) -> Button<'a, Message> { Button::new( state, - Text::new(label).horizontal_alignment(HorizontalAlignment::Center), + Text::new(label).horizontal_alignment(alignment::Horizontal::Center), ) .padding(12) .min_width(100) diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 526965cbe8..9a9457cfc2 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -7,8 +7,9 @@ use iced_graphics::backend; use iced_graphics::font; use iced_graphics::Layer; use iced_graphics::Primitive; +use iced_native::alignment; use iced_native::mouse; -use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment}; +use iced_native::{Font, Size}; /// A [`glow`] graphics backend for [`iced`]. /// @@ -147,24 +148,24 @@ impl Backend { }], layout: glow_glyph::Layout::default() .h_align(match text.horizontal_alignment { - HorizontalAlignment::Left => { + alignment::Horizontal::Left => { glow_glyph::HorizontalAlign::Left } - HorizontalAlignment::Center => { + alignment::Horizontal::Center => { glow_glyph::HorizontalAlign::Center } - HorizontalAlignment::Right => { + alignment::Horizontal::Right => { glow_glyph::HorizontalAlign::Right } }) .v_align(match text.vertical_alignment { - VerticalAlignment::Top => { + alignment::Vertical::Top => { glow_glyph::VerticalAlign::Top } - VerticalAlignment::Center => { + alignment::Vertical::Center => { glow_glyph::VerticalAlign::Center } - VerticalAlignment::Bottom => { + alignment::Vertical::Bottom => { glow_glyph::VerticalAlign::Bottom } }), diff --git a/glow/src/lib.rs b/glow/src/lib.rs index 888492d88f..418f7e3d3a 100644 --- a/glow/src/lib.rs +++ b/glow/src/lib.rs @@ -29,10 +29,9 @@ pub(crate) use iced_graphics::Transformation; pub use widget::*; pub use iced_graphics::{Error, Viewport}; -pub use iced_native::{ - Background, Color, Command, HorizontalAlignment, Length, Vector, - VerticalAlignment, -}; + +pub use iced_native::alignment; +pub use iced_native::{Alignment, Background, Color, Command, Length, Vector}; /// A [`glow`] graphics renderer for [`iced`]. /// diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 7dce1d4c0e..9653a2e477 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,10 +1,10 @@ //! Organize rendering primitives into a flattened list of layers. +use crate::alignment; use crate::image; use crate::svg; use crate::triangle; use crate::{ - Background, Font, HorizontalAlignment, Point, Primitive, Rectangle, Size, - Vector, VerticalAlignment, Viewport, + Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, }; /// A group of primitives that should be clipped together. @@ -55,8 +55,8 @@ impl<'a> Layer<'a> { color: [0.9, 0.9, 0.9, 1.0], size: 20.0, font: Font::Default, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, }; overlay.text.push(text); @@ -293,10 +293,10 @@ pub struct Text<'a> { pub font: Font, /// The horizontal alignment of the [`Text`]. - pub horizontal_alignment: HorizontalAlignment, + pub horizontal_alignment: alignment::Horizontal, /// The vertical alignment of the [`Text`]. - pub vertical_alignment: VerticalAlignment, + pub vertical_alignment: alignment::Vertical, } /// A raster or vector image. diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 143886532c..54cdcb773f 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -39,7 +39,7 @@ pub use renderer::Renderer; pub use transformation::Transformation; pub use viewport::Viewport; +pub use iced_native::alignment; pub use iced_native::{ - Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size, - Vector, VerticalAlignment, + Alignment, Background, Color, Font, Point, Rectangle, Size, Vector, }; diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 9e91a0ef64..53f47984e1 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,10 +1,9 @@ //! Build and show dropdown menus. +use crate::alignment; use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; -use iced_native::{ - mouse, overlay, Color, Font, HorizontalAlignment, Padding, Point, - Rectangle, VerticalAlignment, -}; + +use iced_native::{mouse, overlay, Color, Font, Padding, Point, Rectangle}; pub use iced_style::menu::Style; @@ -100,8 +99,8 @@ where } else { style.text_color }, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Center, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Center, }); } diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 30263bd429..32f8383d63 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -1,9 +1,10 @@ use iced_native::{ - image, svg, Background, Color, Font, HorizontalAlignment, Rectangle, Size, - Vector, VerticalAlignment, + image, svg, Background, Color, Font, Rectangle, Size, Vector, }; +use crate::alignment; use crate::triangle; + use std::sync::Arc; /// A rendering primitive. @@ -29,9 +30,9 @@ pub enum Primitive { /// The font of the text font: Font, /// The horizontal alignment of the text - horizontal_alignment: HorizontalAlignment, + horizontal_alignment: alignment::Horizontal, /// The vertical alignment of the text - vertical_alignment: VerticalAlignment, + vertical_alignment: alignment::Vertical, }, /// A quad primitive Quad { diff --git a/graphics/src/widget/canvas/text.rs b/graphics/src/widget/canvas/text.rs index c4cae30efd..ab070a7042 100644 --- a/graphics/src/widget/canvas/text.rs +++ b/graphics/src/widget/canvas/text.rs @@ -1,4 +1,5 @@ -use iced_native::{Color, Font, HorizontalAlignment, Point, VerticalAlignment}; +use crate::alignment; +use crate::{Color, Font, Point}; /// A bunch of text that can be drawn to a canvas #[derive(Debug, Clone)] @@ -14,9 +15,9 @@ pub struct Text { /// The font of the text pub font: Font, /// The horizontal alignment of the text - pub horizontal_alignment: HorizontalAlignment, + pub horizontal_alignment: alignment::Horizontal, /// The vertical alignment of the text - pub vertical_alignment: VerticalAlignment, + pub vertical_alignment: alignment::Vertical, } impl Default for Text { @@ -27,8 +28,8 @@ impl Default for Text { color: Color::BLACK, size: 16.0, font: Font::Default, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, } } } diff --git a/graphics/src/widget/checkbox.rs b/graphics/src/widget/checkbox.rs index cb7fd2cf3b..620bfc9e2e 100644 --- a/graphics/src/widget/checkbox.rs +++ b/graphics/src/widget/checkbox.rs @@ -1,9 +1,10 @@ //! Show toggle controls using checkboxes. +use crate::alignment; use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; +use crate::{Primitive, Rectangle, Renderer}; + use iced_native::checkbox; use iced_native::mouse; -use iced_native::{HorizontalAlignment, Rectangle, VerticalAlignment}; pub use iced_style::checkbox::{Style, StyleSheet}; @@ -57,8 +58,8 @@ where ..bounds }, color: style.checkmark_color, - horizontal_alignment: HorizontalAlignment::Center, - vertical_alignment: VerticalAlignment::Center, + horizontal_alignment: alignment::Horizontal::Center, + vertical_alignment: alignment::Vertical::Center, }; vec![checkbox, check, label] diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 88a590b5c6..532840b866 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -1,10 +1,9 @@ //! Display a dropdown list of selectable values. +use crate::alignment; use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; -use iced_native::{ - mouse, Font, HorizontalAlignment, Padding, Point, Rectangle, - VerticalAlignment, -}; + +use iced_native::{mouse, Font, Padding, Point, Rectangle}; use iced_style::menu; pub use iced_native::pick_list::State; @@ -64,8 +63,8 @@ where ..bounds }, color: style.text_color, - horizontal_alignment: HorizontalAlignment::Right, - vertical_alignment: VerticalAlignment::Center, + horizontal_alignment: alignment::Horizontal::Right, + vertical_alignment: alignment::Vertical::Center, }; ( @@ -85,8 +84,8 @@ where y: bounds.center_y(), ..bounds }, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Center, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Center, }; vec![background, label, arrow_down] diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index 645c8414d9..d6d446d320 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -1,11 +1,10 @@ //! Write some text for your users to read. use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; +use iced_native::alignment; use iced_native::mouse; use iced_native::text; -use iced_native::{ - Color, Font, HorizontalAlignment, Point, Rectangle, Size, VerticalAlignment, -}; +use iced_native::{Color, Font, Point, Rectangle, Size}; /// A paragraph of text. /// @@ -62,19 +61,19 @@ where size: u16, font: Font, color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, ) -> Self::Output { let x = match horizontal_alignment { - iced_native::HorizontalAlignment::Left => bounds.x, - iced_native::HorizontalAlignment::Center => bounds.center_x(), - iced_native::HorizontalAlignment::Right => bounds.x + bounds.width, + alignment::Horizontal::Left => bounds.x, + alignment::Horizontal::Center => bounds.center_x(), + alignment::Horizontal::Right => bounds.x + bounds.width, }; let y = match vertical_alignment { - iced_native::VerticalAlignment::Top => bounds.y, - iced_native::VerticalAlignment::Center => bounds.center_y(), - iced_native::VerticalAlignment::Bottom => bounds.y + bounds.height, + alignment::Vertical::Top => bounds.y, + alignment::Vertical::Center => bounds.center_y(), + alignment::Vertical::Bottom => bounds.y + bounds.height, }; ( diff --git a/graphics/src/widget/text_input.rs b/graphics/src/widget/text_input.rs index c269022b11..1516b00769 100644 --- a/graphics/src/widget/text_input.rs +++ b/graphics/src/widget/text_input.rs @@ -1,14 +1,15 @@ //! Display fields that can be filled with text. //! //! A [`TextInput`] has some local [`State`]. +use crate::alignment; use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; +use crate::{ + Background, Color, Font, Point, Primitive, Rectangle, Renderer, Size, + Vector, +}; + use iced_native::mouse; use iced_native::text_input::{self, cursor}; -use iced_native::{ - Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size, - Vector, VerticalAlignment, -}; use std::f32; pub use iced_native::text_input::State; @@ -116,8 +117,8 @@ where ..text_bounds }, size: f32::from(size), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Center, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Center, }; let (contents_primitive, offset) = if state.is_focused() { diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index dfb5288b1b..5fbcbca0ce 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 2239a6541e..e9e6058e39 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 cf8b0da5c2..170a588bec 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 2c47ddf2d6..b5921582bb 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 e74515c6ee..8bdb6b78e6 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 83e5760ae4..30cf0781e5 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 ae18db25c2..0e86ab62cc 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 1ab51051a9..513b2fce63 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 e200e69783..1923f213d7 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 0a2155cc0d..a8e467d36e 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 d8bc0a0003..168d49c2dc 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 34dc52a094..c624be4c59 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, )) } diff --git a/src/lib.rs b/src/lib.rs index 5ef25d2e89..0020ed8743 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,8 +245,9 @@ pub use result::Result; pub use sandbox::Sandbox; pub use settings::Settings; +pub use runtime::alignment; +pub use runtime::futures; pub use runtime::{ - futures, Align, Background, Color, Command, CrossAlign, Font, - HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector, - VerticalAlignment, + Alignment, Background, Color, Command, Font, Length, Point, Rectangle, + Size, Subscription, Vector, }; diff --git a/web/src/css.rs b/web/src/css.rs index c132d04543..07589150c6 100644 --- a/web/src/css.rs +++ b/web/src/css.rs @@ -1,5 +1,6 @@ //! Style your widgets. -use crate::{bumpalo, Align, Background, Color, CrossAlign, Length, Padding}; +use crate::bumpalo; +use crate::{Alignment, Background, Color, Length, Padding}; use std::collections::BTreeMap; @@ -195,22 +196,13 @@ pub fn background(background: Background) -> String { } } -/// Returns the style value for the given [`Align`]. -pub fn align(align: Align) -> &'static str { - match align { - Align::Start => "flex-start", - Align::Center => "center", - Align::End => "flex-end", - } -} - -/// Returns the style value for the given [`CrossAlign`]. -pub fn cross_align(align: CrossAlign) -> &'static str { - match align { - CrossAlign::Start => "flex-start", - CrossAlign::Center => "center", - CrossAlign::End => "flex-end", - CrossAlign::Fill => "stretch", +/// Returns the style value for the given [`Alignment`]. +pub fn alignment(alignment: Alignment) -> &'static str { + match alignment { + Alignment::Start => "flex-start", + Alignment::Center => "center", + Alignment::End => "flex-end", + Alignment::Fill => "stretch", } } diff --git a/web/src/lib.rs b/web/src/lib.rs index b968c96fba..4679b457da 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -73,13 +73,18 @@ pub use css::Css; pub use dodrio; pub use element::Element; pub use hasher::Hasher; +pub use subscription::Subscription; + +pub use iced_core::alignment; +pub use iced_core::keyboard; +pub use iced_core::mouse; +pub use iced_futures::executor; +pub use iced_futures::futures; + pub use iced_core::{ - keyboard, mouse, 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}; -pub use subscription::Subscription; #[doc(no_inline)] pub use widget::*; diff --git a/web/src/widget/column.rs b/web/src/widget/column.rs index 8738c2af63..30a57c41c7 100644 --- a/web/src/widget/column.rs +++ b/web/src/widget/column.rs @@ -1,4 +1,5 @@ -use crate::{css, Align, Bus, Css, Element, Length, Padding, Widget}; +use crate::css; +use crate::{Alignment, Bus, Css, Element, Length, Padding, Widget}; use dodrio::bumpalo; use std::u32; @@ -14,7 +15,7 @@ pub struct Column<'a, Message> { height: Length, max_width: u32, max_height: u32, - align_items: Align, + align_items: Alignment, children: Vec>, } @@ -33,7 +34,7 @@ impl<'a, Message> Column<'a, Message> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: Align::Start, + align_items: Alignment::Start, children, } } @@ -79,7 +80,7 @@ impl<'a, Message> Column<'a, Message> { } /// 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: Alignment) -> Self { self.align_items = align; self } @@ -129,7 +130,7 @@ impl<'a, Message> Widget for Column<'a, Message> { css::max_length(self.max_width), css::max_length(self.max_height), css::padding(self.padding), - css::align(self.align_items) + css::alignment(self.align_items) ).into_bump_str() ) .children(children) diff --git a/web/src/widget/container.rs b/web/src/widget/container.rs index c006e01117..24aa7cefe3 100644 --- a/web/src/widget/container.rs +++ b/web/src/widget/container.rs @@ -1,5 +1,8 @@ //! Decorate content and apply alignment. -use crate::{bumpalo, css, Align, Bus, Css, Element, Length, Padding, Widget}; +use crate::alignment::{self, Alignment}; +use crate::bumpalo; +use crate::css; +use crate::{Bus, Css, Element, Length, Padding, Widget}; pub use iced_style::container::{Style, StyleSheet}; @@ -14,8 +17,8 @@ pub struct Container<'a, Message> { max_width: u32, #[allow(dead_code)] max_height: u32, - horizontal_alignment: Align, - vertical_alignment: Align, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, style_sheet: Box, content: Element<'a, Message>, } @@ -34,8 +37,8 @@ impl<'a, Message> Container<'a, Message> { 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_sheet: Default::default(), content: content.into(), } @@ -73,14 +76,14 @@ impl<'a, Message> Container<'a, Message> { /// 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 } @@ -122,8 +125,8 @@ where css::length(self.height), css::max_length(self.max_width), css::padding(self.padding), - css::align(self.horizontal_alignment), - css::align(self.vertical_alignment), + css::alignment(Alignment::from(self.horizontal_alignment)), + css::alignment(Alignment::from(self.vertical_alignment)), style.background.map(css::background).unwrap_or(String::from("initial")), style.text_color.map(css::color).unwrap_or(String::from("inherit")), style.border_width, diff --git a/web/src/widget/row.rs b/web/src/widget/row.rs index ffb515cfbc..13eab27d4d 100644 --- a/web/src/widget/row.rs +++ b/web/src/widget/row.rs @@ -1,4 +1,5 @@ -use crate::{css, Align, Bus, Css, Element, Length, Padding, Widget}; +use crate::css; +use crate::{Alignment, Bus, Css, Element, Length, Padding, Widget}; use dodrio::bumpalo; use std::u32; @@ -14,7 +15,7 @@ pub struct Row<'a, Message> { height: Length, max_width: u32, max_height: u32, - align_items: Align, + align_items: Alignment, children: Vec>, } @@ -33,7 +34,7 @@ impl<'a, Message> Row<'a, Message> { height: Length::Shrink, max_width: u32::MAX, max_height: u32::MAX, - align_items: Align::Start, + align_items: Alignment::Start, children, } } @@ -79,7 +80,7 @@ impl<'a, Message> Row<'a, Message> { } /// 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: Alignment) -> Self { self.align_items = align; self } @@ -129,7 +130,7 @@ impl<'a, Message> Widget for Row<'a, Message> { css::max_length(self.max_width), css::max_length(self.max_height), css::padding(self.padding), - css::align(self.align_items) + css::alignment(self.align_items) ).into_bump_str() ) .children(children) diff --git a/web/src/widget/scrollable.rs b/web/src/widget/scrollable.rs index ce0a10d48e..847bf5a045 100644 --- a/web/src/widget/scrollable.rs +++ b/web/src/widget/scrollable.rs @@ -1,7 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. -use crate::{ - bumpalo, css, Align, Bus, Column, Css, Element, Length, Padding, Widget, -}; +use crate::bumpalo; +use crate::css; +use crate::{Alignment, Bus, Column, Css, Element, Length, Padding, Widget}; pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; @@ -72,7 +72,7 @@ impl<'a, Message> Scrollable<'a, Message> { } /// 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: Alignment) -> Self { self.content = self.content.align_items(align_items); self } diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index 72232dc053..53d57bfdaa 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -1,7 +1,6 @@ -use crate::{ - css, Bus, Color, Css, Element, Font, HorizontalAlignment, Length, - VerticalAlignment, Widget, -}; +use crate::alignment; +use crate::css; +use crate::{Bus, Color, Css, Element, Font, Length, Widget}; use dodrio::bumpalo; /// A paragraph of text. @@ -22,8 +21,8 @@ pub struct Text { font: Font, width: Length, height: Length, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, } impl Text { @@ -36,8 +35,8 @@ impl Text { font: Font::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, } } @@ -74,14 +73,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 } @@ -111,9 +113,9 @@ impl<'a, Message> Widget for Text { let height = css::length(self.height); let text_align = match self.horizontal_alignment { - HorizontalAlignment::Left => "left", - HorizontalAlignment::Center => "center", - HorizontalAlignment::Right => "right", + alignment::Horizontal::Left => "left", + alignment::Horizontal::Center => "center", + alignment::Horizontal::Right => "right", }; let style = bumpalo::format!( diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 51429e8483..73fa5c5f4f 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -7,8 +7,9 @@ use iced_graphics::backend; use iced_graphics::font; use iced_graphics::layer::Layer; use iced_graphics::{Primitive, Viewport}; +use iced_native::alignment; use iced_native::mouse; -use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment}; +use iced_native::{Font, Size}; #[cfg(any(feature = "image_rs", feature = "svg"))] use crate::image; @@ -207,24 +208,24 @@ impl Backend { }], layout: wgpu_glyph::Layout::default() .h_align(match text.horizontal_alignment { - HorizontalAlignment::Left => { + alignment::Horizontal::Left => { wgpu_glyph::HorizontalAlign::Left } - HorizontalAlignment::Center => { + alignment::Horizontal::Center => { wgpu_glyph::HorizontalAlign::Center } - HorizontalAlignment::Right => { + alignment::Horizontal::Right => { wgpu_glyph::HorizontalAlign::Right } }) .v_align(match text.vertical_alignment { - VerticalAlignment::Top => { + alignment::Vertical::Top => { wgpu_glyph::VerticalAlign::Top } - VerticalAlignment::Center => { + alignment::Vertical::Center => { wgpu_glyph::VerticalAlign::Center } - VerticalAlignment::Bottom => { + alignment::Vertical::Bottom => { wgpu_glyph::VerticalAlign::Bottom } }),