Skip to content

Commit

Permalink
Refactor alignment types into an alignment module
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Sep 20, 2021
1 parent 5fae6e5 commit a0ad399
Show file tree
Hide file tree
Showing 54 changed files with 401 additions and 376 deletions.
64 changes: 0 additions & 64 deletions core/src/align.rs

This file was deleted.

63 changes: 63 additions & 0 deletions core/src/alignment.rs
Original file line number Diff line number Diff line change
@@ -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<Horizontal> for Alignment {
fn from(horizontal: Horizontal) -> Self {
match horizontal {
Horizontal::Left => Self::Start,
Horizontal::Center => Self::Center,
Horizontal::Right => Self::End,
}
}
}

impl From<Vertical> 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,
}
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions examples/color_palette/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -298,7 +297,7 @@ impl<C: 'static + ColorSpace + Copy> ColorPicker<C> {

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)))
Expand Down
4 changes: 2 additions & 2 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Sandbox for Counter {
fn view(&mut self) -> Element<Message> {
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),
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_widget/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions examples/download_progress/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions examples/events/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
};

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions examples/geometry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 \
Expand Down
Loading

0 comments on commit a0ad399

Please sign in to comment.