rlvgl_core/
widget.rs

1//! Basic widget traits and geometry types.
2
3use crate::event::Event;
4use crate::renderer::Renderer;
5
6/// Rectangle bounds of a widget.
7///
8/// Coordinates are relative to the parent widget. Width and height are signed
9/// integers to simplify layout calculations.
10#[derive(Debug, Clone, Copy)]
11pub struct Rect {
12    /// X coordinate relative to the parent widget.
13    pub x: i32,
14    /// Y coordinate relative to the parent widget.
15    pub y: i32,
16    /// Width in pixels.
17    pub width: i32,
18    /// Height in pixels.
19    pub height: i32,
20}
21
22/// RGB color used by the renderer.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct Color(
25    /// Red component in the range `0..=255`.
26    pub u8,
27    /// Green component in the range `0..=255`.
28    pub u8,
29    /// Blue component in the range `0..=255`.
30    pub u8,
31);
32
33/// Base trait implemented by all widgets.
34///
35/// A widget is expected to provide its bounds, draw itself using a
36/// [`Renderer`], and optionally handle input [`Event`]s.
37pub trait Widget {
38    /// Return the area this widget occupies relative to its parent.
39    fn bounds(&self) -> Rect;
40    /// Render the widget using the provided [`Renderer`].
41    fn draw(&self, renderer: &mut dyn Renderer);
42    /// Handle an event and return `true` if it was consumed.
43    ///
44    /// The default implementation for most widgets will simply ignore the
45    /// event and return `false`.
46    fn handle_event(&mut self, event: &Event) -> bool;
47}