generalise command gathering

This commit is contained in:
Stephan Dilly 2020-05-22 15:28:49 +02:00
parent 572be62b5f
commit 21cbc3b136
3 changed files with 34 additions and 20 deletions

View file

@ -48,6 +48,7 @@ macro_rules! accessors {
}; };
} }
/// returns `true` if event was consumed
pub fn event_pump( pub fn event_pump(
ev: Event, ev: Event,
components: &mut [&mut dyn Component], components: &mut [&mut dyn Component],
@ -61,6 +62,23 @@ pub fn event_pump(
false false
} }
/// helper fn to simplify delegating command
/// gathering down into child components
/// see `event_pump`,`accessors`
pub fn command_pump(
out: &mut Vec<CommandInfo>,
force_all: bool,
components: &[&dyn Component],
) {
for c in components {
if c.commands(out, force_all) != CommandBlocking::PassingOn
&& !force_all
{
break;
}
}
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum ScrollType { pub enum ScrollType {
Up, Up,

View file

@ -1,7 +1,8 @@
use crate::{ use crate::{
accessors,
components::{ components::{
CommandBlocking, CommandInfo, Component, DrawableComponent, command_pump, event_pump, CommandBlocking, CommandInfo,
FileTreeComponent, Component, DrawableComponent, FileTreeComponent,
}, },
keys, keys,
queue::{InternalEvent, NeedsUpdate, Queue}, queue::{InternalEvent, NeedsUpdate, Queue},
@ -36,6 +37,8 @@ pub struct Stashing {
} }
impl Stashing { impl Stashing {
accessors!(self, [index]);
/// ///
pub fn new( pub fn new(
sender: &Sender<AsyncNotification>, sender: &Sender<AsyncNotification>,
@ -162,7 +165,7 @@ impl Component for Stashing {
out: &mut Vec<CommandInfo>, out: &mut Vec<CommandInfo>,
force_all: bool, force_all: bool,
) -> CommandBlocking { ) -> CommandBlocking {
self.index.commands(out, force_all); command_pump(out, force_all, self.components().as_slice());
out.push(CommandInfo::new( out.push(CommandInfo::new(
commands::STASHING_SAVE, commands::STASHING_SAVE,
@ -189,9 +192,7 @@ impl Component for Stashing {
fn event(&mut self, ev: crossterm::event::Event) -> bool { fn event(&mut self, ev: crossterm::event::Event) -> bool {
if self.visible { if self.visible {
let conusmed = self.index.event(ev); if event_pump(ev, self.components_mut().as_mut_slice()) {
if conusmed {
return true; return true;
} }

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
accessors, accessors,
components::{ components::{
event_pump, ChangesComponent, CommandBlocking, CommandInfo, self, event_pump, ChangesComponent, CommandBlocking,
Component, DiffComponent, DrawableComponent, CommandInfo, Component, DiffComponent, DrawableComponent,
FileTreeItemKind, FileTreeItemKind,
}, },
keys, keys,
@ -14,6 +14,7 @@ use asyncgit::{
sync::status::StatusType, AsyncDiff, AsyncNotification, sync::status::StatusType, AsyncDiff, AsyncNotification,
AsyncStatus, DiffParams, StatusParams, AsyncStatus, DiffParams, StatusParams,
}; };
use components::command_pump;
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
use crossterm::event::Event; use crossterm::event::Event;
use strings::commands; use strings::commands;
@ -262,14 +263,11 @@ impl Component for Status {
force_all: bool, force_all: bool,
) -> CommandBlocking { ) -> CommandBlocking {
if self.visible { if self.visible {
for c in self.components() { command_pump(
if c.commands(out, force_all) out,
!= CommandBlocking::PassingOn force_all,
&& !force_all self.components().as_slice(),
{ );
break;
}
}
{ {
let focus_on_diff = self.focus == Focus::Diff; let focus_on_diff = self.focus == Focus::Diff;
@ -325,10 +323,7 @@ impl Component for Status {
fn event(&mut self, ev: crossterm::event::Event) -> bool { fn event(&mut self, ev: crossterm::event::Event) -> bool {
if self.visible { if self.visible {
let conusmed = if event_pump(ev, self.components_mut().as_mut_slice()) {
event_pump(ev, self.components_mut().as_mut_slice());
if conusmed {
return true; return true;
} }