use dioxus_native_core::NodeId;
use dioxus_native_core::{node::NodeType, real_dom::NodeImmutable};
use freya_core::node::NodeState;
use freya_node_state::{CustomAttributeValues, Style, ViewportState};
use torin::{geometry::Area, prelude::LayoutNode};
use crate::test_utils::TestUtils;
#[derive(Clone)]
pub struct TestNode {
pub(crate) node_id: NodeId,
pub(crate) utils: TestUtils,
pub(crate) height: u16,
pub(crate) children_ids: Vec<NodeId>,
pub(crate) state: NodeState,
pub(crate) node_type: NodeType<CustomAttributeValues>,
}
impl TestNode {
#[track_caller]
pub fn get(&self, child_index: usize) -> Self {
self.child(child_index)
.unwrap_or_else(|| panic!("Child by index {child_index} not found"))
}
pub fn child(&self, child_index: usize) -> Option<Self> {
let child_id = self.children_ids.get(child_index)?;
let child: TestNode = self.utils.get_node_by_id(*child_id);
Some(child)
}
pub fn text(&self) -> Option<&str> {
self.node_type.text()
}
pub fn state(&self) -> &NodeState {
&self.state
}
pub fn layout(&self) -> Option<LayoutNode> {
self.utils()
.sdom()
.get()
.layout()
.get(self.node_id)
.cloned()
}
pub fn area(&self) -> Option<Area> {
self.layout().map(|l| l.area)
}
pub fn style(&self) -> Style {
self.utils
.sdom
.get()
.rdom()
.get(self.node_id)
.unwrap()
.get::<Style>()
.unwrap()
.clone()
}
pub fn utils(&self) -> &TestUtils {
&self.utils
}
pub fn parent_id(&self) -> Option<NodeId> {
let sdom = self.utils().sdom();
let fdom = sdom.get();
let dom = fdom.rdom();
let node = dom.get(self.node_id).unwrap();
node.parent_id()
}
pub fn dom_height(&self) -> u16 {
self.height
}
pub fn is_visible(&self) -> bool {
let Some(area) = self.area() else {
return false;
};
let sdom = self.utils().sdom();
let fdom = sdom.get();
let dom = fdom.rdom();
let node = dom.get(self.node_id).unwrap();
let node_viewports = node.get::<ViewportState>().unwrap();
let layout = fdom.layout();
for viewport_id in &node_viewports.viewports {
let viewport = layout.get(*viewport_id).unwrap().visible_area();
if !viewport.intersects(&area) {
return false;
}
}
true
}
pub fn children_ids(&self) -> Vec<NodeId> {
self.children_ids.clone()
}
pub fn is_element(&self) -> bool {
self.node_type.is_element()
}
pub fn is_text(&self) -> bool {
self.node_type.is_text()
}
pub fn is_placeholder(&self) -> bool {
self.node_type.is_placeholder()
}
pub fn get_by_text(&self, matching_text: &str) -> Option<Self> {
self.utils()
.get_node_matching_inside_id(self.node_id, |node| {
if let NodeType::Text(text) = &*node.node_type() {
matching_text == text
} else {
false
}
})
.first()
.cloned()
}
}