Turn popup functions into macros (#383)

* Make popup functions into macros
* update to master
* simplify

Co-authored-by: Stephan Dilly <dilly.stephan@gmail.com>
This commit is contained in:
Richard Menzies 2021-05-27 21:02:09 +01:00 committed by GitHub
parent ce5a1a754b
commit 594a1a936d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 57 deletions

View File

@ -14,6 +14,7 @@ use crate::{
input::{Input, InputEvent, InputState}, input::{Input, InputEvent, InputState},
keys::{KeyConfig, SharedKeyConfig}, keys::{KeyConfig, SharedKeyConfig},
queue::{Action, InternalEvent, NeedsUpdate, Queue}, queue::{Action, InternalEvent, NeedsUpdate, Queue},
setup_popups,
strings::{self, order}, strings::{self, order},
tabs::{Revlog, StashList, Stashing, Status}, tabs::{Revlog, StashList, Stashing, Status},
ui::style::{SharedTheme, Theme}, ui::style::{SharedTheme, Theme},
@ -412,6 +413,29 @@ impl App {
] ]
); );
setup_popups!(
self,
[
commit,
help,
reset,
msg,
stashmsg_popup,
inspect_commit_popup,
blame_file_popup,
external_editor_popup,
tag_commit_popup,
create_branch_popup,
push_popup,
push_tags_popup,
pull_popup,
select_branch_popup,
tags_popup,
rename_branch_popup,
revision_files_popup
]
);
fn check_quit_key(&mut self, ev: Event) -> bool { fn check_quit_key(&mut self, ev: Event) -> bool {
if let Event::Key(e) = ev { if let Event::Key(e) = ev {
if e == self.key_config.exit { if e == self.key_config.exit {
@ -714,63 +738,6 @@ impl App {
res res
} }
//TODO: make this automatic, i keep forgetting to add popups here
fn any_popup_visible(&self) -> bool {
self.commit.is_visible()
|| self.help.is_visible()
|| self.reset.is_visible()
|| self.msg.is_visible()
|| self.stashmsg_popup.is_visible()
|| self.inspect_commit_popup.is_visible()
|| self.blame_file_popup.is_visible()
|| self.external_editor_popup.is_visible()
|| self.tag_commit_popup.is_visible()
|| self.create_branch_popup.is_visible()
|| self.push_popup.is_visible()
|| self.push_tags_popup.is_visible()
|| self.pull_popup.is_visible()
|| self.select_branch_popup.is_visible()
|| self.tags_popup.is_visible()
|| self.rename_branch_popup.is_visible()
|| self.revision_files_popup.is_visible()
}
fn draw_popups<B: Backend>(
&self,
f: &mut Frame<B>,
) -> Result<()> {
let size = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Min(1),
Constraint::Length(self.cmdbar.borrow().height()),
]
.as_ref(),
)
.split(f.size())[0];
self.commit.draw(f, size)?;
self.stashmsg_popup.draw(f, size)?;
self.help.draw(f, size)?;
self.inspect_commit_popup.draw(f, size)?;
self.blame_file_popup.draw(f, size)?;
self.external_editor_popup.draw(f, size)?;
self.tag_commit_popup.draw(f, size)?;
self.select_branch_popup.draw(f, size)?;
self.tags_popup.draw(f, size)?;
self.create_branch_popup.draw(f, size)?;
self.rename_branch_popup.draw(f, size)?;
self.revision_files_popup.draw(f, size)?;
self.push_popup.draw(f, size)?;
self.push_tags_popup.draw(f, size)?;
self.pull_popup.draw(f, size)?;
self.reset.draw(f, size)?;
self.msg.draw(f, size)?;
Ok(())
}
//TODO: make this dynamic //TODO: make this dynamic
fn draw_tabs<B: Backend>(&self, f: &mut Frame<B>, r: Rect) { fn draw_tabs<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
let r = r.inner(&Margin { let r = r.inner(&Margin {

View File

@ -86,6 +86,49 @@ macro_rules! accessors {
}; };
} }
/// creates a function to determine if any popup is visible
#[macro_export]
macro_rules! any_popup_visible {
($self:ident, [$($element:ident),+]) => {
fn any_popup_visible(& $self) -> bool{
($($self.$element.is_visible()) || +)
}
};
}
/// creates the draw popup function
#[macro_export]
macro_rules! draw_popups {
($self:ident, [$($element:ident),+]) => {
fn draw_popups<B: Backend>(& $self, mut f: &mut Frame<B>) -> Result<()>{
let size = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Min(1),
Constraint::Length($self.cmdbar.borrow().height()),
]
.as_ref(),
)
.split(f.size())[0];
($($self.$element.draw(&mut f, size)?) , +);
return Ok(());
}
};
}
/// simply calls
/// any_popup_visible!() and draw_popups!() macros
#[macro_export]
macro_rules! setup_popups {
($self:ident, [$($element:ident),+]) => {
crate::any_popup_visible!($self, [$($element),+]);
crate::draw_popups!($self, [ $($element),+ ]);
};
}
/// returns `true` if event was consumed /// returns `true` if event was consumed
pub fn event_pump( pub fn event_pump(
ev: Event, ev: Event,