use std::sync::Arc;
use crate::color::Color;
use crate::parser::ast::{
self, Alignment, Dimension, Document, Element, PanelElement, TextElement,
};
use crate::resource::{ResourceCategory, ResourceGovernor};
use super::events::{EventBinding, EventBindings};
use super::node::{
AbsoluteData, Action, AnimationData, BorderStyleTag, ButtonData, CellData, FlowData,
FlowSource, HrData, InputData, LinkData, LiveData, NodeBuilder, NodeId, NodeKind, OptionData,
SelectData, TextContent, TextRun, TextSource,
};
use super::tree::{AmlIdIndex, Scene, SceneNodes};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Category {
NodeBearing,
Ancillary,
Inline,
}
pub fn classify(element: &Element) -> Category {
match element {
Element::Box(_)
| Element::Row(_)
| Element::Col(_)
| Element::Hr(_)
| Element::Spacer(_)
| Element::Header(_)
| Element::Body(_)
| Element::Footer(_)
| Element::Nav(_)
| Element::Thead(_)
| Element::Tbody(_)
| Element::Pagination(_)
| Element::List(_)
| Element::Item(_)
| Element::Form(_) => Category::NodeBearing,
Element::Text(_)
| Element::Pre(_)
| Element::Heading(_)
| Element::Art(_)
| Element::ElementDef(_)
| Element::TextAnimate(_) => Category::NodeBearing,
Element::Link(_) | Element::Input(_) | Element::Select(_) | Element::Button(_) => {
Category::NodeBearing
}
Element::Option(_) => Category::NodeBearing,
Element::Table(_) | Element::Tr(_) | Element::Th(_) | Element::Td(_) => {
Category::NodeBearing
}
Element::Animate(_) | Element::Frame(_) => Category::NodeBearing,
Element::Live(_) => Category::NodeBearing,
Element::Panel(_) | Element::State(_) => Category::NodeBearing,
Element::Details(_) => Category::NodeBearing,
Element::Tween(_) | Element::On(_) | Element::Include(_) => Category::Ancillary,
}
}
pub fn from_document(doc: &Document) -> Scene {
from_document_with_governor(doc, None)
}
pub fn from_document_governed(doc: &Document, governor: &ResourceGovernor) -> Scene {
from_document_with_governor(doc, Some(governor.clone()))
}
fn from_document_with_governor(doc: &Document, governor: Option<ResourceGovernor>) -> Scene {
let element_bound = count_elements(&doc.page.children);
let node_capacity = element_bound.and_then(|elements| elements.checked_add(2));
let requested_node_bytes = node_capacity.and_then(SceneNodes::requested_bytes);
let mut node_lease = match (governor.as_ref(), requested_node_bytes) {
(Some(governor), Some(bytes)) => governor
.reserve(ResourceCategory::RemoteCollections, bytes)
.ok(),
(None, Some(_)) => None,
(_, None) => None,
};
let mut node_failed =
requested_node_bytes.is_none() || governor.is_some() && node_lease.is_none();
let mut nodes = if node_failed {
SceneNodes::default()
} else {
match node_capacity.and_then(|capacity| SceneNodes::try_with_capacity(capacity).ok()) {
Some(nodes) => nodes,
None => {
node_failed = true;
SceneNodes::default()
}
}
};
if let (Some(lease), Some(actual)) = (node_lease.as_mut(), nodes.retained_bytes()) {
node_failed |= lease.try_resize_with_cost(actual, actual).is_err();
}
if node_failed {
nodes = SceneNodes::default();
node_lease = None;
}
let requested_relation_bytes = element_bound
.and_then(|elements| elements.checked_mul(2))
.and_then(|slots| slots.checked_add(1))
.and_then(|slots| slots.checked_mul(std::mem::size_of::<NodeId>()));
let mut relation_lease = match (governor.as_ref(), requested_relation_bytes) {
(Some(governor), Some(bytes)) => governor
.reserve(ResourceCategory::RemoteCollections, bytes)
.ok(),
(None, Some(_)) => None,
(_, None) => None,
};
let mut relation_failed = requested_relation_bytes.is_none()
|| governor.is_some() && relation_lease.is_none()
|| super::tree::reject_scene_allocation(super::tree::SceneAllocationSite::RelationTopology);
let mut aml_id_index = AmlIdIndex::new();
if !relation_failed
&& aml_id_index
.try_reserve_exact(element_bound.unwrap_or(0))
.is_err()
{
relation_failed = true;
}
let root_id = if node_failed {
NodeId::default()
} else {
nodes
.insert_with_key(|id| NodeBuilder::new(NodeKind::Root).finish(id))
.unwrap_or_else(|| {
node_failed = true;
NodeId::default()
})
};
let mut cx = BuildCtx {
nodes: &mut nodes,
aml_id_index: &mut aml_id_index,
relation_failed: &mut relation_failed,
};
if !node_failed {
for child in &doc.page.children {
if let Some(child_id) = build_element(&mut cx, child) {
attach_child(&mut cx, root_id, child_id);
}
}
}
if !relation_failed
&& nodes
.get_mut(root_id)
.is_none_or(|root| root.children.try_reserve_exact(1).is_err())
{
relation_failed = true;
}
let actual_relation_bytes = aml_id_index
.capacity()
.checked_add(
nodes
.values()
.try_fold(0usize, |total, node| {
total.checked_add(node.children.capacity())
})
.unwrap_or(usize::MAX),
)
.and_then(|slots| slots.checked_mul(std::mem::size_of::<NodeId>()));
if let (Some(lease), Some(actual)) = (relation_lease.as_mut(), actual_relation_bytes) {
relation_failed |= lease.try_resize_with_cost(actual, actual).is_err();
} else if actual_relation_bytes.is_none() {
relation_failed = true;
}
if relation_failed {
aml_id_index = Vec::new();
for node in nodes.values_mut() {
node.children = Vec::new();
node.parent = None;
}
relation_lease = None;
}
let event_bindings = collect_event_bindings(&doc.page.children);
let invalidation =
super::invalidation::Invalidation::try_for_nodes(nodes.len(), governor.as_ref());
let resource_error =
node_failed || relation_failed || invalidation.is_none() || event_bindings.is_none();
let event_bindings = event_bindings.unwrap_or_default();
Scene {
root: root_id,
nodes,
aml_id_index,
invalidation: invalidation.unwrap_or_default(),
focus: None,
scroll: super::tree::ScrollState::default(),
event_bindings,
page_mode: doc.page.mode,
default_fg: doc.page.style.as_ref().and_then(|s| s.default_fg.clone()),
default_bg: doc.page.style.as_ref().and_then(|s| s.default_bg.clone()),
transition: doc.page.transition,
transition_duration_ms: doc.page.transition_duration_ms,
title: doc.page.title.as_deref().map(Arc::from),
resource_error,
governor,
relayout_journal: None,
page_transition_overlay: None,
_node_topology_lease: node_lease,
_node_relation_topology_lease: relation_lease,
layout_pass: 0,
}
}
fn count_elements(elements: &[Element]) -> Option<usize> {
elements.iter().try_fold(0usize, |total, element| {
let descendants = count_elements(element.children())?;
let summary = match element {
Element::Details(details) => count_elements(&details.summary_children)?,
_ => 0,
};
total
.checked_add(1)?
.checked_add(descendants)?
.checked_add(summary)
})
}
fn collect_event_bindings(elements: &[Element]) -> Option<EventBindings> {
let mut out = EventBindings::new();
walk_for_on(elements, &mut out).then_some(out)
}
fn walk_for_on(elements: &[Element], out: &mut EventBindings) -> bool {
for elem in elements {
if let Element::On(e) = elem {
let Some(source) = try_clone_optional_string(&e.source) else {
return false;
};
let Some(target) = try_clone_string(&e.target) else {
return false;
};
let Some(to) = try_clone_optional_string(&e.to) else {
return false;
};
if out
.try_push(EventBinding {
event: e.event,
source,
action: e.action,
target,
to,
delay_ms: e.delay_ms,
})
.is_err()
{
return false;
}
continue;
}
let children = elem.children();
if !children.is_empty() && !walk_for_on(children, out) {
return false;
}
}
true
}
fn try_clone_string(value: &str) -> Option<String> {
let mut owned = String::new();
owned.try_reserve_exact(value.len()).ok()?;
owned.push_str(value);
Some(owned)
}
fn try_clone_optional_string(value: &Option<String>) -> Option<Option<String>> {
match value {
Some(value) => try_clone_string(value).map(Some),
None => Some(None),
}
}
#[cfg(test)]
thread_local! {
static AML_ID_COPY_REJECTION: std::cell::Cell<bool> = const {
std::cell::Cell::new(false)
};
}
#[cfg(test)]
pub(super) fn reject_next_aml_id_copy() {
AML_ID_COPY_REJECTION.with(|site| site.set(true));
}
fn try_clone_aml_id(cx: &mut BuildCtx<'_>, value: Option<&str>) -> Option<String> {
let value = value?;
#[cfg(test)]
if AML_ID_COPY_REJECTION.with(|site| site.replace(false)) {
*cx.relation_failed = true;
return None;
}
match try_clone_string(value) {
Some(value) => Some(value),
None => {
*cx.relation_failed = true;
None
}
}
}
struct BuildCtx<'a> {
nodes: &'a mut SceneNodes,
aml_id_index: &'a mut AmlIdIndex,
relation_failed: &'a mut bool,
}
fn count_flattened_runs(child: &TextElement) -> Option<usize> {
let mut total = usize::from(!child.content.is_empty());
for grandchild in &child.children {
if let Element::Text(gc) = grandchild {
total = total.checked_add(count_flattened_runs(gc)?)?;
}
}
Some(total)
}
fn count_text_runs(t: &TextElement) -> Option<usize> {
let mut total = usize::from(!t.content.is_empty());
for child in &t.children {
if let Element::Text(child_t) = child {
total = total.checked_add(count_flattened_runs(child_t)?)?;
}
}
Some(total)
}
fn insert_node(cx: &mut BuildCtx, builder: NodeBuilder) -> NodeId {
let indexed = builder.aml_id.is_some();
let Some(id) = cx.nodes.insert_with_key(|id| builder.finish(id)) else {
*cx.relation_failed = true;
return NodeId::default();
};
if indexed {
if cx.aml_id_index.len() < cx.aml_id_index.capacity() {
cx.aml_id_index.push(id);
} else {
*cx.relation_failed = true;
}
}
id
}
fn attach_child(cx: &mut BuildCtx, parent: NodeId, child: NodeId) {
if *cx.relation_failed {
return;
}
if cx
.nodes
.get_mut(parent)
.is_none_or(|parent_node| parent_node.children.try_reserve_exact(1).is_err())
{
*cx.relation_failed = true;
return;
}
if let Some(child_node) = cx.nodes.get_mut(child) {
child_node.parent = Some(parent);
}
if let Some(parent_node) = cx.nodes.get_mut(parent) {
parent_node.children.push(child);
}
}
fn build_element(cx: &mut BuildCtx, element: &Element) -> Option<NodeId> {
build_element_inner(cx, element)
}
fn build_element_inner(cx: &mut BuildCtx, element: &Element) -> Option<NodeId> {
match element {
Element::Box(b) => Some(build_box(cx, b)),
Element::Row(r) => Some(build_row(cx, r)),
Element::Col(c) => {
let data = FlowData {
source: FlowSource::Col,
width: Some(c.w),
align: c.align,
..FlowData::default()
};
Some(build_flow_container_with_data(cx, data, &c.children))
}
Element::Hr(h) => Some(build_leaf(
cx,
NodeKind::Hr(HrData {
fg: color_name(h.fg.as_ref()),
style: h.style,
}),
)),
Element::Spacer(s) => Some(build_leaf(cx, NodeKind::Spacer { lines: s.lines })),
Element::Header(c) => Some(build_flow_container(cx, FlowSource::Header, &c.children)),
Element::Body(c) => Some(build_flow_container(cx, FlowSource::Body, &c.children)),
Element::Footer(c) => Some(build_flow_container(cx, FlowSource::Footer, &c.children)),
Element::Nav(n) => {
let data = FlowData {
source: FlowSource::Nav,
sticky: n.sticky,
..FlowData::default()
};
Some(build_flow_container_with_data(cx, data, &n.children))
}
Element::Thead(c) => Some(build_flow_container(cx, FlowSource::Thead, &c.children)),
Element::Tbody(c) => Some(build_flow_container(cx, FlowSource::Tbody, &c.children)),
Element::Pagination(c) => Some(build_flow_container(
cx,
FlowSource::Pagination,
&c.children,
)),
Element::List(l) => {
let data = FlowData {
source: FlowSource::List,
list_style: Some(l.style),
list_bullet_char: l.bullet_char,
..FlowData::default()
};
Some(build_flow_container_with_data(cx, data, &l.children))
}
Element::Item(i) => Some(build_flow_container(cx, FlowSource::Item, &i.children)),
Element::Form(f) => {
let data = FlowData {
source: FlowSource::Form,
form_action: Some(f.action.clone()),
..FlowData::default()
};
Some(build_flow_container_with_data(cx, data, &f.children))
}
Element::Text(t) => Some(build_text(cx, t)),
Element::Pre(p) => {
let tc = TextContent {
runs: vec![TextRun {
text: p.content.clone(),
fg: color_name(p.fg.as_ref()),
bg: color_name(p.bg.as_ref()),
..TextRun::default()
}],
align: p.align,
source: TextSource::Pre,
};
Some(build_leaf(cx, NodeKind::Text(tc)))
}
Element::Heading(h) => {
let tc = TextContent {
runs: vec![TextRun {
text: h.content.clone(),
fg: color_name(h.fg.as_ref()),
bold: true,
..TextRun::default()
}],
align: Alignment::Left,
source: TextSource::Heading(h.level),
};
let node = insert_node(cx, NodeBuilder::new(NodeKind::Text(tc)));
for child in &h.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
Some(node)
}
Element::Art(a) => {
let tc = TextContent {
runs: vec![TextRun {
text: a.content.clone(),
..TextRun::default()
}],
align: Alignment::Left,
source: TextSource::Art,
};
Some(build_leaf(cx, NodeKind::Text(tc)))
}
Element::ElementDef(e) => {
let tc = TextContent {
runs: vec![TextRun {
text: e.content.trim().to_string(),
fg: color_name(e.fg.as_ref()),
..TextRun::default()
}],
align: Alignment::Left,
source: TextSource::ElementDef,
};
let aml_id = try_clone_aml_id(cx, Some(e.id.as_str()));
Some(insert_node(
cx,
NodeBuilder::new(NodeKind::Text(tc)).aml_id(aml_id),
))
}
Element::TextAnimate(ta) => {
let tc = TextContent {
runs: vec![TextRun {
text: ta.content.clone(),
..TextRun::default()
}],
align: Alignment::Left,
source: TextSource::TextAnimate,
};
Some(build_leaf(cx, NodeKind::Text(tc)))
}
Element::Link(l) => Some(build_link(cx, l)),
Element::Input(i) => Some(build_input(cx, i)),
Element::Select(s) => Some(build_select(cx, s)),
Element::Option(o) => {
Some(build_leaf(
cx,
NodeKind::OptionLeaf(OptionData {
value: o.value.clone(),
selected: o.selected,
label: o.label.clone(),
}),
))
}
Element::Button(b) => Some(build_button(cx, b)),
Element::Table(_) => {
let node = insert_node(cx, NodeBuilder::new(NodeKind::Table));
if let Element::Table(tbl) = element {
for child in &tbl.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
}
Some(node)
}
Element::Tr(tr) => {
let node = insert_node(cx, NodeBuilder::new(NodeKind::Tr));
for child in &tr.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
Some(node)
}
Element::Th(c) => {
let node = insert_node(
cx,
NodeBuilder::new(NodeKind::Th(CellData {
fg: color_name(c.fg.as_ref()),
})),
);
for child in &c.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
Some(node)
}
Element::Td(c) => {
let node = insert_node(
cx,
NodeBuilder::new(NodeKind::Td(CellData {
fg: color_name(c.fg.as_ref()),
})),
);
for child in &c.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
Some(node)
}
Element::Animate(a) => Some(build_animation(cx, a)),
Element::Frame(f) => Some(build_flow_container(cx, FlowSource::Frame, &f.children)),
Element::Live(l) => Some(build_live(cx, l)),
Element::Panel(p) => Some(build_panel(cx, p)),
Element::State(s) => Some(build_state(cx, s)),
Element::Details(d) => Some(build_details(cx, d)),
Element::Tween(_) | Element::On(_) | Element::Include(_) => None,
}
}
fn build_leaf(cx: &mut BuildCtx, kind: NodeKind) -> NodeId {
insert_node(cx, NodeBuilder::new(kind))
}
fn build_flow_container(cx: &mut BuildCtx, source: FlowSource, children: &[Element]) -> NodeId {
let flow = FlowData {
source,
..FlowData::default()
};
build_flow_container_with_data(cx, flow, children)
}
fn build_flow_container_with_data(
cx: &mut BuildCtx,
flow: FlowData,
children: &[Element],
) -> NodeId {
let node = insert_node(cx, NodeBuilder::new(NodeKind::Flow(flow)));
for child in children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_box(cx: &mut BuildCtx, b: &ast::BoxElement) -> NodeId {
let border = border_tag(b.border);
let kind = if b.x.is_some() || b.y.is_some() {
NodeKind::Absolute(AbsoluteData {
x: b.x,
y: b.y,
w: b.w,
h: b.h,
border,
title: b.title.clone(),
join_top: b.join_top,
join_bottom: b.join_bottom,
join_left: b.join_left,
join_right: b.join_right,
padding: b.padding,
fg: b.fg.clone(),
bg: b.bg.clone(),
align: b.align,
})
} else {
NodeKind::Flow(FlowData {
source: FlowSource::Box,
border,
title: b.title.clone(),
join_top: b.join_top,
join_bottom: b.join_bottom,
join_left: b.join_left,
join_right: b.join_right,
padding: b.padding,
width: Some(b.w),
height: Some(b.h),
fg: b.fg.clone(),
bg: b.bg.clone(),
align: b.align,
sticky: b.sticky,
..FlowData::default()
})
};
let node = insert_node(cx, NodeBuilder::new(kind));
for child in &b.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_row(cx: &mut BuildCtx, r: &ast::RowElement) -> NodeId {
let data = super::node::RowData {
gap: r.gap,
align: r.align,
};
let node = insert_node(cx, NodeBuilder::new(NodeKind::Row(data)));
for child in &r.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_text(cx: &mut BuildCtx, t: &TextElement) -> NodeId {
let style = TextRun {
bold: t.bold,
italic: t.italic,
underline: t.underline,
strikethrough: t.strikethrough,
dim: t.dim,
blink: t.blink,
fg: color_name(t.fg.as_ref()),
bg: color_name(t.bg.as_ref()),
text: String::new(),
};
let has_node_bearing_inline = t
.children
.iter()
.any(|c| !matches!(c, Element::Text(_)) && classify(c) == Category::NodeBearing);
let run_bound = if has_node_bearing_inline {
Some(usize::from(!t.content.is_empty()))
} else {
count_text_runs(t)
};
let mut runs = Vec::new();
match run_bound {
Some(bound) if runs.try_reserve_exact(bound).is_ok() => {}
_ => *cx.relation_failed = true,
}
if !*cx.relation_failed && !t.content.is_empty() {
runs.push(TextRun {
text: t.content.clone(),
..style.clone()
});
}
let mut inline_child_ids: Vec<NodeId> = Vec::new();
if has_node_bearing_inline
&& !*cx.relation_failed
&& inline_child_ids
.try_reserve_exact(t.children.len())
.is_err()
{
*cx.relation_failed = true;
}
if has_node_bearing_inline && !*cx.relation_failed {
for child in &t.children {
match child {
Element::Text(child_t) => {
let mut child_runs = Vec::new();
match count_flattened_runs(child_t) {
Some(bound) if child_runs.try_reserve_exact(bound).is_ok() => {
flatten_inline_text(&style, child_t, &mut child_runs);
}
_ => *cx.relation_failed = true,
}
if !child_runs.is_empty() {
let tc = TextContent {
runs: child_runs,
align: child_t.align,
source: TextSource::Text,
};
let id = insert_node(cx, NodeBuilder::new(NodeKind::Text(tc)));
inline_child_ids.push(id);
}
}
other if classify(other) == Category::NodeBearing => {
if let Some(child_id) = build_element(cx, other) {
inline_child_ids.push(child_id);
}
}
_ => {}
}
}
} else if !*cx.relation_failed {
for child in &t.children {
if let Element::Text(child_t) = child {
flatten_inline_text(&style, child_t, &mut runs);
}
}
}
let tc = TextContent {
runs,
align: t.align,
source: TextSource::Text,
};
let node = insert_node(cx, NodeBuilder::new(NodeKind::Text(tc)));
for child_id in inline_child_ids {
attach_child(cx, node, child_id);
}
node
}
fn flatten_inline_text(parent_style: &TextRun, child: &TextElement, runs: &mut Vec<TextRun>) {
let merged = TextRun {
text: String::new(),
bold: parent_style.bold || child.bold,
italic: parent_style.italic || child.italic,
underline: parent_style.underline || child.underline,
strikethrough: parent_style.strikethrough || child.strikethrough,
dim: parent_style.dim || child.dim,
blink: parent_style.blink || child.blink,
fg: color_name(child.fg.as_ref()).or_else(|| parent_style.fg.clone()),
bg: color_name(child.bg.as_ref()).or_else(|| parent_style.bg.clone()),
};
if !child.content.is_empty() {
runs.push(TextRun {
text: child.content.clone(),
..merged.clone()
});
}
for grandchild in &child.children {
if let Element::Text(gc) = grandchild {
flatten_inline_text(&merged, gc, runs);
}
}
}
fn build_link(cx: &mut BuildCtx, l: &ast::LinkElement) -> NodeId {
let kind = NodeKind::Link(LinkData {
href: l.href.clone(),
key: l.key,
prefetch: l.prefetch,
transition: l.transition,
transition_duration_ms: l.transition_duration_ms,
defer_animation: l.defer_animation.clone(),
});
let action = Action::Navigate {
href: l.href.clone(),
transition: l.transition,
transition_duration_ms: l.transition_duration_ms,
defer_animation: l.defer_animation.clone(),
};
let aml_id = try_clone_aml_id(cx, l.id.as_deref());
let node = insert_node(
cx,
NodeBuilder::new(kind)
.aml_id(aml_id)
.focusable(true)
.hit_target(Some(action)),
);
for child in &l.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_input(cx: &mut BuildCtx, i: &ast::InputElement) -> NodeId {
let kind = NodeKind::Input(InputData {
name: i.name.clone(),
maxlen: i.maxlen,
placeholder: i.placeholder.clone(),
multiline: i.multiline,
rows: i.rows,
password: i.password,
value: i.value.clone(),
});
let aml_id = try_clone_aml_id(cx, i.id.as_deref());
insert_node(cx, NodeBuilder::new(kind).aml_id(aml_id).focusable(true))
}
fn build_select(cx: &mut BuildCtx, s: &ast::SelectElement) -> NodeId {
let selected_index = s
.children
.iter()
.filter_map(|child| match child {
Element::Option(option) => Some(option),
_ => None,
})
.position(|option| option.selected)
.unwrap_or(0);
let kind = NodeKind::Select(SelectData {
name: s.name.clone(),
label: s.label.clone(),
selected_index,
});
let node = insert_node(cx, NodeBuilder::new(kind).focusable(true));
for child in &s.children {
if let Element::Option(_) = child
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_button(cx: &mut BuildCtx, b: &ast::ButtonElement) -> NodeId {
let action = button_action(b);
let kind = NodeKind::Button(ButtonData {
label: b.label.clone(),
key: b.key,
action: b.action,
target: b.target.clone(),
states: b.states.clone(),
to: b.to.clone(),
href: b.href.clone(),
transition: b.transition,
transition_duration_ms: b.transition_duration_ms,
});
insert_node(
cx,
NodeBuilder::new(kind)
.focusable(true)
.hit_target(Some(action)),
)
}
fn button_action(b: &ast::ButtonElement) -> Action {
use ast::ButtonAction as BA;
match b.action {
BA::Navigate => Action::Navigate {
href: b.href.clone().unwrap_or_default(),
transition: b.transition,
transition_duration_ms: b.transition_duration_ms,
defer_animation: None,
},
BA::Toggle => Action::TogglePanel {
panel: b.target.clone().unwrap_or_default(),
states: b.states.clone().unwrap_or_default(),
},
BA::Set => Action::SetPanelState {
panel: b.target.clone().unwrap_or_default(),
state: b.to.clone().unwrap_or_default(),
},
BA::Submit => Action::Submit {
form: b.target.clone().unwrap_or_default(),
},
}
}
fn build_animation(cx: &mut BuildCtx, a: &ast::AnimateElement) -> NodeId {
let mut frame_ids = Vec::new();
let frame_bound = a
.children
.iter()
.filter(|child| matches!(child, Element::Frame(_)))
.count();
if frame_ids.try_reserve_exact(frame_bound).is_err() {
*cx.relation_failed = true;
}
let placeholder = NodeKind::Animation(AnimationData {
fps: a.fps as u16,
autoplay: a.autoplay,
loop_behavior: a.loop_behavior,
loop_: !matches!(a.loop_behavior, ast::LoopBehavior::None),
background: a.background,
src: a.src.clone(),
frames: Vec::new(),
delay_ms: a.delay_ms,
after: a.after.clone(),
x: a.x,
y: a.y,
w: a.w,
h: a.h,
});
let aml_id = try_clone_aml_id(cx, Some(a.id.as_str()));
let node = insert_node(cx, NodeBuilder::new(placeholder).aml_id(aml_id));
for child in &a.children {
if let Element::Frame(_) = child {
if let Some(child_id) = build_element(cx, child) {
attach_child(cx, node, child_id);
frame_ids.push(child_id);
}
} else if classify(child) == Category::NodeBearing {
if let Some(child_id) = build_element(cx, child) {
attach_child(cx, node, child_id);
}
}
}
if let Some(node_mut) = cx.nodes.get_mut(node)
&& let NodeKind::Animation(data) = &mut node_mut.kind
{
data.frames = frame_ids;
}
node
}
fn build_live(cx: &mut BuildCtx, l: &ast::LiveElement) -> NodeId {
let kind = NodeKind::LiveRegion(LiveData {
endpoint: l.endpoint.clone(),
height: l.height,
scroll: l.scroll,
buffer: l.buffer,
delta: l.delta,
});
let aml_id = try_clone_aml_id(cx, Some(l.id.as_str()));
let node = insert_node(cx, NodeBuilder::new(kind).aml_id(aml_id));
for child in &l.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_panel(cx: &mut BuildCtx, p: &PanelElement) -> NodeId {
let mut state_ids = Vec::new();
let state_bound = p
.children
.iter()
.filter(|child| matches!(child, Element::State(_)))
.count();
if state_ids.try_reserve_exact(state_bound).is_err() {
*cx.relation_failed = true;
}
for child in &p.children {
if let Element::State(_) = child
&& let Some(child_id) = build_element(cx, child)
{
state_ids.push(child_id);
}
}
let active = state_ids
.iter()
.copied()
.find(|&id| {
cx.nodes
.get(id)
.and_then(|n| {
if let NodeKind::Flow(fd) = n.kind()
&& fd.source == FlowSource::State
{
return n.aml_id();
}
None
})
.is_some_and(|name| name == p.initial_state.as_str())
})
.or_else(|| state_ids.first().copied())
.unwrap_or(NodeId::default());
let kind = NodeKind::Panel {
states: state_ids.clone(),
active,
initial_state: p.initial_state.clone(),
};
let aml_id = try_clone_aml_id(cx, Some(p.id.as_str()));
let node = insert_node(cx, NodeBuilder::new(kind).aml_id(aml_id));
for state_id in state_ids {
attach_child(cx, node, state_id);
}
node
}
fn build_state(cx: &mut BuildCtx, s: &ast::StateElement) -> NodeId {
let flow = FlowData {
source: FlowSource::State,
state_name: Some(s.name.clone()),
state_transition: s.transition.as_deref().and_then(ast::parse_transition_kind),
state_transition_duration_ms: s.duration_ms,
..FlowData::default()
};
let aml_id = try_clone_aml_id(cx, Some(s.name.as_str()));
let node = insert_node(cx, NodeBuilder::new(NodeKind::Flow(flow)).aml_id(aml_id));
for child in &s.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
node
}
fn build_details(cx: &mut BuildCtx, d: &ast::DetailsElement) -> NodeId {
let flow = FlowData {
source: FlowSource::Details,
details_open: d.open,
details_summary: if d.summary_children.is_empty() {
Some(d.summary.clone())
} else {
None
},
details_summary_count: 0, ..FlowData::default()
};
let node = insert_node(cx, NodeBuilder::new(NodeKind::Flow(flow)));
let mut summary_count: u16 = 0;
for child in &d.summary_children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
summary_count = summary_count.saturating_add(1);
}
}
if d.open {
for child in &d.children {
if classify(child) == Category::NodeBearing
&& let Some(child_id) = build_element(cx, child)
{
attach_child(cx, node, child_id);
}
}
}
if let Some(n) = cx.nodes.get_mut(node)
&& let NodeKind::Flow(ref mut fd) = n.kind
{
fd.details_summary_count = summary_count;
}
node
}
fn border_tag(border: ast::BorderStyle) -> BorderStyleTag {
match border {
ast::BorderStyle::None => BorderStyleTag::None,
ast::BorderStyle::Single => BorderStyleTag::Single,
ast::BorderStyle::Double => BorderStyleTag::Double,
ast::BorderStyle::Rounded => BorderStyleTag::Rounded,
ast::BorderStyle::Heavy => BorderStyleTag::Heavy,
ast::BorderStyle::Ascii => BorderStyleTag::Ascii,
}
}
fn color_name(c: Option<&Color>) -> Option<Color> {
c.cloned()
}
#[allow(dead_code)]
const _D: Option<Dimension> = None;