use crate::geometry::Size;
use crate::style::{Align, Display, FlexDirection, GridPlacement, TextDirection, Track};
use crate::units::{Length, ResolveContext, Resolved};
use crate::widget::{HorizontalAlign, LayoutDirection, NodeId, Prop, TextAlign, WidgetKind};
use super::{Fit, Inner};
impl Inner {
pub(super) fn resolve_styles(&mut self) {
self.styles_dirty = false;
let body = self.metrics.font_sizes.body;
for window in self.windows.clone() {
let viewport = self.nodes[&window].window_size;
let rtl = self.rtl;
self.resolve_node(window, body, rtl, viewport, false);
}
}
fn resolve_node(
&mut self,
id: NodeId,
inherited_font: f32,
inherited_rtl: bool,
viewport: Size,
in_stretching_column: bool,
) {
let node = &self.nodes[&id];
let font_size = match crate::find_prop!(node.props, TextStyle) {
Some(style) => self.metrics.font_sizes.get(style),
None => inherited_font,
};
let rtl = match node.style.direction {
TextDirection::Inherit => inherited_rtl,
TextDirection::Ltr => false,
TextDirection::Rtl => true,
};
let mut split_props = Vec::new();
if let Some(t) = node.taffy {
let cx = ResolveContext {
font_size,
root_font_size: self.metrics.font_sizes.body,
viewport,
spacing: &self.metrics.spacing,
};
let mut style = match node.kind {
WidgetKind::Tabs => {
let mut own = node.style.clone();
own.display = Display::Grid;
let fill = Track::MinMax(Length::Px(0.0), Length::Fr(1.0));
own.grid_template_columns = vec![fill];
own.grid_template_rows = vec![fill];
if let Some((index, vertical)) = self.pane(id) {
place_pane(&mut own, index, vertical);
}
own.to_taffy(&cx, rtl)
}
_ if self.is_page(id) => {
let mut own = node.style.clone();
own.grid_column = GridPlacement::at(1);
own.grid_row = GridPlacement::at(1);
own.to_taffy(&cx, rtl)
}
WidgetKind::Split => {
let (tracks, least, shown) = self.split_tracks(id, &cx);
split_props = vec![least, shown];
let mut own = node.style.clone();
own.display = Display::Grid;
let vertical = crate::find_prop!(node.props, Orientation).is_some_and(|o| o.vertical());
let (along, across) = match vertical {
false => (&mut own.grid_template_columns, &mut own.grid_template_rows),
true => (&mut own.grid_template_rows, &mut own.grid_template_columns),
};
*along = tracks;
*across = vec![Track::MinMax(Length::Px(0.0), Length::Fr(1.0))];
if let Some((index, vertical)) = self.pane(id) {
place_pane(&mut own, index, vertical);
}
own.to_taffy(&cx, rtl)
}
_ if let Some((index, vertical)) = self.pane(id) => {
let mut own = node.style.clone();
place_pane(&mut own, index, vertical);
own.to_taffy(&cx, rtl)
}
_ => node.style.to_taffy(&cx, rtl),
};
if node.kind == WidgetKind::Tabs {
let insets = node.insets.unwrap_or(self.metrics.tab_insets);
style.padding = taffy::Rect {
left: taffy::LengthPercentage::length(insets.left),
right: taffy::LengthPercentage::length(insets.right),
top: taffy::LengthPercentage::length(insets.top),
bottom: taffy::LengthPercentage::length(insets.bottom),
};
tab_strip_minimum(&mut style, node.strip);
}
if node.kind == WidgetKind::Group {
let titled = crate::find_prop!(node.props, Title).is_some_and(|t| !t.is_empty());
let insets = node.insets.unwrap_or(if titled {
self.metrics.titled_group_insets
} else {
self.metrics.group_insets
});
add(&mut style.padding.left, insets.left);
add(&mut style.padding.right, insets.right);
add(&mut style.padding.top, insets.top);
add(&mut style.padding.bottom, insets.bottom);
tab_strip_minimum(&mut style, node.strip);
}
if node.kind == WidgetKind::ScrollView {
let insets = node.viewport_insets;
add(&mut style.padding.right, insets.left + insets.right);
add(&mut style.padding.bottom, insets.top + insets.bottom);
}
if matches!(node.kind, WidgetKind::Checkbox | WidgetKind::Switch) {
style.justify_self.get_or_insert(taffy::AlignSelf::START);
if in_stretching_column {
style.align_self.get_or_insert(taffy::AlignSelf::START);
}
}
if node.kind == WidgetKind::Text
&& crate::find_prop!(node.props, MaxLines).flatten().is_some()
&& style.min_size.width == taffy::LengthPercentageAuto::auto()
{
style.min_size.width = taffy::LengthPercentageAuto::length(0.0);
}
if node.kind.has_rows() {
style.overflow = taffy::Point { x: taffy::Overflow::Scroll, y: taffy::Overflow::Scroll };
}
if node.kind == WidgetKind::Window {
style.size = taffy::Size {
width: taffy::Dimension::length(node.window_size.width),
height: if node.fit == Fit::Once {
taffy::Dimension::auto()
} else {
taffy::Dimension::length(node.window_size.height)
},
};
}
if self.taffy.style(t).ok() != Some(&style) {
let _ = self.taffy.set_style(t, style);
}
}
if node.kind == WidgetKind::Text {
self.resolve_text_align(id, rtl);
}
for prop in split_props {
if self.nodes[&id].prop(&prop) != Some(&prop) {
let node = self.nodes.get_mut(&id).unwrap();
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop.clone());
self.queue_prop(id, prop);
}
}
self.resolve_direction(id, rtl);
let node = &self.nodes[&id];
let in_stretching_column = match node.taffy {
Some(_) => {
let s = &node.style;
s.display == Display::Flex
&& matches!(s.flex_direction, FlexDirection::Column | FlexDirection::ColumnReverse)
&& matches!(s.align_items, None | Some(Align::Stretch))
}
None => in_stretching_column,
};
for child in self.nodes[&id].children.clone() {
self.resolve_node(child, font_size, rtl, viewport, in_stretching_column);
}
}
fn resolve_direction(&mut self, id: NodeId, rtl: bool) {
let node = &self.nodes[&id];
if !node.kind.is_native() {
return;
}
let had = crate::find_prop!(node.props, LayoutDirection).is_some();
if !(rtl || self.rtl || had) {
return;
}
let prop = Prop::LayoutDirection(if rtl { LayoutDirection::RightToLeft } else { LayoutDirection::LeftToRight });
if node.prop(&prop) != Some(&prop) {
let node = self.nodes.get_mut(&id).unwrap();
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop.clone());
self.queue_prop(id, prop);
}
}
fn resolve_text_align(&mut self, id: NodeId, rtl: bool) {
let node = &self.nodes[&id];
let had = crate::find_prop!(node.props, TextAlign).is_some();
let Some(align) = node.style.text_align.or((had || rtl).then_some(TextAlign::Start)) else { return };
let align = match (align, rtl) {
(TextAlign::Center, _) => HorizontalAlign::Center,
(TextAlign::Start, false) | (TextAlign::End, true) => HorizontalAlign::Left,
(TextAlign::Start, true) | (TextAlign::End, false) => HorizontalAlign::Right,
};
let prop = Prop::TextAlign(align);
if node.prop(&prop) != Some(&prop) {
let node = self.nodes.get_mut(&id).unwrap();
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop.clone());
self.queue_prop(id, prop);
}
}
}
impl Inner {
fn pane(&self, id: NodeId) -> Option<(usize, bool)> {
let parent = self.nodes[&id].parent?;
let split = &self.nodes[&parent];
if split.kind != WidgetKind::Split {
return None;
}
let index = split.children.iter().position(|c| *c == id)?;
Some((index, crate::find_prop!(split.props, Orientation).is_some_and(|o| o.vertical())))
}
fn split_tracks(&self, id: NodeId, cx: &ResolveContext<'_>) -> (Vec<Track>, Prop, Prop) {
let node = &self.nodes[&id];
let vertical = crate::find_prop!(node.props, Orientation).is_some_and(|o| o.vertical());
let least = |pane: Option<&NodeId>| {
let style = &self.nodes[pane?].style;
match (if vertical { style.min_height } else { style.min_width }).resolve(cx) {
Resolved::Length(points) => Some(points),
_ => None,
}
};
let (first, second) = (least(node.children.first()).unwrap_or(0.0), least(node.children.get(1)).unwrap_or(0.0));
let divider = self.metrics.split_divider;
let position = crate::find_prop!(node.props, Number).map(|p| (p as f32).max(first));
let shown = |pane: Option<&NodeId>| pane.is_some_and(|p| !self.nodes[p].style.is_hidden());
let shown = [shown(node.children.first()), shown(node.children.get(1))];
let none = Track::Size(Length::Px(0.0));
let tracks = match shown {
[true, true] => vec![
match position {
Some(points) => Track::MinMax(Length::Px(first), Length::Px(points)),
None => Track::MinMax(Length::Px(first), Length::Auto),
},
Track::Size(Length::Px(divider)),
Track::MinMax(Length::Px(second), Length::Fr(1.0)),
],
[true, false] => vec![Track::MinMax(Length::Px(first), Length::Fr(1.0)), none, none],
[false, _] => vec![none, none, Track::MinMax(Length::Px(second), Length::Fr(1.0))],
};
(tracks, Prop::PaneMinimums([first, second]), Prop::PanesShown(shown))
}
}
fn place_pane(style: &mut crate::style::Style, index: usize, vertical: bool) {
let (along, across) = match vertical {
false => (&mut style.grid_column, &mut style.grid_row),
true => (&mut style.grid_row, &mut style.grid_column),
};
*along = GridPlacement::at(if index == 0 { 1 } else { 3 });
*across = GridPlacement::at(1);
}
fn tab_strip_minimum(style: &mut taffy::Style, strip: Size) {
let auto = taffy::LengthPercentageAuto::auto();
let at_least = |min: &mut taffy::LengthPercentageAuto, v: f32| {
if *min == auto || *min == taffy::LengthPercentageAuto::length(0.0) {
*min = if v > 0.0 { taffy::LengthPercentageAuto::length(v) } else { auto };
}
};
at_least(&mut style.min_size.width, strip.width);
at_least(&mut style.min_size.height, strip.height);
}
fn add(side: &mut taffy::LengthPercentage, inset: f32) {
let own = side.into_raw();
*side = match own.tag() {
taffy::CompactLength::LENGTH_TAG => taffy::LengthPercentage::length(own.value() + inset),
_ => taffy::LengthPercentage::length(inset),
};
}