waft_material/compose/
pane.rs1use waft_embedder::{Rect, Size, TextDirection};
4use waft_painting::{EdgeInsetsGeometry, ShapeBorderRef};
5use waft_widgets::{IntoWidget, WidgetRef};
6
7#[derive(Clone, Copy, Debug, PartialEq)]
9pub enum PanePreferredSize {
10 Fixed(f64),
12 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#[derive(Clone, Debug)]
27pub struct Pane {
28 pub child: WidgetRef,
30 pub preferred_width: Option<PanePreferredSize>,
32 pub preferred_height: Option<PanePreferredSize>,
34 pub margins: EdgeInsetsGeometry,
38 pub shape: Option<ShapeBorderRef>,
41}
42
43impl Pane {
44 #[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 pub fn preferred_width(mut self, preferred_width: PanePreferredSize) -> Pane {
58 self.preferred_width = Some(preferred_width);
59 self
60 }
61
62 pub fn preferred_height(mut self, preferred_height: PanePreferredSize) -> Pane {
64 self.preferred_height = Some(preferred_height);
65 self
66 }
67
68 pub fn margins(mut self, margins: impl Into<EdgeInsetsGeometry>) -> Pane {
70 let margins = margins.into();
71 self.margins = margins;
72 self
73 }
74
75 pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> Pane {
77 self.shape = Some(shape.into());
78 self
79 }
80
81 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}