gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 497HTML 254C 19JavaScript 81329 Sutichaykuna

⎇ 059be099f10c50035a15cb5220f6de70f2027e6f src/geometry.rs Rust · 136 siq'ikuna · 4.2 KB Ñawpa willay Pi imata llamkarqan ch'uya

cargo fmt 059be099f · Emil Sjölander ·

1use std::ops::Add;
2
3use crate::number::Number;
4use crate::style;
5
6#[derive(Debug, Copy, Clone, PartialEq)]
7pub struct Rect<T> {
8    pub start: T,
9    pub end: T,
10    pub top: T,
11    pub bottom: T,
12}
13
14impl<T> Rect<T> {
15    pub(crate) fn map<R, F>(self, f: F) -> Rect<R>
16    where
17        F: Fn(T) -> R,
18    {
19        Rect { start: f(self.start), end: f(self.end), top: f(self.top), bottom: f(self.bottom) }
20    }
21}
22
23impl<T> Rect<T>
24where
25    T: Add<Output = T> + Copy + Clone,
26{
27    pub(crate) fn horizontal(&self) -> T {
28        self.start + self.end
29    }
30
31    pub(crate) fn vertical(&self) -> T {
32        self.top + self.bottom
33    }
34
35    pub(crate) fn main(&self, direction: style::FlexDirection) -> T {
36        match direction {
37            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start + self.end,
38            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top + self.bottom,
39        }
40    }
41
42    pub(crate) fn cross(&self, direction: style::FlexDirection) -> T {
43        match direction {
44            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top + self.bottom,
45            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start + self.end,
46        }
47    }
48}
49
50impl<T> Rect<T>
51where
52    T: Copy + Clone,
53{
54    pub(crate) fn main_start(&self, direction: style::FlexDirection) -> T {
55        match direction {
56            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start,
57            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top,
58        }
59    }
60
61    pub(crate) fn main_end(&self, direction: style::FlexDirection) -> T {
62        match direction {
63            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.end,
64            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.bottom,
65        }
66    }
67
68    pub(crate) fn cross_start(&self, direction: style::FlexDirection) -> T {
69        match direction {
70            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top,
71            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start,
72        }
73    }
74
75    pub(crate) fn cross_end(&self, direction: style::FlexDirection) -> T {
76        match direction {
77            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.bottom,
78            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.end,
79        }
80    }
81}
82
83#[derive(Debug, Copy, Clone, PartialEq)]
84pub struct Size<T> {
85    pub width: T,
86    pub height: T,
87}
88
89impl Size<()> {
90    pub fn undefined() -> Size<Number> {
91        Size { width: Number::Undefined, height: Number::Undefined }
92    }
93}
94
95impl<T> Size<T> {
96    pub(crate) fn map<R, F>(self, f: F) -> Size<R>
97    where
98        F: Fn(T) -> R,
99    {
100        Size { width: f(self.width), height: f(self.height) }
101    }
102
103    pub(crate) fn set_main(&mut self, direction: style::FlexDirection, value: T) {
104        match direction {
105            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.width = value,
106            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.height = value,
107        }
108    }
109
110    pub(crate) fn set_cross(&mut self, direction: style::FlexDirection, value: T) {
111        match direction {
112            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.height = value,
113            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.width = value,
114        }
115    }
116
117    pub(crate) fn main(self, direction: style::FlexDirection) -> T {
118        match direction {
119            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.width,
120            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.height,
121        }
122    }
123
124    pub(crate) fn cross(self, direction: style::FlexDirection) -> T {
125        match direction {
126            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.height,
127            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.width,
128        }
129    }
130}
131
132#[derive(Debug, Copy, Clone, PartialEq)]
133pub struct Point<T> {
134    pub x: T,
135    pub y: T,
136}