introduce EventState enum (#666)

This commit is contained in:
Agung Baptiso Sorlawan 2021-04-26 16:33:45 +07:00 committed by GitHub
parent 9a93bfe8d8
commit ad8ecd6a1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 270 additions and 207 deletions

View file

@ -243,7 +243,9 @@ impl App {
let mut flags = NeedsUpdate::empty();
if event_pump(ev, self.components_mut().as_mut_slice())? {
if event_pump(ev, self.components_mut().as_mut_slice())?
.is_consumed()
{
flags.insert(NeedsUpdate::COMMANDS);
} else if let Event::Key(k) = ev {
let new_flags = if k == self.key_config.tab_toggle {

View file

@ -1,6 +1,6 @@
use super::{
utils, visibility_blocking, CommandBlocking, CommandInfo,
Component, DrawableComponent,
Component, DrawableComponent, EventState,
};
use crate::{
components::{utils::string_width_align, ScrollType},
@ -182,7 +182,7 @@ impl Component for BlameFileComponent {
fn event(
&mut self,
event: crossterm::event::Event,
) -> Result<bool> {
) -> Result<EventState> {
if self.is_visible() {
if let Event::Key(key) = event {
if key == self.key_config.exit_popup {
@ -207,23 +207,23 @@ impl Component for BlameFileComponent {
self.hide();
return self.selected_commit().map_or(
Ok(false),
Ok(EventState::NotConsumed),
|id| {
self.queue.borrow_mut().push_back(
InternalEvent::InspectCommit(
id, None,
),
);
Ok(true)
Ok(EventState::Consumed)
},
);
}
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent,
DrawableComponent, EventState,
};
use crate::{
components::ScrollType,
@ -161,19 +161,27 @@ impl Component for BranchListComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.exit_popup {
self.hide()
} else if e == self.key_config.move_down {
return self.move_selection(ScrollType::Up);
return self
.move_selection(ScrollType::Up)
.map(Into::into);
} else if e == self.key_config.move_up {
return self.move_selection(ScrollType::Down);
return self
.move_selection(ScrollType::Down)
.map(Into::into);
} else if e == self.key_config.page_down {
return self.move_selection(ScrollType::PageDown);
return self
.move_selection(ScrollType::PageDown)
.map(Into::into);
} else if e == self.key_config.page_up {
return self.move_selection(ScrollType::PageUp);
return self
.move_selection(ScrollType::PageUp)
.map(Into::into);
} else if e == self.key_config.enter {
try_or_popup!(
self,
@ -220,9 +228,9 @@ impl Component for BranchListComponent {
}
}
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
}
}

View file

@ -4,7 +4,7 @@ use super::{
CommandBlocking, DrawableComponent,
};
use crate::{
components::{CommandInfo, Component},
components::{CommandInfo, Component, EventState},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings, try_or_popup,
@ -236,9 +236,9 @@ impl Component for ChangesComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, ev: Event) -> Result<bool> {
if self.files.event(ev)? {
return Ok(true);
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.files.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if self.focused() {
@ -250,7 +250,7 @@ impl Component for ChangesComponent {
self.queue
.borrow_mut()
.push_back(InternalEvent::OpenCommit);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.enter {
try_or_popup!(
self,
@ -261,7 +261,7 @@ impl Component for ChangesComponent {
self.queue.borrow_mut().push_back(
InternalEvent::Update(NeedsUpdate::ALL),
);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.status_stage_all
&& !self.is_empty()
{
@ -274,23 +274,23 @@ impl Component for ChangesComponent {
} else {
self.stage_remove_all()?;
}
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.status_reset_item
&& self.is_working_dir
{
Ok(self.dispatch_reset_workdir())
Ok(self.dispatch_reset_workdir().into())
} else if e == self.key_config.status_ignore_file
&& self.is_working_dir
&& !self.is_empty()
{
Ok(self.add_to_ignore())
Ok(self.add_to_ignore().into())
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn focused(&self) -> bool {

View file

@ -1,7 +1,7 @@
use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
ExternalEditorComponent,
EventState, ExternalEditorComponent,
};
use crate::{
get_app_config_path,
@ -85,10 +85,10 @@ impl Component for CommitComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)? {
return Ok(true);
if self.input.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -106,11 +106,11 @@ impl Component for CommitComponent {
} else {
}
// stop key event propagation
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,7 +1,8 @@
use crate::{
components::{
dialog_paragraph, utils::time_to_string, CommandBlocking,
CommandInfo, Component, DrawableComponent, ScrollType,
CommandInfo, Component, DrawableComponent, EventState,
ScrollType,
},
keys::SharedKeyConfig,
strings::{self, order},
@ -409,28 +410,28 @@ impl Component for DetailsComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, event: Event) -> Result<bool> {
fn event(&mut self, event: Event) -> Result<EventState> {
if self.focused {
if let Event::Key(e) = event {
return Ok(if e == self.key_config.move_up {
self.move_scroll_top(ScrollType::Up)
self.move_scroll_top(ScrollType::Up).into()
} else if e == self.key_config.move_down {
self.move_scroll_top(ScrollType::Down)
self.move_scroll_top(ScrollType::Down).into()
} else if e == self.key_config.home
|| e == self.key_config.shift_up
{
self.move_scroll_top(ScrollType::Home)
self.move_scroll_top(ScrollType::Home).into()
} else if e == self.key_config.end
|| e == self.key_config.shift_down
{
self.move_scroll_top(ScrollType::End)
self.move_scroll_top(ScrollType::End).into()
} else {
false
EventState::NotConsumed
});
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn focused(&self) -> bool {

View file

@ -2,7 +2,7 @@ mod details;
use super::{
command_pump, event_pump, CommandBlocking, CommandInfo,
Component, DrawableComponent, FileTreeComponent,
Component, DrawableComponent, EventState, FileTreeComponent,
};
use crate::{
accessors, keys::SharedKeyConfig, queue::Queue, strings,
@ -158,9 +158,11 @@ impl Component for CommitDetailsComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, ev: Event) -> Result<bool> {
if event_pump(ev, self.components_mut().as_mut_slice())? {
return Ok(true);
fn event(&mut self, ev: Event) -> Result<EventState> {
if event_pump(ev, self.components_mut().as_mut_slice())?
.is_consumed()
{
return Ok(EventState::Consumed);
}
if self.focused() {
@ -170,20 +172,20 @@ impl Component for CommitDetailsComponent {
{
self.details.focus(false);
self.file_tree.focus(true);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.focus_above
&& self.file_tree.focused()
{
self.file_tree.focus(false);
self.details.focus(true);
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -2,7 +2,7 @@ use super::utils::logitems::{ItemBatch, LogEntry};
use crate::{
components::{
utils::string_width_align, CommandBlocking, CommandInfo,
Component, DrawableComponent, ScrollType,
Component, DrawableComponent, EventState, ScrollType,
},
keys::SharedKeyConfig,
strings,
@ -341,7 +341,7 @@ impl DrawableComponent for CommitList {
}
impl Component for CommitList {
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if let Event::Key(k) = ev {
let selection_changed = if k == self.key_config.move_up {
self.move_selection(ScrollType::Up)?
@ -362,10 +362,10 @@ impl Component for CommitList {
} else {
false
};
return Ok(selection_changed);
return Ok(selection_changed.into());
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn commands(

View file

@ -1,6 +1,7 @@
use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
EventState,
};
use crate::{
keys::SharedKeyConfig,
@ -52,10 +53,10 @@ impl Component for CreateBranchComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)? {
return Ok(true);
if self.input.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -63,10 +64,10 @@ impl Component for CreateBranchComponent {
self.create_branch();
}
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -4,7 +4,7 @@ use tui::{backend::Backend, layout::Rect, Frame};
use asyncgit::sync::cred::BasicAuthCredential;
use crate::components::{InputType, TextInputComponent};
use crate::components::{EventState, InputType, TextInputComponent};
use crate::{
components::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
@ -100,18 +100,17 @@ impl Component for CredComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.exit_popup {
self.hide();
return Ok(true);
return Ok(EventState::Consumed);
}
if self.input_username.event(ev)?
|| self.input_password.event(ev)?
if self.input_username.event(ev)?.is_consumed()
|| self.input_password.event(ev)?.is_consumed()
{
return Ok(true);
return Ok(EventState::Consumed);
} else if e == self.key_config.enter {
if self.input_username.is_visible() {
self.cred = BasicAuthCredential::new(
@ -135,16 +134,15 @@ impl Component for CredComponent {
);
self.input_password.hide();
self.input_password.clear();
return Ok(false);
return Ok(EventState::NotConsumed);
} else {
self.hide();
}
}
}
return Ok(true);
return Ok(EventState::Consumed);
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -2,7 +2,7 @@ use super::{
CommandBlocking, Direction, DrawableComponent, ScrollType,
};
use crate::{
components::{CommandInfo, Component},
components::{CommandInfo, Component, EventState},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings, try_or_popup,
@ -728,33 +728,33 @@ impl Component for DiffComponent {
}
#[allow(clippy::cognitive_complexity)]
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.focused {
if let Event::Key(e) = ev {
return if e == self.key_config.move_down {
self.move_selection(ScrollType::Down);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.shift_down {
self.modify_selection(Direction::Down);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.shift_up {
self.modify_selection(Direction::Up);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.end {
self.move_selection(ScrollType::End);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.home {
self.move_selection(ScrollType::Home);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.move_up {
self.move_selection(ScrollType::Up);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.page_up {
self.move_selection(ScrollType::PageUp);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.page_down {
self.move_selection(ScrollType::PageDown);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.enter
&& !self.is_immutable
{
@ -764,7 +764,7 @@ impl Component for DiffComponent {
self.stage_unstage_hunk()
);
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.status_reset_item
&& !self.is_immutable
&& !self.is_stage()
@ -776,12 +776,12 @@ impl Component for DiffComponent {
self.reset_hunk();
}
}
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.diff_stage_lines
&& !self.is_immutable
{
self.stage_lines();
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.diff_reset_lines
&& !self.is_immutable
&& !self.is_stage()
@ -792,17 +792,17 @@ impl Component for DiffComponent {
self.reset_lines();
}
}
Ok(true)
Ok(EventState::Consumed)
} else if e == self.key_config.copy {
self.copy_selection();
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn focused(&self) -> bool {

View file

@ -1,7 +1,7 @@
use crate::{
components::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent,
DrawableComponent, EventState,
},
keys::SharedKeyConfig,
strings,
@ -165,12 +165,12 @@ impl Component for ExternalEditorComponent {
visibility_blocking(self)
}
fn event(&mut self, _ev: Event) -> Result<bool> {
fn event(&mut self, _ev: Event) -> Result<EventState> {
if self.visible {
return Ok(true);
return Ok(EventState::Consumed);
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -6,7 +6,7 @@ use super::{
CommandBlocking, DrawableComponent,
};
use crate::{
components::{CommandInfo, Component},
components::{CommandInfo, Component, EventState},
keys::SharedKeyConfig,
queue::{InternalEvent, NeedsUpdate, Queue},
strings::{self, order},
@ -400,7 +400,7 @@ impl Component for FileTreeComponent {
CommandBlocking::PassingOn
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.focused {
if let Event::Key(e) = ev {
return if e == self.key_config.blame {
@ -412,33 +412,40 @@ impl Component for FileTreeComponent {
),
);
Ok(true)
Ok(EventState::Consumed)
}
_ => Ok(false),
_ => Ok(EventState::NotConsumed),
}
} else if e == self.key_config.move_down {
Ok(self.move_selection(MoveSelection::Down))
.map(Into::into)
} else if e == self.key_config.move_up {
Ok(self.move_selection(MoveSelection::Up))
Ok(self.move_selection(MoveSelection::Up).into())
} else if e == self.key_config.home
|| e == self.key_config.shift_up
{
Ok(self.move_selection(MoveSelection::Home))
Ok(self
.move_selection(MoveSelection::Home)
.into())
} else if e == self.key_config.end
|| e == self.key_config.shift_down
{
Ok(self.move_selection(MoveSelection::End))
Ok(self.move_selection(MoveSelection::End).into())
} else if e == self.key_config.move_left {
Ok(self.move_selection(MoveSelection::Left))
Ok(self
.move_selection(MoveSelection::Left)
.into())
} else if e == self.key_config.move_right {
Ok(self.move_selection(MoveSelection::Right))
Ok(self
.move_selection(MoveSelection::Right)
.into())
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn focused(&self) -> bool {

View file

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent,
DrawableComponent, EventState,
};
use crate::{keys::SharedKeyConfig, strings, ui, version::Version};
use anyhow::Result;
@ -121,7 +121,7 @@ impl Component for HelpComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.exit_popup {
@ -134,16 +134,16 @@ impl Component for HelpComponent {
}
}
Ok(true)
Ok(EventState::Consumed)
} else if let Event::Key(k) = ev {
if k == self.key_config.open_help {
self.show()?;
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
}
} else {
Ok(false)
Ok(EventState::NotConsumed)
}
}

View file

@ -1,7 +1,7 @@
use super::{
command_pump, event_pump, visibility_blocking, CommandBlocking,
CommandInfo, CommitDetailsComponent, Component, DiffComponent,
DrawableComponent,
DrawableComponent, EventState,
};
use crate::{
accessors, keys::SharedKeyConfig, queue::Queue, strings,
@ -103,10 +103,12 @@ impl Component for InspectCommitComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if event_pump(ev, self.components_mut().as_mut_slice())? {
return Ok(true);
if event_pump(ev, self.components_mut().as_mut_slice())?
.is_consumed()
{
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -126,12 +128,11 @@ impl Component for InspectCommitComponent {
self.hide();
}
// stop key event propagation
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -50,6 +50,7 @@ pub use utils::filetree::FileTreeItemKind;
use crate::ui::style::Theme;
use anyhow::Result;
use crossterm::event::Event;
use std::convert::From;
use tui::{
backend::Backend,
layout::{Alignment, Rect},
@ -83,14 +84,14 @@ macro_rules! accessors {
pub fn event_pump(
ev: Event,
components: &mut [&mut dyn Component],
) -> Result<bool> {
) -> Result<EventState> {
for c in components {
if c.event(ev)? {
return Ok(true);
if c.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
/// helper fn to simplify delegating command
@ -154,6 +155,29 @@ pub trait DrawableComponent {
) -> Result<()>;
}
///
#[derive(PartialEq)]
pub enum EventState {
Consumed,
NotConsumed,
}
impl EventState {
pub fn is_consumed(&self) -> bool {
*self == Self::Consumed
}
}
impl From<bool> for EventState {
fn from(consumed: bool) -> Self {
if consumed {
Self::Consumed
} else {
Self::NotConsumed
}
}
}
/// base component trait
pub trait Component {
///
@ -163,9 +187,8 @@ pub trait Component {
force_all: bool,
) -> CommandBlocking;
/// returns true if event propagation needs to end (event was consumed)
//TODO: lets introduce an enum `EventState` as `enum EventState { Consumed, NotConsumed }` instead of bool
fn event(&mut self, ev: Event) -> Result<bool>;
///
fn event(&mut self, ev: Event) -> Result<EventState>;
///
fn focused(&self) -> bool {

View file

@ -1,6 +1,6 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent,
DrawableComponent, EventState,
};
use crate::{keys::SharedKeyConfig, strings, ui};
use crossterm::event::Event;
@ -89,16 +89,16 @@ impl Component for MsgComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.enter {
self.hide();
}
}
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
}
}

View file

@ -2,7 +2,7 @@ use super::PushComponent;
use crate::{
components::{
cred::CredComponent, visibility_blocking, CommandBlocking,
CommandInfo, Component, DrawableComponent,
CommandInfo, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, Queue},
@ -256,7 +256,7 @@ impl Component for PullComponent {
}
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(_) = ev {
if self.input_cred.is_visible() {
@ -272,9 +272,9 @@ impl Component for PullComponent {
}
}
}
return Ok(true);
return Ok(EventState::Consumed);
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,7 +1,7 @@
use crate::{
components::{
cred::CredComponent, visibility_blocking, CommandBlocking,
CommandInfo, Component, DrawableComponent,
CommandInfo, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{InternalEvent, Queue},
@ -266,7 +266,7 @@ impl Component for PushComponent {
}
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if self.input_cred.is_visible() {
@ -287,9 +287,9 @@ impl Component for PushComponent {
self.hide();
}
}
return Ok(true);
return Ok(EventState::Consumed);
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,7 +1,7 @@
use crate::{
components::{
cred::CredComponent, visibility_blocking, CommandBlocking,
CommandInfo, Component, DrawableComponent,
CommandInfo, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{InternalEvent, Queue},
@ -223,7 +223,7 @@ impl Component for PushTagsComponent {
}
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if self.input_cred.is_visible() {
@ -243,9 +243,9 @@ impl Component for PushTagsComponent {
self.hide();
}
}
return Ok(true);
return Ok(EventState::Consumed);
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,6 +1,7 @@
use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
EventState,
};
use crate::{
keys::SharedKeyConfig,
@ -56,10 +57,10 @@ impl Component for RenameBranchComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)? {
return Ok(true);
if self.input.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -67,10 +68,10 @@ impl Component for RenameBranchComponent {
self.rename_branch();
}
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,7 +1,7 @@
use crate::{
components::{
popup_paragraph, visibility_blocking, CommandBlocking,
CommandInfo, Component, DrawableComponent,
CommandInfo, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, Queue},
@ -70,7 +70,7 @@ impl Component for ResetComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.exit_popup {
@ -79,11 +79,11 @@ impl Component for ResetComponent {
self.confirm();
}
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,6 +1,7 @@
use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
EventState,
};
use crate::{
keys::SharedKeyConfig,
@ -54,10 +55,10 @@ impl Component for StashMsgComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)? {
return Ok(true);
if self.input.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -100,10 +101,10 @@ impl Component for StashMsgComponent {
}
// stop key event propagation
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,6 +1,7 @@
use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
EventState,
};
use crate::{
keys::SharedKeyConfig,
@ -56,10 +57,10 @@ impl Component for TagCommitComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)? {
return Ok(true);
if self.input.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
@ -67,10 +68,10 @@ impl Component for TagCommitComponent {
self.tag()
}
return Ok(true);
return Ok(EventState::Consumed);
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -2,7 +2,7 @@ use crate::ui::Size;
use crate::{
components::{
popup_paragraph, visibility_blocking, CommandBlocking,
CommandInfo, Component, DrawableComponent,
CommandInfo, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
strings,
@ -330,12 +330,12 @@ impl Component for TextInputComponent {
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
if let Event::Key(e) = ev {
if e == self.key_config.exit_popup {
self.hide();
return Ok(true);
return Ok(EventState::Consumed);
}
let is_ctrl =
@ -345,39 +345,39 @@ impl Component for TextInputComponent {
KeyCode::Char(c) if !is_ctrl => {
self.msg.insert(self.cursor_position, c);
self.incr_cursor();
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::Delete => {
if self.cursor_position < self.msg.len() {
self.msg.remove(self.cursor_position);
}
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::Backspace => {
self.backspace();
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::Left => {
self.decr_cursor();
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::Right => {
self.incr_cursor();
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::Home => {
self.cursor_position = 0;
return Ok(true);
return Ok(EventState::Consumed);
}
KeyCode::End => {
self.cursor_position = self.msg.len();
return Ok(true);
return Ok(EventState::Consumed);
}
_ => (),
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -2,7 +2,7 @@ use crate::{
components::{
visibility_blocking, CommandBlocking, CommandInfo,
CommitDetailsComponent, CommitList, Component,
DrawableComponent,
DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{InternalEvent, Queue},
@ -197,41 +197,41 @@ impl DrawableComponent for Revlog {
}
impl Component for Revlog {
fn event(&mut self, ev: Event) -> Result<bool> {
fn event(&mut self, ev: Event) -> Result<EventState> {
if self.visible {
let event_used = self.list.event(ev)?;
if event_used {
if event_used.is_consumed() {
self.update()?;
return Ok(true);
return Ok(EventState::Consumed);
} else if let Event::Key(k) = ev {
if k == self.key_config.enter {
self.commit_details.toggle_visible()?;
self.update()?;
return Ok(true);
return Ok(EventState::Consumed);
} else if k == self.key_config.copy {
self.copy_commit_hash()?;
return Ok(true);
return Ok(EventState::Consumed);
} else if k == self.key_config.push {
self.queue
.borrow_mut()
.push_back(InternalEvent::PushTags);
return Ok(true);
return Ok(EventState::Consumed);
} else if k == self.key_config.log_tag_commit {
return self.selected_commit().map_or(
Ok(false),
Ok(EventState::NotConsumed),
|id| {
self.queue.borrow_mut().push_back(
InternalEvent::TagCommit(id),
);
Ok(true)
Ok(EventState::Consumed)
},
);
} else if k == self.key_config.focus_right
&& self.commit_details.is_visible()
{
return self.selected_commit().map_or(
Ok(false),
Ok(EventState::NotConsumed),
|id| {
self.queue.borrow_mut().push_back(
InternalEvent::InspectCommit(
@ -241,19 +241,19 @@ impl Component for Revlog {
)),
),
);
Ok(true)
Ok(EventState::Consumed)
},
);
} else if k == self.key_config.select_branch {
self.queue
.borrow_mut()
.push_back(InternalEvent::SelectBranch);
return Ok(true);
return Ok(EventState::Consumed);
}
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn commands(

View file

@ -3,7 +3,7 @@ use crate::{
components::{
command_pump, event_pump, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
FileTreeComponent,
EventState, FileTreeComponent,
},
keys::SharedKeyConfig,
queue::{InternalEvent, Queue},
@ -208,10 +208,15 @@ impl Component for Stashing {
visibility_blocking(self)
}
fn event(&mut self, ev: crossterm::event::Event) -> Result<bool> {
fn event(
&mut self,
ev: crossterm::event::Event,
) -> Result<EventState> {
if self.visible {
if event_pump(ev, self.components_mut().as_mut_slice())? {
return Ok(true);
if event_pump(ev, self.components_mut().as_mut_slice())?
.is_consumed()
{
return Ok(EventState::Consumed);
}
if let Event::Key(k) = ev {
@ -222,26 +227,26 @@ impl Component for Stashing {
InternalEvent::PopupStashing(self.options),
);
Ok(true)
Ok(EventState::Consumed)
} else if k == self.key_config.stashing_toggle_index {
self.options.keep_index =
!self.options.keep_index;
self.update()?;
Ok(true)
Ok(EventState::Consumed)
} else if k
== self.key_config.stashing_toggle_untracked
{
self.options.stash_untracked =
!self.options.stash_untracked;
self.update()?;
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
};
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -1,7 +1,7 @@
use crate::{
components::{
visibility_blocking, CommandBlocking, CommandInfo,
CommitList, Component, DrawableComponent,
CommitList, Component, DrawableComponent, EventState,
},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, Queue},
@ -183,10 +183,13 @@ impl Component for StashList {
visibility_blocking(self)
}
fn event(&mut self, ev: crossterm::event::Event) -> Result<bool> {
fn event(
&mut self,
ev: crossterm::event::Event,
) -> Result<EventState> {
if self.visible {
if self.list.event(ev)? {
return Ok(true);
if self.list.event(ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(k) = ev {
@ -203,7 +206,7 @@ impl Component for StashList {
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {

View file

@ -3,7 +3,8 @@ use crate::{
components::{
command_pump, event_pump, visibility_blocking,
ChangesComponent, CommandBlocking, CommandInfo, Component,
DiffComponent, DrawableComponent, FileTreeItemKind,
DiffComponent, DrawableComponent, EventState,
FileTreeItemKind,
},
keys::SharedKeyConfig,
queue::{Action, InternalEvent, Queue, ResetItem},
@ -20,6 +21,7 @@ use asyncgit::{
};
use crossbeam_channel::Sender;
use crossterm::event::Event;
use std::convert::Into;
use std::convert::TryFrom;
use tui::{
layout::{Alignment, Constraint, Direction, Layout},
@ -578,11 +580,16 @@ impl Component for Status {
visibility_blocking(self)
}
fn event(&mut self, ev: crossterm::event::Event) -> Result<bool> {
fn event(
&mut self,
ev: crossterm::event::Event,
) -> Result<EventState> {
if self.visible {
if event_pump(ev, self.components_mut().as_mut_slice())? {
if event_pump(ev, self.components_mut().as_mut_slice())?
.is_consumed()
{
self.git_action_executed = true;
return Ok(true);
return Ok(EventState::Consumed);
}
if let Event::Key(k) = ev {
@ -597,60 +604,62 @@ impl Component for Status {
)),
);
}
Ok(true)
Ok(EventState::Consumed)
} else if k == self.key_config.toggle_workarea
&& !self.is_focus_on_diff()
{
self.switch_focus(self.focus.toggled_focus())
.map(Into::into)
} else if k == self.key_config.focus_right
&& self.can_focus_diff()
{
self.switch_focus(Focus::Diff)
self.switch_focus(Focus::Diff).map(Into::into)
} else if k == self.key_config.focus_left {
self.switch_focus(match self.diff_target {
DiffTarget::Stage => Focus::Stage,
DiffTarget::WorkingDir => Focus::WorkDir,
})
.map(Into::into)
} else if k == self.key_config.move_down
&& self.focus == Focus::WorkDir
&& !self.index.is_empty()
{
self.switch_focus(Focus::Stage)
self.switch_focus(Focus::Stage).map(Into::into)
} else if k == self.key_config.move_up
&& self.focus == Focus::Stage
&& !self.index_wd.is_empty()
{
self.switch_focus(Focus::WorkDir)
self.switch_focus(Focus::WorkDir).map(Into::into)
} else if k == self.key_config.select_branch
&& !self.is_focus_on_diff()
{
self.queue
.borrow_mut()
.push_back(InternalEvent::SelectBranch);
Ok(true)
Ok(EventState::Consumed)
} else if k == self.key_config.force_push
&& !self.is_focus_on_diff()
&& self.can_push()
{
self.push(true);
Ok(true)
Ok(EventState::Consumed)
} else if k == self.key_config.push
&& !self.is_focus_on_diff()
{
self.push(false);
Ok(true)
Ok(EventState::Consumed)
} else if k == self.key_config.pull
&& !self.is_focus_on_diff()
{
self.pull();
Ok(true)
Ok(EventState::Consumed)
} else {
Ok(false)
Ok(EventState::NotConsumed)
};
}
}
Ok(false)
Ok(EventState::NotConsumed)
}
fn is_visible(&self) -> bool {