tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Sutichaykuna

⎇ 32973e7ca8635aa870a77f52d0d0070381ae5f8c src/geometry.rs Rust · 99 siq'ikuna · 3.0 KB Ñawpa willay Pi imata llamkarqan ch'uya

Factor out some logic into geometry module 32973e7ca · Emil Sjölander ·

1use std::ops::Add;
2
3use crate::style;
4
5#[derive(Debug, Copy, Clone)]
6pub struct Rect<T> {
7    pub start: T,
8    pub end: T,
9    pub top: T,
10    pub bottom: T,
11}
12
13impl<T> Rect<T>
14where
15    T: Add<Output = T> + Copy + Clone,
16{
17    pub(crate) fn horizontal(&self) -> T {
18        self.start + self.end
19    }
20
21    pub(crate) fn vertical(&self) -> T {
22        self.top + self.bottom
23    }
24
25    pub(crate) fn main(&self, direction: style::FlexDirection) -> T {
26        match direction {
27            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start + self.end,
28            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top + self.bottom,
29        }
30    }
31
32    pub(crate) fn cross(&self, direction: style::FlexDirection) -> T {
33        match direction {
34            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top + self.bottom,
35            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start + self.end,
36        }
37    }
38}
39
40impl<T> Rect<T>
41where
42    T: Copy + Clone,
43{
44    pub(crate) fn main_start(&self, direction: style::FlexDirection) -> T {
45        match direction {
46            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start,
47            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top,
48        }
49    }
50
51    pub(crate) fn main_end(&self, direction: style::FlexDirection) -> T {
52        match direction {
53            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.end,
54            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.bottom,
55        }
56    }
57
58    pub(crate) fn cross_start(&self, direction: style::FlexDirection) -> T {
59        match direction {
60            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top,
61            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start,
62        }
63    }
64
65    pub(crate) fn cross_end(&self, direction: style::FlexDirection) -> T {
66        match direction {
67            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.bottom,
68            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.end,
69        }
70    }
71}
72
73#[derive(Debug, Copy, Clone)]
74pub struct Size<T> {
75    pub width: T,
76    pub height: T,
77}
78
79impl<T> Size<T> {
80    pub(crate) fn main(self, direction: style::FlexDirection) -> T {
81        match direction {
82            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.width,
83            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.height,
84        }
85    }
86
87    pub(crate) fn cross(self, direction: style::FlexDirection) -> T {
88        match direction {
89            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.height,
90            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.width,
91        }
92    }
93}
94
95#[derive(Debug, Copy, Clone)]
96pub struct Point<T> {
97    pub x: T,
98    pub y: T,
99}