tessera_ui_basic_components/
shape_def.rs

1//! Defines the [`Shape`] enum and its variants, used for describing the geometric form of UI components.
2//!
3//! This module provides a flexible way to define very basic components' shape, including
4//! [`crate::surface::surface`], [`crate::fluid_glass::fluid_glass`].
5
6use tessera_ui::dp::Dp;
7
8/// Shape definitions for UI components
9///
10/// `Shape` is used by multiple components (`surface`, `fluid_glass`, sliders, progress, buttons)
11/// to define:
12///
13/// * Visual outline (fill / border / highlight pipelines)
14/// * Interaction & ripple hit-testing region
15/// * Automatic corner radius derivation for capsule variants
16///
17/// # Variants
18/// * [`Shape::RoundedRectangle`] – Independent corner radii + `g2_k_value` curvature control
19/// * [`Shape::Ellipse`] – Ellipse filling the component bounds
20/// * [`Shape::HorizontalCapsule`] – Pill where corner radius = height / 2 (resolved at render)
21/// * [`Shape::VerticalCapsule`] – Pill where corner radius = width / 2 (resolved at render)
22///
23/// Capsule variants are convenience markers; they are internally converted into a rounded rectangle
24/// whose four radii equal half of the minor axis (height for horizontal, width for vertical).
25///
26/// # Example
27///
28/// ```
29/// use tessera_ui::dp::Dp;
30/// use tessera_ui_basic_components::shape_def::Shape;
31///
32/// // Explicit rounded rectangle
33/// let rr = Shape::RoundedRectangle {
34///     top_left: Dp(8.0),
35///     top_right: Dp(8.0),
36///     bottom_right: Dp(8.0),
37///     bottom_left: Dp(8.0),
38///     g2_k_value: 3.0,
39/// };
40///
41/// // Ellipse
42/// let ellipse = Shape::Ellipse;
43///
44/// // Capsules (auto radius from minor axis)
45/// let h_capsule = Shape::HorizontalCapsule;
46/// let v_capsule = Shape::VerticalCapsule;
47/// ```
48#[derive(Clone, Copy, Debug, PartialEq)]
49pub enum Shape {
50    /// Rounded rectangle with independent corner radii and a curvature factor:
51    /// * `g2_k_value` controls the transition curve (G2 continuity parameter).
52    RoundedRectangle {
53        top_left: Dp,
54        top_right: Dp,
55        bottom_right: Dp,
56        bottom_left: Dp,
57        g2_k_value: f32,
58    },
59    /// Ellipse fitting the component bounds.
60    Ellipse,
61    /// Horizontal capsule (pill) – rendered as a rounded rectangle whose corner radius
62    /// is computed as `height / 2.0` at draw time.
63    HorizontalCapsule,
64    /// Vertical capsule (pill) – rendered as a rounded rectangle whose corner radius
65    /// is computed as `width / 2.0` at draw time.
66    VerticalCapsule,
67}
68
69impl Default for Shape {
70    /// Returns the default shape, which is a rectangle with zero corner radius.
71    ///
72    /// # Example
73    ///
74    /// ```
75    /// use tessera_ui::dp::Dp;
76    /// use tessera_ui_basic_components::shape_def::Shape;
77    /// let default_shape = Shape::default();
78    /// assert_eq!(default_shape, Shape::RoundedRectangle { top_left: Dp(0.0), top_right: Dp(0.0), bottom_right: Dp(0.0), bottom_left: Dp(0.0), g2_k_value: 3.0 });
79    /// ```
80    fn default() -> Self {
81        Shape::RoundedRectangle {
82            top_left: Dp(0.0),
83            top_right: Dp(0.0),
84            bottom_right: Dp(0.0),
85            bottom_left: Dp(0.0),
86            g2_k_value: 3.0,
87        }
88    }
89}
90
91impl Shape {
92    /// A pure rectangle shape with no rounded corners.
93    pub const RECTANGLE: Self = Shape::RoundedRectangle {
94        top_left: Dp(0.0),
95        top_right: Dp(0.0),
96        bottom_right: Dp(0.0),
97        bottom_left: Dp(0.0),
98        g2_k_value: 3.0,
99    };
100
101    /// A Quick helper to create a uniform rounded rectangle shape.
102    ///
103    /// # Example
104    ///
105    /// ```
106    /// use tessera_ui::dp::Dp;
107    /// use tessera_ui_basic_components::shape_def::Shape;
108    /// let shape = Shape::rounded_rectangle(Dp(8.0));
109    /// assert_eq!(shape, Shape::RoundedRectangle { top_left: Dp(8.0), top_right: Dp(8.0), bottom_right: Dp(8.0), bottom_left: Dp(8.0), g2_k_value: 3.0 });
110    /// ```
111    pub const fn rounded_rectangle(radius: Dp) -> Self {
112        Shape::RoundedRectangle {
113            top_left: radius,
114            top_right: radius,
115            bottom_right: radius,
116            bottom_left: radius,
117            g2_k_value: 3.0,
118        }
119    }
120}