bevy-react 0.3.0

Drive bevy_ui from a React app over an embedded V8 runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! The `backdropFilter` chain components — the backdrop instance of
//! [`resolve_chains`](super::resolve_chains).
//!
//! `backdropFilter` shares everything with the content `filter` style except
//! its source: the chain filters what is rendered *behind* the node (v1: the
//! camera's post-processed 3D frame), and the result composites as an opaque
//! quad under the node's own content. Wire format, registry validation,
//! param packing, and interpolation are all the shared `filters` machinery;
//! this module only names the second channel's component pair and its
//! resolver instance parameters.
//!
//! One deliberate semantic difference: resolved backdrop chains are ALWAYS
//! `always_dirty` ([`ChainInput::FORCE_ALWAYS_DIRTY`]). The backdrop source
//! is the live frame — the snapshot re-blits and the chain re-runs every
//! frame, so caching a backdrop filter output is never valid.

use bevy::prelude::*;

use super::resolve::{ChainInput, ResolvedChain, ResolvedFilterChain};
use super::wire::FilterChain;

/// The wire `backdropFilter` chain of a node, mirrored off the applied style
/// by `crate::ui_map::apply_style_masked`'s BACKDROP arm — present iff the
/// style carries a non-empty chain. Same contract as
/// [`FilterInput`](super::FilterInput): the style apply owns writes (the
/// applied style may be hover/press/focus-merged), the resolver only reads.
#[derive(Component, Debug, Clone, Default, PartialEq)]
pub struct BackdropInput(pub FilterChain);

impl ChainInput for BackdropInput {
    const KIND_UNKNOWN: &'static str = "backdropFilterUnknown";
    const KIND_PARAMS: &'static str = "backdropFilterParams";
    const FORCE_ALWAYS_DIRTY: bool = true;
    fn chain(&self) -> &FilterChain {
        &self.0
    }
}

/// A node's fully resolved `backdropFilter` chain — a newtype over
/// [`ResolvedFilterChain`] so extract, transitions, animations, and devtools
/// reuse the inner type via `.0`. Attached to promoted roots by the backdrop
/// [`resolve_chains`](super::resolve_chains) instance; absent when the chain
/// has no valid entries. Removed on demotion by
/// `evaluate_layer_promotions`'s cleanup (like the content chain).
#[derive(Component, Debug, Clone, Default)]
pub struct ResolvedBackdropChain(pub ResolvedFilterChain);

