waft_material/
drawer_header.rs1use std::rc::Rc;
4use std::time::Duration;
5
6use waft_animation::{Curve, Curves};
7use waft_foundation::App;
8use waft_painting::{Border, BoxDecoration, DecorationRef, EdgeInsets, EdgeInsetsGeometry};
9use waft_widgets::{
10 AnimatedContainer, BuildContext, Container, DefaultTextStyle, IntoWidget, KeyRef, MediaQuery,
11 Semantics, StatelessWidget, WidgetRef, debug_check_has_media_query,
12};
13
14use crate::debug::debug_check_has_material;
15use crate::divider::Divider;
16use crate::theme::Theme;
17
18const K_DRAWER_HEADER_HEIGHT: f64 = 160.0 + 1.0; #[derive(Debug)]
35pub struct DrawerHeader {
36 pub key: Option<KeyRef>,
37
38 pub decoration: Option<DecorationRef>,
46
47 pub padding: EdgeInsetsGeometry,
54
55 pub margin: Option<EdgeInsetsGeometry>,
57
58 pub duration: Duration,
60
61 pub curve: Rc<dyn Curve>,
63
64 pub child: Option<WidgetRef>,
69}
70
71impl DrawerHeader {
72 pub fn new(child: Option<WidgetRef>) -> DrawerHeader {
77 DrawerHeader {
78 key: None,
79 decoration: None,
80 padding: EdgeInsetsGeometry::Insets(EdgeInsets::from_ltrb(16.0, 16.0, 16.0, 8.0)),
81 margin: Some(EdgeInsetsGeometry::Insets(EdgeInsets::only(
82 0.0, 0.0, 0.0, 8.0,
83 ))),
84 duration: Duration::from_millis(250),
85 curve: Curves::fast_out_slow_in(),
86 child,
87 }
88 }
89
90 pub fn key(mut self, key: KeyRef) -> DrawerHeader {
92 self.key = Some(key);
93 self
94 }
95
96 pub fn decoration(mut self, decoration: impl Into<DecorationRef>) -> DrawerHeader {
98 self.decoration = Some(decoration.into());
99 self
100 }
101
102 pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> DrawerHeader {
104 let padding = padding.into();
105 self.padding = padding;
106 self
107 }
108
109 pub fn margin(mut self, margin: Option<EdgeInsetsGeometry>) -> DrawerHeader {
111 self.margin = margin;
112 self
113 }
114
115 pub fn duration(mut self, duration: Duration) -> DrawerHeader {
117 self.duration = duration;
118 self
119 }
120
121 pub fn curve(mut self, curve: Rc<dyn Curve>) -> DrawerHeader {
123 self.curve = curve;
124 self
125 }
126}
127
128impl StatelessWidget for DrawerHeader {
129 fn key(&self) -> Option<&KeyRef> {
130 self.key.as_ref()
131 }
132
133 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
134 debug_assert!(debug_check_has_material(app, context));
135 debug_assert!(debug_check_has_media_query(app, context));
136 let theme = Theme::of(app, context);
137 let status_bar_height = MediaQuery::padding_of(app, context).top;
138 let mut container = Container::new()
139 .height(status_bar_height + K_DRAWER_HEADER_HEIGHT)
140 .decoration(BoxDecoration::new().border(Border {
141 bottom: Divider::create_border_side(app, Some(context), None, None),
142 ..Border::default()
143 }));
144 if let Some(margin) = self.margin {
145 container = container.margin(margin);
146 }
147
148 let mut animated_container = AnimatedContainer::new(self.duration)
149 .padding(
150 self.padding
151 .add(EdgeInsetsGeometry::Insets(EdgeInsets::only(
152 0.0,
153 status_bar_height,
154 0.0,
155 0.0,
156 ))),
157 )
158 .curve(Rc::clone(&self.curve));
159 animated_container.decoration = self.decoration.clone();
160 if let Some(child) = &self.child {
161 animated_container = animated_container.child(DefaultTextStyle::new(
162 theme
163 .text_theme
164 .body_large
165 .clone()
166 .expect("the text theme has a body large style"),
167 MediaQuery::remove_padding(app, context, false, true, false, false, child.clone())
168 .into_widget(),
169 ));
170 }
171 Semantics::new()
172 .header(true)
173 .child(container.child(animated_container))
174 .into_widget()
175 }
176}