Skip to main content

waft_widgets/widgets/
composition_layer_view.rs

1//! A composition layer the host composites, in the widget tree: Android's `SurfaceView`, over
2//! [`RenderCompositionLayerView`].
3//!
4//! A composition layer carries pixels only: it takes no input, focus or accessibility, so it is
5//! not a native view such as a web view or a map, which Flutter's platform views embed. The widget
6//! fills the box it is given and tells nobody its size: whoever holds the layer knows the layout
7//! and tells the producer. Put a `Listener` or a `MouseRegion` around it for the input it takes
8//! none of itself.
9
10use std::fmt::{self, Debug};
11use std::sync::Arc;
12
13use waft_embedder::CompositionLayer;
14use waft_foundation::App;
15use waft_rendering::{AnyRenderObject, RenderBox, RenderCompositionLayerView, RenderHandle};
16
17use crate::framework::{BuildContext, KeyRef, LeafRenderObjectWidget, RenderObjectWidget};
18
19/// Shows a composition layer where it is placed.
20#[derive(Clone)]
21pub struct CompositionLayerView {
22    /// See [`Widget::key`](crate::Widget::key).
23    pub key: Option<KeyRef>,
24    /// The layer a producer feeds (`Compositor::make_layer`), which the frames of the view the
25    /// widget is in place.
26    pub composition_layer: Arc<dyn CompositionLayer>,
27}
28
29impl CompositionLayerView {
30    pub fn new(composition_layer: Arc<dyn CompositionLayer>) -> CompositionLayerView {
31        CompositionLayerView {
32            key: None,
33            composition_layer,
34        }
35    }
36
37    pub fn key(mut self, key: KeyRef) -> CompositionLayerView {
38        self.key = Some(key);
39        self
40    }
41}
42
43impl Debug for CompositionLayerView {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.debug_struct("CompositionLayerView")
46            .field(
47                "composition_layer",
48                &Arc::as_ptr(&self.composition_layer).cast::<()>(),
49            )
50            .finish()
51    }
52}
53
54impl RenderObjectWidget for CompositionLayerView {
55    type RenderObject = RenderCompositionLayerView;
56
57    fn key(&self) -> Option<&KeyRef> {
58        self.key.as_ref()
59    }
60
61    fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
62        RenderCompositionLayerView::new(app, Arc::clone(&self.composition_layer)).as_object()
63    }
64
65    fn update_render_object(
66        &self,
67        app: &mut App,
68        _context: BuildContext,
69        render_object: RenderHandle<RenderCompositionLayerView>,
70    ) {
71        render_object.set_composition_layer(app, Arc::clone(&self.composition_layer));
72    }
73}
74
75impl LeafRenderObjectWidget for CompositionLayerView {}