summaryrefslogtreecommitdiff
path: root/src/geom.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-05 16:34:20 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-05 16:44:35 +0200
commit5a7a32a9bafbcc69077e7766451310ab5ece62bf (patch)
tree29d5fc9ec380c2bd1b63bff1fd605408e8f53eea /src/geom.rs
parent93eaafb236bba73cd72c8cd142279366b8afa0be (diff)
Streamline generic layouting primitives 🛫
Diffstat (limited to 'src/geom.rs')
-rw-r--r--src/geom.rs128
1 files changed, 36 insertions, 92 deletions
diff --git a/src/geom.rs b/src/geom.rs
index 0a65a090..d38d6fe7 100644
--- a/src/geom.rs
+++ b/src/geom.rs
@@ -12,17 +12,11 @@ use crate::layout::primitive::{Dir, GenAlign, LayoutAlign, LayoutSystem, SpecAxi
///
/// [sizes]: ../../kurbo/struct.Size.html
pub trait SizeExt {
- /// Return the primary component of this specialized size.
- fn primary(self, sys: LayoutSystem) -> f64;
+ /// Return the component for the specified axis.
+ fn get(self, axis: SpecAxis) -> f64;
- /// Borrow the primary component of this specialized size mutably.
- fn primary_mut(&mut self, sys: LayoutSystem) -> &mut f64;
-
- /// Return the secondary component of this specialized size.
- fn secondary(self, sys: LayoutSystem) -> f64;
-
- /// Borrow the secondary component of this specialized size mutably.
- fn secondary_mut(&mut self, sys: LayoutSystem) -> &mut f64;
+ /// Borrow the component for the specified axis mutably.
+ fn get_mut(&mut self, axis: SpecAxis) -> &mut f64;
/// Returns the generalized version of a `Size` based on the layouting
/// system, that is:
@@ -38,44 +32,26 @@ pub trait SizeExt {
/// values are smaller or equal.
fn fits(self, other: Self) -> bool;
- /// The anchor position along the given axis for an item with the given
- /// alignment in a container with this size.
+ /// The anchor position for an object to be aligned according to `align` in
+ /// a container with this size.
///
- /// This assumes the size to be generalized such that `x` corresponds to the
- /// primary axis.
+ /// This assumes the size to be generalized such that `width` corresponds to
+ /// the primary and `height` to the secondary axis.
fn anchor(self, align: LayoutAlign, sys: LayoutSystem) -> Point;
}
impl SizeExt for Size {
- fn primary(self, sys: LayoutSystem) -> f64 {
- if sys.primary.axis() == SpecAxis::Horizontal {
- self.width
- } else {
- self.height
- }
- }
-
- fn primary_mut(&mut self, sys: LayoutSystem) -> &mut f64 {
- if sys.primary.axis() == SpecAxis::Horizontal {
- &mut self.width
- } else {
- &mut self.height
- }
- }
-
- fn secondary(self, sys: LayoutSystem) -> f64 {
- if sys.primary.axis() == SpecAxis::Horizontal {
- self.height
- } else {
- self.width
+ fn get(self, axis: SpecAxis) -> f64 {
+ match axis {
+ SpecAxis::Horizontal => self.width,
+ SpecAxis::Vertical => self.height,
}
}
- fn secondary_mut(&mut self, sys: LayoutSystem) -> &mut f64 {
- if sys.primary.axis() == SpecAxis::Horizontal {
- &mut self.height
- } else {
- &mut self.width
+ fn get_mut(&mut self, axis: SpecAxis) -> &mut f64 {
+ match axis {
+ SpecAxis::Horizontal => &mut self.width,
+ SpecAxis::Vertical => &mut self.height,
}
}
@@ -87,8 +63,8 @@ impl SizeExt for Size {
}
fn specialized(self, sys: LayoutSystem) -> Self {
- // In fact, generalized is its own inverse. For reasons of clarity
- // at the call site, we still have this second function.
+ // Even though generalized is its own inverse, we still have this second
+ // function, for clarity at the call-site.
self.generalized(sys)
}
@@ -97,7 +73,7 @@ impl SizeExt for Size {
}
fn anchor(self, align: LayoutAlign, sys: LayoutSystem) -> Point {
- fn length_anchor(length: f64, align: GenAlign, dir: Dir) -> f64 {
+ fn anchor(length: f64, align: GenAlign, dir: Dir) -> f64 {
match (dir.is_positive(), align) {
(true, GenAlign::Start) | (false, GenAlign::End) => 0.0,
(_, GenAlign::Center) => length / 2.0,
@@ -106,8 +82,8 @@ impl SizeExt for Size {
}
Point::new(
- length_anchor(self.width, align.primary, sys.primary),
- length_anchor(self.height, align.secondary, sys.secondary),
+ anchor(self.width, align.primary, sys.primary),
+ anchor(self.height, align.secondary, sys.secondary),
)
}
}
@@ -116,7 +92,12 @@ impl SizeExt for Size {
///
/// [rectangles]: ../../kurbo/struct.Rect.html
pub trait RectExt {
- /// Get a mutable reference to the value for the specified direction at the
+ /// Return the side identified by direction and alignment.
+ ///
+ /// Center alignment is treated the same as origin alignment.
+ fn get(&mut self, dir: Dir, align: GenAlign) -> f64;
+
+ /// Get a mutable reference to the side identified by direction and
/// alignment.
///
/// Center alignment is treated the same as origin alignment.
@@ -124,6 +105,15 @@ pub trait RectExt {
}
impl RectExt for Rect {
+ fn get(&mut self, dir: Dir, align: GenAlign) -> f64 {
+ match if align == GenAlign::End { dir.inv() } else { dir } {
+ Dir::LTR => self.x0,
+ Dir::TTB => self.y0,
+ Dir::RTL => self.x1,
+ Dir::BTT => self.y1,
+ }
+ }
+
fn get_mut(&mut self, dir: Dir, align: GenAlign) -> &mut f64 {
match if align == GenAlign::End { dir.inv() } else { dir } {
Dir::LTR => &mut self.x0,
@@ -134,52 +124,6 @@ impl RectExt for Rect {
}
}
-/// A generic container for `[left, top, right, bottom]` values.
-#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
-pub struct Sides<T> {
- /// The value for the left side.
- pub left: T,
- /// The value for the top side.
- pub top: T,
- /// The value for the right side.
- pub right: T,
- /// The value for the bottom side.
- pub bottom: T,
-}
-
-impl<T> Sides<T> {
- /// Create a new box from four sizes.
- pub fn new(left: T, top: T, right: T, bottom: T) -> Self {
- Self { left, top, right, bottom }
- }
-
- /// Create an instance with all four components set to the same `value`.
- pub fn uniform(value: T) -> Self
- where
- T: Clone,
- {
- Self {
- left: value.clone(),
- top: value.clone(),
- right: value.clone(),
- bottom: value,
- }
- }
-
- /// Get a mutable reference to the value for the specified direction at the
- /// alignment.
- ///
- /// Center alignment is treated the same as origin alignment.
- pub fn get_mut(&mut self, dir: Dir, align: GenAlign) -> &mut T {
- match if align == GenAlign::End { dir.inv() } else { dir } {
- Dir::LTR => &mut self.left,
- Dir::RTL => &mut self.right,
- Dir::TTB => &mut self.top,
- Dir::BTT => &mut self.bottom,
- }
- }
-}
-
/// A function that depends linearly on one value.
///
/// This represents a function `f(x) = rel * x + abs`.