use crate::{
input::{ContainerOption, WidgetBehaviourOption},
layout::{SizePolicy, StackDirection},
ContainerHandle, Custom, Id, Node,
};
use super::{TreeCustomRender, WidgetHandle, WidgetStateHandleDyn};
pub type NodeId = Id;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Policy {
pub width: SizePolicy,
pub height: SizePolicy,
}
impl Policy {
pub const fn new(width: SizePolicy, height: SizePolicy) -> Self {
Self { width, height }
}
pub const fn auto() -> Self {
Self::new(SizePolicy::Auto, SizePolicy::Auto)
}
pub const fn fixed(width: i32, height: i32) -> Self {
Self::new(SizePolicy::Fixed(width), SizePolicy::Fixed(height))
}
pub const fn fixed_width(width: i32) -> Self {
Self::new(SizePolicy::Fixed(width), SizePolicy::Auto)
}
pub const fn fixed_height(height: i32) -> Self {
Self::new(SizePolicy::Auto, SizePolicy::Fixed(height))
}
pub const fn fill() -> Self {
Self::new(SizePolicy::Remainder(0), SizePolicy::Remainder(0))
}
}
pub(crate) enum WidgetTreeNodeKind {
Widget {
widget: Box<dyn WidgetStateHandleDyn>,
},
CustomRender {
state: WidgetHandle<Custom>,
render: TreeCustomRender,
},
Container {
handle: ContainerHandle,
opt: ContainerOption,
behaviour: WidgetBehaviourOption,
},
Header {
state: WidgetHandle<Node>,
},
Tree {
state: WidgetHandle<Node>,
},
Row {
widths: Vec<SizePolicy>,
height: SizePolicy,
},
Grid {
widths: Vec<SizePolicy>,
heights: Vec<SizePolicy>,
},
Column,
Stack {
width: SizePolicy,
height: SizePolicy,
direction: StackDirection,
},
}
impl WidgetTreeNodeKind {
pub(super) fn tag(&self) -> u8 {
match self {
Self::Widget { .. } => 1,
Self::CustomRender { .. } => 2,
Self::Container { .. } => 3,
Self::Header { .. } => 4,
Self::Tree { .. } => 5,
Self::Row { .. } => 6,
Self::Grid { .. } => 7,
Self::Column => 8,
Self::Stack { .. } => 9,
}
}
}
pub struct WidgetTreeNode {
pub(super) id: NodeId,
pub(super) policy: Policy,
pub(super) kind: WidgetTreeNodeKind,
pub(super) children: Vec<WidgetTreeNode>,
}
impl WidgetTreeNode {
pub fn id(&self) -> NodeId {
self.id
}
pub fn policy(&self) -> Policy {
self.policy
}
#[cfg(test)]
pub(crate) fn kind(&self) -> &WidgetTreeNodeKind {
&self.kind
}
pub fn children(&self) -> &[WidgetTreeNode] {
&self.children
}
pub(crate) fn parts(&self) -> (NodeId, &WidgetTreeNodeKind, &[WidgetTreeNode]) {
(self.id, &self.kind, &self.children)
}
}
#[derive(Default)]
pub struct WidgetTree {
pub(super) roots: Vec<WidgetTreeNode>,
}
impl WidgetTree {
pub fn roots(&self) -> &[WidgetTreeNode] {
&self.roots
}
}