brain: Avoid redundant acceptance redraws
e7679e75a447ce7d0e7e98ddb3c9c933421aa605
Accepting a hunk previously repainted optimistically and again when the persistence worker completed, making the whole interface flicker. Let the event loop perform the optimistic redraw and repaint on completion only when a failed update rolls visible state back. Assisted-by: Codex:gpt-5.6
1 parent
38de92fc
crates/brain/src/session.rs
+13 -15
| 489 | 489 | let current = self |
|
| 490 | 490 | .pending |
|
| 491 | 491 | .as_ref() |
|
| 492 | 492 | .is_some_and(|pending| pending.worker.thread().id() == worker); |
|
| 493 | 493 | if current { |
|
| 494 | - | self.sync_acceptance()?; |
|
| 495 | - | Ok(Update::Redraw) |
|
| 494 | + | Ok(match self.sync_acceptance()? { |
|
| 495 | + | ViewChange::Changed => Update::Redraw, |
|
| 496 | + | ViewChange::Unchanged => Update::Continue, |
|
| 497 | + | }) |
|
| 496 | 498 | } else { |
|
| 497 | 499 | Ok(Update::Continue) |
|
| 498 | 500 | } |
|
| 499 | 501 | } |
|
| 500 | 502 | } |
|
| 501 | 503 | } |
|
| 502 | 504 | ||
| 503 | 505 | /// Waits for pending acceptance before leaving the session. |
|
| 504 | 506 | fn try_quit(&mut self) -> Result<Update, Error> { |
|
| 505 | - | self.sync_acceptance()?; |
|
| 507 | + | let _ = self.sync_acceptance()?; |
|
| 506 | 508 | Ok(if self.error.is_some() { |
|
| 507 | 509 | Update::Redraw |
|
| 508 | 510 | } else { |
|
| 509 | 511 | Update::Quit |
|
| 510 | 512 | }) |
|
| 511 | 513 | } |
|
| 512 | 514 | ||
| 513 | 515 | /// Dispatches one input event, handling review keys before the viewer. |
|
| 514 | 516 | fn handle_key(&mut self, event: Event<()>, area: Rect) -> Result<Control, Error> { |
|
| 515 | 517 | if self.pending.is_some() && matches!(&event, Event::Key(Key::Char('a' | 'A' | 'u'))) { |
|
| 516 | - | self.sync_acceptance()?; |
|
| 518 | + | let _ = self.sync_acceptance()?; |
|
| 517 | 519 | if self.error.is_some() { |
|
| 518 | 520 | return Ok(Control::Redraw); |
|
| 519 | 521 | } |
|
| 520 | 522 | } |
|
| 521 | 523 | match event { |
| 657 | 659 | } |
|
| 658 | 660 | } |
|
| 659 | 661 | ||
| 660 | 662 | /// Toggles the rejected state of one commit after pending persistence settles. |
|
| 661 | 663 | fn toggle_commit_rejected(&mut self, index: usize) -> Result<(), Error> { |
|
| 662 | - | self.sync_acceptance()?; |
|
| 664 | + | let _ = self.sync_acceptance()?; |
|
| 663 | 665 | if self.error.is_some() { |
|
| 664 | 666 | return Ok(()); |
|
| 665 | 667 | } |
|
| 666 | 668 | let commit = self.commits[index].commit.id; |
|
| 667 | 669 | self.brain.toggle_commit_rejected(commit)?; |
| 669 | 671 | Ok(()) |
|
| 670 | 672 | } |
|
| 671 | 673 | ||
| 672 | 674 | /// Toggles the dropped state of one commit after pending persistence settles. |
|
| 673 | 675 | fn toggle_commit_dropped(&mut self, index: usize) -> Result<(), Error> { |
|
| 674 | - | self.sync_acceptance()?; |
|
| 676 | + | let _ = self.sync_acceptance()?; |
|
| 675 | 677 | if self.error.is_some() { |
|
| 676 | 678 | return Ok(()); |
|
| 677 | 679 | } |
|
| 678 | 680 | let commit = self.commits[index].commit.id; |
|
| 679 | 681 | self.brain.toggle_commit_dropped(commit)?; |
| 840 | 842 | if let Err(error) = self.reconcile_reviews() { |
|
| 841 | 843 | self.brain.rollback(rollback); |
|
| 842 | 844 | self.reconcile_state(); |
|
| 843 | 845 | return Err(error); |
|
| 844 | 846 | } |
|
| 845 | - | if let Err(error) = self.draw_frame() { |
|
| 846 | - | self.brain.rollback(rollback); |
|
| 847 | - | self.reconcile_state(); |
|
| 848 | - | return Err(error.into()); |
|
| 849 | - | } |
|
| 850 | 847 | ||
| 851 | 848 | let mut patches = Vec::new(); |
|
| 852 | 849 | for slot in &self.commits { |
|
| 853 | 850 | let Some(identities) = slot |
|
| 854 | 851 | .identities |
| 909 | 906 | worker, |
|
| 910 | 907 | }); |
|
| 911 | 908 | Ok(()) |
|
| 912 | 909 | } |
|
| 913 | 910 | ||
| 914 | - | /// Applies a completed acceptance or restores its optimistic state. |
|
| 915 | - | fn sync_acceptance(&mut self) -> Result<(), Error> { |
|
| 911 | + | /// Returns whether a failed update changed visible state through rollback. |
|
| 912 | + | fn sync_acceptance(&mut self) -> Result<ViewChange, Error> { |
|
| 916 | 913 | let Some(pending) = self.pending.take() else { |
|
| 917 | - | return Ok(()); |
|
| 914 | + | return Ok(ViewChange::Unchanged); |
|
| 918 | 915 | }; |
|
| 919 | 916 | let PendingAcceptance { |
|
| 920 | 917 | rollback, |
|
| 921 | 918 | undo, |
|
| 922 | 919 | navigation, |
| 928 | 925 | ||
| 929 | 926 | match result { |
|
| 930 | 927 | Ok(brain) => { |
|
| 931 | 928 | self.brain = brain; |
|
| 932 | 929 | self.accepted = undo; |
|
| 930 | + | Ok(ViewChange::Unchanged) |
|
| 933 | 931 | } |
|
| 934 | 932 | Err(message) => { |
|
| 935 | 933 | let navigation = navigation.and_then(|navigation| { |
|
| 936 | 934 | let loaded = self.commits.get(navigation.commit)?.loaded.as_ref()?; |
|
| 937 | 935 | (loaded.viewer.state() == &navigation.after) |
| 942 | 940 | ||
| 943 | 941 | if let Some((commit, state)) = navigation { |
|
| 944 | 942 | self.load(commit)?.viewer.set_state(state); |
|
| 945 | 943 | } |
|
| 946 | 944 | self.error = Some(message.replace('\n', " · ")); |
|
| 945 | + | Ok(ViewChange::Changed) |
|
| 947 | 946 | } |
|
| 948 | 947 | } |
|
| 949 | - | Ok(()) |
|
| 950 | 948 | } |
|
| 951 | 949 | ||
| 952 | 950 | /// Opens the hunk view for the focused hunk to author a comment. |
|
| 953 | 951 | fn enter_hunk_view(&mut self, area: Rect) -> Result<(), Error> { |
|
| 954 | 952 | if !matches!( |
a.rs
+0 -0