impl ResolvedChain for ResolvedBackdropChain {
    fn from_inner(inner: ResolvedFilterChain) -> Self {
        Self(inner)
    }
    fn inner(&self) -> &ResolvedFilterChain {
        &self.0
    }
    fn inner_mut(&mut self) -> &mut ResolvedFilterChain {
        &mut self.0
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::super::test_util::{
        anim_app, create, drain_dirt, ease_app, entity_of, resolve_app, tick, update,
    };
    use super::*;
    use crate::layer::{LayerContentDirt, PromotedLayer};

    /// A `backdropFilter` create resolves into a [`ResolvedBackdropChain`] on
    /// the promoted root — and the chain is `always_dirty` even for a filter
    /// with no `USES_TIME` (the forced backdrop semantics: the source frame
    /// is live). The content-chain component stays absent — independent
    /// channels.
    #[test]
    fn backdrop_create_attaches_forced_always_dirty_chain() {
        let (mut app, ops_tx) = resolve_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": { "backdropFilter": { "name": "grayscale" } } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        assert!(app.world().get::<PromotedLayer>(e).is_some(), "promoted");
        assert!(
            app.world().get::<BackdropInput>(e).is_some(),
            "input stamped"
        );
        let chain = app
            .world()
            .get::<ResolvedBackdropChain>(e)
            .expect("backdrop chain resolved");
        assert_eq!(chain.0.version, 1);
        assert_eq!(chain.0.passes.len(), 1);
        assert_eq!(chain.0.passes[0].params[0].w, 1.0, "full grayscale");
        assert!(chain.0.always_dirty, "backdrop chains are forced live");
        assert!(
            app.world()
                .get::<super::super::ResolvedFilterChain>(e)
                .is_none(),
            "content channel untouched"
        );
    }

    /// A backdrop param delta bumps the version and lands in
    /// `composite_only` — never `nodes`: a backdrop delta must not dirty the
    /// subtree capture (it only touches pixels behind the node).
    #[test]
    fn backdrop_param_update_is_composite_only() {
        let (mut app, ops_tx) = resolve_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": { "backdropFilter": { "name": "grayscale" } } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        drain_dirt(&mut app);

        ops_tx
            .send(vec![update(
                1,
                json!({ "style": { "backdropFilter": {
                    "name": "grayscale", "params": { "amount": 0.5 }
                } } }),
                &[],
            )])
            .unwrap();
        app.update();
        let chain = app.world().get::<ResolvedBackdropChain>(e).expect("chain");
        assert_eq!(chain.0.version, 2);
        assert_eq!(chain.0.passes[0].params[0].w, 0.5);
        let dirt = app.world().resource::<LayerContentDirt>();
        assert!(dirt.composite_only.contains(&e), "{dirt:?}");
        assert!(!dirt.nodes.contains(&e), "{dirt:?}");
    }

    /// Backdrop validation failures warn under the backdrop-specific kinds
    /// (`backdropFilterUnknown`/`backdropFilterParams`), attributed to the
    /// node — devtools routes them at the `backdropFilter` style row.
    #[cfg(all(feature = "devtools", debug_assertions))]
    #[test]
    fn backdrop_validation_warns_with_backdrop_kinds() {
        let _lock = crate::diag::test_lock();
        crate::diag::arm_runtime();
        let _ = crate::diag::take_runtime_warnings();

        let (mut app, ops_tx) = resolve_app();
        ops_tx
            .send(vec![create(
                7,
                json!({ "style": { "backdropFilter": [
                    { "name": "nope" },
                    { "name": "blur", "params": { "radius": "50%" } },
                    { "name": "sepia" }
                ] } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 7);
        let chain = app.world().get::<ResolvedBackdropChain>(e).expect("chain");
        assert_eq!(chain.0.passes.len(), 1, "only sepia survives");

        let mut kinds: Vec<_> = crate::diag::take_runtime_warnings()
            .into_iter()
            .filter(|w| w.node == Some(7))
            .map(|w| w.kind)
            .collect();
        kinds.sort();
        assert_eq!(kinds, ["backdropFilterParams", "backdropFilterUnknown"]);
    }

    /// Unsetting `backdropFilter` on a node held promoted by another reason
    /// removes the resolved chain (the `RemovedComponents` cleanup path);
    /// full demotion removes it too (the evaluator's cleanup).
    #[test]
    fn unset_removes_chain_while_promoted_and_on_demote() {
        let (mut app, ops_tx) = resolve_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": {
                    "cache": "always",
                    "backdropFilter": { "name": "grayscale" }
                } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        assert!(app.world().get::<ResolvedBackdropChain>(e).is_some());

        ops_tx
            .send(vec![update(1, json!({}), &["backdropFilter"])])
            .unwrap();
        app.update();
        assert!(
            app.world().get::<PromotedLayer>(e).is_some(),
            "cache: always still holds the layer"
        );
        assert!(app.world().get::<BackdropInput>(e).is_none(), "input gone");
        assert!(
            app.world().get::<ResolvedBackdropChain>(e).is_none(),
            "chain cleaned up while still promoted"
        );

        ops_tx
            .send(vec![update(
                1,
                json!({ "style": { "backdropFilter": { "name": "sepia" } } }),
                &["cache"],
            )])
            .unwrap();
        app.update();
        assert!(app.world().get::<ResolvedBackdropChain>(e).is_some());
        ops_tx
            .send(vec![update(1, json!({}), &["backdropFilter"])])
            .unwrap();
        app.update();
        assert!(app.world().get::<PromotedLayer>(e).is_none(), "demoted");
        assert!(
            app.world().get::<ResolvedBackdropChain>(e).is_none(),
            "demote removes the resolved chain"
        );
    }

    /// `transition: { backdropFilter }` eases the backdrop chain's packed
    /// params (composite-only dirt each easing frame) and settles bit-exact
    /// on the resolver's own output — the second channel runs the same
    /// whole-value machinery as `filter`.
    #[test]
    fn backdrop_transition_eases_and_settles_exactly() {
        let (mut app, ops_tx) = ease_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": {
                    "backdropFilter": { "name": "grayscale", "params": { "amount": 0.0 } },
                    "transition": { "backdropFilter": { "duration": 1000, "easing": "linear" } },
                } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        assert_eq!(
            app.world()
                .get::<ResolvedBackdropChain>(e)
                .unwrap()
                .0
                .passes[0]
                .params[0]
                .w,
            0.0,
            "mount snaps, no fade-in"
        );
        drain_dirt(&mut app);

        ops_tx
            .send(vec![update(
                1,
                json!({ "style": { "backdropFilter": {
                    "name": "grayscale", "params": { "amount": 1.0 }
                } } }),
                &[],
            )])
            .unwrap();
        tick(&mut app, 0.5);
        {
            let chain = &app.world().get::<ResolvedBackdropChain>(e).unwrap().0;
            let w = chain.passes[0].params[0].w;
            assert!(w > 0.0 && w < 1.0, "mid-ease: {w}");
            assert!(chain.always_dirty, "forced-live survives the ease write");
            let dirt = app.world().resource::<LayerContentDirt>();
            assert!(dirt.composite_only.contains(&e), "{dirt:?}");
            assert!(!dirt.nodes.contains(&e), "{dirt:?}");
        }

        tick(&mut app, 0.6);
        let settled_version = {
            let world = app.world();
            let chain = &world.get::<ResolvedBackdropChain>(e).unwrap().0;
            let expected =
                (world.resource::<crate::filters::FilterRegistry>().entries["grayscale"].resolve)(
                    &json!({ "amount": 1.0 }),
                    world.resource::<bevy::asset::AssetServer>(),
                )
                .unwrap();
            assert_eq!(chain.passes, expected, "settles on the resolver's output");
            chain.version
        };
        drain_dirt(&mut app);
        tick(&mut app, 0.25);
        assert_eq!(
            app.world()
                .get::<ResolvedBackdropChain>(e)
                .unwrap()
                .0
                .version,
            settled_version,
            "settled: no more churn"
        );
    }

    /// The two chains are independent transition channels: easing one leaves
    /// the other's params and version untouched.
    #[test]
    fn filter_and_backdrop_channels_ease_independently() {
        let (mut app, ops_tx) = ease_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": {
                    "filter": { "name": "grayscale", "params": { "amount": 0.0 } },
                    "backdropFilter": { "name": "sepia", "params": { "amount": 0.0 } },
                    "transition": { "all": { "duration": 1000, "easing": "linear" } },
                } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        drain_dirt(&mut app);

        // Retarget ONLY the backdrop chain.
        ops_tx
            .send(vec![update(
                1,
                json!({ "style": { "backdropFilter": {
                    "name": "sepia", "params": { "amount": 1.0 }
                } } }),
                &[],
            )])
            .unwrap();
        tick(&mut app, 0.5);
        let backdrop = &app.world().get::<ResolvedBackdropChain>(e).unwrap().0;
        let w = backdrop.passes[0].params[1].x;
        assert!(w > 0.0 && w < 1.0, "backdrop mid-ease: {w}");
        let content = app
            .world()
            .get::<crate::filters::ResolvedFilterChain>(e)
            .unwrap();
        assert_eq!(content.passes[0].params[0].w, 0.0, "content untouched");
        assert_eq!(content.version, 1, "content version quiet");
    }

    /// Easing TOWARD an unset `backdropFilter` snaps (the documented
    /// empty-chain rule): unsetting demotes and removes the resolved chain
    /// the same frame — no lingering eased writes on later frames.
    #[test]
    fn backdrop_transition_to_unset_snaps() {
        let (mut app, ops_tx) = ease_app();
        ops_tx
            .send(vec![create(
                1,
                json!({ "style": {
                    "backdropFilter": { "name": "grayscale" },
                    "transition": { "backdropFilter": { "duration": 1000 } },
                } }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        ops_tx
            .send(vec![update(1, json!({}), &["backdropFilter"])])
            .unwrap();
        tick(&mut app, 0.1);
        assert!(
            app.world().get::<ResolvedBackdropChain>(e).is_none(),
            "unset demotes + removes — snap, no ease"
        );
        tick(&mut app, 0.1);
        assert!(app.world().get::<ResolvedBackdropChain>(e).is_none());
    }

    /// Inline `{ animated }` backdrop-param bindings drive the
    /// backdrop chain through the whole ops → resolve → apply pipeline
    /// (blur's H+V passes both driven, physical-px rewrite, composite-only
    /// dirt), while the CONTENT chain — same node, same param name — stays
    /// untouched: the two binding namespaces are independent.
    #[test]
    fn backdrop_param_binding_follows_shared_value_through_pipeline() {
        let (mut app, ops_tx, anim_tx) = anim_app();
        anim_tx
            .send(crate::animations::AnimationCommand::Set { id: 1, value: 4.0 })
            .unwrap();
        ops_tx
            .send(vec![create(
                1,
                json!({
                    "style": {
                        "filter": { "name": "blur", "params": { "radius": 10 } },
                        "backdropFilter": { "name": "blur",
                            "params": { "radius": { "animated": { "id": 1 } } } },
                    },
                }),
            )])
            .unwrap();
        app.update();
        let e = entity_of(&app, 1);
        {
            let chain = &app.world().get::<ResolvedBackdropChain>(e).unwrap().0;
            assert_eq!(chain.passes.len(), 2, "blur expands to H+V");
            assert_eq!(chain.passes[0].params[0].x, 4.0, "H radius driven");
            assert_eq!(chain.passes[1].params[0].x, 4.0, "V radius driven");
            let content = app
                .world()
                .get::<crate::filters::ResolvedFilterChain>(e)
                .unwrap();
            assert_eq!(
                content.passes[0].params[0].x, 10.0,
                "content chain keeps its static radius"
            );
        }

        drain_dirt(&mut app);
        anim_tx
            .send(crate::animations::AnimationCommand::Set { id: 1, value: 6.0 })
            .unwrap();
        tick(&mut app, 0.016);
        {
            let chain = &app.world().get::<ResolvedBackdropChain>(e).unwrap().0;
            assert_eq!(chain.passes[0].params[0].x, 6.0);
            assert_eq!(chain.passes[1].params[0].x, 6.0);
        }
        let dirt = app.world().resource::<LayerContentDirt>();
        assert!(dirt.composite_only.contains(&e), "{dirt:?}");
        assert!(!dirt.nodes.contains(&e), "never capture dirt: {dirt:?}");
    }
}