on master help sign in
Replace the Clear widget with buffer.clear/clear_with.
commitff4c1d05deee2b4b4d85c29a627fb01e60886c7f authorBlaž Hrastnik <blaz@mxxn.io> dateSun 9 May 2021, 18:13 (5 years ago) parent1255bcb
7 files changed+25−77
helix-term/src/ui/completion.rs+1−8 helix-term/src/ui/picker.rs+1−8 helix-term/src/ui/popup.rs+1−8 helix-term/src/ui/prompt.rs+2−14 helix-tui/src/buffer.rs+20 helix-tui/src/widgets/clear.rs−36 helix-tui/src/widgets/mod.rs−3
helix-term/src/ui/completion.rs+1−8
@@ -202,14 +202,7 @@ impl Component for Completion {
202202
203203 // clear area
204204 let background = cx.editor.theme.get("ui.popup");
205− for y in area.top()..area.bottom() {
206− for x in area.left()..area.right() {
207− let cell = surface.get_mut(x, y);
208− cell.reset();
209− // cell.symbol.clear();
210− cell.set_style(background);
211− }
212− }
205+ surface.clear_with(area, background);
213206 doc.render(area, surface, cx);
214207 }
215208 None => (),
helix-term/src/ui/picker.rs+1−8
@@ -205,14 +205,7 @@ impl<T: 'static> Component for Picker<T> {
205205
206206 // clear area
207207 let background = cx.editor.theme.get("ui.background");
208− for y in area.top()..area.bottom() {
209− for x in area.left()..area.right() {
210− let cell = surface.get_mut(x, y);
211− cell.reset();
212− // cell.symbol.clear();
213− cell.set_style(background);
214− }
215− }
208+ surface.clear_with(area, background);
216209
217210 use tui::widgets::Widget;
218211 // don't like this but the lifetime sucks
helix-term/src/ui/popup.rs+1−8
@@ -148,14 +148,7 @@ impl<T: Component> Component for Popup<T> {
148148
149149 // clear area
150150 let background = cx.editor.theme.get("ui.popup");
151− for y in area.top()..area.bottom() {
152− for x in area.left()..area.right() {
153− let cell = surface.get_mut(x, y);
154− cell.reset();
155− // cell.symbol.clear();
156− cell.set_style(background);
157− }
158− }
151+ surface.clear_with(area, background);
159152
160153 self.contents.render(area, surface, cx);
161154 }
helix-term/src/ui/prompt.rs+2−14
@@ -126,13 +126,7 @@ impl Prompt {
126126 let area = completion_area;
127127 let background = theme.get("ui.statusline");
128128
129− for y in area.top()..area.bottom() {
130− for x in area.left()..area.right() {
131− let cell = surface.get_mut(x, y);
132− cell.reset();
133− cell.set_style(background);
134− }
135− }
129+ surface.clear_with(area, background);
136130
137131 let mut row = 0;
138132 let mut col = 0;
@@ -172,13 +166,7 @@ impl Prompt {
172166 );
173167
174168 let background = theme.get("ui.window");
175− for y in area.top()..area.bottom() {
176− for x in area.left()..area.right() {
177− let cell = surface.get_mut(x, y);
178− cell.reset();
179− cell.set_style(background);
180− }
181− }
169+ surface.clear_with(area, background);
182170
183171 use tui::layout::Margin;
184172 text.render(
helix-tui/src/buffer.rs+20
@@ -379,6 +379,26 @@ impl Buffer {
379379 }
380380 }
381381
382+ /// Clear an area in the buffer
383+ pub fn clear(&mut self, area: Rect) {
384+ for x in area.left()..area.right() {
385+ for y in area.top()..area.bottom() {
386+ self.get_mut(x, y).reset();
387+ }
388+ }
389+ }
390+
391+ /// Clear an area in the buffer with a default style.
392+ pub fn clear_with(&mut self, area: Rect, style: Style) {
393+ for x in area.left()..area.right() {
394+ for y in area.top()..area.bottom() {
395+ let cell = self.get_mut(x, y);
396+ cell.reset();
397+ cell.set_style(style);
398+ }
399+ }
400+ }
401+
382402 /// Merge an other buffer into this one
383403 pub fn merge(&mut self, other: &Buffer) {
384404 let area = self.area.union(other.area);
helix-tui/src/widgets/clear.rsdeleted−36
@@ -1,36 +0,0 @@
1−use crate::buffer::Buffer;
2−use crate::layout::Rect;
3−use crate::widgets::Widget;
4−
5−/// A widget to to clear/reset a certain area to allow overdrawing (e.g. for popups)
6−///
7−/// # Examples
8−///
9−/// ```
10−/// # use helix_tui::widgets::{Clear, Block, Borders};
11−/// # use helix_tui::layout::Rect;
12−/// # use helix_tui::Frame;
13−/// # use helix_tui::backend::Backend;
14−/// fn draw_on_clear<B: Backend>(f: &mut Frame<B>, area: Rect) {
15−/// let block = Block::default().title("Block").borders(Borders::ALL);
16−/// f.render_widget(Clear, area); // <- this will clear/reset the area first
17−/// f.render_widget(block, area); // now render the block widget
18−/// }
19−/// ```
20−///
21−/// # Popup Example
22−///
23−/// For a more complete example how to utilize `Clear` to realize popups see
24−/// the example `examples/popup.rs`
25−#[derive(Debug, Clone)]
26−pub struct Clear;
27−
28−impl Widget for Clear {
29− fn render(self, area: Rect, buf: &mut Buffer) {
30− for x in area.left()..area.right() {
31− for y in area.top()..area.bottom() {
32− buf.get_mut(x, y).reset();
33− }
34− }
35− }
36−}
helix-tui/src/widgets/mod.rs−3
@@ -9,17 +9,14 @@
99//! - [`List`]
1010//! - [`Table`]
1111//! - [`Paragraph`]
12−//! - [`Clear`]
1312
1413mod block;
15−mod clear;
1614// mod list;
1715mod paragraph;
1816mod reflow;
1917// mod table;
2018
2119pub use self::block::{Block, BorderType};
22−pub use self::clear::Clear;
2320// pub use self::list::{List, ListItem, ListState};
2421pub use self::paragraph::{Paragraph, Wrap};
2522// pub use self::table::{Cell, Row, Table, TableState};