1use std::rc::Rc;
4use std::time::Duration;
5
6use waft_animation::{
7 Animatable, Animation, AnimationBehavior, AnimationController, AnimationStatus,
8 AnimationStatusListener, Curve, CurvedAnimation, SizeTween,
9};
10use waft_embedder::{Clip, Offset, Size, TextBaseline, TextDirection};
11use waft_foundation::{App, DiagnosticPropertiesBuilder, Handle, Listener};
12use waft_painting::AlignmentGeometry;
13
14use crate::box_::{AnyRenderBox, BoxConstraints, BoxHitTestResult, RenderBox, RenderBoxData};
15use crate::layer::{ClipRectLayer, LayerHandle};
16use crate::object::{
17 AnyRenderObject, Constraints, RenderHandle, RenderObject, RenderObjectBase, RenderObjectData,
18 RenderObjectWithChildData, RenderObjectWithChildMixin,
19};
20use crate::painting_context::PaintingContext;
21use crate::pipeline_owner::PipelineOwner;
22use crate::shifted_box::{
23 RenderAligningShiftedBox, RenderAligningShiftedBoxData, RenderShiftedBox,
24};
25use crate::sliver_persistent_header::TickerProviderRef;
26use waft_foundation::Ref;
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30pub enum RenderAnimatedSizeState {
31 Start,
36 Stable,
42 Changed,
45 Unstable,
48}
49
50pub struct RenderAnimatedSize {
53 render_object: RenderObjectData,
54 render_box: RenderBoxData,
55 child: RenderObjectWithChildData<AnyRenderBox>,
56 aligning: RenderAligningShiftedBoxData,
57 vsync: TickerProviderRef,
58 clip_behavior: Clip,
59 controller: Ref<AnimationController>,
60 animation: Ref<CurvedAnimation>,
61 size_tween: Ref<SizeTween>,
62 has_visual_overflow: bool,
63 last_value: Option<f64>,
64 state: RenderAnimatedSizeState,
65 current_size: Size,
66 on_end: Option<Listener>,
67 clip_rect_layer: LayerHandle<Handle<ClipRectLayer>>,
68}
69
70impl RenderAnimatedSize {
71 #[allow(clippy::too_many_arguments)]
73 pub fn new(
74 app: &mut App,
75 vsync: TickerProviderRef,
76 duration: Duration,
77 reverse_duration: Option<Duration>,
78 curve: Rc<dyn Curve>,
79 alignment: AlignmentGeometry,
80 text_direction: Option<TextDirection>,
81 clip_behavior: Clip,
82 on_end: Option<Listener>,
83 child: Option<AnyRenderBox>,
84 ) -> RenderHandle<Self> {
85 let controller = AnimationController::new(
86 app,
87 None,
88 Some(duration),
89 reverse_duration,
90 0.0,
91 1.0,
92 AnimationBehavior::Normal,
93 vsync.clone(),
94 );
95 let animation = CurvedAnimation::new(app, controller.erase(), curve, None);
96 let size_tween = SizeTween::new(app, None, None);
97 let controller_handle = controller.handle();
98 let this = RenderHandle::new_box(
99 app,
100 RenderAnimatedSize {
101 render_object: RenderObjectData::new(),
102 render_box: RenderBoxData::new(),
103 child: RenderObjectWithChildData::new(),
104 aligning: RenderAligningShiftedBoxData::new(alignment, text_direction),
105 vsync,
106 clip_behavior,
107 controller,
108 animation,
109 size_tween,
110 has_visual_overflow: false,
111 last_value: None,
112 state: RenderAnimatedSizeState::Start,
113 current_size: Size::ZERO,
114 on_end,
115 clip_rect_layer: LayerHandle::new(),
116 },
117 );
118 controller_handle.add_listener(
119 app,
120 Listener::handle_method(this.handle(), on_controller_tick),
121 );
122 this.set_child(app, child);
123 this
124 }
125
126 pub fn duration(self: RenderHandle<Self>, app: &App) -> Duration {
128 app.get(&self.get(app).controller)
129 .duration
130 .expect("duration")
131 }
132
133 pub fn set_duration(self: RenderHandle<Self>, app: &mut App, value: Duration) {
135 let controller = self.get(app).controller.handle();
136 if app.get(controller).duration == Some(value) {
137 return;
138 }
139 app.get_mut(controller).duration = Some(value);
140 }
141
142 pub fn reverse_duration(self: RenderHandle<Self>, app: &App) -> Option<Duration> {
144 app.get(&self.get(app).controller).reverse_duration
145 }
146
147 pub fn set_reverse_duration(self: RenderHandle<Self>, app: &mut App, value: Option<Duration>) {
149 let controller = self.get(app).controller.handle();
150 if app.get(controller).reverse_duration == value {
151 return;
152 }
153 app.get_mut(controller).reverse_duration = value;
154 }
155
156 pub fn curve(self: RenderHandle<Self>, app: &App) -> Rc<dyn Curve> {
158 app.get(&self.get(app).animation).curve.clone()
159 }
160
161 pub fn set_curve(self: RenderHandle<Self>, app: &mut App, value: Rc<dyn Curve>) {
163 let animation = self.get(app).animation.handle();
164 if Rc::ptr_eq(&app.get(animation).curve, &value) {
165 return;
166 }
167 app.get_mut(animation).curve = value;
168 }
169
170 pub fn clip_behavior(self: RenderHandle<Self>, app: &App) -> Clip {
172 self.get(app).clip_behavior
173 }
174
175 pub fn set_clip_behavior(self: RenderHandle<Self>, app: &mut App, value: Clip) {
177 if self.get(app).clip_behavior == value {
178 return;
179 }
180 self.get_mut(app).clip_behavior = value;
181 RenderBox::mark_needs_paint(self, app);
182 }
183
184 pub fn is_animating(self: RenderHandle<Self>, app: &App) -> bool {
186 self.get(app).controller.as_animation().is_animating(app)
187 }
188
189 pub fn vsync(self: RenderHandle<Self>, app: &App) -> TickerProviderRef {
191 self.get(app).vsync.clone()
192 }
193
194 pub fn set_vsync(self: RenderHandle<Self>, app: &mut App, value: TickerProviderRef) {
196 if self.get(app).vsync == value {
197 return;
198 }
199 self.get_mut(app).vsync = value.clone();
200 let controller = self.get(app).controller.handle();
201 controller.resync(app, value);
202 }
203
204 pub fn on_end(self: RenderHandle<Self>, app: &App) -> Option<Listener> {
206 self.get(app).on_end.clone()
207 }
208
209 pub fn set_on_end(self: RenderHandle<Self>, app: &mut App, value: Option<Listener>) {
211 self.get_mut(app).on_end = value;
212 }
213
214 pub fn state(self: RenderHandle<Self>, app: &App) -> RenderAnimatedSizeState {
216 self.get(app).state
217 }
218
219 fn animated_size(self: RenderHandle<Self>, app: &App) -> Option<Size> {
220 let this = self.get(app);
221 Animatable::evaluate(&this.size_tween, app, this.animation.as_animation())
222 }
223
224 fn restart_animation(self: RenderHandle<Self>, app: &mut App) {
225 self.get_mut(app).last_value = Some(0.0);
226 let controller = self.get(app).controller.handle();
227 controller.forward(app, Some(0.0));
228 }
229
230 fn layout_start(self: RenderHandle<Self>, app: &mut App) {
231 let child_size = self.child(app).expect("child").size(app);
232 let size_tween = self.get(app).size_tween.handle();
233 app.get_mut(size_tween).begin = Some(child_size);
234 app.get_mut(size_tween).end = Some(child_size);
235 self.get_mut(app).state = RenderAnimatedSizeState::Stable;
236 }
237
238 fn layout_stable(self: RenderHandle<Self>, app: &mut App) {
239 let child_size = self.child(app).expect("child").size(app);
240 let size_tween = self.get(app).size_tween.handle();
241 let end = app.get(size_tween).end;
242 if end != Some(child_size) {
243 let size = self.size(app);
244 app.get_mut(size_tween).begin = Some(size);
245 app.get_mut(size_tween).end = Some(child_size);
246 self.restart_animation(app);
247 self.get_mut(app).state = RenderAnimatedSizeState::Changed;
248 } else {
249 let controller = self.get(app).controller.handle();
250 let value = controller.value(app);
251 let upper = app.get(controller).upper_bound;
252 if value == upper {
253 app.get_mut(size_tween).begin = Some(child_size);
254 app.get_mut(size_tween).end = Some(child_size);
255 } else if !controller.is_animating(app) {
256 controller.forward(app, None);
257 }
258 }
259 }
260
261 fn layout_changed(self: RenderHandle<Self>, app: &mut App) {
262 let child_size = self.child(app).expect("child").size(app);
263 let size_tween = self.get(app).size_tween.handle();
264 if app.get(size_tween).end != Some(child_size) {
265 app.get_mut(size_tween).begin = Some(child_size);
266 app.get_mut(size_tween).end = Some(child_size);
267 self.restart_animation(app);
268 self.get_mut(app).state = RenderAnimatedSizeState::Unstable;
269 } else {
270 self.get_mut(app).state = RenderAnimatedSizeState::Stable;
271 let controller = self.get(app).controller.handle();
272 if !controller.is_animating(app) {
273 controller.forward(app, None);
274 }
275 }
276 }
277
278 fn layout_unstable(self: RenderHandle<Self>, app: &mut App) {
279 let child_size = self.child(app).expect("child").size(app);
280 let size_tween = self.get(app).size_tween.handle();
281 if app.get(size_tween).end != Some(child_size) {
282 app.get_mut(size_tween).begin = Some(child_size);
283 app.get_mut(size_tween).end = Some(child_size);
284 self.restart_animation(app);
285 } else {
286 let controller = self.get(app).controller.handle();
287 controller.stop(app, true);
288 self.get_mut(app).state = RenderAnimatedSizeState::Stable;
289 }
290 }
291}
292
293impl RenderObjectWithChildMixin for RenderAnimatedSize {
294 type ChildType = AnyRenderBox;
295
296 fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
297 &self.get(app).child
298 }
299
300 fn child_data_mut(
301 self: RenderHandle<Self>,
302 app: &mut App,
303 ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
304 &mut self.get_mut(app).child
305 }
306}
307
308impl RenderShiftedBox for RenderAnimatedSize {}
309
310impl RenderAligningShiftedBox for RenderAnimatedSize {
311 fn aligning_data(self: RenderHandle<Self>, app: &App) -> &RenderAligningShiftedBoxData {
312 &self.get(app).aligning
313 }
314
315 fn aligning_data_mut(
316 self: RenderHandle<Self>,
317 app: &mut App,
318 ) -> &mut RenderAligningShiftedBoxData {
319 &mut self.get_mut(app).aligning
320 }
321}
322
323impl RenderObject for RenderAnimatedSize {
324 crate::render_object_accessors!();
325
326 fn debug_fill_properties(
327 self: RenderHandle<Self>,
328 app: &App,
329 properties: &mut DiagnosticPropertiesBuilder,
330 ) {
331 RenderAligningShiftedBox::debug_fill_properties(self, app, properties);
332 }
333
334 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
335 if let Some(child) = self.child(app) {
336 child.as_object().attach(app, owner);
337 }
338 match self.get(app).state {
339 RenderAnimatedSizeState::Start | RenderAnimatedSizeState::Stable => {}
340 RenderAnimatedSizeState::Changed | RenderAnimatedSizeState::Unstable => {
341 RenderBox::mark_needs_layout(self, app);
342 }
343 }
344 let controller = self.get(app).controller.handle();
345 controller.add_status_listener(
346 app,
347 AnimationStatusListener::handle_method(self.handle(), animation_status_listener),
348 );
349 }
350
351 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
352 let controller = self.get(app).controller.handle();
353 controller.stop(app, true);
354 controller.remove_status_listener(
355 app,
356 &AnimationStatusListener::handle_method(self.handle(), animation_status_listener),
357 );
358 if let Some(child) = self.child(app) {
359 child.as_object().detach(app);
360 }
361 }
362
363 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
364 let controller = self.get(app).controller.handle();
365 let value = controller.value(app);
366 self.get_mut(app).last_value = Some(value);
367 self.get_mut(app).has_visual_overflow = false;
368 let constraints = self.constraints(app);
369 if self.child(app).is_none() || constraints.is_tight() {
370 controller.stop(app, true);
371 let smallest = constraints.smallest();
372 let size_tween = self.get(app).size_tween.handle();
373 app.get_mut(size_tween).begin = Some(smallest);
374 app.get_mut(size_tween).end = Some(smallest);
375 self.set_size(app, smallest);
376 self.get_mut(app).current_size = smallest;
377 self.get_mut(app).state = RenderAnimatedSizeState::Start;
378 if let Some(child) = self.child(app) {
379 child.layout(app, constraints, false);
380 }
381 return;
382 }
383
384 let child = self.child(app).expect("child");
385 child.layout(app, constraints, true);
386
387 match self.get(app).state {
388 RenderAnimatedSizeState::Start => self.layout_start(app),
389 RenderAnimatedSizeState::Stable => self.layout_stable(app),
390 RenderAnimatedSizeState::Changed => self.layout_changed(app),
391 RenderAnimatedSizeState::Unstable => self.layout_unstable(app),
392 }
393
394 let animated = self.animated_size(app).expect("animated size");
395 let size = constraints.constrain(animated);
396 self.set_size(app, size);
397 self.get_mut(app).current_size = size;
398 self.align_child(app);
399
400 let end = app.get(&self.get(app).size_tween).end.expect("end");
401 if size.width() < end.width() || size.height() < end.height() {
402 self.get_mut(app).has_visual_overflow = true;
403 }
404 }
405
406 fn paint(
407 self: RenderHandle<Self>,
408 app: &mut App,
409 context: &mut PaintingContext,
410 offset: Offset,
411 ) {
412 if self.child(app).is_some()
413 && self.get(app).has_visual_overflow
414 && self.get(app).clip_behavior != Clip::None
415 {
416 let clip_rect = Offset::ZERO & self.size(app);
417 let clip_behavior = self.get(app).clip_behavior;
418 let old = self.get(app).clip_rect_layer.layer();
419 let layer = context.push_clip_rect(
420 app,
421 self.as_object().needs_compositing(app),
422 offset,
423 clip_rect,
424 |app, context, offset| RenderShiftedBox::paint(self, app, context, offset),
425 clip_behavior,
426 old,
427 );
428 LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, layer);
429 } else {
430 LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, None);
431 RenderShiftedBox::paint(self, app, context, offset);
432 }
433 }
434
435 fn visit_children(
436 self: RenderHandle<Self>,
437 app: &App,
438 visitor: &mut dyn FnMut(AnyRenderObject),
439 ) {
440 if let Some(child) = self.child(app) {
441 visitor(child.as_object());
442 }
443 }
444
445 fn dispose(self: RenderHandle<Self>, app: &mut App) {
446 LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, None);
447 let controller = self.get(app).controller.handle();
448 let animation = self.get(app).animation.handle();
449 controller.dispose(app);
450 animation.dispose(app);
451 RenderObjectBase::dispose(self, app);
452 }
453}
454
455impl RenderBox for RenderAnimatedSize {
456 crate::render_box_accessors!();
457
458 fn hit_test_children(
459 self: RenderHandle<Self>,
460 app: &mut App,
461 result: &mut BoxHitTestResult<'_>,
462 position: Offset,
463 ) -> bool {
464 RenderShiftedBox::hit_test_children(self, app, result, position)
465 }
466
467 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
468 RenderShiftedBox::compute_min_intrinsic_width(self, app, height)
469 }
470
471 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
472 RenderShiftedBox::compute_max_intrinsic_width(self, app, height)
473 }
474
475 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
476 RenderShiftedBox::compute_min_intrinsic_height(self, app, width)
477 }
478
479 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
480 RenderShiftedBox::compute_max_intrinsic_height(self, app, width)
481 }
482
483 fn compute_distance_to_actual_baseline(
484 self: RenderHandle<Self>,
485 app: &mut App,
486 baseline: TextBaseline,
487 ) -> Option<f64> {
488 RenderShiftedBox::compute_distance_to_actual_baseline(self, app, baseline)
489 }
490
491 fn compute_dry_layout(
492 self: RenderHandle<Self>,
493 app: &mut App,
494 constraints: BoxConstraints,
495 ) -> Size {
496 let Some(child) = self.child(app) else {
497 return constraints.smallest();
498 };
499 if constraints.is_tight() {
500 return constraints.smallest();
501 }
502 let child_size = child.get_dry_layout(app, constraints);
503 match self.get(app).state {
504 RenderAnimatedSizeState::Start => return constraints.constrain(child_size),
505 RenderAnimatedSizeState::Stable => {
506 if app.get(&self.get(app).size_tween).end != Some(child_size) {
507 return constraints.constrain(self.get(app).current_size);
508 }
509 let controller = self.get(app).controller.handle();
510 if controller.value(app) == app.get(controller).upper_bound {
511 return constraints.constrain(child_size);
512 }
513 }
514 RenderAnimatedSizeState::Unstable | RenderAnimatedSizeState::Changed => {
515 if app.get(&self.get(app).size_tween).end != Some(child_size) {
516 return constraints.constrain(child_size);
517 }
518 }
519 }
520 constraints.constrain(self.animated_size(app).expect("animated size"))
521 }
522
523 fn compute_dry_baseline(
524 self: RenderHandle<Self>,
525 app: &mut App,
526 constraints: BoxConstraints,
527 baseline: TextBaseline,
528 ) -> Option<f64> {
529 let child = self.child(app)?;
530 let result = child.get_dry_baseline(app, constraints, baseline)?;
531 let child_size = child.get_dry_layout(app, constraints);
532 let my_size = self.get_dry_layout(app, constraints);
533 let offset = self
534 .resolved_alignment(app)
535 .along_offset(my_size - child_size);
536 Some(result + offset.dy())
537 }
538}
539
540impl std::fmt::Debug for RenderAnimatedSize {
541 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
542 f.debug_struct("RenderAnimatedSize").finish_non_exhaustive()
543 }
544}
545
546fn on_controller_tick(this: Handle<RenderAnimatedSize>, app: &mut App) {
547 let this = RenderHandle::from_handle(this);
548 let value = this.get(app).controller.value(app);
549 if this.get(app).last_value != Some(value) {
550 RenderBox::mark_needs_layout(this, app);
551 }
552}
553
554fn animation_status_listener(
555 this: Handle<RenderAnimatedSize>,
556 app: &mut App,
557 status: AnimationStatus,
558) {
559 let this = RenderHandle::from_handle(this);
560 if status.is_completed()
561 && let Some(on_end) = this.get(app).on_end.clone()
562 {
563 on_end.call(app);
564 }
565}