make screen states a stack
This commit is contained in:
parent
dd52430508
commit
a0deb3d53e
2 changed files with 35 additions and 11 deletions
|
@ -1,4 +1,8 @@
|
|||
use std::{collections::BTreeMap, io, net::SocketAddr};
|
||||
use std::{
|
||||
collections::{BTreeMap, VecDeque},
|
||||
io,
|
||||
net::SocketAddr,
|
||||
};
|
||||
|
||||
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind};
|
||||
use futures::{FutureExt, StreamExt};
|
||||
|
@ -19,7 +23,7 @@ pub type Peers = BTreeMap<SocketAddr, (String, String)>;
|
|||
|
||||
pub struct App {
|
||||
pub state: JoecalState,
|
||||
pub screen: CurrentScreen,
|
||||
pub screen: VecDeque<CurrentScreen>,
|
||||
pub events: EventStream,
|
||||
// addr -> (alias, fingerprint)
|
||||
pub peers: Peers,
|
||||
|
@ -37,7 +41,7 @@ impl App {
|
|||
pub fn new(state: JoecalState) -> Self {
|
||||
App {
|
||||
state,
|
||||
screen: CurrentScreen::Main,
|
||||
screen: VecDeque::from([CurrentScreen::Main]),
|
||||
peers: Default::default(),
|
||||
events: Default::default(),
|
||||
}
|
||||
|
@ -48,7 +52,9 @@ impl App {
|
|||
terminal.draw(|frame| self.draw(frame))?;
|
||||
self.handle_events().await?;
|
||||
|
||||
if self.screen == CurrentScreen::Stopping {
|
||||
if let Some(&top) = self.screen.back()
|
||||
&& top == CurrentScreen::Stopping
|
||||
{
|
||||
self.state.stop().await;
|
||||
break;
|
||||
}
|
||||
|
@ -91,21 +97,30 @@ impl App {
|
|||
KeyCode::Char('q') => self.exit(),
|
||||
KeyCode::Char('s') => self.send(),
|
||||
KeyCode::Char('r') => self.recv(),
|
||||
KeyCode::Char('d') => {}
|
||||
KeyCode::Esc => self.pop(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn exit(&mut self) {
|
||||
self.screen = CurrentScreen::Stopping;
|
||||
self.screen.clear();
|
||||
self.screen.push_back(CurrentScreen::Stopping);
|
||||
}
|
||||
|
||||
fn send(&mut self) {
|
||||
self.screen = CurrentScreen::Sending;
|
||||
self.screen.clear();
|
||||
self.screen.push_back(CurrentScreen::Sending);
|
||||
}
|
||||
|
||||
fn recv(&mut self) {
|
||||
self.screen = CurrentScreen::Receiving;
|
||||
self.screen.clear();
|
||||
self.screen.push_back(CurrentScreen::Receiving);
|
||||
}
|
||||
|
||||
fn pop(&mut self) {
|
||||
if self.screen.pop_back().is_none() {
|
||||
self.screen.push_back(CurrentScreen::Main);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,7 +142,10 @@ impl Widget for &App {
|
|||
.title_bottom(instructions.centered())
|
||||
.border_set(border::THICK);
|
||||
|
||||
let current_screen = format!("{:?}", self.screen);
|
||||
let current_screen = format!(
|
||||
"{:?}",
|
||||
self.screen.back().cloned().unwrap_or(CurrentScreen::Main)
|
||||
);
|
||||
let text = Text::from(Line::from(current_screen.yellow()));
|
||||
|
||||
Paragraph::new(text)
|
||||
|
|
|
@ -53,6 +53,10 @@ impl App {
|
|||
frame.render_widget(self, frame.area());
|
||||
network_info(frame, footer_left);
|
||||
peers(&self.peers, frame, footer_right);
|
||||
|
||||
// match self.screen {
|
||||
// _ => {}
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,7 +69,8 @@ fn peers(peers: &Peers, frame: &mut Frame, area: Rect) {
|
|||
let item = ListItem::new(s);
|
||||
items.push(item);
|
||||
}
|
||||
let block = Block::default().title("Peers").borders(Borders::all());
|
||||
let title = Line::from(" Peers ".bold()).centered();
|
||||
let block = Block::default().title(title).borders(Borders::all());
|
||||
let list = List::new(items).block(block);
|
||||
frame.render_widget(list, area);
|
||||
}
|
||||
|
@ -89,7 +94,8 @@ fn network_info(frame: &mut Frame, area: Rect) {
|
|||
ListItem::new(multicast),
|
||||
ListItem::new(http),
|
||||
];
|
||||
let block = Block::default().title("Listeners").borders(Borders::all());
|
||||
let title = Line::from(" Listeners ".bold()).centered();
|
||||
let block = Block::default().title(title).borders(Borders::all());
|
||||
let list = List::new(items).block(block);
|
||||
frame.render_widget(list, area);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue