1use std::any::Any;
7use std::rc::Rc;
8
9use waft_embedder::{Canvas, Matrix4, Offset, Size, TextBaseline};
10use waft_foundation::{App, Handle, Listenable, Listener};
11use waft_painting::ClipContext;
12
13use crate::box_::{AnyRenderBox, BoxConstraints, BoxHitTestResult, RenderBox, RenderBoxData};
14use crate::object::{
15 AnyRenderObject, RenderHandle, RenderObject, RenderObjectData, RenderObjectWithChildData,
16 RenderObjectWithChildMixin,
17};
18use crate::painting_context::PaintingContext;
19use crate::pipeline_owner::PipelineOwner;
20use crate::proxy_box::{RenderProxyBoxMixin, same_instance};
21
22pub trait CustomPainter: 'static {
61 fn repaint(&self) -> Option<&Rc<dyn Listenable>> {
67 None
68 }
69
70 fn paint(&self, app: &mut App, canvas: &mut Canvas, size: Size);
91
92 fn should_repaint(&self, app: &App, old_delegate: &dyn CustomPainter) -> bool;
119
120 fn hit_test(&self, app: &App, position: Offset) -> Option<bool> {
134 let _ = (app, position);
135 None
136 }
137
138 fn as_any(&self) -> &dyn Any;
140}
141
142impl Listenable for dyn CustomPainter {
143 fn add_listener(&self, app: &mut App, listener: Listener) {
147 if let Some(repaint) = self.repaint() {
148 repaint.add_listener(app, listener);
149 }
150 }
151
152 fn remove_listener(&self, app: &mut App, listener: &Listener) {
155 if let Some(repaint) = self.repaint() {
156 repaint.remove_listener(app, listener);
157 }
158 }
159}
160
161pub struct RenderCustomPaint {
164 render_object: RenderObjectData,
165 render_box: RenderBoxData,
166 child: RenderObjectWithChildData<AnyRenderBox>,
167 painter: Option<Rc<dyn CustomPainter>>,
168 foreground_painter: Option<Rc<dyn CustomPainter>>,
169 preferred_size: Size,
170 is_complex: bool,
171 will_change: bool,
172}
173
174impl RenderCustomPaint {
175 pub fn new(
177 app: &mut App,
178 painter: Option<Rc<dyn CustomPainter>>,
179 foreground_painter: Option<Rc<dyn CustomPainter>>,
180 preferred_size: Size,
181 is_complex: bool,
182 will_change: bool,
183 child: Option<AnyRenderBox>,
184 ) -> RenderHandle<RenderCustomPaint> {
185 let this = RenderHandle::new_box(
186 app,
187 RenderCustomPaint {
188 render_object: RenderObjectData::new(),
189 render_box: RenderBoxData::new(),
190 child: RenderObjectWithChildData::new(),
191 painter,
192 foreground_painter,
193 preferred_size,
194 is_complex,
195 will_change,
196 },
197 );
198 this.set_child(app, child);
199 this
200 }
201
202 pub fn painter(self: RenderHandle<Self>, app: &App) -> Option<Rc<dyn CustomPainter>> {
206 self.get(app).painter.clone()
207 }
208
209 pub fn set_painter(
222 self: RenderHandle<Self>,
223 app: &mut App,
224 value: Option<Rc<dyn CustomPainter>>,
225 ) {
226 let old_painter = self.get(app).painter.clone();
227 if same_instance(old_painter.as_ref(), value.as_ref()) {
228 return;
229 }
230 self.get_mut(app).painter = value.clone();
231 self.did_update_painter(app, value.as_ref(), old_painter.as_ref());
232 }
233
234 pub fn foreground_painter(
238 self: RenderHandle<Self>,
239 app: &App,
240 ) -> Option<Rc<dyn CustomPainter>> {
241 self.get(app).foreground_painter.clone()
242 }
243
244 pub fn set_foreground_painter(
257 self: RenderHandle<Self>,
258 app: &mut App,
259 value: Option<Rc<dyn CustomPainter>>,
260 ) {
261 let old_painter = self.get(app).foreground_painter.clone();
262 if same_instance(old_painter.as_ref(), value.as_ref()) {
263 return;
264 }
265 self.get_mut(app).foreground_painter = value.clone();
266 self.did_update_painter(app, value.as_ref(), old_painter.as_ref());
267 }
268
269 fn did_update_painter(
270 self: RenderHandle<Self>,
271 app: &mut App,
272 new_painter: Option<&Rc<dyn CustomPainter>>,
273 old_painter: Option<&Rc<dyn CustomPainter>>,
274 ) {
275 let repaint = match (new_painter, old_painter) {
277 (None, old_painter) => {
278 debug_assert!(old_painter.is_some()); true
280 }
281 (Some(new_painter), Some(old_painter)) => {
282 new_painter.as_any().type_id() != old_painter.as_any().type_id()
283 || new_painter.should_repaint(app, &**old_painter)
284 }
285 (Some(_), None) => true,
286 };
287 if repaint {
288 self.mark_needs_paint(app);
289 }
290 if self.attached(app) {
291 if let Some(old_painter) = old_painter {
292 old_painter.remove_listener(app, &self.paint_listener());
293 }
294 if let Some(new_painter) = new_painter {
295 new_painter.add_listener(app, self.paint_listener());
296 }
297 }
298 }
300
301 fn paint_listener(self: RenderHandle<Self>) -> Listener {
303 Listener::handle_method(self.handle(), mark_needs_paint)
304 }
305
306 pub fn preferred_size(self: RenderHandle<Self>, app: &App) -> Size {
314 self.get(app).preferred_size
315 }
316
317 pub fn set_preferred_size(self: RenderHandle<Self>, app: &mut App, value: Size) {
319 if self.preferred_size(app) == value {
320 return;
321 }
322 self.get_mut(app).preferred_size = value;
323 self.mark_needs_layout(app);
324 }
325
326 pub fn is_complex(self: RenderHandle<Self>, app: &App) -> bool {
334 self.get(app).is_complex
335 }
336
337 pub fn set_is_complex(self: RenderHandle<Self>, app: &mut App, value: bool) {
339 self.get_mut(app).is_complex = value;
340 }
341
342 pub fn will_change(self: RenderHandle<Self>, app: &App) -> bool {
350 self.get(app).will_change
351 }
352
353 pub fn set_will_change(self: RenderHandle<Self>, app: &mut App, value: bool) {
355 self.get_mut(app).will_change = value;
356 }
357
358 fn paint_with_painter(
359 self: RenderHandle<Self>,
360 app: &mut App,
361 canvas: &mut Canvas,
362 offset: Offset,
363 painter: &dyn CustomPainter,
364 ) {
365 canvas.save();
366 let debug_previous_canvas_save_count = canvas.save_count();
367 if offset != Offset::ZERO {
368 canvas.translate(offset.dx() as f32, offset.dy() as f32);
369 }
370 let size = self.size(app);
371 painter.paint(app, canvas, size);
372 debug_assert_eq!(
376 debug_previous_canvas_save_count,
377 canvas.save_count(),
378 "the custom painter had mismatching save and restore calls"
379 );
380 canvas.restore();
381 }
382
383 fn set_raster_cache_hints(self: RenderHandle<Self>, app: &App, context: &mut PaintingContext) {
384 if self.get(app).is_complex {
385 context.set_is_complex_hint();
386 }
387 if self.get(app).will_change {
388 context.set_will_change_hint();
389 }
390 }
391}
392
393fn mark_needs_paint(this: Handle<RenderCustomPaint>, app: &mut App) {
394 RenderHandle::from_handle(this).mark_needs_paint(app);
395}
396
397impl RenderObjectWithChildMixin for RenderCustomPaint {
398 type ChildType = AnyRenderBox;
399
400 fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
401 &self.get(app).child
402 }
403
404 fn child_data_mut(
405 self: RenderHandle<Self>,
406 app: &mut App,
407 ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
408 &mut self.get_mut(app).child
409 }
410}
411
412impl RenderProxyBoxMixin for RenderCustomPaint {
413 fn compute_size_for_no_child(
414 self: RenderHandle<Self>,
415 app: &App,
416 constraints: BoxConstraints,
417 ) -> Size {
418 constraints.constrain(self.get(app).preferred_size)
419 }
420}
421
422impl RenderObject for RenderCustomPaint {
423 crate::render_object_accessors!();
424
425 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
426 if let Some(child) = self.child(app) {
427 child.as_object().attach(app, owner);
428 }
429 if let Some(painter) = self.painter(app) {
430 painter.add_listener(app, self.paint_listener());
431 }
432 if let Some(foreground_painter) = self.foreground_painter(app) {
433 foreground_painter.add_listener(app, self.paint_listener());
434 }
435 }
436
437 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
438 if let Some(painter) = self.painter(app) {
439 painter.remove_listener(app, &self.paint_listener());
440 }
441 if let Some(foreground_painter) = self.foreground_painter(app) {
442 foreground_painter.remove_listener(app, &self.paint_listener());
443 }
444 if let Some(child) = self.child(app) {
445 child.as_object().detach(app);
446 }
447 }
448
449 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
450 RenderProxyBoxMixin::perform_layout(self, app);
451 }
452
453 fn paint(
454 self: RenderHandle<Self>,
455 app: &mut App,
456 context: &mut PaintingContext,
457 offset: Offset,
458 ) {
459 if let Some(painter) = self.painter(app) {
460 self.paint_with_painter(app, context.canvas(), offset, &*painter);
461 self.set_raster_cache_hints(app, context);
462 }
463 RenderProxyBoxMixin::paint(self, app, context, offset);
464 if let Some(foreground_painter) = self.foreground_painter(app) {
465 self.paint_with_painter(app, context.canvas(), offset, &*foreground_painter);
466 self.set_raster_cache_hints(app, context);
467 }
468 }
469
470 fn visit_children(
471 self: RenderHandle<Self>,
472 app: &App,
473 visitor: &mut dyn FnMut(AnyRenderObject),
474 ) {
475 if let Some(child) = self.child(app) {
476 visitor(child.as_object());
477 }
478 }
479}
480
481impl RenderBox for RenderCustomPaint {
482 crate::render_box_accessors!();
483
484 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
485 RenderProxyBoxMixin::setup_parent_data(self, app, child);
486 }
487
488 fn apply_paint_transform(
489 self: RenderHandle<Self>,
490 app: &App,
491 child: AnyRenderObject,
492 transform: &mut Matrix4,
493 ) {
494 RenderProxyBoxMixin::apply_paint_transform(self, app, child, transform);
495 }
496
497 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
498 if self.child(app).is_none() {
499 let width = self.preferred_size(app).width();
500 return if width.is_finite() { width } else { 0.0 };
501 }
502 RenderProxyBoxMixin::compute_min_intrinsic_width(self, app, height)
503 }
504
505 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
506 if self.child(app).is_none() {
507 let width = self.preferred_size(app).width();
508 return if width.is_finite() { width } else { 0.0 };
509 }
510 RenderProxyBoxMixin::compute_max_intrinsic_width(self, app, height)
511 }
512
513 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
514 if self.child(app).is_none() {
515 let height = self.preferred_size(app).height();
516 return if height.is_finite() { height } else { 0.0 };
517 }
518 RenderProxyBoxMixin::compute_min_intrinsic_height(self, app, width)
519 }
520
521 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
522 if self.child(app).is_none() {
523 let height = self.preferred_size(app).height();
524 return if height.is_finite() { height } else { 0.0 };
525 }
526 RenderProxyBoxMixin::compute_max_intrinsic_height(self, app, width)
527 }
528
529 fn compute_distance_to_actual_baseline(
530 self: RenderHandle<Self>,
531 app: &mut App,
532 baseline: TextBaseline,
533 ) -> Option<f64> {
534 RenderProxyBoxMixin::compute_distance_to_actual_baseline(self, app, baseline)
535 }
536
537 fn compute_dry_baseline(
538 self: RenderHandle<Self>,
539 app: &mut App,
540 constraints: BoxConstraints,
541 baseline: TextBaseline,
542 ) -> Option<f64> {
543 RenderProxyBoxMixin::compute_dry_baseline(self, app, constraints, baseline)
544 }
545
546 fn compute_dry_layout(
547 self: RenderHandle<Self>,
548 app: &mut App,
549 constraints: BoxConstraints,
550 ) -> Size {
551 RenderProxyBoxMixin::compute_dry_layout(self, app, constraints)
552 }
553
554 fn hit_test_self(self: RenderHandle<Self>, app: &App, position: Offset) -> bool {
555 match &self.get(app).painter {
556 Some(painter) => painter.hit_test(app, position).unwrap_or(true),
557 None => false,
558 }
559 }
560
561 fn hit_test_children(
562 self: RenderHandle<Self>,
563 app: &mut App,
564 result: &mut BoxHitTestResult<'_>,
565 position: Offset,
566 ) -> bool {
567 if let Some(foreground_painter) = self.foreground_painter(app)
568 && foreground_painter.hit_test(app, position).unwrap_or(false)
569 {
570 return true;
571 }
572 RenderProxyBoxMixin::hit_test_children(self, app, result, position)
573 }
574}
575
576#[cfg(test)]
577mod tests {
578 use std::cell::Cell;
579 use waft_foundation::AppCell;
580
581 use waft_embedder::{Color, Paint, valo};
582 use waft_foundation::ValueNotifier;
583 use waft_gestures::HitTestResult;
584 use waft_painting::AlignmentGeometry;
585
586 use super::*;
587 use crate::box_::BoxHitTestResult;
588 use crate::object::AnyRenderObject;
589 use crate::testing::{EnginePhase, layout_with, painted_ops, pump_frame};
590
591 const RED: Color = Color::from_argb(255, 255, 0, 0);
592 const BLUE: Color = Color::from_argb(255, 0, 0, 255);
593 const GREEN: Color = Color::from_argb(255, 0, 255, 0);
594
595 struct Recorder {
598 color: Color,
599 paints: Rc<Cell<u32>>,
600 hit: Option<bool>,
601 repaint: Option<Rc<dyn Listenable>>,
602 should_repaint: bool,
603 }
604
605 impl Recorder {
606 fn new(color: Color) -> Recorder {
607 Recorder {
608 color,
609 paints: Rc::new(Cell::new(0)),
610 hit: None,
611 repaint: None,
612 should_repaint: false,
613 }
614 }
615 }
616
617 impl CustomPainter for Recorder {
618 fn repaint(&self) -> Option<&Rc<dyn Listenable>> {
619 self.repaint.as_ref()
620 }
621
622 fn paint(&self, _app: &mut App, canvas: &mut Canvas, size: Size) {
623 self.paints.set(self.paints.get() + 1);
624 canvas.draw_rect(
625 Offset::ZERO & size,
626 &Paint {
627 color: self.color.into(),
628 ..Paint::default()
629 },
630 );
631 }
632
633 fn should_repaint(&self, _app: &App, _old_delegate: &dyn CustomPainter) -> bool {
634 self.should_repaint
635 }
636
637 fn hit_test(&self, _app: &App, _position: Offset) -> Option<bool> {
638 self.hit
639 }
640
641 fn as_any(&self) -> &dyn Any {
642 self
643 }
644 }
645
646 fn drawn_colors(app: &mut App, render: AnyRenderObject) -> Vec<valo::Color> {
648 painted_ops(app, render)
649 .into_iter()
650 .filter_map(|op| match op {
651 valo::Op::DrawRect { paint, .. } => Some(paint.color),
652 _ => None,
653 })
654 .collect()
655 }
656
657 fn hit_test(app: &mut App, target: AnyRenderBox, position: Offset) -> (bool, usize) {
658 let mut result = HitTestResult::new();
659 let is_hit = target.hit_test(app, &mut BoxHitTestResult::wrap(&mut result), position);
660 (is_hit, result.path().len())
661 }
662
663 #[test]
664 fn both_painters_paint_around_the_child() {
665 let cell = AppCell::new();
666 let mut app = cell.borrow_mut();
667 let background = Rc::new(Recorder::new(RED));
668 let foreground = Rc::new(Recorder::new(BLUE));
669 let child = crate::proxy_box::RenderColoredBox::new(&mut app, GREEN, true, None);
670 let custom = RenderCustomPaint::new(
671 &mut app,
672 Some(background.clone()),
673 Some(foreground.clone()),
674 Size::ZERO,
675 false,
676 false,
677 Some(child.as_box()),
678 );
679 layout_with(
680 &mut app,
681 custom.as_box(),
682 None,
683 AlignmentGeometry::CENTER,
684 EnginePhase::Paint,
685 );
686
687 assert_eq!((background.paints.get(), foreground.paints.get()), (1, 1));
688 assert_eq!(
689 drawn_colors(&mut app, custom.as_object()),
690 [RED.into(), GREEN.into(), BLUE.into()],
691 "background, child, foreground"
692 );
693 }
694
695 #[test]
696 fn a_painter_repaints_when_its_repaint_listenable_notifies() {
697 let cell = AppCell::new();
698 let mut app = cell.borrow_mut();
699 let notifier = app.create(ValueNotifier::new(0));
700 let painter = Rc::new(Recorder {
701 repaint: Some(Rc::new(notifier.clone())),
702 ..Recorder::new(RED)
703 });
704 let custom = RenderCustomPaint::new(
705 &mut app,
706 Some(painter.clone()),
707 None,
708 Size::new(10.0, 10.0),
709 false,
710 false,
711 None,
712 );
713 layout_with(
714 &mut app,
715 custom.as_box(),
716 None,
717 AlignmentGeometry::CENTER,
718 EnginePhase::Paint,
719 );
720 assert_eq!(painter.paints.get(), 1);
721 assert!(!custom.as_object().debug_needs_paint(&app));
722
723 notifier.set_value(&mut app, 1);
724 assert!(custom.as_object().debug_needs_paint(&app));
725 pump_frame(&mut app, EnginePhase::Paint);
726 assert_eq!(painter.paints.get(), 2);
727 }
728
729 #[test]
731 fn replacing_the_painter_asks_it_whether_to_repaint() {
732 let cell = AppCell::new();
733 let mut app = cell.borrow_mut();
734 let notifier = app.create(ValueNotifier::new(0));
735 let painter = Rc::new(Recorder {
736 repaint: Some(Rc::new(notifier.clone())),
737 ..Recorder::new(RED)
738 });
739 let custom = RenderCustomPaint::new(
740 &mut app,
741 Some(painter.clone()),
742 None,
743 Size::new(10.0, 10.0),
744 false,
745 false,
746 None,
747 );
748 layout_with(
749 &mut app,
750 custom.as_box(),
751 None,
752 AlignmentGeometry::CENTER,
753 EnginePhase::Paint,
754 );
755
756 let quiet = Rc::new(Recorder::new(BLUE));
757 custom.set_painter(&mut app, Some(quiet.clone()));
758 assert!(
759 !custom.as_object().debug_needs_paint(&app),
760 "should_repaint is false"
761 );
762 notifier.set_value(&mut app, 1);
763 assert!(
764 !custom.as_object().debug_needs_paint(&app),
765 "the old painter's listenable was unsubscribed"
766 );
767
768 let eager = Rc::new(Recorder {
769 should_repaint: true,
770 ..Recorder::new(BLUE)
771 });
772 custom.set_painter(&mut app, Some(eager.clone()));
773 assert!(custom.as_object().debug_needs_paint(&app));
774 pump_frame(&mut app, EnginePhase::Paint);
775 assert_eq!((quiet.paints.get(), eager.paints.get()), (0, 1));
776
777 custom.set_painter(&mut app, None);
778 assert!(
779 custom.as_object().debug_needs_paint(&app),
780 "a removal repaints"
781 );
782 }
783
784 #[test]
785 fn preferred_size_is_used_without_a_child() {
786 let cell = AppCell::new();
787 let mut app = cell.borrow_mut();
788 let custom = RenderCustomPaint::new(
789 &mut app,
790 None,
791 None,
792 Size::new(20.0, 30.0),
793 false,
794 false,
795 None,
796 );
797 custom.layout(&mut app, BoxConstraints::new(), false);
798 assert_eq!(custom.size(&app), Size::new(20.0, 30.0));
799
800 custom.layout(
801 &mut app,
802 BoxConstraints::loose(Size::new(10.0, 10.0)),
803 false,
804 );
805 assert_eq!(
806 custom.size(&app),
807 Size::new(10.0, 10.0),
808 "the preferred size is constrained"
809 );
810 }
811
812 #[test]
813 fn a_child_sizes_the_custom_paint() {
814 let cell = AppCell::new();
815 let mut app = cell.borrow_mut();
816 let child = crate::proxy_box::RenderConstrainedBox::new(
817 &mut app,
818 BoxConstraints::tight(Size::new(40.0, 40.0)),
819 None,
820 );
821 let custom = RenderCustomPaint::new(
822 &mut app,
823 None,
824 None,
825 Size::new(20.0, 30.0),
826 false,
827 false,
828 Some(child.as_box()),
829 );
830 custom.layout(&mut app, BoxConstraints::new(), false);
831 assert_eq!(custom.size(&app), Size::new(40.0, 40.0));
832 }
833
834 #[test]
835 fn the_painters_answer_hit_tests() {
836 let cell = AppCell::new();
837 let mut app = cell.borrow_mut();
838 let background = Rc::new(Recorder::new(RED));
839 let custom = RenderCustomPaint::new(
840 &mut app,
841 Some(background),
842 None,
843 Size::new(100.0, 100.0),
844 false,
845 false,
846 None,
847 );
848 custom.layout(&mut app, BoxConstraints::new(), false);
849 assert_eq!(
850 hit_test(&mut app, custom.as_box(), Offset::new(50.0, 50.0)),
851 (true, 1),
852 "a background painter is hit by default"
853 );
854
855 let missing = Rc::new(Recorder {
856 hit: Some(false),
857 ..Recorder::new(RED)
858 });
859 custom.set_painter(&mut app, Some(missing));
860 assert_eq!(
861 hit_test(&mut app, custom.as_box(), Offset::new(50.0, 50.0)),
862 (false, 0)
863 );
864
865 custom.set_painter(&mut app, None);
866 assert_eq!(
867 hit_test(&mut app, custom.as_box(), Offset::new(50.0, 50.0)),
868 (false, 0),
869 "no painter, no hit"
870 );
871
872 let foreground = Rc::new(Recorder {
873 hit: Some(true),
874 ..Recorder::new(BLUE)
875 });
876 custom.set_foreground_painter(&mut app, Some(foreground));
877 assert_eq!(
878 hit_test(&mut app, custom.as_box(), Offset::new(50.0, 50.0)),
879 (true, 1),
880 "a foreground painter answers for the children"
881 );
882 }
883}