summaryrefslogtreecommitdiff
path: root/src/geom/align.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-10 22:19:36 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-10 22:19:36 +0200
commit92c01da36016e94ff20163806ddcbcf7e33d4031 (patch)
tree1a900b3c11edcc93e9153fada3ce92310db5768b /src/geom/align.rs
parent42500d5ed85539c5ab04dd3544beaf802da29be9 (diff)
Switch back to custom geometry types, unified with layout primitives 🏞
Diffstat (limited to 'src/geom/align.rs')
-rw-r--r--src/geom/align.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/geom/align.rs b/src/geom/align.rs
new file mode 100644
index 00000000..1030a133
--- /dev/null
+++ b/src/geom/align.rs
@@ -0,0 +1,48 @@
+use super::*;
+
+/// Where to align something along a directed axis.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
+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,
+}
+
+impl Align {
+ /// Returns the position of this alignment in the given range.
+ pub fn apply(self, range: Range<Length>) -> Length {
+ match self {
+ Self::Start => range.start,
+ Self::Center => (range.start + range.end) / 2.0,
+ Self::End => range.end,
+ }
+ }
+
+ /// The inverse alignment.
+ pub fn inv(self) -> Self {
+ match self {
+ Self::Start => Self::End,
+ Self::Center => Self::Center,
+ Self::End => Self::Start,
+ }
+ }
+}
+
+impl Default for Align {
+ fn default() -> Self {
+ Self::Start
+ }
+}
+
+impl Display for Align {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.pad(match self {
+ Self::Start => "start",
+ Self::Center => "center",
+ Self::End => "end",
+ })
+ }
+}