Skip to main content

zenthra_core/
widget.rs

1// crates/zenthra-core/src/widget.rs
2
3use crate::event::{Event, EventResponse};
4use crate::rect::{Rect, Size};
5
6/// Layout constraints passed down the widget tree.
7#[derive(Debug, Clone, Copy)]
8pub struct Constraints {
9    pub min: Size,
10    pub max: Size,
11}
12
13impl Constraints {
14    pub fn loose(max: Size) -> Self {
15        Self {
16            min: Size::ZERO,
17            max,
18        }
19    }
20    pub fn tight(size: Size) -> Self {
21        Self {
22            min: size,
23            max: size,
24        }
25    }
26
27    pub fn constrain(&self, size: Size) -> Size {
28        size.clamp(self.min, self.max)
29    }
30}
31
32/// Every UI element implements this.
33pub trait Widget: std::fmt::Debug {
34    /// Measure and return the widget's size given constraints.
35    fn layout(&mut self, constraints: Constraints) -> Size;
36
37    /// Called after layout — widget knows its final rect.
38    fn paint(&self, rect: Rect);
39
40    /// Handle an event. Default: ignore.
41    fn on_event(&mut self, event: &Event, rect: Rect) -> EventResponse {
42        let _ = (event, rect);
43        EventResponse::Ignored
44    }
45
46    /// Human-readable name for debugging.
47    fn name(&self) -> &str {
48        "Widget"
49    }
50}