use std::time::{Duration, Instant};
use y901_ui::layout::LayoutCache;
use y901_ui::{
Constraints, EventCtx, EventResult, Rect, RenderCtx, Semantics, Size, TextMetrics, UiEvent,
UiTree, Widget,
};
struct Leaf {
id: String,
rect: Rect,
}
impl Leaf {
fn new(id: &str) -> Self {
Self {
id: id.to_string(),
rect: Rect::ZERO,
}
}
}
impl Widget for Leaf {
fn id(&self) -> &str {
&self.id
}
fn measure(&mut self, _m: &dyn TextMetrics, c: Constraints, _cache: &mut LayoutCache) -> Size {
c.constrain(Size::new(40.0, 20.0))
}
fn arrange(&mut self, rect: Rect, _m: &dyn TextMetrics, _cache: &mut LayoutCache) {
self.rect = rect;
}
fn rect(&self) -> Rect {
self.rect
}
fn event(&mut self, _c: &mut EventCtx, _e: &UiEvent) -> EventResult {
EventResult::Ignored
}
fn children(&self) -> &[Box<dyn Widget>] {
&[]
}
fn render(&mut self, _ctx: &mut dyn RenderCtx) {}
fn semantics(&self) -> Semantics {
Semantics::default()
}
}
struct Container {
id: String,
rect: Rect,
kids: Vec<Box<dyn Widget>>,
}
impl Container {
fn new(id: &str, kids: Vec<Box<dyn Widget>>) -> Self {
Self {
id: id.to_string(),
rect: Rect::ZERO,
kids,
}
}
}
impl Widget for Container {
fn id(&self) -> &str {
&self.id
}
fn measure(&mut self, _m: &dyn TextMetrics, c: Constraints, _cache: &mut LayoutCache) -> Size {
c.constrain(Size::new(200.0, 100.0))
}
fn arrange(&mut self, rect: Rect, _m: &dyn TextMetrics, _cache: &mut LayoutCache) {
self.rect = rect;
for (i, k) in self.kids.iter_mut().enumerate() {
k.arrange(
Rect::new(rect.x, rect.y + i as f32 * 24.0, rect.w, 20.0),
_m,
_cache,
);
}
}
fn rect(&self) -> Rect {
self.rect
}
fn event(&mut self, _c: &mut EventCtx, _e: &UiEvent) -> EventResult {
EventResult::Ignored
}
fn children(&self) -> &[Box<dyn Widget>] {
&self.kids
}
fn children_mut(&mut self) -> Option<&mut Vec<Box<dyn Widget>>> {
Some(&mut self.kids)
}
fn render(&mut self, _ctx: &mut dyn RenderCtx) {}
fn semantics(&self) -> Semantics {
Semantics::default()
}
}
struct Dummy;
impl TextMetrics for Dummy {
fn measure_text(&self, text: &str, _style: &y901_ui::TextStyle) -> Size {
Size::new(text.chars().count() as f32 * 8.0, 16.0)
}
fn line_height(&self, _style: &y901_ui::TextStyle) -> f32 {
16.0
}
fn scale(&self) -> f32 {
1.0
}
}
fn tree_with_child() -> UiTree {
let root = Container::new("root", vec![Box::new(Leaf::new("a"))]);
let mut tree = UiTree::new(Box::new(root));
tree.layout(Size::new(200.0, 100.0), &Dummy);
tree
}
#[test]
fn subscription_of_removed_node_is_cleaned_up_on_next_poll() {
let mut tree = tree_with_child();
assert!(
tree.subscribe("a", Duration::from_millis(16)),
"存在的节点应能订阅"
);
assert_eq!(tree.subscription_count(), 1);
assert!(tree.remove_node("a"), "摘除 a 应成功");
assert_eq!(
tree.subscription_count(),
1,
"remove_node 不做立即清理(懒清理设计)"
);
let _ = tree.poll_subscriptions(Instant::now() + Duration::from_secs(1));
assert_eq!(
tree.subscription_count(),
0,
"节点已被删的订阅必须在下一次 poll 被剔除(否则就是泄漏)"
);
}
#[test]
fn live_subscriptions_survive_poll() {
let mut tree = tree_with_child();
assert!(tree.subscribe("a", Duration::from_millis(16)));
let _ = tree.poll_subscriptions(Instant::now() + Duration::from_secs(1));
assert_eq!(tree.subscription_count(), 1, "节点存在时,订阅必须保留");
let _ = tree.poll_subscriptions(Instant::now() + Duration::from_secs(2));
assert_eq!(tree.subscription_count(), 1, "重复 poll 也不应删掉有效订阅");
}