matrix_gui/widgets/background.rs
1//! Background widget for clearing the entire screen.
2//!
3//! This module provides a widget that clears the entire background area
4//! of the UI. It's typically used as the first widget in a UI layout
5//! to ensure a clean background before drawing other widgets.
6
7use crate::prelude::*;
8
9/// Background widget that clears the entire screen.
10///
11/// This widget clears the entire background area of the UI using the
12/// background color from the current style. It's typically used as
13/// the first widget in a UI layout to ensure a clean background.
14///
15/// # Type Parameters
16///
17/// * `ID` - The widget ID type implementing [`WidgetId`]
18pub struct Background<ID: WidgetId> {
19 /// The widget identifier.
20 widget_id: ID,
21}
22
23impl<ID: WidgetId> Background<ID> {
24 pub const fn new(widget_id: ID) -> Background<ID> {
25 Background { widget_id }
26 }
27}
28
29impl<DRAW: DrawTarget<Color = COL>, ID: WidgetId, COL: PixelColor> Widget<DRAW, COL>
30 for Background<ID>
31{
32 fn draw(&mut self, ui: &mut Ui<DRAW, COL>) -> GuiResult<Response> {
33 let widget_state = ui.get_widget_state(self.widget_id)?;
34 if widget_state.compare_set(RenderStatus::Rendered) {
35 return Ok(Response::Idle);
36 }
37
38 ui.clear_background()?;
39
40 Ok(Response::Idle)
41 }
42}