use super::layout::TextMeasure;
use super::style::Style;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NodeId(pub(crate) usize);
#[derive(Debug, Clone, PartialEq)]
pub enum NodeKind {
Box,
Text(String),
Path {
d: Vec<String>,
view_box: [f32; 4],
stroke: Option<f32>,
fill: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
pub fn contains(&self, x: f32, y: f32) -> bool {
x >= self.x && y >= self.y && x < self.x + self.width && y < self.y + self.height
}
pub fn hit(&self, x: f32, y: f32) -> bool {
x + 1.0 > self.x && y + 1.0 > self.y && x < self.x + self.width && y < self.y + self.height
}
}
#[derive(Debug, Clone)]
pub(crate) struct Node {
pub style: Style,
pub kind: NodeKind,
pub parent: Option<NodeId>,
pub children: Vec<NodeId>,
pub rect: Rect,
pub removed: bool,
pub scroll_height: f32,
pub scrolled: f32,
pub lines: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct Ui {
pub(crate) nodes: Vec<Node>,
pub(crate) layout_units: bool,
}
impl Default for Ui {
fn default() -> Self {
Self::new()
}
}
impl Ui {
pub fn new() -> Self {
Self {
nodes: vec![Node {
style: Style::default(),
kind: NodeKind::Box,
parent: None,
children: vec![],
rect: Rect::default(),
removed: false,
scroll_height: 0.0,
scrolled: 0.0,
lines: vec![],
}],
layout_units: false,
}
}
pub fn set_layout_units(&mut self, on: bool) {
self.layout_units = on;
}
pub fn root(&self) -> NodeId {
NodeId(0)
}
fn add(&mut self, parent: NodeId, style: Style, kind: NodeKind) -> NodeId {
let id = NodeId(self.nodes.len());
self.nodes.push(Node { style, kind, parent: Some(parent), children: vec![], rect: Rect::default(), removed: false, scroll_height: 0.0, scrolled: 0.0, lines: vec![] });
self.nodes[parent.0].children.push(id);
id
}
pub fn add_box(&mut self, parent: NodeId, style: Style) -> NodeId {
self.add(parent, style, NodeKind::Box)
}
pub fn add_text(&mut self, parent: NodeId, text: impl Into<String>) -> NodeId {
let p = &self.nodes[parent.0].style;
let style = Style {
color: p.color,
font_family: p.font_family.clone(),
font_size: p.font_size,
font_weight: p.font_weight,
line_height: p.line_height,
line_through: p.line_through,
wrap: p.wrap,
italic: p.italic,
vertical: p.vertical,
..Style::default()
};
self.add(parent, style, NodeKind::Text(text.into()))
}
pub fn add_path(&mut self, parent: NodeId, style: Style, d: Vec<String>, view_box: [f32; 4], stroke: Option<f32>, fill: bool) -> NodeId {
self.add(parent, style, NodeKind::Path { d, view_box, stroke, fill })
}
pub fn remove(&mut self, id: NodeId) {
if let Some(p) = self.nodes[id.0].parent {
self.nodes[p.0].children.retain(|&c| c != id);
}
let mut stack = vec![id];
while let Some(n) = stack.pop() {
self.nodes[n.0].removed = true;
stack.extend(self.nodes[n.0].children.clone());
}
}
pub fn offset(&mut self, id: NodeId, dx: f32, dy: f32) {
let mut stack = vec![id];
while let Some(n) = stack.pop() {
self.nodes[n.0].rect.x += dx;
self.nodes[n.0].rect.y += dy;
stack.extend(self.nodes[n.0].children.iter().copied());
}
}
pub fn move_child(&mut self, parent: NodeId, child: NodeId, index: usize) {
let children = &mut self.nodes[parent.0].children;
if let Some(i) = children.iter().position(|&c| c == child) {
children.remove(i);
children.insert(index.min(children.len()), child);
}
}
pub fn reparent(&mut self, child: NodeId, parent: NodeId) {
if let Some(p) = self.nodes[child.0].parent {
self.nodes[p.0].children.retain(|&c| c != child);
}
self.nodes[child.0].parent = Some(parent);
self.nodes[parent.0].children.push(child);
}
pub fn clear_children(&mut self, id: NodeId) {
for c in self.nodes[id.0].children.clone() {
self.remove(c);
}
}
pub fn style(&self, id: NodeId) -> &Style {
&self.nodes[id.0].style
}
pub fn style_mut(&mut self, id: NodeId) -> &mut Style {
&mut self.nodes[id.0].style
}
pub fn set_style(&mut self, id: NodeId, style: Style) {
self.nodes[id.0].style = style;
}
pub fn kind(&self, id: NodeId) -> &NodeKind {
&self.nodes[id.0].kind
}
pub fn set_text(&mut self, id: NodeId, text: impl Into<String>) {
self.nodes[id.0].kind = NodeKind::Text(text.into());
}
pub fn children(&self, id: NodeId) -> &[NodeId] {
&self.nodes[id.0].children
}
pub fn parent(&self, id: NodeId) -> Option<NodeId> {
self.nodes[id.0].parent
}
pub fn rect(&self, id: NodeId) -> Rect {
self.nodes[id.0].rect
}
pub fn layout(&mut self, width: f32, height: f32, text: &mut dyn TextMeasure) {
super::layout::layout(self, width, height, text);
}
pub fn hit_test(&self, x: f32, y: f32) -> Option<NodeId> {
self.hit(self.root(), x, y)
}
fn hit(&self, id: NodeId, x: f32, y: f32) -> Option<NodeId> {
let node = &self.nodes[id.0];
if node.style.display == super::style::Display::None {
return None;
}
let inside = node.rect.hit(x, y);
if node.style.clip && !inside {
return None;
}
for c in self.paint_order(id).into_iter().rev() {
if let Some(h) = self.hit(c, x, y) {
return Some(h);
}
}
inside.then_some(id)
}
pub fn paint_order(&self, id: NodeId) -> Vec<NodeId> {
let children = &self.nodes[id.0].children;
let positioned = |c: &NodeId| self.nodes[c.0].style.position != super::style::Position::Static;
children.iter().copied().filter(|c| !positioned(c)).chain(children.iter().copied().filter(positioned)).collect()
}
pub fn scroll_extent(&self, id: NodeId) -> (f32, f32, f32) {
let n = &self.nodes[id.0];
let s = &n.style;
(n.scroll_height, (n.rect.height - s.border.vertical()).max(0.0), n.scrolled)
}
pub fn scroll_container(&self, id: NodeId) -> Option<NodeId> {
self.ancestors(id).find(|n| self.nodes[n.0].style.scroll)
}
pub fn ancestors(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
std::iter::successors(Some(id), move |n| self.nodes[n.0].parent)
}
}