From a9f29e24fe02bfa72a92f3b78874a6865a242230 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 15 Oct 2022 11:54:48 +0200 Subject: [PATCH] Show popup message while connecting to server --- rust/cli_player/src/lib.rs | 2 ++ rust/cli_player/src/main.rs | 2 ++ .../cli_player/src/ui_screens/popup_screen.rs | 30 +++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/rust/cli_player/src/lib.rs b/rust/cli_player/src/lib.rs index b9fa78c..d290538 100644 --- a/rust/cli_player/src/lib.rs +++ b/rust/cli_player/src/lib.rs @@ -1,3 +1,5 @@ +extern crate core; + pub mod cli_args; pub mod client; pub mod constants; diff --git a/rust/cli_player/src/main.rs b/rust/cli_player/src/main.rs index 4a7651a..808280c 100644 --- a/rust/cli_player/src/main.rs +++ b/rust/cli_player/src/main.rs @@ -17,6 +17,7 @@ use cli_player::client::Client; use cli_player::server::start_server_if_missing; use cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen; use cli_player::ui_screens::game_screen::GameScreen; +use cli_player::ui_screens::popup_screen::PopupScreen; use cli_player::ui_screens::select_play_mode::{SelectPlayModeResult, SelectPlayModeScreen}; use cli_player::ui_screens::*; use sea_battle_backend::data::GameRules; @@ -90,6 +91,7 @@ async fn run_app(terminal: &mut Terminal) -> Result<(), Box { title: &'a str, msg: &'a str, + can_close: bool, } impl<'a> PopupScreen<'a> { @@ -24,9 +25,15 @@ impl<'a> PopupScreen<'a> { Self { title: "Message", msg, + can_close: true, } } + pub fn set_can_close(mut self, can_close: bool) -> Self { + self.can_close = can_close; + self + } + pub fn show(mut self, terminal: &mut Terminal) -> io::Result> { let mut last_tick = Instant::now(); loop { @@ -53,12 +60,27 @@ impl<'a> PopupScreen<'a> { } } + /// Show message once message, without polling messages + pub fn show_once(mut self, terminal: &mut Terminal) -> io::Result<()> { + self.can_close = false; + terminal.draw(|f| self.ui(f))?; + Ok(()) + } + fn ui(&mut self, f: &mut Frame) { // Preprocess message let lines = textwrap::wrap(self.msg, f.size().width as usize - 20); let line_max_len = lines.iter().map(|l| l.len()).max().unwrap(); - let area = centered_rect_size(line_max_len as u16 + 4, 5 + lines.len() as u16, &f.size()); + let area = centered_rect_size( + line_max_len as u16 + 4, + match self.can_close { + // reserve space for button + true => 5, + false => 2, + } + lines.len() as u16, + &f.size(), + ); let block = Block::default().borders(Borders::ALL).title(self.title); f.render_widget(block, area); @@ -85,7 +107,9 @@ impl<'a> PopupScreen<'a> { let paragraph = Paragraph::new(text); f.render_widget(paragraph, chunks[0]); - let ok_button = ButtonWidget::new("OK", true); - f.render_widget(ok_button, chunks[1]); + if self.can_close { + let ok_button = ButtonWidget::new("OK", true); + f.render_widget(ok_button, chunks[1]); + } } }