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///
11/// Used by [`Widget`](crate::widget::Widget) implementations to describe layout
12/// and passed to [`Renderer::fill_rect`](crate::renderer::Renderer::fill_rect)
13/// when drawing.
14#[derive(Debug, Clone, Copy)]
15pub struct Rect {
16    /// X coordinate relative to the parent widget.
17    pub x: i32,
18    /// Y coordinate relative to the parent widget.
19    pub y: i32,
20    /// Width in pixels.
21    pub width: i32,
22    /// Height in pixels.
23    pub height: i32,
24}
25
26/// RGBA color used by the renderer.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct Color(
29    /// Red component in the range `0..=255`.
30    pub u8,
31    /// Green component in the range `0..=255`.
32    pub u8,
33    /// Blue component in the range `0..=255`.
34    pub u8,
35    /// Alpha component in the range `0..=255`.
36    ///
37    /// A value of `255` is fully opaque and `0` is fully transparent.
38    pub u8,
39);
40
41impl Color {
42    /// Convert this color to a packed ARGB8888 integer.
43    ///
44    /// Used by display backends in the
45    /// [`rlvgl-platform`](https://docs.rs/rlvgl-platform) crate.
46    pub fn to_argb8888(self) -> u32 {
47        ((self.3 as u32) << 24) | ((self.0 as u32) << 16) | ((self.1 as u32) << 8) | (self.2 as u32)
48    }
49}
50
51/// Base trait implemented by all widgets.
52///
53/// A widget is expected to provide its bounds, draw itself using a
54/// [`Renderer`], and optionally handle input [`Event`]s.
55pub trait Widget {
56    /// Return the area this widget occupies relative to its parent.
57    fn bounds(&self) -> Rect;
58    /// Render the widget using the provided [`Renderer`].
59    fn draw(&self, renderer: &mut dyn Renderer);
60    /// Handle an event and return `true` if it was consumed.
61    ///
62    /// The default implementation for most widgets will simply ignore the
63    /// event and return `false`.
64    fn handle_event(&mut self, event: &Event) -> bool;
65}