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/// RGBA 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    /// Alpha component in the range `0..=255`.
32    ///
33    /// A value of `255` is fully opaque and `0` is fully transparent.
34    pub u8,
35);
36
37/// Base trait implemented by all widgets.
38///
39/// A widget is expected to provide its bounds, draw itself using a
40/// [`Renderer`], and optionally handle input [`Event`]s.
41pub trait Widget {
42    /// Return the area this widget occupies relative to its parent.
43    fn bounds(&self) -> Rect;
44    /// Render the widget using the provided [`Renderer`].
45    fn draw(&self, renderer: &mut dyn Renderer);
46    /// Handle an event and return `true` if it was consumed.
47    ///
48    /// The default implementation for most widgets will simply ignore the
49    /// event and return `false`.
50    fn handle_event(&mut self, event: &Event) -> bool;
51}