Can press 'Esc' key to close windows

This commit is contained in:
Pierre HUBERT 2022-10-18 09:11:08 +02:00
parent 10c099e03b
commit c763a24ca9
7 changed files with 12 additions and 6 deletions

View File

@ -69,7 +69,7 @@ impl GameRulesConfigurationScreen {
if let Event::Key(key) = event::read()? {
match key.code {
// Quit app
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
KeyCode::Char('q') | KeyCode::Esc => return Ok(ScreenResult::Canceled),
// Navigate between fields
KeyCode::Up | KeyCode::Left => cursor_pos -= 1,

View File

@ -18,6 +18,7 @@ use crate::ui_widgets::button_widget::ButtonWidget;
pub fn confirm<B: Backend>(terminal: &mut Terminal<B>, msg: &str) -> bool {
matches!(
ConfirmDialogScreen::new(msg)
.set_can_escape(true)
.show(terminal)
.unwrap_or(ScreenResult::Canceled),
ScreenResult::Ok(true)
@ -41,6 +42,11 @@ impl<'a> ConfirmDialogScreen<'a> {
}
}
pub fn set_can_escape(mut self, v: bool) -> Self {
self.can_escape = v;
self
}
pub fn show<B: Backend>(
mut self,
terminal: &mut Terminal<B>,

View File

@ -147,7 +147,7 @@ impl GameScreen {
match key.code {
// Leave game
KeyCode::Char('q')
KeyCode::Char('q') | KeyCode::Esc
if confirm(terminal, "Do you really want to leave game?") =>
{
self.client.close_connection().await;

View File

@ -52,7 +52,7 @@ impl<'a> PopupScreen<'a> {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
KeyCode::Char('q') | KeyCode::Esc => return Ok(ScreenResult::Canceled),
KeyCode::Enter => {
return Ok(ScreenResult::Ok(()));
}

View File

@ -49,7 +49,7 @@ impl SelectBotTypeScreen {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
KeyCode::Char('q') | KeyCode::Esc => return Ok(ScreenResult::Canceled),
KeyCode::Enter => {
return Ok(ScreenResult::Ok(self.types[self.curr_selection].r#type));
}

View File

@ -92,7 +92,7 @@ impl SelectPlayModeScreen {
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
KeyCode::Char('q') | KeyCode::Esc => return Ok(ScreenResult::Canceled),
KeyCode::Enter => {
return Ok(ScreenResult::Ok(
AVAILABLE_PLAY_MODES[self.curr_selection].value,

View File

@ -66,7 +66,7 @@ impl<'a> SetBoatsLayoutScreen<'a> {
let event = event::read()?;
if let Event::Key(key) = &event {
match key.code {
KeyCode::Char('q') => {
KeyCode::Char('q') | KeyCode::Esc => {
if !self.confirm_on_cancel
|| confirm(terminal, "Do you really want to quit?")
{