From 0d84abd1c6bb81cbd619c8b58411a941cc2beb81 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Tue, 23 Jun 2020 11:03:02 +0200 Subject: [PATCH] make draw take shared ref again --- src/app.rs | 25 +++++++-------- src/components/changes.rs | 2 +- src/components/commit.rs | 2 +- src/components/commit_details/details.rs | 2 +- src/components/commit_details/mod.rs | 2 +- src/components/commitlist.rs | 36 +++++++++++---------- src/components/diff.rs | 40 +++++++++++++----------- src/components/filetree.rs | 2 +- src/components/help.rs | 2 +- src/components/inspect_commit.rs | 2 +- src/components/mod.rs | 2 +- src/components/msg.rs | 2 +- src/components/reset.rs | 2 +- src/components/stashmsg.rs | 2 +- src/components/textinput.rs | 2 +- src/main.rs | 6 ++-- src/tabs/revlog.rs | 2 +- src/tabs/stashing.rs | 2 +- src/tabs/stashlist.rs | 2 +- src/tabs/status.rs | 2 +- 20 files changed, 71 insertions(+), 68 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8f46cf8e..3aac96f6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,7 +18,7 @@ use anyhow::{anyhow, Result}; use asyncgit::{sync, AsyncNotification, CWD}; use crossbeam_channel::Sender; use crossterm::event::{Event, KeyEvent}; -use std::rc::Rc; +use std::{cell::RefCell, rc::Rc}; use strings::{commands, order}; use tui::{ backend::Backend, @@ -37,7 +37,7 @@ pub struct App { commit: CommitComponent, stashmsg_popup: StashMsgComponent, inspect_commit_popup: InspectCommitComponent, - cmdbar: CommandBar, + cmdbar: RefCell, tab: usize, revlog: Revlog, status_tab: Status, @@ -71,7 +71,7 @@ impl App { theme.clone(), ), do_quit: false, - cmdbar: CommandBar::new(theme.clone()), + cmdbar: RefCell::new(CommandBar::new(theme.clone())), help: HelpComponent::new(theme.clone()), msg: MsgComponent::new(theme.clone()), tab: 0, @@ -89,13 +89,10 @@ impl App { } /// - pub fn draw( - &mut self, - f: &mut Frame, - ) -> Result<()> { + pub fn draw(&self, f: &mut Frame) -> Result<()> { let fsize = f.size(); - self.cmdbar.refresh_width(fsize.width); + self.cmdbar.borrow_mut().refresh_width(fsize.width); let chunks_main = Layout::default() .direction(Direction::Vertical) @@ -103,13 +100,13 @@ impl App { [ Constraint::Length(2), Constraint::Min(2), - Constraint::Length(self.cmdbar.height()), + Constraint::Length(self.cmdbar.borrow().height()), ] .as_ref(), ) .split(fsize); - self.cmdbar.draw(f, chunks_main[2]); + self.cmdbar.borrow().draw(f, chunks_main[2]); self.draw_tabs(f, chunks_main[0]); @@ -160,7 +157,7 @@ impl App { } keys::CMD_BAR_TOGGLE => { - self.cmdbar.toggle_more(); + self.cmdbar.borrow_mut().toggle_more(); NeedsUpdate::empty() } @@ -312,7 +309,7 @@ impl App { fn update_commands(&mut self) { self.help.set_cmds(self.commands(true)); - self.cmdbar.set_cmds(self.commands(false)); + self.cmdbar.borrow_mut().set_cmds(self.commands(false)); } fn process_queue(&mut self) -> Result { @@ -428,7 +425,7 @@ impl App { } fn draw_popups( - &mut self, + &self, f: &mut Frame, ) -> Result<()> { let size = Layout::default() @@ -436,7 +433,7 @@ impl App { .constraints( [ Constraint::Min(1), - Constraint::Length(self.cmdbar.height()), + Constraint::Length(self.cmdbar.borrow().height()), ] .as_ref(), ) diff --git a/src/components/changes.rs b/src/components/changes.rs index 262f6aba..6f7067c1 100644 --- a/src/components/changes.rs +++ b/src/components/changes.rs @@ -184,7 +184,7 @@ impl ChangesComponent { impl DrawableComponent for ChangesComponent { fn draw( - &mut self, + &self, f: &mut Frame, r: Rect, ) -> Result<()> { diff --git a/src/components/commit.rs b/src/components/commit.rs index fdce3e11..a1b26cca 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -26,7 +26,7 @@ pub struct CommitComponent { impl DrawableComponent for CommitComponent { fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()> { diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 6d9d0821..51d7d06d 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -178,7 +178,7 @@ impl DetailsComponent { impl DrawableComponent for DetailsComponent { fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()> { diff --git a/src/components/commit_details/mod.rs b/src/components/commit_details/mod.rs index 5b7c185a..60a21e63 100644 --- a/src/components/commit_details/mod.rs +++ b/src/components/commit_details/mod.rs @@ -107,7 +107,7 @@ impl CommitDetailsComponent { impl DrawableComponent for CommitDetailsComponent { fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()> { diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index 872d83d6..a47a0198 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -12,7 +12,9 @@ use crate::{ use anyhow::Result; use asyncgit::sync; use crossterm::event::Event; -use std::{borrow::Cow, cmp, convert::TryFrom, time::Instant}; +use std::{ + borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant, +}; use sync::Tags; use tui::{ backend::Backend, @@ -32,8 +34,8 @@ pub struct CommitList { items: ItemBatch, scroll_state: (Instant, f32), tags: Option, - current_size: (u16, u16), - scroll_top: usize, + current_size: Cell<(u16, u16)>, + scroll_top: Cell, theme: SharedTheme, } @@ -47,8 +49,8 @@ impl CommitList { count_total: 0, scroll_state: (Instant::now(), 0_f32), tags: None, - current_size: (0, 0), - scroll_top: 0, + current_size: Cell::new((0, 0)), + scroll_top: Cell::new(0), theme, title: String::from(title), } @@ -70,8 +72,8 @@ impl CommitList { } /// - pub const fn current_size(&self) -> (u16, u16) { - self.current_size + pub fn current_size(&self) -> (u16, u16) { + self.current_size.get() } /// @@ -121,7 +123,7 @@ impl CommitList { usize::try_from(self.scroll_state.1 as i64)?.max(1); let page_offset = - usize::from(self.current_size.1).saturating_sub(1); + usize::from(self.current_size.get().1).saturating_sub(1); let new_selection = match scroll { ScrollType::Up => { @@ -239,7 +241,7 @@ impl CommitList { for (idx, e) in self .items .iter() - .skip(self.scroll_top) + .skip(self.scroll_top.get()) .take(height) .enumerate() { @@ -253,7 +255,7 @@ impl CommitList { Self::add_entry( e, - idx + self.scroll_top == selection, + idx + self.scroll_top.get() == selection, &mut txt, tags, &self.theme, @@ -271,23 +273,23 @@ impl CommitList { impl DrawableComponent for CommitList { fn draw( - &mut self, + &self, f: &mut Frame, area: Rect, ) -> Result<()> { - self.current_size = ( + self.current_size.set(( area.width.saturating_sub(2), area.height.saturating_sub(2), - ); + )); - let height_in_lines = self.current_size.1 as usize; + let height_in_lines = self.current_size.get().1 as usize; let selection = self.relative_selection(); - self.scroll_top = calc_scroll_top( - self.scroll_top, + self.scroll_top.set(calc_scroll_top( + self.scroll_top.get(), height_in_lines, selection, - ); + )); let branch_post_fix = self.branch.as_ref().map(|b| format!("- {{{}}}", b)); diff --git a/src/components/diff.rs b/src/components/diff.rs index 70d1088f..f004563b 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -9,7 +9,7 @@ use crate::{ use asyncgit::{hash, sync, DiffLine, DiffLineType, FileDiff, CWD}; use bytesize::ByteSize; use crossterm::event::Event; -use std::{borrow::Cow, cmp, path::Path}; +use std::{borrow::Cow, cell::Cell, cmp, path::Path}; use strings::commands; use tui::{ backend::Backend, @@ -33,10 +33,10 @@ pub struct DiffComponent { diff: Option, selection: usize, selected_hunk: Option, - current_size: (u16, u16), + current_size: Cell<(u16, u16)>, focused: bool, current: Current, - scroll_top: usize, + scroll_top: Cell, queue: Option, theme: SharedTheme, } @@ -50,9 +50,9 @@ impl DiffComponent { current: Current::default(), selected_hunk: None, diff: None, - current_size: (0, 0), + current_size: Cell::new((0, 0)), selection: 0, - scroll_top: 0, + scroll_top: Cell::new(0), theme, } } @@ -71,7 +71,7 @@ impl DiffComponent { pub fn clear(&mut self) -> Result<()> { self.current = Current::default(); self.diff = None; - self.scroll_top = 0; + self.scroll_top.set(0); self.selection = 0; self.selected_hunk = None; @@ -97,7 +97,7 @@ impl DiffComponent { Self::find_selected_hunk(&diff, self.selection)?; self.diff = Some(diff); - self.scroll_top = 0; + self.scroll_top.set(0); self.selection = 0; } @@ -120,12 +120,13 @@ impl DiffComponent { ScrollType::End => max, ScrollType::PageDown => { self.selection.saturating_add( - self.current_size.1.saturating_sub(1) + self.current_size.get().1.saturating_sub(1) as usize, ) } ScrollType::PageUp => self.selection.saturating_sub( - self.current_size.1.saturating_sub(1) as usize, + self.current_size.get().1.saturating_sub(1) + as usize, ), }; @@ -207,7 +208,7 @@ impl DiffComponent { } else { let selection = self.selection; - let min = self.scroll_top; + let min = self.scroll_top.get(); let max = min + height as usize; let mut line_cursor = 0_usize; @@ -415,24 +416,27 @@ impl DiffComponent { impl DrawableComponent for DiffComponent { fn draw( - &mut self, + &self, f: &mut Frame, r: Rect, ) -> Result<()> { - self.current_size = - (r.width.saturating_sub(2), r.height.saturating_sub(2)); + self.current_size.set(( + r.width.saturating_sub(2), + r.height.saturating_sub(2), + )); - self.scroll_top = calc_scroll_top( - self.scroll_top, - self.current_size.1 as usize, + self.scroll_top.set(calc_scroll_top( + self.scroll_top.get(), + self.current_size.get().1 as usize, self.selection, - ); + )); let title = format!("{}{}", strings::TITLE_DIFF, self.current.path); f.render_widget( Paragraph::new( - self.get_text(r.width, self.current_size.1)?.iter(), + self.get_text(r.width, self.current_size.get().1)? + .iter(), ) .block( Block::default() diff --git a/src/components/filetree.rs b/src/components/filetree.rs index a011b65a..c4f5fd75 100644 --- a/src/components/filetree.rs +++ b/src/components/filetree.rs @@ -211,7 +211,7 @@ impl FileTreeComponent { impl DrawableComponent for FileTreeComponent { fn draw( - &mut self, + &self, f: &mut Frame, r: Rect, ) -> Result<()> { diff --git a/src/components/help.rs b/src/components/help.rs index 1b6a83e0..376f1f4a 100644 --- a/src/components/help.rs +++ b/src/components/help.rs @@ -29,7 +29,7 @@ pub struct HelpComponent { impl DrawableComponent for HelpComponent { fn draw( - &mut self, + &self, f: &mut Frame, _rect: Rect, ) -> Result<()> { diff --git a/src/components/inspect_commit.rs b/src/components/inspect_commit.rs index b864d1af..a78141f4 100644 --- a/src/components/inspect_commit.rs +++ b/src/components/inspect_commit.rs @@ -31,7 +31,7 @@ pub struct InspectCommitComponent { impl DrawableComponent for InspectCommitComponent { fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()> { diff --git a/src/components/mod.rs b/src/components/mod.rs index 638bc39b..78eab8b5 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -120,7 +120,7 @@ pub fn visibility_blocking( pub trait DrawableComponent { /// fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()>; diff --git a/src/components/msg.rs b/src/components/msg.rs index fe9c4a46..36935a92 100644 --- a/src/components/msg.rs +++ b/src/components/msg.rs @@ -24,7 +24,7 @@ use anyhow::Result; impl DrawableComponent for MsgComponent { fn draw( - &mut self, + &self, f: &mut Frame, _rect: Rect, ) -> Result<()> { diff --git a/src/components/reset.rs b/src/components/reset.rs index a3ffa70a..5d0e96aa 100644 --- a/src/components/reset.rs +++ b/src/components/reset.rs @@ -28,7 +28,7 @@ pub struct ResetComponent { impl DrawableComponent for ResetComponent { fn draw( - &mut self, + &self, f: &mut Frame, _rect: Rect, ) -> Result<()> { diff --git a/src/components/stashmsg.rs b/src/components/stashmsg.rs index f5ea37ad..2d15cbd1 100644 --- a/src/components/stashmsg.rs +++ b/src/components/stashmsg.rs @@ -22,7 +22,7 @@ pub struct StashMsgComponent { impl DrawableComponent for StashMsgComponent { fn draw( - &mut self, + &self, f: &mut Frame, rect: Rect, ) -> Result<()> { diff --git a/src/components/textinput.rs b/src/components/textinput.rs index 9d21d8e1..ed303272 100644 --- a/src/components/textinput.rs +++ b/src/components/textinput.rs @@ -100,7 +100,7 @@ impl TextInputComponent { impl DrawableComponent for TextInputComponent { fn draw( - &mut self, + &self, f: &mut Frame, _rect: Rect, ) -> Result<()> { diff --git a/src/main.rs b/src/main.rs index 4ff6e18f..e88c03e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,7 +98,7 @@ fn main() -> Result<()> { let spinner_ticker = tick(SPINNER_INTERVAL); app.update()?; - draw(&mut terminal, &mut app)?; + draw(&mut terminal, &app)?; let mut spinner = Spinner::default(); @@ -131,7 +131,7 @@ fn main() -> Result<()> { input.set_polling(!app.any_work_pending()); if needs_draw { - draw(&mut terminal, &mut app)?; + draw(&mut terminal, &app)?; } spinner.draw( @@ -162,7 +162,7 @@ fn shutdown_terminal() -> Result<()> { fn draw( terminal: &mut Terminal, - app: &mut App, + app: &App, ) -> io::Result<()> { terminal.draw(|mut f| { if let Err(e) = app.draw(&mut f) { diff --git a/src/tabs/revlog.rs b/src/tabs/revlog.rs index 7ad1091c..14c10e02 100644 --- a/src/tabs/revlog.rs +++ b/src/tabs/revlog.rs @@ -133,7 +133,7 @@ impl Revlog { impl DrawableComponent for Revlog { fn draw( - &mut self, + &self, f: &mut Frame, area: Rect, ) -> Result<()> { diff --git a/src/tabs/stashing.rs b/src/tabs/stashing.rs index d54e79b3..ba5df5c8 100644 --- a/src/tabs/stashing.rs +++ b/src/tabs/stashing.rs @@ -130,7 +130,7 @@ impl Stashing { impl DrawableComponent for Stashing { fn draw( - &mut self, + &self, f: &mut tui::Frame, rect: tui::layout::Rect, ) -> Result<()> { diff --git a/src/tabs/stashlist.rs b/src/tabs/stashlist.rs index 768f0f0a..f6e59192 100644 --- a/src/tabs/stashlist.rs +++ b/src/tabs/stashlist.rs @@ -89,7 +89,7 @@ impl StashList { impl DrawableComponent for StashList { fn draw( - &mut self, + &self, f: &mut tui::Frame, rect: tui::layout::Rect, ) -> Result<()> { diff --git a/src/tabs/status.rs b/src/tabs/status.rs index a98b9a83..66b30108 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -52,7 +52,7 @@ pub struct Status { impl DrawableComponent for Status { fn draw( - &mut self, + &self, f: &mut tui::Frame, rect: tui::layout::Rect, ) -> Result<()> {