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