use nativeview_engine::InteractionTracker;
use nativeview_store::{NodeId, NodeStore, NvTable, Prop, RowPatchView, ScalarField};
use nativeview_tables::interaction::visit_state_rules_in_cascade;
use nativeview_tables::media::{ConditionalTier, cascade_display};
use nativeview_tables::semantics::SemanticStyleOracle;
use nativeview_tables::snapshots::transform_from_patch_view;
use nativeview_tables::tables::{
ContainerType, Display, GridItem, GridLayout, Interaction, Isolation, Layout, LayoutRef, Overflow, Position,
StateRules, StateRulesView, Transform as TransformTable, Typography, Visibility, Visual,
is_closed_overlay,
};
use nativeview_tables::transform::Transform2D;
use crate::StoreLayoutEngine;
use crate::scene::OverlayState;
pub trait ResponsivePatches {
fn visit_responsive_patches<'a>(
&self,
store: &'a NodeStore,
id: NodeId,
f: &mut dyn FnMut(ConditionalTier, RowPatchView<'a>),
);
fn is_show_on_hidden(&self, store: &NodeStore, id: NodeId) -> bool;
}
impl<L: StoreLayoutEngine + ?Sized> ResponsivePatches for L {
#[inline]
fn visit_responsive_patches<'a>(
&self,
store: &'a NodeStore,
id: NodeId,
f: &mut dyn FnMut(ConditionalTier, RowPatchView<'a>),
) {
StoreLayoutEngine::visit_responsive_patches(self, store, id, f);
}
#[inline]
fn is_show_on_hidden(&self, store: &NodeStore, id: NodeId) -> bool {
StoreLayoutEngine::is_show_on_hidden(self, store, id)
}
}
pub struct StyleCascade<'a, R: ResponsivePatches + ?Sized = dyn ResponsivePatches> {
pub responsive: &'a R,
pub interaction: Option<&'a InteractionTracker>,
pub inert: Option<&'a OverlayState>,
}
impl<R: ResponsivePatches + ?Sized> Clone for StyleCascade<'_, R> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<R: ResponsivePatches + ?Sized> Copy for StyleCascade<'_, R> {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct StackingStyle {
pub(crate) display: Display,
pub(crate) position: Position,
pub(crate) z_index: Option<i32>,
pub(crate) opacity: f32,
pub(crate) isolation: Isolation,
pub(crate) transform: Option<Transform2D>,
pub(crate) visibility: Option<Visibility>,
pub(crate) overflow_x: Option<Overflow>,
pub(crate) overflow_y: Option<Overflow>,
}
impl<R: ResponsivePatches + ?Sized> StyleCascade<'_, R> {
pub(crate) fn stacking_style(&self, store: &NodeStore, id: NodeId) -> StackingStyle {
let visual = store.try_row::<Visual>(id);
let mut style = StackingStyle {
display: Display::None,
position: Position::Static,
z_index: visual.and_then(|v| v.z_index()),
opacity: visual.map_or(1.0, |v| v.opacity()),
isolation: visual.map_or(Isolation::Auto, |v| v.isolation()),
transform: transform_of_row(store, id),
visibility: visual.map(|v| v.visibility()),
overflow_x: visual.map(|v| v.overflow_x()),
overflow_y: visual.map(|v| v.overflow_y()),
};
let effective = self.fold_box(store, id, |_, pv| {
if let Some(z) = pv.get_prop(Visual::Z_INDEX) {
style.z_index = z;
}
if let Some(o) = pv.get_prop(Visual::OPACITY) {
style.opacity = o;
}
if let Some(i) = pv.get_prop(Visual::ISOLATION) {
style.isolation = i;
}
if let Some(v) = pv.get_prop(Visual::VISIBILITY) {
style.visibility = Some(v);
}
if let Some(x) = pv.get_prop(Visual::OVERFLOW_X) {
style.overflow_x = Some(x);
}
if let Some(y) = pv.get_prop(Visual::OVERFLOW_Y) {
style.overflow_y = Some(y);
}
if let Some(tf) = transform_from_patch_view(pv) {
style.transform = (!tf.is_identity()).then_some(tf);
}
});
style.display = effective.display;
style.position = effective.position;
style
}
#[inline]
pub fn visit<'s>(
&self,
store: &'s NodeStore,
id: NodeId,
mut f: impl FnMut(Option<ConditionalTier>, RowPatchView<'s>),
) {
self.responsive
.visit_responsive_patches(store, id, &mut |tier, pv| f(Some(tier), pv));
self.visit_state(store, id, |pv| f(None, pv));
}
#[inline]
fn visit_state<'s>(&self, store: &'s NodeStore, id: NodeId, f: impl FnMut(RowPatchView<'s>)) {
let Some(interaction) = self.interaction else {
return;
};
let Some(rules) = state_rules_of(store, id) else {
return;
};
let state = interaction.state(id);
if !state.is_empty() {
visit_state_rules_in_cascade(rules, state, f);
}
}
#[inline]
pub fn effective_box(&self, store: &NodeStore, id: NodeId) -> EffectiveBox {
self.fold_box(store, id, |_, _| {})
}
#[inline]
fn fold_box<'s>(
&self,
store: &'s NodeStore,
id: NodeId,
mut also: impl FnMut(Option<ConditionalTier>, RowPatchView<'s>),
) -> EffectiveBox {
if self.responsive.is_show_on_hidden(store, id) {
return EffectiveBox::FORCED_HIDDEN;
}
let mut effective = EffectiveBox::base(store, id);
if effective.forced_hidden {
return effective;
}
self.responsive
.visit_responsive_patches(store, id, &mut |tier, pv| {
effective.apply(store, id, Some(tier), pv);
also(Some(tier), pv);
});
self.fold_state(store, id, effective, |pv| also(None, pv))
}
#[inline]
pub(crate) fn apply_state(&self, store: &NodeStore, id: NodeId, base: EffectiveBox) -> EffectiveBox {
self.fold_state(store, id, base, |_| {})
}
#[inline]
fn fold_state<'s>(
&self,
store: &'s NodeStore,
id: NodeId,
mut base: EffectiveBox,
mut also: impl FnMut(RowPatchView<'s>),
) -> EffectiveBox {
if !base.forced_hidden {
self.visit_state(store, id, |pv| {
base.apply(store, id, None, pv);
also(pv);
});
}
if base.display == Display::None {
base.container_type = ContainerType::Normal;
}
base
}
#[inline]
pub fn patched<T: NvTable, V: ScalarField>(
&self,
store: &NodeStore,
id: NodeId,
prop: Prop<T, V>,
) -> Option<V> {
let mut out = None;
self.visit(store, id, |_, pv| {
if let Some(v) = pv.get_prop(prop) {
out = Some(v);
}
});
out
}
#[inline]
pub fn display(&self, store: &NodeStore, id: NodeId) -> Display {
self.effective_box(store, id).display
}
}
impl<R: ResponsivePatches + ?Sized> SemanticStyleOracle for StyleCascade<'_, R> {
#[inline]
fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
!store.is_live(id) || self.effective_box(store, id).is_none()
}
#[inline]
fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
id != NodeId::ROOT && store.is_live(id) && self.effective_box(store, id).is_contents()
}
#[inline]
fn visibility(&self, store: &NodeStore, id: NodeId) -> Visibility {
self.patched(store, id, Visual::VISIBILITY).unwrap_or_else(|| {
store
.try_row::<Visual>(id)
.map_or(Visibility::Inherit, |v| v.visibility())
})
}
#[inline]
fn is_inert(&self, store: &NodeStore, id: NodeId) -> bool {
self.inert.is_some_and(|o| o.is_inert_under_modal(store, id))
}
#[inline]
fn is_node_disabled(&self, store: &NodeStore, id: NodeId) -> bool {
self.interaction.is_some_and(|i| i.is_disabled(id))
|| store.try_row::<Interaction>(id).is_some_and(|i| i.disabled())
}
}
#[inline]
pub(crate) fn transform_of_row(store: &NodeStore, id: NodeId) -> Option<Transform2D> {
let t = store.try_row::<TransformTable>(id)?;
let tf = Transform2D {
translate_x: t.translate_x(),
translate_y: t.translate_y(),
scale_x: t.scale_x(),
scale_y: t.scale_y(),
rotate_deg: t.rotate_deg(),
origin_x: t.origin_x(),
origin_y: t.origin_y(),
};
(!tf.is_identity()).then_some(tf)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EffectiveBox {
pub display: Display,
pub position: Position,
pub container_type: ContainerType,
pub forced_hidden: bool,
}
impl EffectiveBox {
pub(crate) const FORCED_HIDDEN: Self = Self {
display: Display::None,
position: Position::Static,
container_type: ContainerType::Normal,
forced_hidden: true,
};
#[inline]
pub(crate) fn from_layout(
store: &NodeStore,
id: NodeId,
layout: Option<LayoutRef<'_>>,
show_on_hidden: bool,
) -> Self {
if show_on_hidden || is_closed_overlay(store, id) {
return Self::FORCED_HIDDEN;
}
layout.map_or(
Self {
display: Display::default(),
position: Position::Static,
container_type: ContainerType::Normal,
forced_hidden: false,
},
|l| Self {
display: l.display(),
position: l.position(),
container_type: l.container_type(),
forced_hidden: false,
},
)
}
#[inline]
fn base(store: &NodeStore, id: NodeId) -> Self {
Self::from_layout(store, id, store.try_row::<Layout>(id), false)
}
#[inline]
fn apply(
&mut self,
store: &NodeStore,
id: NodeId,
tier: Option<ConditionalTier>,
pv: RowPatchView<'_>,
) {
if let Some(d) = pv.get_prop(Layout::DISPLAY) {
self.display = cascade_display(store, id, self.display, tier, d);
}
if let Some(p) = pv.get_prop(Layout::POSITION) {
self.position = p;
}
if let Some(c) = pv.get_prop(Layout::CONTAINER_TYPE) {
self.container_type = c;
}
}
#[inline]
pub fn is_none(self) -> bool {
self.display == Display::None
}
#[inline]
pub fn is_contents(self) -> bool {
self.display == Display::Contents
}
#[inline]
pub fn is_out_of_flow(self) -> bool {
!matches!(self.display, Display::None | Display::Contents)
&& matches!(self.position, Position::Absolute | Position::Fixed)
}
}
pub fn state_rules_patch_box(store: &NodeStore, id: NodeId) -> bool {
state_rules_of(store, id).is_some_and(|rules| {
rules.iter().any(|(_, pv)| {
pv.get_prop(Layout::DISPLAY).is_some()
|| pv.get_prop(Layout::POSITION).is_some()
|| pv.get_prop(Layout::CONTAINER_TYPE).is_some()
})
})
}
pub(crate) fn state_rules_touch_layout(store: &NodeStore, id: NodeId) -> bool {
state_rules_of(store, id).is_some_and(|rules| {
rules.iter().any(|(_, pv)| {
pv.has_table::<Layout>()
|| pv.has_table::<GridLayout>()
|| pv.has_table::<GridItem>()
|| pv.has_table::<Typography>()
})
})
}
#[inline]
pub(crate) fn state_rules_of(store: &NodeStore, id: NodeId) -> Option<StateRulesView<'_>> {
store
.try_row::<Interaction>(id)
.filter(|row| !row.state_rules().is_none())
.and_then(|row| store.resource::<StateRules>(row.state_rules()))
}
pub struct NoResponsivePatches;
impl ResponsivePatches for NoResponsivePatches {
#[inline]
fn visit_responsive_patches<'a>(
&self,
_store: &'a NodeStore,
_id: NodeId,
_f: &mut dyn FnMut(ConditionalTier, RowPatchView<'a>),
) {
}
#[inline]
fn is_show_on_hidden(&self, _store: &NodeStore, _id: NodeId) -> bool {
false
}
}