Skip to main content

Ui

Struct Ui 

Source
pub struct Ui<'a, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,
{ /* private fields */ }
Expand description

Main UI context for managing widgets, drawing, and interactions.

This struct is the central hub for all UI operations. It manages the draw target, widget states, styling, interactions, and focus management. All widgets are rendered through this context.

§Type Parameters

  • 'a - Lifetime of the borrowed resources
  • DRAW - The draw target type implementing DrawTarget
  • COL - The pixel color type implementing PixelColor

§Example

use matrix_gui::prelude::*;
use embedded_graphics::primitives::Rectangle;

// Create UI context with custom bounds
let bounds = Rectangle::new(Point::new(0, 0), Size::new(320, 240));
let mut ui = Ui::new(&mut display, bounds, &widget_states, &style);

// Or create fullscreen UI
let mut ui = Ui::new_fullscreen(&mut display, &widget_states, &style);

Implementations§

Source§

impl<DRAW, COL> Ui<'_, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,

Source

pub const fn get_screen_width(&self) -> u32

Returns the screen width in pixels.

§Returns

The width of the UI bounds in pixels.

Source

pub const fn get_screen_height(&self) -> u32

Returns the screen height in pixels.

§Returns

The height of the UI bounds in pixels.

Source

pub const fn get_screen_bounds(&self) -> Rectangle

Returns the screen bounding rectangle.

§Returns

A Rectangle representing the UI bounds.

Source§

impl<'a, COL, DRAW> Ui<'a, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,

Source

pub fn new( drawable: &'a mut DRAW, bounds: Rectangle, widget_states: &'a WidgetStates<'a>, style: &'a Style<COL>, ) -> Self

Creates a new UI context with the specified bounds.

§Arguments
  • drawable - Mutable reference to the draw target.
  • bounds - The bounding rectangle for the UI area.
  • widget_states - Reference to the widget state storage.
  • style - Reference to the styling configuration.
§Returns

A new Ui instance.

Source

pub fn new_fullscreen( drawable: &'a mut DRAW, widget_states: &'a WidgetStates<'a>, style: &'a Style<COL>, ) -> Self

Creates a new UI context covering the entire draw target.

This is a convenience method that automatically determines the bounds from the draw target’s bounding box.

§Arguments
  • drawable - Mutable reference to the draw target.
  • widget_states - Reference to the widget state storage.
  • style - Reference to the styling configuration.
§Returns

A new Ui instance with bounds covering the entire draw target.

Source

pub fn add(&mut self, widget: impl Widget<DRAW, COL>) -> Response

Adds a widget to the UI and draws it.

This method calls the widget’s draw method and returns the response. If drawing fails, it returns an error response.

§Arguments
  • widget - The widget to add and draw.
§Returns

The widget’s response, or an error response if drawing fails.

Source

pub fn lazy_draw<F, ID: WidgetId>(&mut self, id: ID, f: F) -> Response
where F: FnOnce(&mut Ui<'_, DRAW, COL>) -> Response,

Conditionally draws a widget based on its redraw state.

This method only calls the drawing closure if the widget needs to be redrawn (based on its state). This is useful for optimizing rendering performance by avoiding unnecessary redraws.

§Type Parameters
  • ID - The widget ID type implementing WidgetId
  • F - The closure type that performs the drawing
§Arguments
  • id - The widget’s identifier.
  • f - A closure that performs the drawing operation.
§Returns

The response from the drawing closure, or Response::Idle if no redraw was needed.

Source

pub fn style(&self) -> &Style<COL>

Returns a reference to the current style configuration.

§Returns

A reference to the Style configuration.

Source§

impl<COL, DRAW> Ui<'_, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,

Source

pub const fn cleared(&self) -> bool

Returns whether the background has been cleared.

§Returns

true if the background has been cleared, false otherwise.

Source

pub fn clear_area(&mut self, area: &Rectangle) -> GuiResult<()>

Clears the specified area with the background color.

This method fills the given rectangle with the background color from the current style. If the background has already been cleared and the debug-color feature is not enabled, this method returns early.

§Arguments
  • area - The rectangle to clear.
§Returns

Ok(()) if clearing succeeds, Err(GuiError) otherwise.

Source

pub fn clear_area_raw(&mut self, area: &Rectangle, color: COL) -> GuiResult<()>

Clears the specified area with a custom color.

This method fills the given rectangle with the specified color. It uses either the standard StyledDrawable trait or the optimized fill-rect feature if enabled.

§Arguments
  • area - The rectangle to clear.
  • color - The color to fill the area with.
§Returns

Ok(()) if clearing succeeds, Err(GuiError::DrawError) otherwise.

Source

pub fn clear_background(&mut self) -> GuiResult<()>

Clears the entire background area.

This method fills the entire UI bounds with the background color from the current style and sets the cleared flag to true.

§Returns

Ok(()) if clearing succeeds, Err(GuiError) otherwise.

Source§

impl<'a, COL, DRAW> Ui<'a, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,

Source

pub fn draw(&mut self, item: &impl Drawable<Color = COL>) -> GuiResult<()>

Draws a drawable item to the UI.

This method provides direct access to draw any item that implements the Drawable trait to the UI’s draw target.

§Arguments
  • item - Reference to the item to draw.
§Returns

Ok(()) if drawing succeeds, Err(GuiError::DrawError) otherwise.

Source§

impl<'a, COL, DRAW> Ui<'a, DRAW, COL>
where DRAW: DrawTarget<Color = COL>, COL: PixelColor,

Source

pub fn get_widget_state<ID: WidgetId>( &self, widget_id: ID, ) -> GuiResult<&RenderState>

Retrieves the render state for a widget.

This method returns a reference to the render state for the widget with the specified ID. The render state contains information about whether the widget needs to be redrawn.

§Type Parameters
  • ID - The widget ID type implementing WidgetId
§Arguments
  • widget_id - The identifier of the widget.
§Returns

A reference to the RenderState if the widget exists, Err(GuiError::InvalidWidgetId) otherwise.

Source

pub fn force_redraw<ID: WidgetId>(&self, widget_id: ID)

Forces a widget to be redrawn on the next frame.

This method marks the widget with the specified ID as needing to be redrawn, regardless of its current state.

§Type Parameters
  • ID - The widget ID type implementing WidgetId
§Arguments
  • widget_id - The identifier of the widget to force redraw.
Source

pub fn force_redraw_all(&self)

Forces all widgets to be redrawn on the next frame.

This method marks all widgets as needing to be redrawn, regardless of their current state. This is useful for refreshing the entire UI.

Auto Trait Implementations§

§

impl<'a, DRAW, COL> Freeze for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> !RefUnwindSafe for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> !Send for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> !Sync for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> Unpin for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> UnsafeUnpin for Ui<'a, DRAW, COL>

§

impl<'a, DRAW, COL> !UnwindSafe for Ui<'a, DRAW, COL>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.