summaryrefslogtreecommitdiff
path: root/src/geom/align.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-17 17:09:19 +0100
committerLaurenz <laurmaedje@gmail.com>2021-11-17 17:09:19 +0100
commit89f2e71852e96062ea9b756bf92fbf4e894871b1 (patch)
tree836099ebd17adf30a24fc62464dfdd3d9c248480 /src/geom/align.rs
parent9a800daa82833c57eee04e92c701ca9a05a67d3b (diff)
Align node
Diffstat (limited to 'src/geom/align.rs')
-rw-r--r--src/geom/align.rs64
1 files changed, 20 insertions, 44 deletions
diff --git a/src/geom/align.rs b/src/geom/align.rs
index 59960084..b0cf69db 100644
--- a/src/geom/align.rs
+++ b/src/geom/align.rs
@@ -3,18 +3,14 @@ use super::*;
/// Where to align something along an axis.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Align {
- /// Align at the start of the axis.
- Start,
- /// Align in the middle of the axis.
- Center,
- /// Align at the end of the axis.
- End,
/// Align at the left side of the axis.
Left,
- /// Align at the right side of the axis.
- Right,
/// Align at the top side of the axis.
Top,
+ /// Align in the middle of the axis.
+ Center,
+ /// Align at the right side of the axis.
+ Right,
/// Align at the bottom side of the axis.
Bottom,
}
@@ -23,12 +19,10 @@ impl Align {
/// The axis this alignment belongs to if it is specific.
pub fn axis(self) -> Option<SpecAxis> {
match self {
- Self::Start => None,
- Self::Center => None,
- Self::End => None,
Self::Left => Some(SpecAxis::Horizontal),
- Self::Right => Some(SpecAxis::Horizontal),
Self::Top => Some(SpecAxis::Vertical),
+ Self::Center => None,
+ Self::Right => Some(SpecAxis::Horizontal),
Self::Bottom => Some(SpecAxis::Vertical),
}
}
@@ -36,60 +30,42 @@ impl Align {
/// The inverse alignment.
pub fn inv(self) -> Self {
match self {
- Self::Start => Self::End,
- Self::Center => Self::Center,
- Self::End => Self::Start,
Self::Left => Self::Right,
- Self::Right => Self::Left,
Self::Top => Self::Bottom,
+ Self::Center => Self::Center,
+ Self::Right => Self::Left,
Self::Bottom => Self::Top,
}
}
/// Returns the position of this alignment in the given range.
- pub fn resolve(self, dir: Dir, range: Range<Length>) -> Length {
- #[cfg(debug_assertions)]
- if let Some(axis) = self.axis() {
- debug_assert_eq!(axis, dir.axis())
- }
-
+ pub fn resolve(self, range: Range<Length>) -> Length {
match self {
- Self::Start => {
- if dir.is_positive() {
- range.start
- } else {
- range.end
- }
- }
- Self::Center => (range.start + range.end) / 2.0,
- Self::End => {
- if dir.is_positive() {
- range.end
- } else {
- range.start
- }
- }
Self::Left | Self::Top => range.start,
Self::Right | Self::Bottom => range.end,
+ Self::Center => (range.start + range.end) / 2.0,
}
}
}
-impl Default for Align {
- fn default() -> Self {
- Self::Start
+impl From<Side> for Align {
+ fn from(side: Side) -> Self {
+ match side {
+ Side::Left => Self::Left,
+ Side::Top => Self::Top,
+ Side::Right => Self::Right,
+ Side::Bottom => Self::Bottom,
+ }
}
}
impl Debug for Align {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
- Self::Start => "start",
- Self::Center => "center",
- Self::End => "end",
Self::Left => "left",
- Self::Right => "right",
Self::Top => "top",
+ Self::Center => "center",
+ Self::Right => "right",
Self::Bottom => "bottom",
})
}