Show popup message while connecting to server

This commit is contained in:
Pierre HUBERT 2022-10-15 11:54:48 +02:00
parent 19993c560a
commit a9f29e24fe
3 changed files with 31 additions and 3 deletions

View File

@ -1,3 +1,5 @@
extern crate core;
pub mod cli_args;
pub mod client;
pub mod constants;

View File

@ -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<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn E
};
// Then connect to server
PopupScreen::new("Connecting...").show_once(terminal)?;
let client = Client::start_bot_play(&rules).await?;
// Wait for the server to become ready

View File

@ -17,6 +17,7 @@ use crate::ui_widgets::button_widget::ButtonWidget;
pub struct PopupScreen<'a> {
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<B: Backend>(mut self, terminal: &mut Terminal<B>) -> io::Result<ScreenResult<()>> {
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<B: Backend>(mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
self.can_close = false;
terminal.draw(|f| self.ui(f))?;
Ok(())
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
// 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]);
if self.can_close {
let ok_button = ButtonWidget::new("OK", true);
f.render_widget(ok_button, chunks[1]);
}
}
}