use std::cell::Ref;
use nativeview_abi::define_bitflags;
use nativeview_store::{Length, NodeId, NodeStore, NvTable, Word};
use nativeview_tables::tables::{
AlignContent as NvAlignContent, AlignItems as NvAlignItems, AlignSelf as NvAlignSelf,
BoxSizing as NvBoxSizing, ContainerType, Display as NvDisplay,
FlexDirection as NvFlexDirection, FlexWrap as NvFlexWrap, GridLine as NvGridLine,
JustifyContent as NvJustifyContent, Layout as NvLayout, Overflow as NvOverflow,
Position as NvPosition, Visual,
};
use taffy::geometry::{Line, Point, Rect, Size};
use taffy::style::{
AlignContent, AlignItems, AlignSelf, BoxGenerationMode, BoxSizing, Contain,
ContainingBlockClaims, CoreStyle, Dimension, Direction, FlexDirection, FlexWrap,
FlexboxContainerStyle, FlexboxItemStyle, GridPlacement, LengthPercentage, LengthPercentageAuto,
Overflow, Position,
};
mod grid;
use super::responsive::ResolvedResponsiveStyle;
use crate::scene::EffectiveBox;
use super::transform_cb::has_store_transform;
define_bitflags! {
#[derive(Default)]
pub(crate) struct StyleViewFlags(u8) {
const IS_ROOT = 1 << 0;
const HOISTED = 1 << 1;
const ROOT_FILLS_VIEWPORT = 1 << 2;
const RTL = 1 << 3;
}
}
pub(crate) const DEFAULT_LAYOUT_WORDS: &[Word; 26] =
match NvLayout::DESC.defaults.first_chunk::<26>() {
Some(w) => w,
None => panic!("Layout table must have 26 words"),
};
pub(crate) enum LayoutSource<'a> {
Direct(&'a [Word; 26]),
Responsive(Ref<'a, ResolvedResponsiveStyle>),
}
pub(crate) struct StyleView<'a> {
pub(crate) store: &'a NodeStore,
pub(crate) source: LayoutSource<'a>,
pub(crate) viewport: (f32, f32),
pub(crate) id: NodeId,
pub(crate) effective: EffectiveBox,
pub(crate) flags: StyleViewFlags,
}
const _: () = assert!(core::mem::size_of::<StyleView<'_>>() <= 64);
impl<'a> StyleView<'a> {
#[inline(always)]
pub(crate) fn is_root(&self) -> bool {
self.flags.contains(StyleViewFlags::IS_ROOT)
}
#[inline(always)]
pub(crate) fn rtl(&self) -> bool {
self.flags.contains(StyleViewFlags::RTL)
}
#[inline(always)]
pub(crate) fn responsive(&self) -> Option<&ResolvedResponsiveStyle> {
match &self.source {
LayoutSource::Direct(_) => None,
LayoutSource::Responsive(r) => Some(&**r),
}
}
#[inline(always)]
pub(crate) fn layout(&self) -> nativeview_tables::tables::LayoutRef<'_> {
let words: &[Word; 26] = match &self.source {
LayoutSource::Direct(w) => w,
LayoutSource::Responsive(r) => &r.layout_words,
};
self.store.row_from_words::<NvLayout>(words)
}
#[inline(always)]
pub(crate) fn is_hidden(&self) -> bool {
self.effective.is_none()
}
#[inline(always)]
fn is_hoisted_open_overlay(&self) -> bool {
self.flags.contains(StyleViewFlags::HOISTED)
}
#[inline(always)]
pub(crate) fn effective_display(&self) -> NvDisplay {
self.effective.display
}
#[inline(always)]
pub(crate) fn effective_container_type(&self) -> ContainerType {
self.effective.container_type
}
#[inline(always)]
fn effective_border_width(&self) -> f32 {
match self.responsive() {
Some(r) => r.border_width,
None => self.store.row::<Visual>(self.id).border_width(),
}
}
#[inline(always)]
fn effective_overflow(&self) -> (NvOverflow, NvOverflow) {
match self.responsive() {
Some(r) => (r.overflow_x, r.overflow_y),
None => {
let v = self.store.row::<Visual>(self.id);
(v.overflow_x(), v.overflow_y())
}
}
}
#[inline(always)]
pub(crate) fn dim(l: Length) -> Dimension {
if l.is_points() {
Dimension::length(l.value())
} else if l.is_percent() {
Dimension::percent(l.value() / 100.0)
} else {
Dimension::auto()
}
}
#[inline(always)]
pub(crate) fn lpa(l: Length) -> LengthPercentageAuto {
if l.is_points() {
LengthPercentageAuto::length(l.value())
} else if l.is_percent() {
LengthPercentageAuto::percent(l.value() / 100.0)
} else {
LengthPercentageAuto::auto()
}
}
#[inline(always)]
pub(crate) fn lp(l: Length) -> LengthPercentage {
if l.is_percent() {
LengthPercentage::percent(l.value() / 100.0)
} else if l.is_points() {
LengthPercentage::length(l.value())
} else {
LengthPercentage::length(0.0)
}
}
#[inline(always)]
pub(crate) fn placement(start: NvGridLine, end: NvGridLine) -> Line<GridPlacement<String>> {
fn map_line(g: NvGridLine) -> GridPlacement<String> {
match g {
NvGridLine::Auto => GridPlacement::Auto,
NvGridLine::Line(n) => GridPlacement::Line(n.into()),
NvGridLine::Span(n) => GridPlacement::Span(n),
}
}
Line {
start: map_line(start),
end: map_line(end),
}
}
}
impl CoreStyle for StyleView<'_> {
type CustomIdent = String;
#[inline]
fn box_generation_mode(&self) -> BoxGenerationMode {
if self.is_hidden() {
BoxGenerationMode::None
} else {
BoxGenerationMode::Normal
}
}
#[inline]
fn contain(&self) -> Contain {
let ct = self.effective_container_type();
if ct != ContainerType::Normal
&& (self.is_root() || self.effective_display() != NvDisplay::Contents)
{
Contain::LAYOUT
} else {
Contain::NONE
}
}
#[inline]
fn box_sizing(&self) -> BoxSizing {
match self.layout().box_sizing() {
NvBoxSizing::BorderBox => BoxSizing::BorderBox,
NvBoxSizing::ContentBox => BoxSizing::ContentBox,
}
}
#[inline]
fn direction(&self) -> Direction {
if self.rtl() {
Direction::Rtl
} else {
Direction::Ltr
}
}
#[inline]
fn overflow(&self) -> Point<Overflow> {
let (ox, oy) = self.effective_overflow();
let map = |o: NvOverflow| match o {
NvOverflow::Visible => Overflow::Visible,
NvOverflow::Hidden => Overflow::Hidden,
NvOverflow::Scroll | NvOverflow::Auto => Overflow::Scroll,
};
Point {
x: map(ox),
y: map(oy),
}
}
#[inline]
fn position(&self) -> Position {
if self.is_hoisted_open_overlay() {
return Position::Fixed;
}
match self.effective.position {
NvPosition::Static => Position::Static,
NvPosition::Relative => Position::Relative,
NvPosition::Absolute => Position::Absolute,
NvPosition::Sticky => Position::Sticky,
NvPosition::Fixed => Position::Fixed,
}
}
#[inline]
fn is_containing_block(&self) -> ContainingBlockClaims {
let is_open_ov = self.is_hoisted_open_overlay();
let is_contents = !self.is_root() && self.effective_display() == NvDisplay::Contents;
let has_tf = !self.is_hidden()
&& !is_contents
&& match self.responsive() {
Some(r) => r.has_transform,
None => has_store_transform(self.store, self.id),
};
ContainingBlockClaims {
absolute: has_tf || is_open_ov || self.position().is_positioned(),
fixed: has_tf || is_open_ov,
}
}
#[inline]
fn inset(&self) -> Rect<LengthPercentageAuto> {
let l = self.layout();
if !self.is_hoisted_open_overlay()
&& matches!(self.effective.position, NvPosition::Static | NvPosition::Sticky)
{
return Rect::auto();
}
Rect {
top: Self::lpa(l.inset_top()),
right: Self::lpa(l.inset_right()),
bottom: Self::lpa(l.inset_bottom()),
left: Self::lpa(l.inset_left()),
}
}
#[inline]
fn size(&self) -> Size<Dimension> {
if self.is_root() && self.flags.contains(StyleViewFlags::ROOT_FILLS_VIEWPORT) {
return Size {
width: Dimension::length(self.viewport.0),
height: Dimension::length(self.viewport.1),
};
}
let l = self.layout();
Size {
width: Self::dim(l.width()),
height: Self::dim(l.height()),
}
}
#[inline]
fn min_size(&self) -> Size<LengthPercentageAuto> {
let l = self.layout();
Size {
width: Self::lpa(l.min_width()),
height: Self::lpa(l.min_height()),
}
}
#[inline]
fn max_size(&self) -> Size<LengthPercentageAuto> {
let l = self.layout();
Size {
width: Self::lpa(l.max_width()),
height: Self::lpa(l.max_height()),
}
}
#[inline]
fn aspect_ratio(&self) -> Option<f32> {
self.layout()
.aspect_ratio()
.filter(|&r| r.is_finite() && r > 0.0)
}
#[inline]
fn margin(&self) -> Rect<LengthPercentageAuto> {
let l = self.layout();
let (left, right) = if self.rtl() && l.margin_inline_logical() {
(l.margin_right(), l.margin_left())
} else {
(l.margin_left(), l.margin_right())
};
Rect {
top: Self::lpa(l.margin_top()),
right: Self::lpa(right),
bottom: Self::lpa(l.margin_bottom()),
left: Self::lpa(left),
}
}
#[inline]
fn padding(&self) -> Rect<LengthPercentage> {
let l = self.layout();
let (left, right) = if self.rtl() && l.padding_inline_logical() {
(l.padding_right(), l.padding_left())
} else {
(l.padding_left(), l.padding_right())
};
Rect {
top: Self::lp(l.padding_top()),
right: Self::lp(right),
bottom: Self::lp(l.padding_bottom()),
left: Self::lp(left),
}
}
#[inline]
fn border(&self) -> Rect<LengthPercentage> {
let bw = self.effective_border_width();
if bw > 0.0 && bw.is_finite() {
Rect::length(bw)
} else {
Rect::zero()
}
}
}
impl FlexboxContainerStyle for StyleView<'_> {
#[inline]
fn flex_direction(&self) -> FlexDirection {
match self.layout().flex_direction() {
NvFlexDirection::Column => FlexDirection::Column,
NvFlexDirection::Row => FlexDirection::Row,
NvFlexDirection::ColumnReverse => FlexDirection::ColumnReverse,
NvFlexDirection::RowReverse => FlexDirection::RowReverse,
}
}
#[inline]
fn flex_wrap(&self) -> FlexWrap {
match self.layout().flex_wrap() {
NvFlexWrap::NoWrap => FlexWrap::NoWrap,
NvFlexWrap::Wrap => FlexWrap::Wrap,
NvFlexWrap::WrapReverse => FlexWrap::WrapReverse,
}
}
#[inline]
fn gap(&self) -> Size<LengthPercentage> {
let l = self.layout();
Size {
width: Self::lp(l.column_gap()),
height: Self::lp(l.row_gap()),
}
}
#[inline]
fn align_content(&self) -> AlignContent {
match self.layout().align_content() {
NvAlignContent::Stretch => AlignContent::STRETCH,
NvAlignContent::FlexStart => AlignContent::FLEX_START,
NvAlignContent::Center => AlignContent::CENTER,
NvAlignContent::FlexEnd => AlignContent::FLEX_END,
NvAlignContent::SpaceBetween => AlignContent::SPACE_BETWEEN,
NvAlignContent::SpaceAround => AlignContent::SPACE_AROUND,
NvAlignContent::SpaceEvenly => AlignContent::SPACE_EVENLY,
}
}
#[inline]
fn align_items(&self) -> AlignItems {
match self.layout().align_items() {
NvAlignItems::Stretch => AlignItems::STRETCH,
NvAlignItems::FlexStart => AlignItems::FLEX_START,
NvAlignItems::Center => AlignItems::CENTER,
NvAlignItems::FlexEnd => AlignItems::FLEX_END,
NvAlignItems::Baseline => AlignItems::BASELINE,
}
}
#[inline]
fn justify_content(&self) -> AlignContent {
match self.layout().justify_content() {
NvJustifyContent::FlexStart => AlignContent::FLEX_START,
NvJustifyContent::Center => AlignContent::CENTER,
NvJustifyContent::FlexEnd => AlignContent::FLEX_END,
NvJustifyContent::SpaceBetween => AlignContent::SPACE_BETWEEN,
NvJustifyContent::SpaceAround => AlignContent::SPACE_AROUND,
NvJustifyContent::SpaceEvenly => AlignContent::SPACE_EVENLY,
}
}
}
impl FlexboxItemStyle for StyleView<'_> {
#[inline]
fn flex_basis(&self) -> Dimension {
Self::dim(self.layout().flex_basis())
}
#[inline]
fn flex_grow(&self) -> f32 {
self.layout().flex_grow()
}
#[inline]
fn flex_shrink(&self) -> f32 {
self.layout().flex_shrink()
}
#[inline]
fn align_self(&self) -> Option<AlignSelf> {
match self.layout().align_self() {
NvAlignSelf::Auto => None,
NvAlignSelf::Stretch => Some(AlignSelf::STRETCH),
NvAlignSelf::FlexStart => Some(AlignSelf::FLEX_START),
NvAlignSelf::Center => Some(AlignSelf::CENTER),
NvAlignSelf::FlexEnd => Some(AlignSelf::FLEX_END),
NvAlignSelf::Baseline => Some(AlignSelf::BASELINE),
}
}
}