waft_widgets/widgets/
stretch_effect.rs1use std::fmt::{self, Debug};
9
10use waft_embedder::{FilterQuality, Matrix4, TextDirection};
11use waft_foundation::App;
12use waft_painting::{AlignmentDirectional, AlignmentGeometry, Axis};
13
14use crate::framework::{BuildContext, IntoWidget, KeyRef, StatelessWidget, WidgetRef};
15use crate::widgets::basic::{Directionality, Transform};
16
17pub struct StretchEffect {
24 pub key: Option<KeyRef>,
25
26 pub stretch_strength: f64,
48
49 pub axis: Axis,
53
54 pub child: WidgetRef,
56}
57
58impl StretchEffect {
59 #[cfg_attr(debug_assertions, track_caller)]
65 pub fn new<K>(axis: Axis, child: impl IntoWidget<K>) -> StretchEffect {
66 StretchEffect {
67 key: None,
68 stretch_strength: 0.0,
69 axis,
70 child: child.into_widget(),
71 }
72 }
73
74 pub fn key(mut self, key: KeyRef) -> StretchEffect {
76 self.key = Some(key);
77 self
78 }
79
80 pub fn stretch_strength(mut self, stretch_strength: f64) -> StretchEffect {
82 debug_assert!(
83 (-1.0..=1.0).contains(&stretch_strength),
84 "stretchStrength must be between -1.0 and 1.0"
85 );
86 self.stretch_strength = stretch_strength;
87 self
88 }
89
90 fn get_alignment(&self, direction: TextDirection) -> AlignmentGeometry {
91 let is_forward = self.stretch_strength > 0.0;
92
93 if self.axis == Axis::Vertical {
94 return if is_forward {
95 AlignmentGeometry::Directional(AlignmentDirectional::TOP_CENTER)
96 } else {
97 AlignmentGeometry::Directional(AlignmentDirectional::BOTTOM_CENTER)
98 };
99 }
100
101 if direction == TextDirection::Rtl {
103 if is_forward {
104 AlignmentGeometry::CENTER_END
105 } else {
106 AlignmentGeometry::CENTER_START
107 }
108 } else if is_forward {
109 AlignmentGeometry::CENTER_START
110 } else {
111 AlignmentGeometry::CENTER_END
112 }
113 }
114}
115
116impl Debug for StretchEffect {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 f.debug_struct("StretchEffect")
119 .field("stretchStrength", &self.stretch_strength)
120 .field("axis", &self.axis)
121 .finish_non_exhaustive()
122 }
123}
124
125impl StatelessWidget for StretchEffect {
126 fn key(&self) -> Option<&KeyRef> {
127 self.key.as_ref()
128 }
129
130 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
131 let text_direction = Directionality::of(app, context);
132 let mut x = 1.0;
133 let mut y = 1.0;
134
135 match self.axis {
136 Axis::Horizontal => x += self.stretch_strength.abs(),
137 Axis::Vertical => y += self.stretch_strength.abs(),
138 }
139
140 let mut transform = Transform::new(Matrix4::scale(x as f32, y as f32))
142 .alignment(self.get_alignment(text_direction))
143 .child(self.child.clone());
144 transform.filter_quality = if self.stretch_strength == 0.0 {
145 None
146 } else {
147 Some(FilterQuality::Medium)
148 };
149 transform.into_widget()
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use std::cell::RefCell;
156 use std::rc::Rc;
157
158 use waft_foundation::AppCell;
159
160 use super::*;
161 use crate::framework::downcast_widget;
162 use crate::test_harness::Harness;
163 use crate::widgets::basic::{Builder, SizedBox};
164
165 fn built(effect: StretchEffect, text_direction: TextDirection) -> WidgetRef {
167 let cell = AppCell::new();
168 let mut app = cell.borrow_mut();
169 let effect = Rc::new(effect);
170 let built: Rc<RefCell<Option<WidgetRef>>> = Rc::default();
171 let probe = Builder::new({
172 let built = Rc::clone(&built);
173 move |app, context| {
174 *built.borrow_mut() = Some(effect.build(app, context));
175 SizedBox::shrink().into_widget()
176 }
177 });
178 let harness = Harness::mount(
179 &mut app,
180 Directionality::new(text_direction, probe).into_widget(),
181 );
182 harness.pump(&mut app);
183 let widget = built.borrow().clone();
184 widget.expect("the probe built")
185 }
186
187 fn transform_of(widget: &WidgetRef) -> &Transform {
188 downcast_widget::<Transform>(&**widget).expect("the stretch is a Transform")
189 }
190
191 #[test]
192 fn a_vertical_stretch_scales_from_the_edge_it_is_pulled_from() {
193 let pulled_down = built(
194 StretchEffect::new(Axis::Vertical, SizedBox::shrink()).stretch_strength(0.5),
195 TextDirection::Ltr,
196 );
197 let transform = transform_of(&pulled_down);
198 assert_eq!(transform.transform, Matrix4::scale(1.0, 1.5));
199 assert_eq!(
200 transform.alignment,
201 Some(AlignmentGeometry::Directional(
202 AlignmentDirectional::TOP_CENTER
203 ))
204 );
205
206 let pulled_up = built(
207 StretchEffect::new(Axis::Vertical, SizedBox::shrink()).stretch_strength(-0.25),
208 TextDirection::Ltr,
209 );
210 let transform = transform_of(&pulled_up);
211 assert_eq!(transform.transform, Matrix4::scale(1.0, 1.25));
212 assert_eq!(
213 transform.alignment,
214 Some(AlignmentGeometry::Directional(
215 AlignmentDirectional::BOTTOM_CENTER
216 ))
217 );
218 }
219
220 #[test]
221 fn a_horizontal_stretch_scales_from_the_start_or_end_by_text_direction() {
222 for (strength, text_direction, alignment) in [
223 (0.5, TextDirection::Ltr, AlignmentGeometry::CENTER_START),
224 (-0.5, TextDirection::Ltr, AlignmentGeometry::CENTER_END),
225 (0.5, TextDirection::Rtl, AlignmentGeometry::CENTER_END),
226 (-0.5, TextDirection::Rtl, AlignmentGeometry::CENTER_START),
227 ] {
228 let stretched = built(
229 StretchEffect::new(Axis::Horizontal, SizedBox::shrink()).stretch_strength(strength),
230 text_direction,
231 );
232 let transform = transform_of(&stretched);
233 assert_eq!(transform.transform, Matrix4::scale(1.5, 1.0));
234 assert_eq!(
235 transform.alignment,
236 Some(alignment),
237 "{strength} {text_direction:?}"
238 );
239 }
240 }
241
242 #[test]
243 fn a_stretch_filters_at_medium_quality_and_a_resting_one_does_not() {
244 let stretched = built(
245 StretchEffect::new(Axis::Vertical, SizedBox::shrink()).stretch_strength(0.5),
246 TextDirection::Ltr,
247 );
248 assert_eq!(
249 transform_of(&stretched).filter_quality,
250 Some(FilterQuality::Medium)
251 );
252
253 let resting = built(
254 StretchEffect::new(Axis::Vertical, SizedBox::shrink()),
255 TextDirection::Ltr,
256 );
257 assert_eq!(transform_of(&resting).filter_quality, None);
258 }
259
260 #[test]
261 #[cfg(debug_assertions)]
262 #[should_panic(expected = "stretchStrength must be between -1.0 and 1.0")]
263 fn a_stretch_strength_past_one_is_refused() {
264 let _ = StretchEffect::new(Axis::Vertical, SizedBox::shrink()).stretch_strength(1.5);
265 }
266}