bevy_react_core 0.7.0

The core bridge of bevy-react (drive bevy_ui from React over an embedded V8 runtime). Apps depend on the `bevy-react` crate.
//! Auto-stamping: a feature property that no writer reads lands on the
//! entity as a [`StyleValue<T>`] component, for the feature's own system to
//! react to (`Query<&StyleValue<Glow>>` + `Changed`), ordered
//! `.after(ReactApplySet)`.

use bevy::prelude::*;

use super::property::PropertyValue;
use super::{Style, Writer, WriterCtx};

/// The value of a stamped style property on its node (see the module doc).
/// Present exactly while the merged style (hover/press/focus variants
/// included) sets the property; updated compare-before-write.
///
/// A stamped property's value type must be unique among stamped properties
/// (a newtype such as `struct Tooltip(String)`); registering two with one
/// type panics at startup.
#[derive(Component, Debug, Clone, PartialEq)]
pub struct StyleValue<T: PropertyValue>(pub T);

/// The core writer behind auto-stamping: it reads every feature property no
/// other writer reads (the registry wires those reads) and stamps each.
pub static STAMP_WRITER: Writer = Writer {
    reads: &[],
    attrs: &[],
    writes: &[],
    apply: apply_stamps,
};

fn apply_stamps(ctx: &WriterCtx, s: &Style, ec: &mut EntityCommands) {
    for &id in ctx.styles.stamped() {
        if let Some(property) = ctx.styles.by_id(id) {
            property.stamp(s, ec, ctx.fresh);
        }
    }
}

/// Stamp `value` (or clear the component) — the typed half of
/// [`AnyStyleProperty::stamp`](super::AnyStyleProperty::stamp).
pub(super) fn stamp<T: PropertyValue>(value: Option<&T>, ec: &mut EntityCommands, fresh: bool) {
    match value {
        Some(value) => {
            ec.queue(crate::ui_map::set_if_neq_or_insert(StyleValue(
                value.clone(),
            )));
        }
        None if fresh => {}
        None => {
            ec.remove::<StyleValue<T>>();
        }
    }
}