taitank/
flex.rs

1// Flex 类型和枚举定义
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum TaitankDirection {
5    Inherit,
6    Ltr,
7    Rtl,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum FlexDirection {
12    Row,
13    RowReverse,
14    Column,
15    ColumnReverse,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum FlexWrapMode {
20    NoWrap,
21    Wrap,
22    WrapReverse,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum FlexAlign {
27    Auto,
28    Start,
29    Center,
30    End,
31    Stretch,
32    BaseLine,
33    SpaceBetween,
34    SpaceAround,
35    SpaceEvenly,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum CSSDirection {
40    Left = 0,
41    Top = 1,
42    Right = 2,
43    Bottom = 3,
44    Start = 4,
45    End = 5,
46    Horizontal = 6,
47    Vertical = 7,
48    All = 8,
49    None = -1,
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum Dimension {
54    Width = 0,
55    Height = 1,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum PositionType {
60    Relative,
61    Absolute,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum DisplayType {
66    Flex,
67    None,
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum OverflowType {
72    Visible,
73    Hidden,
74    Scroll,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub enum NodeType {
79    Default,
80    Text,
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum FlexLayoutAction {
85    MeasureWidth = 1,
86    MeasureHeight = 2,
87    Layout = 3,
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub enum MeasureMode {
92    Undefined,
93    Exactly,
94    AtMost,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum FlexSign {
99    PositiveFlexibility,
100    NegativeFlexibility,
101}
102
103#[derive(Debug, Clone, Copy)]
104pub struct TaitankSize {
105    pub width: f32,
106    pub height: f32,
107}
108
109impl TaitankSize {
110    pub fn new(width: f32, height: f32) -> Self {
111        Self { width, height }
112    }
113}
114
115#[derive(Debug, Clone, Copy)]
116pub struct TaitankSizeMode {
117    pub width_measure_mode: MeasureMode,
118    pub height_measure_mode: MeasureMode,
119}
120
121#[derive(Debug, Clone)]
122pub struct TaitankLayout {
123    pub position: [f32; 4], // [left, top, right, bottom]
124    pub cached_position: [f32; 4],
125    pub dim: [f32; 2], // [width, height]
126    pub margin: [f32; 4],
127    pub padding: [f32; 4],
128    pub border: [f32; 4],
129    pub had_overflow: bool,
130    pub direction: TaitankDirection,
131    pub flex_base_size: f32,
132    pub hypothetical_main_axis_margin_boxsize: f32,
133    pub hypothetical_main_axis_size: f32,
134}
135
136impl Default for TaitankLayout {
137    fn default() -> Self {
138        use crate::util::VALUE_UNDEFINED;
139        Self {
140            position: [0.0; 4],
141            cached_position: [0.0; 4],
142            dim: [VALUE_UNDEFINED; 2],
143            margin: [VALUE_UNDEFINED; 4],
144            padding: [0.0; 4],
145            border: [0.0; 4],
146            had_overflow: false,
147            direction: TaitankDirection::Inherit,
148            flex_base_size: 0.0,
149            hypothetical_main_axis_margin_boxsize: 0.0,
150            hypothetical_main_axis_size: 0.0,
151        }
152    }
153}
154
155// Axis mapping arrays
156pub const K_AXIS_START: [CSSDirection; 4] = [
157    CSSDirection::Left,   // ROW
158    CSSDirection::Right,  // ROW_REVERSE
159    CSSDirection::Top,    // COLUMN
160    CSSDirection::Bottom, // COLUMN_REVERSE
161];
162
163pub const K_AXIS_END: [CSSDirection; 4] = [
164    CSSDirection::Right,  // ROW
165    CSSDirection::Left,   // ROW_REVERSE
166    CSSDirection::Bottom, // COLUMN
167    CSSDirection::Top,    // COLUMN_REVERSE
168];
169
170pub const K_AXIS_DIM: [Dimension; 4] = [
171    Dimension::Width,  // ROW
172    Dimension::Width,  // ROW_REVERSE
173    Dimension::Height, // COLUMN
174    Dimension::Height, // COLUMN_REVERSE
175];
176
177pub fn is_row_direction(dir: FlexDirection) -> bool {
178    matches!(dir, FlexDirection::Row | FlexDirection::RowReverse)
179}
180
181pub fn is_column_direction(dir: FlexDirection) -> bool {
182    matches!(dir, FlexDirection::Column | FlexDirection::ColumnReverse)
183}
184
185pub fn is_reverse_direction(dir: FlexDirection) -> bool {
186    matches!(
187        dir,
188        FlexDirection::ColumnReverse | FlexDirection::RowReverse
189    )
190}