allow all draw calls to mutate self

This commit is contained in:
Stephan Dilly 2020-05-12 13:22:55 +02:00
parent 434c793d1c
commit 70e5201956
10 changed files with 55 additions and 57 deletions

View file

@ -86,7 +86,6 @@ impl App {
if self.tab == 0 {
self.status_tab.draw(f, chunks_main[1]);
} else {
self.revlog.prepare_draw(chunks_main[1]);
self.revlog.draw(f, chunks_main[1]);
}
@ -311,7 +310,7 @@ impl App {
|| self.msg.is_visible()
}
fn draw_popups<B: Backend>(&self, f: &mut Frame<B>) {
fn draw_popups<B: Backend>(&mut self, f: &mut Frame<B>) {
let size = f.size();
self.commit.draw(f, size);

View file

@ -255,7 +255,7 @@ impl ChangesComponent {
}
impl DrawableComponent for ChangesComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, r: Rect) {
let selection_offset =
self.tree.tree.items().iter().enumerate().fold(
0,

View file

@ -27,7 +27,7 @@ pub struct CommitComponent {
}
impl DrawableComponent for CommitComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _rect: Rect) {
if self.visible {
let txt = if self.msg.is_empty() {
[Text::Styled(

View file

@ -104,8 +104,8 @@ impl DiffComponent {
}
ScrollType::Home => self.scroll = 0,
ScrollType::End => self.scroll = scroll_max,
//TODO:
_ => (),
ScrollType::PageDown => (),
ScrollType::PageUp => (),
}
if old != self.scroll {
@ -296,7 +296,7 @@ impl DiffComponent {
}
impl DrawableComponent for DiffComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, r: Rect) {
let mut style_border = Style::default().fg(Color::DarkGray);
let mut style_title = Style::default();
if self.focused {

View file

@ -25,7 +25,7 @@ pub struct HelpComponent {
}
impl DrawableComponent for HelpComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _rect: Rect) {
if self.visible {
let (txt, selected_line) = self.get_text();

View file

@ -82,7 +82,7 @@ pub fn visibility_blocking<T: Component>(
///
pub trait DrawableComponent {
///
fn draw<B: Backend>(&self, f: &mut Frame<B>, rect: Rect);
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, rect: Rect);
}
/// base component trait

View file

@ -20,7 +20,7 @@ pub struct MsgComponent {
}
impl DrawableComponent for MsgComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _rect: Rect) {
if self.visible {
let txt = vec![Text::Raw(Cow::from(self.msg.as_str()))];

View file

@ -26,7 +26,7 @@ pub struct ResetComponent {
}
impl DrawableComponent for ResetComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _rect: Rect) {
if self.visible {
let mut txt = Vec::new();
txt.push(Text::Styled(

View file

@ -2,7 +2,8 @@ mod utils;
use crate::{
components::{
CommandBlocking, CommandInfo, Component, ScrollType,
CommandBlocking, CommandInfo, Component, DrawableComponent,
ScrollType,
},
keys,
strings::commands,
@ -72,50 +73,6 @@ impl Revlog {
}
}
///
pub fn prepare_draw(&mut self, area: Rect) {
self.current_height = area.height.saturating_sub(2);
}
///
pub fn draw<B: Backend>(&self, f: &mut Frame<B>, area: Rect) {
let height = area.height as usize;
let selection =
self.selection.saturating_sub(self.items.index_offset);
let height_d2 = height as usize / 2;
let min = selection.saturating_sub(height_d2);
let mut txt = Vec::new();
for (idx, e) in self.items.items.iter().enumerate() {
let tag = if let Some(tag_name) = self.tags.get(&e.hash) {
tag_name.as_str()
} else {
""
};
Self::add_entry(e, idx == selection, &mut txt, tag);
}
let title = format!(
"commit {}/{}",
self.selection, self.selection_max
);
f.render_widget(
Paragraph::new(
txt.iter()
.skip(min * ELEMENTS_PER_LINE)
.take(height * ELEMENTS_PER_LINE),
)
.block(
Block::default()
.borders(Borders::ALL)
.title(title.as_str()),
)
.alignment(Alignment::Left),
area,
);
}
///
pub fn any_work_pending(&self) -> bool {
self.git_log.is_pending()
@ -273,6 +230,48 @@ impl Revlog {
}
}
impl DrawableComponent for Revlog {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect) {
self.current_height = area.height.saturating_sub(2);
let height = area.height as usize;
let selection =
self.selection.saturating_sub(self.items.index_offset);
let height_d2 = height as usize / 2;
let min = selection.saturating_sub(height_d2);
let mut txt = Vec::new();
for (idx, e) in self.items.items.iter().enumerate() {
let tag = if let Some(tag_name) = self.tags.get(&e.hash) {
tag_name.as_str()
} else {
""
};
Self::add_entry(e, idx == selection, &mut txt, tag);
}
let title = format!(
"commit {}/{}",
self.selection, self.selection_max
);
f.render_widget(
Paragraph::new(
txt.iter()
.skip(min * ELEMENTS_PER_LINE)
.take(height * ELEMENTS_PER_LINE),
)
.block(
Block::default()
.borders(Borders::ALL)
.title(title.as_str()),
)
.alignment(Alignment::Left),
area,
);
}
}
impl Component for Revlog {
fn event(&mut self, ev: Event) -> bool {
if self.visible {

View file

@ -46,7 +46,7 @@ pub struct Status {
impl DrawableComponent for Status {
fn draw<B: tui::backend::Backend>(
&self,
&mut self,
f: &mut tui::Frame<B>,
rect: tui::layout::Rect,
) {