From ff4c1d05deee2b4b4d85c29a627fb01e60886c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 9 May 2021 18:13:50 +0900 Subject: [PATCH] Replace the Clear widget with buffer.clear/clear_with. --- helix-term/src/ui/completion.rs | 9 +-------- helix-term/src/ui/picker.rs | 9 +-------- helix-term/src/ui/popup.rs | 9 +-------- helix-term/src/ui/prompt.rs | 16 ++------------- helix-tui/src/buffer.rs | 20 ++++++++++++++++++ helix-tui/src/widgets/clear.rs | 36 --------------------------------- helix-tui/src/widgets/mod.rs | 3 --- 7 files changed, 25 insertions(+), 77 deletions(-) delete mode 100644 helix-tui/src/widgets/clear.rs diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 3f60dad4..238569f0 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -202,14 +202,7 @@ impl Component for Completion { // clear area let background = cx.editor.theme.get("ui.popup"); - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - let cell = surface.get_mut(x, y); - cell.reset(); - // cell.symbol.clear(); - cell.set_style(background); - } - } + surface.clear_with(area, background); doc.render(area, surface, cx); } None => (), diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 6d77bf1d..55975240 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -205,14 +205,7 @@ impl Component for Picker { // clear area let background = cx.editor.theme.get("ui.background"); - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - let cell = surface.get_mut(x, y); - cell.reset(); - // cell.symbol.clear(); - cell.set_style(background); - } - } + surface.clear_with(area, background); use tui::widgets::Widget; // don't like this but the lifetime sucks diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 7ce7166c..c59287e2 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -148,14 +148,7 @@ impl Component for Popup { // clear area let background = cx.editor.theme.get("ui.popup"); - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - let cell = surface.get_mut(x, y); - cell.reset(); - // cell.symbol.clear(); - cell.set_style(background); - } - } + surface.clear_with(area, background); self.contents.render(area, surface, cx); } diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index b09b8e14..9bde1f57 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -126,13 +126,7 @@ impl Prompt { let area = completion_area; let background = theme.get("ui.statusline"); - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - let cell = surface.get_mut(x, y); - cell.reset(); - cell.set_style(background); - } - } + surface.clear_with(area, background); let mut row = 0; let mut col = 0; @@ -172,13 +166,7 @@ impl Prompt { ); let background = theme.get("ui.window"); - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - let cell = surface.get_mut(x, y); - cell.reset(); - cell.set_style(background); - } - } + surface.clear_with(area, background); use tui::layout::Margin; text.render( diff --git a/helix-tui/src/buffer.rs b/helix-tui/src/buffer.rs index 8a227ce7..c584ee7f 100644 --- a/helix-tui/src/buffer.rs +++ b/helix-tui/src/buffer.rs @@ -379,6 +379,26 @@ impl Buffer { } } + /// Clear an area in the buffer + pub fn clear(&mut self, area: Rect) { + for x in area.left()..area.right() { + for y in area.top()..area.bottom() { + self.get_mut(x, y).reset(); + } + } + } + + /// Clear an area in the buffer with a default style. + pub fn clear_with(&mut self, area: Rect, style: Style) { + for x in area.left()..area.right() { + for y in area.top()..area.bottom() { + let cell = self.get_mut(x, y); + cell.reset(); + cell.set_style(style); + } + } + } + /// Merge an other buffer into this one pub fn merge(&mut self, other: &Buffer) { let area = self.area.union(other.area); diff --git a/helix-tui/src/widgets/clear.rs b/helix-tui/src/widgets/clear.rs deleted file mode 100644 index d1da40de..00000000 --- a/helix-tui/src/widgets/clear.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::buffer::Buffer; -use crate::layout::Rect; -use crate::widgets::Widget; - -/// A widget to to clear/reset a certain area to allow overdrawing (e.g. for popups) -/// -/// # Examples -/// -/// ``` -/// # use helix_tui::widgets::{Clear, Block, Borders}; -/// # use helix_tui::layout::Rect; -/// # use helix_tui::Frame; -/// # use helix_tui::backend::Backend; -/// fn draw_on_clear(f: &mut Frame, area: Rect) { -/// let block = Block::default().title("Block").borders(Borders::ALL); -/// f.render_widget(Clear, area); // <- this will clear/reset the area first -/// f.render_widget(block, area); // now render the block widget -/// } -/// ``` -/// -/// # Popup Example -/// -/// For a more complete example how to utilize `Clear` to realize popups see -/// the example `examples/popup.rs` -#[derive(Debug, Clone)] -pub struct Clear; - -impl Widget for Clear { - fn render(self, area: Rect, buf: &mut Buffer) { - for x in area.left()..area.right() { - for y in area.top()..area.bottom() { - buf.get_mut(x, y).reset(); - } - } - } -} diff --git a/helix-tui/src/widgets/mod.rs b/helix-tui/src/widgets/mod.rs index 76eb73c1..5a48e9b5 100644 --- a/helix-tui/src/widgets/mod.rs +++ b/helix-tui/src/widgets/mod.rs @@ -9,17 +9,14 @@ //! - [`List`] //! - [`Table`] //! - [`Paragraph`] -//! - [`Clear`] mod block; -mod clear; // mod list; mod paragraph; mod reflow; // mod table; pub use self::block::{Block, BorderType}; -pub use self::clear::Clear; // pub use self::list::{List, ListItem, ListState}; pub use self::paragraph::{Paragraph, Wrap}; // pub use self::table::{Cell, Row, Table, TableState};