more clean update messaging

This commit is contained in:
Stephan Dilly 2020-04-13 01:22:09 +02:00
parent c190787a03
commit 6bcdee4d63
9 changed files with 95 additions and 107 deletions

View File

@ -2,11 +2,10 @@ use crate::{
components::{
ChangesComponent, CommandBlocking, CommandInfo,
CommitComponent, Component, DiffComponent, DrawableComponent,
EventUpdate, HelpComponent, MsgComponent, NeedsUpdate,
ResetComponent,
HelpComponent, MsgComponent, ResetComponent,
},
keys,
queue::{InternalEvent, Queue},
queue::{InternalEvent, NeedsUpdate, Queue},
strings,
};
use asyncgit::{
@ -163,17 +162,9 @@ impl App {
let mut flags = NeedsUpdate::empty();
if let Some(e) =
Self::event_pump(ev, self.components_mut().as_mut_slice())
if Self::event_pump(ev, self.components_mut().as_mut_slice())
{
match e {
EventUpdate::All => flags.insert(NeedsUpdate::ALL),
EventUpdate::Commands => {
flags.insert(NeedsUpdate::COMMANDS)
}
EventUpdate::Diff => flags.insert(NeedsUpdate::DIFF),
_ => (),
}
flags.insert(NeedsUpdate::COMMANDS);
} else if let Event::Key(k) = ev {
let new_flags = match k {
keys::EXIT_1 | keys::EXIT_2 => {
@ -202,12 +193,12 @@ impl App {
if flags.contains(NeedsUpdate::ALL) {
self.update();
} else {
if flags.contains(NeedsUpdate::DIFF) {
self.update_diff();
} else if flags.contains(NeedsUpdate::COMMANDS) {
self.update_commands();
}
}
if flags.contains(NeedsUpdate::DIFF) {
self.update_diff();
}
if flags.contains(NeedsUpdate::COMMANDS) {
self.update_commands();
}
}
@ -334,6 +325,7 @@ impl App {
self.msg.show_msg(msg);
flags.insert(NeedsUpdate::ALL);
}
InternalEvent::Update(u) => flags.insert(*u),
};
flags
@ -432,14 +424,14 @@ impl App {
fn event_pump(
ev: Event,
components: &mut [&mut dyn Component],
) -> Option<EventUpdate> {
) -> bool {
for c in components {
if let Some(u) = c.event(ev) {
return Some(u);
if c.event(ev) {
return true;
}
}
None
false
}
fn any_popup_visible(&self) -> bool {

View File

@ -1,8 +1,8 @@
use super::{CommandBlocking, DrawableComponent, EventUpdate};
use super::{CommandBlocking, DrawableComponent};
use crate::{
components::{CommandInfo, Component},
keys,
queue::{InternalEvent, Queue},
queue::{InternalEvent, NeedsUpdate, Queue},
strings, ui,
};
use asyncgit::{hash, sync, StatusItem, StatusItemType, CWD};
@ -86,7 +86,7 @@ impl ChangesComponent {
self.items.is_empty()
}
fn move_selection(&mut self, delta: i32) {
fn move_selection(&mut self, delta: i32) -> bool {
let items_len = self.items.len();
if items_len > 0 {
if let Some(i) = self.selection {
@ -97,11 +97,18 @@ impl ChangesComponent {
if let Ok(i) = usize::try_from(i) {
self.selection = Some(i);
self.queue.borrow_mut().push_back(
InternalEvent::Update(
NeedsUpdate::DIFF,
),
);
return true;
}
}
}
}
}
false
}
fn index_add_remove(&mut self) -> bool {
@ -210,40 +217,32 @@ impl Component for ChangesComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.focused {
if let Event::Key(e) = ev {
return match e {
keys::STATUS_STAGE_FILE => {
if self.index_add_remove() {
Some(EventUpdate::All)
} else {
Some(EventUpdate::None)
self.queue.borrow_mut().push_back(
InternalEvent::Update(
NeedsUpdate::ALL,
),
);
}
true
}
keys::STATUS_RESET_FILE => {
if self.is_working_dir
self.is_working_dir
&& self.dispatch_reset_workdir()
{
Some(EventUpdate::None)
} else {
None
}
}
keys::MOVE_DOWN => {
self.move_selection(1);
Some(EventUpdate::Diff)
}
keys::MOVE_UP => {
self.move_selection(-1);
Some(EventUpdate::Diff)
}
_ => None,
keys::MOVE_DOWN => self.move_selection(1),
keys::MOVE_UP => self.move_selection(-1),
_ => false,
};
}
}
None
false
}
fn focused(&self) -> bool {

View File

@ -1,10 +1,10 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventUpdate,
DrawableComponent,
};
use crate::{
keys,
queue::{InternalEvent, Queue},
queue::{InternalEvent, NeedsUpdate, Queue},
strings, ui,
};
use asyncgit::{sync, CWD};
@ -78,38 +78,35 @@ impl Component for CommitComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.visible {
if let Event::Key(e) = ev {
return Some(match e.code {
match e.code {
KeyCode::Esc => {
self.hide();
EventUpdate::Commands
}
KeyCode::Char(c) => {
self.msg.push(c);
EventUpdate::Commands
}
KeyCode::Enter if self.can_commit() => {
self.commit();
EventUpdate::All
}
KeyCode::Backspace if !self.msg.is_empty() => {
self.msg.pop().unwrap();
EventUpdate::Commands
}
_ => EventUpdate::None,
});
_ => (),
};
return true;
}
} else if let Event::Key(e) = ev {
if let keys::OPEN_COMMIT = e {
if !self.stage_empty {
self.show();
return Some(EventUpdate::All);
return true;
}
}
}
None
false
}
fn is_visible(&self) -> bool {
@ -162,8 +159,11 @@ impl CommitComponent {
}
self.msg.clear();
self.hide();
self.queue
.borrow_mut()
.push_back(InternalEvent::Update(NeedsUpdate::ALL));
}
fn can_commit(&self) -> bool {

View File

@ -1,4 +1,4 @@
use super::{CommandBlocking, DrawableComponent, EventUpdate};
use super::{CommandBlocking, DrawableComponent};
use crate::{
components::{CommandInfo, Component},
queue::{InternalEvent, Queue},
@ -337,28 +337,28 @@ impl Component for DiffComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.focused {
if let Event::Key(e) = ev {
return match e.code {
KeyCode::Down => {
self.scroll(true);
Some(EventUpdate::None)
true
}
KeyCode::Up => {
self.scroll(false);
Some(EventUpdate::None)
true
}
KeyCode::Enter => {
self.add_hunk();
Some(EventUpdate::None)
true
}
_ => None,
_ => false,
};
}
}
None
false
}
fn focused(&self) -> bool {

View File

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventUpdate,
DrawableComponent,
};
use crate::{keys, strings, ui, version::Version};
use asyncgit::hash;
@ -111,7 +111,7 @@ impl Component for HelpComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.visible {
if let Event::Key(e) = ev {
match e {
@ -122,12 +122,12 @@ impl Component for HelpComponent {
}
}
Some(EventUpdate::Commands)
true
} else if let Event::Key(keys::OPEN_HELP) = ev {
self.show();
Some(EventUpdate::Commands)
true
} else {
None
false
}
}

View File

@ -8,7 +8,6 @@ mod diff;
mod help;
mod msg;
mod reset;
use bitflags::bitflags;
pub use changes::ChangesComponent;
pub use command::{CommandInfo, CommandText};
pub use commit::CommitComponent;
@ -17,26 +16,6 @@ pub use help::HelpComponent;
pub use msg::MsgComponent;
pub use reset::ResetComponent;
///
pub enum EventUpdate {
None,
All,
Diff,
Commands,
}
bitflags! {
///
pub struct NeedsUpdate: u32 {
///
const ALL = 0b001;
///
const DIFF = 0b010;
///
const COMMANDS = 0b100;
}
}
///
#[derive(PartialEq)]
pub enum CommandBlocking {
@ -61,7 +40,7 @@ pub trait DrawableComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, rect: Rect);
}
///
/// base component trait
pub trait Component {
///
fn commands(
@ -69,8 +48,10 @@ pub trait Component {
out: &mut Vec<CommandInfo>,
force_all: bool,
) -> CommandBlocking;
///
fn event(&mut self, ev: Event) -> Option<EventUpdate>;
/// returns true if event propagation needs to end (event was consumed)
fn event(&mut self, ev: Event) -> bool;
///
fn focused(&self) -> bool {
false

View File

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventUpdate,
DrawableComponent,
};
use crate::{keys, strings, ui};
use crossterm::event::Event;
@ -54,7 +54,7 @@ impl Component for MsgComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.visible {
if let Event::Key(e) = ev {
if let keys::CLOSE_MSG = e {
@ -62,9 +62,9 @@ impl Component for MsgComponent {
}
}
Some(EventUpdate::Commands)
true
} else {
None
false
}
}

View File

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventUpdate,
DrawableComponent,
};
use crate::{
queue::{InternalEvent, Queue},
@ -18,6 +18,7 @@ use tui::{
Frame,
};
///
pub struct ResetComponent {
path: String,
visible: bool,
@ -67,24 +68,24 @@ impl Component for ResetComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
fn event(&mut self, ev: Event) -> bool {
if self.visible {
if let Event::Key(e) = ev {
return Some(match e.code {
return match e.code {
KeyCode::Esc => {
self.hide();
EventUpdate::Commands
true
}
KeyCode::Enter => {
self.confirm();
EventUpdate::None
true
}
_ => EventUpdate::None,
});
_ => false,
};
}
}
None
false
}
fn is_visible(&self) -> bool {

View File

@ -1,5 +1,18 @@
use bitflags::bitflags;
use std::{cell::RefCell, collections::VecDeque, rc::Rc};
bitflags! {
/// flags defining what part of the app need to update
pub struct NeedsUpdate: u32 {
/// app::update
const ALL = 0b001;
/// diff may have changed (app::update_diff)
const DIFF = 0b010;
/// commands might need updating (app::update_commands)
const COMMANDS = 0b100;
}
}
///
pub enum InternalEvent {
///
@ -10,6 +23,8 @@ pub enum InternalEvent {
AddHunk(u64),
///
ShowMsg(String),
///
Update(NeedsUpdate),
}
///