Skip to main content

azul_layout/solver3/
geometry.rs

1//! TODO: Move these to CSS module
2
3use azul_core::{
4    geom::{LogicalPosition, LogicalRect, LogicalSize},
5    ui_solver::ResolvedOffsets,
6};
7use azul_css::props::layout::LayoutWritingMode;
8
9/// Represents the CSS `box-sizing` property.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum BoxSizing {
12    #[default]
13    ContentBox,
14    BorderBox,
15}
16
17#[derive(Debug, Clone, PartialEq, PartialOrd)]
18pub struct PositionedRectangle {
19    /// The outer bounds of the rectangle
20    pub bounds: LogicalRect,
21    /// Margin of the rectangle.
22    pub margin: ResolvedOffsets,
23    /// Border widths of the rectangle.
24    pub border: ResolvedOffsets,
25    /// Padding of the rectangle.
26    pub padding: ResolvedOffsets,
27}
28
29/// Represents the four edges of a box for properties like margin, padding, border.
30#[derive(Debug, Clone, Copy, Default)]
31pub struct EdgeSizes {
32    pub top: f32,
33    pub right: f32,
34    pub bottom: f32,
35    pub left: f32,
36}
37
38impl EdgeSizes {
39    /// Returns the size of the edge at the start of the main/block axis.
40    pub fn main_start(&self, wm: LayoutWritingMode) -> f32 {
41        match wm {
42            LayoutWritingMode::HorizontalTb => self.top,
43            LayoutWritingMode::VerticalRl | LayoutWritingMode::VerticalLr => self.left,
44        }
45    }
46
47    /// Returns the size of the edge at the end of the main/block axis.
48    pub fn main_end(&self, wm: LayoutWritingMode) -> f32 {
49        match wm {
50            LayoutWritingMode::HorizontalTb => self.bottom,
51            LayoutWritingMode::VerticalRl | LayoutWritingMode::VerticalLr => self.right,
52        }
53    }
54
55    /// Returns the sum of the start and end sizes on the main/block axis.
56    pub fn main_sum(&self, wm: LayoutWritingMode) -> f32 {
57        self.main_start(wm) + self.main_end(wm)
58    }
59
60    /// Returns the size of the edge at the start of the cross/inline axis.
61    pub fn cross_start(&self, wm: LayoutWritingMode) -> f32 {
62        match wm {
63            LayoutWritingMode::HorizontalTb => self.left,
64            LayoutWritingMode::VerticalRl | LayoutWritingMode::VerticalLr => self.top,
65        }
66    }
67
68    /// Returns the size of the edge at the end of the cross/inline axis.
69    pub fn cross_end(&self, wm: LayoutWritingMode) -> f32 {
70        match wm {
71            LayoutWritingMode::HorizontalTb => self.right,
72            LayoutWritingMode::VerticalRl | LayoutWritingMode::VerticalLr => self.bottom,
73        }
74    }
75
76    /// Returns the sum of the start and end sizes on the cross/inline axis.
77    pub fn cross_sum(&self, wm: LayoutWritingMode) -> f32 {
78        self.cross_start(wm) + self.cross_end(wm)
79    }
80}
81
82/// A fully resolved representation of a a node's box model properties.
83#[derive(Debug, Clone, Copy, Default)]
84pub struct BoxProps {
85    pub margin: EdgeSizes,
86    pub padding: EdgeSizes,
87    pub border: EdgeSizes,
88}
89
90impl BoxProps {
91    /// Calculates the inner content-box size from an outer border-box size,
92    /// correctly accounting for the specified writing mode.
93    pub fn inner_size(&self, outer_size: LogicalSize, wm: LayoutWritingMode) -> LogicalSize {
94        let outer_main = outer_size.main(wm);
95        let outer_cross = outer_size.cross(wm);
96
97        // The sum of padding and border along the cross (inline) axis.
98        let cross_axis_spacing = self.padding.cross_sum(wm) + self.border.cross_sum(wm);
99
100        // The sum of padding and border along the main (block) axis.
101        let main_axis_spacing = self.padding.main_sum(wm) + self.border.main_sum(wm);
102
103        let inner_main = (outer_main - main_axis_spacing).max(0.0);
104        let inner_cross = (outer_cross - cross_axis_spacing).max(0.0);
105
106        LogicalSize::from_main_cross(inner_main, inner_cross, wm)
107    }
108}
109
110// Verwende die Typen aus azul_css für float und clear
111pub use azul_css::props::layout::{LayoutClear, LayoutFloat};
112
113/// Represents the intrinsic sizing information for an element, calculated
114/// without knowledge of the final containing block size.
115#[derive(Debug, Clone, Copy, Default)]
116pub struct IntrinsicSizes {
117    /// The narrowest possible width, e.g., the width of the longest word.
118    pub min_content_width: f32,
119    /// The preferred width if infinite horizontal space is available.
120    pub max_content_width: f32,
121    /// The width specified by CSS properties, if any.
122    pub preferred_width: Option<f32>,
123    /// The height of the element at its `min_content_width`.
124    pub min_content_height: f32,
125    /// The height of the element at its `max_content_width`.
126    pub max_content_height: f32,
127    /// The height specified by CSS properties, if any.
128    pub preferred_height: Option<f32>,
129}
130
131impl IntrinsicSizes {
132    /// Creates a zero-sized IntrinsicSizes.
133    pub fn zero() -> Self {
134        Self::default()
135    }
136}