Skip to main content

waft_material/compose/
pane.rs

1//! One pane's content, the size it asks for, its margins and its shape.
2
3use waft_embedder::{Rect, Size, TextDirection};
4use waft_painting::{EdgeInsetsGeometry, ShapeBorderRef};
5use waft_widgets::{IntoWidget, WidgetRef};
6
7/// The size a pane asks for.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub enum PanePreferredSize {
10    /// A fixed size in logical pixels.
11    Fixed(f64),
12    /// A fraction of the scaffold, from 0 to 1.
13    Proportion(f64),
14}
15
16impl PanePreferredSize {
17    pub(crate) fn resolve(self, available: f64) -> f64 {
18        match self {
19            PanePreferredSize::Fixed(size) => size,
20            PanePreferredSize::Proportion(proportion) => available * proportion,
21        }
22    }
23}
24
25/// One pane's content, the size it asks for, its margins and its shape.
26#[derive(Clone, Debug)]
27pub struct Pane {
28    /// The pane's content.
29    pub child: WidgetRef,
30    /// The width the pane asks for; `None` uses the directive's default.
31    pub preferred_width: Option<PanePreferredSize>,
32    /// The height a reflowed pane asks for; `None` uses the directive's default.
33    pub preferred_height: Option<PanePreferredSize>,
34    /// Compose's `paneMargins(fixedMargins)`: how far the pane keeps from each edge of the
35    /// scaffold. A margin only moves an edge the pane shares with the scaffold; the gap between
36    /// two panes is the directive's spacer.
37    pub margins: EdgeInsetsGeometry,
38    /// Compose's `AnimatedPane(shape:)`: the shape the pane is clipped to; `None` is Compose's
39    /// `RectangleShape`.
40    pub shape: Option<ShapeBorderRef>,
41}
42
43impl Pane {
44    /// Creates a pane around `child`.
45    #[cfg_attr(debug_assertions, track_caller)]
46    pub fn new<K>(child: impl IntoWidget<K>) -> Pane {
47        Pane {
48            child: child.into_widget(),
49            preferred_width: None,
50            preferred_height: None,
51            margins: EdgeInsetsGeometry::ZERO,
52            shape: None,
53        }
54    }
55
56    /// The width the pane asks for.
57    pub fn preferred_width(mut self, preferred_width: PanePreferredSize) -> Pane {
58        self.preferred_width = Some(preferred_width);
59        self
60    }
61
62    /// The height a reflowed pane asks for.
63    pub fn preferred_height(mut self, preferred_height: PanePreferredSize) -> Pane {
64        self.preferred_height = Some(preferred_height);
65        self
66    }
67
68    /// How far the pane keeps from each edge of the scaffold.
69    pub fn margins(mut self, margins: impl Into<EdgeInsetsGeometry>) -> Pane {
70        let margins = margins.into();
71        self.margins = margins;
72        self
73    }
74
75    /// The shape the pane is clipped to.
76    pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> Pane {
77        self.shape = Some(shape.into());
78        self
79    }
80
81    /// Compose's `PaneMarginsImpl`: `bounds` with each edge kept at least the margin from the
82    /// same edge of a scaffold `scaffold` wide and high.
83    pub(crate) fn within_margins(
84        &self,
85        bounds: Rect,
86        scaffold: Size,
87        text_direction: TextDirection,
88    ) -> Rect {
89        let margins = self.margins.resolve(Some(text_direction));
90        Rect::from_ltrb(
91            bounds.left.max(margins.left),
92            bounds.top.max(margins.top),
93            bounds.right.min(scaffold.width() - margins.right),
94            bounds.bottom.min(scaffold.height() - margins.bottom),
95        )
96    }
97
98    pub(crate) fn width_for(&self, scaffold: Size, default_width: f64) -> f64 {
99        self.preferred_width
100            .map(|size| size.resolve(scaffold.width()))
101            .unwrap_or(default_width)
102    }
103
104    pub(crate) fn height_for(&self, scaffold: Size, default_height: f64) -> f64 {
105        self.preferred_height
106            .map(|size| size.resolve(scaffold.height()))
107            .unwrap_or(default_height)
108    }
109}
110
111impl From<WidgetRef> for Pane {
112    fn from(child: WidgetRef) -> Pane {
113        Pane::new(child)
114    }
115}