Skip to main content

altui_core/widgets/
clear.rs

1use crate::{buffer::Buffer, layout::Rect, widgets::Widget};
2
3/// A widget to clear/reset a certain area to allow overdrawing (e.g. for popups).
4///
5/// This widget **cannot be used to clear the terminal on the first render** as `altui_core` assumes the
6/// render area is empty. Use [`crate::Terminal::clear`] instead.
7///
8/// # Examples
9///
10/// ```
11/// # use altui_core::widgets::{Clear, Block, Borders};
12/// # use altui_core::layout::Rect;
13/// # use altui_core::Frame;
14/// # use altui_core::backend::Backend;
15/// fn draw_on_clear<B: Backend>(f: &mut Frame<B>, area: Rect) {
16///     let mut block = Block::default().title("Block").borders(Borders::ALL);
17///     f.render_widget(&mut Clear, area); // <- this will clear/reset the area first
18///     f.render_widget(&mut block, area); // now render the block widget
19/// }
20/// ```
21///
22/// # Popup Example
23///
24/// For a more complete example how to utilize `Clear` to realize popups see
25/// the example `examples/popup.rs`
26#[derive(Debug, Clone)]
27pub struct Clear;
28
29impl Widget for Clear {
30    fn render(&mut self, area: Rect, buf: &mut Buffer) {
31        for x in area.left()..area.right() {
32            for y in area.top()..area.bottom() {
33                buf.get_mut(x, y).reset();
34            }
35        }
36    }
37}