Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve redraw performance #948

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ impl App {
self.push_tags_popup.update_git(ev)?;
self.pull_popup.update_git(ev);
self.select_branch_popup.update_git(ev)?;
self.commit.update_git(ev);
}

self.files_tab.update_async(ev);
Expand Down
23 changes: 18 additions & 5 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use asyncgit::{
sync::{
self, get_config_string, CommitId, HookResult, RepoState,
},
CWD,
AsyncGitNotification, CWD,
};
use crossterm::event::Event;
use easy_cast::Cast;
Expand Down Expand Up @@ -44,6 +44,7 @@ pub struct CommitComponent {
git_branch_name: cached::BranchName,
commit_template: Option<String>,
theme: SharedTheme,
can_amend: bool,
}

const FIRST_LINE_LIMIT: usize = 50;
Expand All @@ -70,6 +71,7 @@ impl CommitComponent {
git_branch_name: cached::BranchName::new(CWD),
commit_template: None,
theme,
can_amend: false,
}
}

Expand All @@ -78,6 +80,17 @@ impl CommitComponent {
self.git_branch_name.lookup().ok();
}

///
pub fn update_git(&mut self, ev: AsyncGitNotification) {
if ev == AsyncGitNotification::Status {
self.update_status();
}
}

fn update_status(&mut self) {
self.can_amend = self.get_can_amend();
}

fn draw_branch_name<B: Backend>(&self, f: &mut Frame<B>) {
if let Some(name) = self.git_branch_name.last() {
let w = Paragraph::new(format!("{{{}}}", name))
Expand Down Expand Up @@ -227,7 +240,7 @@ impl CommitComponent {
!self.is_empty() && self.is_changed()
}

fn can_amend(&self) -> bool {
fn get_can_amend(&self) -> bool {
matches!(self.mode, Mode::Normal)
&& sync::get_head(CWD).is_ok()
&& (self.is_empty() || !self.is_changed())
Expand All @@ -243,7 +256,7 @@ impl CommitComponent {
}

fn amend(&mut self) -> Result<()> {
if self.can_amend() {
if self.can_amend {
let id = sync::get_head(CWD)?;
self.mode = Mode::Amend(id);

Expand Down Expand Up @@ -293,7 +306,7 @@ impl Component for CommitComponent {

out.push(CommandInfo::new(
strings::commands::commit_amend(&self.key_config),
self.can_amend(),
self.can_amend,
true,
));

Expand Down Expand Up @@ -323,7 +336,7 @@ impl Component for CommitComponent {
self.commit()
);
} else if e == self.key_config.commit_amend
&& self.can_amend()
&& self.can_amend
{
self.amend()?;
} else if e == self.key_config.open_commit_editor {
Expand Down
32 changes: 19 additions & 13 deletions src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct Status {
git_action_executed: bool,
options: SharedOptions,
key_config: SharedKeyConfig,
repo_state: RepoState,
}

impl DrawableComponent for Status {
Expand Down Expand Up @@ -187,6 +188,7 @@ impl Status {
git_branch_name: cached::BranchName::new(CWD),
key_config,
options,
repo_state: Self::get_repo_state(),
}
}

Expand Down Expand Up @@ -418,6 +420,8 @@ impl Status {

self.update_diff()?;

self.repo_state = Self::get_repo_state();

if self.git_action_executed {
self.git_action_executed = false;

Expand Down Expand Up @@ -558,14 +562,16 @@ impl Status {
.map_or(true, |state| state.ahead > 0)
}

fn can_abort_merge() -> bool {
fn get_repo_state() -> RepoState {
sync::repo_state(CWD).unwrap_or(RepoState::Clean)
== RepoState::Merge
}

fn pending_rebase() -> bool {
sync::repo_state(CWD).unwrap_or(RepoState::Clean)
== RepoState::Rebase
fn can_abort_merge(&self) -> bool {
self.repo_state == RepoState::Merge
}

fn pending_rebase(&self) -> bool {
self.repo_state == RepoState::Rebase
}

pub fn abort_merge(&self) {
Expand Down Expand Up @@ -635,7 +641,7 @@ impl Status {
fn can_commit(&self) -> bool {
self.index.focused()
&& !self.index.is_empty()
&& !Self::pending_rebase()
&& !self.pending_rebase()
}
}

Expand Down Expand Up @@ -692,25 +698,25 @@ impl Component for Status {
out.push(CommandInfo::new(
strings::commands::undo_commit(&self.key_config),
true,
(!Self::pending_rebase() && !focus_on_diff)
(self.pending_rebase() && !focus_on_diff)
|| force_all,
));

out.push(CommandInfo::new(
strings::commands::abort_merge(&self.key_config),
true,
Self::can_abort_merge() || force_all,
self.can_abort_merge() || force_all,
));

out.push(CommandInfo::new(
strings::commands::continue_rebase(&self.key_config),
true,
Self::pending_rebase() || force_all,
self.pending_rebase() || force_all,
));
out.push(CommandInfo::new(
strings::commands::abort_rebase(&self.key_config),
true,
Self::pending_rebase() || force_all,
self.pending_rebase() || force_all,
));
}

Expand Down Expand Up @@ -817,23 +823,23 @@ impl Component for Status {
));
Ok(EventState::Consumed)
} else if k == self.key_config.abort_merge
&& Self::can_abort_merge()
&& self.can_abort_merge()
{
self.queue.push(InternalEvent::ConfirmAction(
Action::AbortMerge,
));

Ok(EventState::Consumed)
} else if k == self.key_config.abort_merge
&& Self::pending_rebase()
&& self.pending_rebase()
{
self.queue.push(InternalEvent::ConfirmAction(
Action::AbortRebase,
));

Ok(EventState::Consumed)
} else if k == self.key_config.rebase_branch
&& Self::pending_rebase()
&& self.pending_rebase()
{
self.continue_rebase();
self.queue.push(InternalEvent::Update(
Expand Down