//! Conditional content.
use super::*;
use crate::mangle;
// ── Conditional content ──
mangle::result_types! {
/// `_ConditionalContent<AnyView, AnyView>`
/// `_ConditionalContent<AnyView, AnyView>`
pub(crate) CONDITIONAL_T = ty("_ConditionalContent").of(vec![ty("AnyView"), ty("AnyView")]);
}
/// The `if`/`else` a `ViewBuilder` produces.
///
/// This matters beyond picking a view. Branching in Rust and handing SwiftUI the
/// winner erases the branch: both arms are `AnyView`, so SwiftUI sees one view
/// whose *value* changed. `_ConditionalContent` tells it a *branch* changed,
/// which is what drives insertion and removal transitions and keeps each arm's
/// identity — and therefore its `@State` — separate.
///
/// The type is one `AnyView` followed by a one-byte tag, so it is built directly.
pub fn conditional(flag: bool, if_true: &ViewHandle, if_false: &ViewHandle) -> ViewHandle {
unsafe {
let (meta, wt, size) = result_type(&CONDITIONAL_T);
let mut buf = vec![0u8; size];
let chosen = if flag { if_true } else { if_false };
let owned = chosen.clone();
buf[..8].copy_from_slice(&owned.to_anyview_word().to_le_bytes());
buf[8] = u8::from(!flag); // 0 = first branch, 1 = second
wrap_buf(&buf, meta, wt)
}
}