Skip to main content

waft_rendering/
pipeline_owner.rs

1//! Flutter counterpart: `rendering/object.dart` (`PipelineOwner`).
2//!
3//! Semantics callbacks and the semantics half of [`PipelineManifold`] wait.
4
5use std::rc::Rc;
6
7use waft_foundation::{App, Handle, Listener, ValueChanged};
8
9use crate::layer::ErasedLayer;
10use crate::object::AnyRenderObject;
11use crate::painting_context::PaintingContext;
12use waft_foundation::Ref;
13
14/// Manages a tree of [`PipelineOwner`]s.
15///
16/// All [`PipelineOwner`]s within a tree are attached to the same
17/// [`PipelineManifold`], which gives them access to shared functionality such
18/// as requesting a visual update (by calling
19/// [`request_visual_update`](Self::request_visual_update)). As such, the
20/// [`PipelineManifold`] gives the [`PipelineOwner`]s access to functionality
21/// usually provided by the bindings without tying the [`PipelineOwner`]s to a
22/// particular binding implementation.
23///
24/// The root of the [`PipelineOwner`] tree is attached to a [`PipelineManifold`] by
25/// passing the manifold to [`PipelineOwner::attach`]. Children are attached to the
26/// same [`PipelineManifold`] as their parent when they are adopted via
27/// [`PipelineOwner::adopt_child`].
28///
29/// Dart's `semanticsEnabled` and the listeners that report its changes wait with
30/// semantics.
31pub trait PipelineManifold {
32    /// Called by a [`PipelineOwner`] connected to this [`PipelineManifold`] when a
33    /// `RenderObject` associated with that pipeline owner wishes to update its
34    /// visual appearance.
35    ///
36    /// Typical implementations of this function will schedule a task to flush the
37    /// various stages of the pipeline. This function might be called multiple
38    /// times in quick succession. Implementations should take care to discard
39    /// duplicate calls quickly.
40    ///
41    /// A [`PipelineOwner`] connected to this [`PipelineManifold`] will call its
42    /// `on_need_visual_update` callback instead of this method if it has been
43    /// configured with one ([`PipelineOwner::new`]).
44    ///
45    /// See also:
46    ///
47    ///  * `SchedulerBinding::ensure_visual_update`, which [`PipelineManifold`]
48    ///    implementations typically call to implement this method.
49    fn request_visual_update(&self, app: &mut App);
50}
51
52/// Signature for a function that is called when the pipeline owner and child owner
53/// finished the paint phase.
54///
55/// The `bool` is `is_dirty`: whether the pipeline owner had dirty render objects that needed to
56/// be painted.
57pub type FlushedPaintCallback = ValueChanged<bool>;
58
59/// The pipeline owner manages the rendering pipeline.
60///
61/// Flutter's counterpart is `PipelineOwner`.
62pub struct PipelineOwner {
63    on_need_visual_update: Option<Listener>,
64    /// Called when this pipeline owner and child owner have finished the paint phase.
65    ///
66    /// The `is_dirty` parameter is true if the pipeline owner has dirty render objects that
67    /// needed to be painted.
68    on_flushed_paint: Option<FlushedPaintCallback>,
69    manifold: Option<Rc<dyn PipelineManifold>>,
70    root_node: Option<AnyRenderObject>,
71    nodes_needing_layout: Vec<AnyRenderObject>,
72    nodes_needing_compositing_bits_update: Vec<AnyRenderObject>,
73    should_merge_dirty_nodes: bool,
74    /// Dart's `_debugAllowMutationsToDirtySubtrees`.
75    debug_allow_mutations_to_dirty_subtrees: bool,
76    debug_doing_layout: bool,
77    debug_doing_child_layout: bool,
78    nodes_needing_paint: Vec<AnyRenderObject>,
79    debug_doing_paint: bool,
80    children: Vec<Ref<PipelineOwner>>,
81    debug_parent: Option<Handle<PipelineOwner>>,
82}
83
84impl PipelineOwner {
85    /// Creates a pipeline owner.
86    ///
87    /// Typically created by the binding, but can be created separately to drive
88    /// off-screen render objects through the rendering pipeline.
89    pub fn new(
90        app: &mut App,
91        on_need_visual_update: Option<Listener>,
92        on_flushed_paint: Option<FlushedPaintCallback>,
93    ) -> Ref<PipelineOwner> {
94        app.create(PipelineOwner {
95            on_need_visual_update,
96            on_flushed_paint,
97            manifold: None,
98            root_node: None,
99            nodes_needing_layout: Vec::new(),
100            nodes_needing_compositing_bits_update: Vec::new(),
101            should_merge_dirty_nodes: false,
102            debug_allow_mutations_to_dirty_subtrees: false,
103            debug_doing_layout: false,
104            debug_doing_child_layout: false,
105            nodes_needing_paint: Vec::new(),
106            debug_doing_paint: false,
107            children: Vec::new(),
108            debug_parent: None,
109        })
110    }
111
112    /// Calls the `on_need_visual_update` callback given to [`new`](Self::new) if there is
113    /// one, otherwise asks the [`PipelineManifold`] this owner is attached to.
114    ///
115    /// Used to notify the pipeline owner that an associated render object wishes
116    /// to update its visual appearance.
117    pub fn request_visual_update(self: Handle<Self>, app: &mut App) {
118        if let Some(callback) = app.get(self).on_need_visual_update.clone() {
119            callback.call(app);
120        } else if let Some(manifold) = app.get(self).manifold.clone() {
121            manifold.request_visual_update(app);
122        }
123    }
124
125    /// The unique object managed by this pipeline that has no parent.
126    pub fn root_node(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
127        app.get(self).root_node
128    }
129
130    /// Sets the unique object managed by this pipeline that has no parent.
131    pub fn set_root_node(self: Handle<Self>, app: &mut App, value: Option<AnyRenderObject>) {
132        let current = app.get(self).root_node;
133        if current == value {
134            return;
135        }
136        if let Some(old) = current {
137            old.detach(app);
138        }
139        app.get_mut(self).root_node = value;
140        if let Some(new) = value {
141            new.attach(app, self);
142        }
143    }
144
145    /// Relayout boundaries which need to be laid out in the next
146    /// [`flush_layout`](Self::flush_layout) pass.
147    pub fn nodes_needing_layout(self: Handle<Self>, app: &App) -> Vec<AnyRenderObject> {
148        app.get(self).nodes_needing_layout.clone()
149    }
150
151    pub(crate) fn add_node_needing_layout(
152        self: Handle<Self>,
153        app: &mut App,
154        node: AnyRenderObject,
155    ) {
156        app.get_mut(self).nodes_needing_layout.push(node);
157    }
158
159    pub(crate) fn add_node_needing_compositing_bits_update(
160        self: Handle<Self>,
161        app: &mut App,
162        node: AnyRenderObject,
163    ) {
164        app.get_mut(self)
165            .nodes_needing_compositing_bits_update
166            .push(node);
167    }
168
169    pub(crate) fn remove_node_needing_paint(
170        self: Handle<Self>,
171        app: &mut App,
172        node: AnyRenderObject,
173    ) {
174        app.get_mut(self)
175            .nodes_needing_paint
176            .retain(|candidate| *candidate != node);
177    }
178
179    /// Whether this pipeline is currently in the layout phase.
180    ///
181    /// Always `false` when debug assertions are disabled.
182    pub fn debug_doing_layout(self: Handle<Self>, app: &App) -> bool {
183        if !cfg!(debug_assertions) {
184            return false;
185        }
186        app.get(self).debug_doing_layout
187    }
188
189    /// See [`AnyRenderObject::invoke_layout_callback`]: runs `callback` with mutations to
190    /// dirty subtrees allowed, then asks [`flush_layout`](Self::flush_layout) to merge the
191    /// nodes it dirtied before continuing.
192    pub(crate) fn enable_mutations_to_dirty_subtrees(
193        self: Handle<Self>,
194        app: &mut App,
195        callback: impl FnOnce(&mut App),
196    ) {
197        debug_assert!(app.get(self).debug_doing_layout);
198        let old_state = app.get(self).debug_allow_mutations_to_dirty_subtrees;
199        if cfg!(debug_assertions) {
200            app.get_mut(self).debug_allow_mutations_to_dirty_subtrees = true;
201        }
202        callback(app);
203        app.get_mut(self).should_merge_dirty_nodes = true;
204        if cfg!(debug_assertions) {
205            app.get_mut(self).debug_allow_mutations_to_dirty_subtrees = old_state;
206        }
207    }
208
209    /// Update the layout information for all dirty render objects.
210    pub fn flush_layout(self: Handle<Self>, app: &mut App) {
211        if cfg!(debug_assertions) {
212            app.get_mut(self).debug_doing_layout = true;
213        }
214        while !app.get(self).nodes_needing_layout.is_empty() {
215            debug_assert!(!app.get(self).should_merge_dirty_nodes);
216            let mut dirty_nodes = std::mem::take(&mut app.get_mut(self).nodes_needing_layout);
217            dirty_nodes.sort_by_key(|node| node.depth(app));
218            let mut i = 0;
219            while i < dirty_nodes.len() {
220                if app.get(self).should_merge_dirty_nodes {
221                    app.get_mut(self).should_merge_dirty_nodes = false;
222                    if !app.get(self).nodes_needing_layout.is_empty() {
223                        app.get_mut(self)
224                            .nodes_needing_layout
225                            .extend(dirty_nodes.drain(i..));
226                        break;
227                    }
228                }
229                let node = dirty_nodes[i];
230                if node.needs_layout(app) && node.owner(app) == Some(self) {
231                    node.layout_without_resize(app);
232                }
233                i += 1;
234            }
235            app.get_mut(self).should_merge_dirty_nodes = false;
236        }
237        if cfg!(debug_assertions) {
238            app.get_mut(self).debug_doing_child_layout = true;
239        }
240        let children: Vec<Handle<PipelineOwner>> = app
241            .get(self)
242            .children
243            .iter()
244            .map(|child| child.handle())
245            .collect();
246        for child in children {
247            child.flush_layout(app);
248        }
249        debug_assert!(
250            app.get(self).nodes_needing_layout.is_empty(),
251            "Child PipelineOwners must not dirty nodes in their parent."
252        );
253        app.get_mut(self).should_merge_dirty_nodes = false;
254        if cfg!(debug_assertions) {
255            app.get_mut(self).debug_doing_layout = false;
256            app.get_mut(self).debug_doing_child_layout = false;
257        }
258    }
259
260    /// Updates the [`AnyRenderObject::needs_compositing`] bits.
261    ///
262    /// Called as part of the rendering pipeline after [`flush_layout`](Self::flush_layout) and
263    /// before [`flush_paint`](Self::flush_paint).
264    pub fn flush_compositing_bits(self: Handle<Self>, app: &mut App) {
265        let mut dirty_nodes =
266            std::mem::take(&mut app.get_mut(self).nodes_needing_compositing_bits_update);
267        dirty_nodes.sort_by_key(|node| node.depth(app));
268        for node in dirty_nodes {
269            if node.needs_compositing_bits_update(app) && node.owner(app) == Some(self) {
270                node.update_compositing_bits(app);
271            }
272        }
273        let children: Vec<Handle<PipelineOwner>> = app
274            .get(self)
275            .children
276            .iter()
277            .map(|child| child.handle())
278            .collect();
279        for child in children {
280            child.flush_compositing_bits(app);
281        }
282        debug_assert!(
283            app.get(self)
284                .nodes_needing_compositing_bits_update
285                .is_empty(),
286            "Child PipelineOwners must not dirty nodes in their parent."
287        );
288    }
289
290    /// Nodes with a dirty layer or paint state, to be updated in the next
291    /// [`flush_paint`](Self::flush_paint) pass.
292    pub fn nodes_needing_paint(self: Handle<Self>, app: &App) -> Vec<AnyRenderObject> {
293        app.get(self).nodes_needing_paint.clone()
294    }
295
296    pub(crate) fn add_node_needing_paint(self: Handle<Self>, app: &mut App, node: AnyRenderObject) {
297        app.get_mut(self).nodes_needing_paint.push(node);
298    }
299
300    /// Whether this pipeline is currently in the paint phase.
301    ///
302    /// Only meaningful when asserts are enabled.
303    pub fn debug_doing_paint(self: Handle<Self>, app: &App) -> bool {
304        app.get(self).debug_doing_paint
305    }
306
307    /// Update the display lists for all render objects.
308    ///
309    /// This function is one of the core stages of the rendering pipeline. Painting occurs after
310    /// layout and before the scene is recomposited so that scene is composited with up-to-date
311    /// display lists for every render object.
312    ///
313    /// A frame paints each of its views by
314    /// [`flush_paint_without_children`](Self::flush_paint_without_children) instead.
315    pub fn flush_paint(self: Handle<Self>, app: &mut App) {
316        if cfg!(debug_assertions) {
317            app.get_mut(self).debug_doing_paint = true;
318        }
319        let is_dirty = self.paint_dirty_nodes(app);
320        let children: Vec<Handle<PipelineOwner>> = app
321            .get(self)
322            .children
323            .iter()
324            .map(|child| child.handle())
325            .collect();
326        for child in children {
327            child.flush_paint(app);
328        }
329        self.did_flush_paint(app, is_dirty);
330        debug_assert!(
331            app.get(self).nodes_needing_paint.is_empty(),
332            "Child PipelineOwners must not dirty nodes in their parent."
333        );
334        if cfg!(debug_assertions) {
335            app.get_mut(self).debug_doing_paint = false;
336        }
337    }
338
339    /// [`flush_paint`](Self::flush_paint) for this owner's render objects alone: the views
340    /// anchored in a view, whose owners are its owner's children, paint at frames of their own.
341    pub fn flush_paint_without_children(self: Handle<Self>, app: &mut App) {
342        if cfg!(debug_assertions) {
343            app.get_mut(self).debug_doing_paint = true;
344        }
345        let is_dirty = self.paint_dirty_nodes(app);
346        self.did_flush_paint(app, is_dirty);
347        if cfg!(debug_assertions) {
348            app.get_mut(self).debug_doing_paint = false;
349        }
350    }
351
352    /// Calls the `on_flushed_paint` callback given to [`new`](Self::new), if there is one.
353    fn did_flush_paint(self: Handle<Self>, app: &mut App, is_dirty: bool) {
354        if let Some(on_flushed_paint) = app.get(self).on_flushed_paint.clone() {
355            on_flushed_paint(app, is_dirty);
356        }
357    }
358
359    /// The paint pass over this owner's dirty render objects, deepest first; answers whether
360    /// there were any.
361    fn paint_dirty_nodes(self: Handle<Self>, app: &mut App) -> bool {
362        let mut dirty_nodes = std::mem::take(&mut app.get_mut(self).nodes_needing_paint);
363        let is_dirty = !dirty_nodes.is_empty();
364        dirty_nodes.sort_by_key(|node| std::cmp::Reverse(node.depth(app)));
365        for node in dirty_nodes {
366            debug_assert!(node.layer(app).is_some() || !cfg!(debug_assertions));
367            if (node.needs_paint(app) || node.needs_composited_layer_update(app))
368                && node.owner(app) == Some(self)
369            {
370                if node
371                    .layer(app)
372                    .is_some_and(|layer| layer.as_layer().attached(app))
373                {
374                    debug_assert!(node.is_repaint_boundary(app));
375                    if node.needs_paint(app) {
376                        PaintingContext::repaint_composited_child(app, node);
377                    } else {
378                        PaintingContext::update_layer_properties(app, node);
379                    }
380                } else {
381                    node.skipped_painting_on_layer(app);
382                }
383            }
384        }
385        is_dirty
386    }
387
388    /// Mark this [`PipelineOwner`] as attached to the given [`PipelineManifold`].
389    ///
390    /// Typically, this is only called directly on the root [`PipelineOwner`].
391    /// Children are automatically attached to their parent's [`PipelineManifold`]
392    /// when [`adopt_child`](Self::adopt_child) is called.
393    pub fn attach(self: Handle<Self>, app: &mut App, manifold: Rc<dyn PipelineManifold>) {
394        debug_assert!(app.get(self).manifold.is_none());
395        app.get_mut(self).manifold = Some(Rc::clone(&manifold));
396        let children: Vec<Handle<PipelineOwner>> = app
397            .get(self)
398            .children
399            .iter()
400            .map(|child| child.handle())
401            .collect();
402        for child in children {
403            child.attach(app, Rc::clone(&manifold));
404        }
405    }
406
407    /// Mark this [`PipelineOwner`] as detached.
408    ///
409    /// Typically, this is only called directly on the root [`PipelineOwner`].
410    /// Children are automatically detached from their parent's [`PipelineManifold`]
411    /// when [`drop_child`](Self::drop_child) is called.
412    pub fn detach(self: Handle<Self>, app: &mut App) {
413        debug_assert!(app.get(self).manifold.is_some());
414        app.get_mut(self).manifold = None;
415        let children: Vec<Handle<PipelineOwner>> = app
416            .get(self)
417            .children
418            .iter()
419            .map(|child| child.handle())
420            .collect();
421        for child in children {
422            child.detach(app);
423        }
424    }
425
426    /// Adds `child` to this [`PipelineOwner`].
427    pub fn adopt_child(self: Handle<Self>, app: &mut App, child: Ref<PipelineOwner>) {
428        debug_assert!(app.get(&child).debug_parent.is_none());
429        debug_assert!(!app.get(self).children.contains(&child));
430        debug_assert!(
431            !app.get(self).debug_doing_child_layout,
432            "Cannot modify child list after layout."
433        );
434        let kept = child;
435        let child = kept.handle();
436        app.get_mut(self).children.push(kept);
437        if cfg!(debug_assertions) {
438            app.get_mut(child).debug_parent = Some(self);
439        }
440        if let Some(manifold) = app.get(self).manifold.clone() {
441            child.attach(app, manifold);
442        }
443    }
444
445    /// Removes a child [`PipelineOwner`] previously added via
446    /// [`adopt_child`](Self::adopt_child).
447    pub fn drop_child(self: Handle<Self>, app: &mut App, child: Handle<PipelineOwner>) {
448        debug_assert_eq!(app.get(child).debug_parent, Some(self));
449        debug_assert!(app.get(self).children.iter().any(|kept| kept == child));
450        debug_assert!(
451            !app.get(self).debug_doing_child_layout,
452            "Cannot modify child list after layout."
453        );
454        let children = &mut app.get_mut(self).children;
455        let index = children
456            .iter()
457            .position(|candidate| candidate == child)
458            .expect("child was in the list");
459        children.remove(index);
460        if cfg!(debug_assertions) {
461            app.get_mut(child).debug_parent = None;
462        }
463        if app.get(self).manifold.is_some() {
464            child.detach(app);
465        }
466    }
467
468    /// Calls `visitor` for each immediate child of this [`PipelineOwner`].
469    pub fn visit_children(
470        self: Handle<Self>,
471        app: &App,
472        visitor: &mut dyn FnMut(Handle<PipelineOwner>),
473    ) {
474        for child in &app.get(self).children {
475            visitor(child.handle());
476        }
477    }
478}
479
480#[cfg(test)]
481mod tests {
482    use std::cell::{Cell, RefCell};
483    use std::rc::Rc;
484
485    use waft_embedder::Size;
486    use waft_foundation::{App, AppCell};
487
488    use super::*;
489    use crate::box_::{RenderBox, RenderBoxData};
490    use crate::object::{RenderHandle, RenderObject, RenderObjectData};
491
492    struct TestLeaf {
493        render_object: RenderObjectData,
494        render_box: RenderBoxData,
495        on_layout: Option<Rc<dyn Fn()>>,
496    }
497
498    impl RenderObject for TestLeaf {
499        crate::render_object_accessors!();
500
501        fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
502            if let Some(on_layout) = self.get(app).on_layout.clone() {
503                on_layout();
504            }
505            let size = self
506                .render_box_data(app)
507                .constraints
508                .as_ref()
509                .map(|constraints| constraints.smallest())
510                .unwrap_or(Size::ZERO);
511            self.set_size(app, size);
512        }
513    }
514
515    impl RenderBox for TestLeaf {
516        crate::render_box_accessors!();
517    }
518
519    fn leaf(on_layout: Option<Rc<dyn Fn()>>) -> TestLeaf {
520        TestLeaf {
521            render_object: RenderObjectData::new(),
522            render_box: RenderBoxData::new(),
523            on_layout,
524        }
525    }
526
527    /// `pipeline_owner_tree_test.dart`: parent's render objects are laid out
528    /// before child's render objects.
529    #[test]
530    fn parent_pipeline_lays_out_before_child_pipeline() {
531        let cell = AppCell::new();
532        let mut app = cell.borrow_mut();
533        let log = Rc::new(RefCell::new(Vec::new()));
534
535        let root_log = Rc::clone(&log);
536        let root_node = RenderHandle::new_box(
537            &mut app,
538            leaf(Some(Rc::new(move || {
539                root_log.borrow_mut().push("layout parent")
540            }))),
541        );
542        let root = PipelineOwner::new(&mut app, None, None);
543        root.set_root_node(&mut app, Some(root_node.as_object()));
544        root_node.schedule_initial_layout(&mut app);
545
546        let child_log = Rc::clone(&log);
547        let child_node = RenderHandle::new_box(
548            &mut app,
549            leaf(Some(Rc::new(move || {
550                child_log.borrow_mut().push("layout child")
551            }))),
552        );
553        let child = PipelineOwner::new(&mut app, None, None);
554        child.set_root_node(&mut app, Some(child_node.as_object()));
555        child_node.schedule_initial_layout(&mut app);
556
557        root.adopt_child(&mut app, child);
558        root.flush_layout(&mut app);
559        assert_eq!(*log.borrow(), ["layout parent", "layout child"]);
560    }
561
562    #[test]
563    fn set_root_node_attaches_and_replacing_detaches() {
564        let cell = AppCell::new();
565        let mut app = cell.borrow_mut();
566        let owner = PipelineOwner::new(&mut app, None, None);
567        let first = RenderHandle::new_box(&mut app, leaf(None));
568        let second = RenderHandle::new_box(&mut app, leaf(None));
569        owner.set_root_node(&mut app, Some(first.as_object()));
570        assert!(first.attached(&app));
571        assert_eq!(first.owner(&app), Some(owner.handle()));
572
573        owner.set_root_node(&mut app, Some(second.as_object()));
574        assert!(!first.attached(&app));
575        assert!(second.attached(&app));
576    }
577
578    #[test]
579    fn schedule_initial_layout_then_flush() {
580        let cell = AppCell::new();
581        let mut app = cell.borrow_mut();
582        let owner = PipelineOwner::new(&mut app, None, None);
583        let node = RenderHandle::new_box(&mut app, leaf(None));
584        owner.set_root_node(&mut app, Some(node.as_object()));
585        node.schedule_initial_layout(&mut app);
586        assert!(node.debug_needs_layout(&app));
587        owner.flush_layout(&mut app);
588        assert!(!node.debug_needs_layout(&app));
589        assert_eq!(node.size(&app), Size::ZERO);
590    }
591
592    /// Counts the visual updates requested through it.
593    struct CountingManifold {
594        requests: Cell<u32>,
595    }
596
597    impl PipelineManifold for CountingManifold {
598        fn request_visual_update(&self, _app: &mut App) {
599            self.requests.set(self.requests.get() + 1);
600        }
601    }
602
603    #[test]
604    fn an_owner_without_a_callback_requests_visual_updates_through_its_manifold() {
605        let cell = AppCell::new();
606        let mut app = cell.borrow_mut();
607        let manifold = Rc::new(CountingManifold {
608            requests: Cell::new(0),
609        });
610        let root = PipelineOwner::new(&mut app, None, None);
611        let adopted_before = PipelineOwner::new(&mut app, None, None);
612        root.adopt_child(&mut app, adopted_before.clone());
613
614        adopted_before.request_visual_update(&mut app);
615        assert_eq!(
616            manifold.requests.get(),
617            0,
618            "unattached owners request nothing"
619        );
620
621        root.attach(&mut app, manifold.clone());
622        root.request_visual_update(&mut app);
623        adopted_before.request_visual_update(&mut app);
624        assert_eq!(
625            manifold.requests.get(),
626            2,
627            "attach reaches existing children"
628        );
629
630        let adopted_after = PipelineOwner::new(&mut app, None, None);
631        root.adopt_child(&mut app, adopted_after.clone());
632        adopted_after.request_visual_update(&mut app);
633        assert_eq!(manifold.requests.get(), 3, "adopt_child attaches the child");
634
635        root.drop_child(&mut app, adopted_after.handle());
636        adopted_after.request_visual_update(&mut app);
637        assert_eq!(manifold.requests.get(), 3, "drop_child detaches the child");
638
639        root.detach(&mut app);
640        adopted_before.request_visual_update(&mut app);
641        assert_eq!(manifold.requests.get(), 3, "detach reaches the children");
642    }
643
644    #[test]
645    fn a_callback_takes_precedence_over_the_manifold() {
646        let cell = AppCell::new();
647        let mut app = cell.borrow_mut();
648        let manifold = Rc::new(CountingManifold {
649            requests: Cell::new(0),
650        });
651        let callbacks = Rc::new(Cell::new(0));
652        let owner = PipelineOwner::new(
653            &mut app,
654            Some(Listener::new({
655                let callbacks = callbacks.clone();
656                move |_app| callbacks.set(callbacks.get() + 1)
657            })),
658            None,
659        );
660        owner.attach(&mut app, manifold.clone());
661        owner.request_visual_update(&mut app);
662        assert_eq!((callbacks.get(), manifold.requests.get()), (1, 0));
663    }
664}