panorama/src/ui/mod.rs

135 lines
3.8 KiB
Rust
Raw Normal View History

2021-03-01 08:20:21 +00:00
//! UI library
2021-02-14 12:27:13 +00:00
mod mail_tab;
2021-03-01 08:20:21 +00:00
use std::io::Stdout;
2021-03-01 09:23:05 +00:00
use std::mem;
2021-02-12 08:54:19 +00:00
use std::time::Duration;
2021-02-12 08:12:43 +00:00
use anyhow::Result;
2021-03-01 09:23:05 +00:00
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyEvent},
style, terminal,
};
use tokio::{sync::mpsc, time};
2021-03-01 08:20:21 +00:00
use tui::{
backend::CrosstermBackend,
2021-03-01 08:20:21 +00:00
layout::{Constraint, Direction, Layout},
2021-03-01 09:35:14 +00:00
style::{Color, Modifier, Style},
2021-03-01 09:23:05 +00:00
text::Spans,
widgets::*,
Frame, Terminal,
2021-02-12 08:12:43 +00:00
};
use self::mail_tab::{MailTab, MailTabState};
// pub(crate) type FrameType<'a> = Frame<'a, CrosstermBackend<Stdout>>;
2021-03-01 09:23:05 +00:00
const FRAME_DURATION: Duration = Duration::from_millis(17);
2021-03-01 08:20:21 +00:00
/// Main entrypoint for the UI
2021-03-01 09:23:05 +00:00
pub async fn run_ui(mut stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
execute!(stdout, cursor::Hide, terminal::EnterAlternateScreen)?;
terminal::enable_raw_mode()?;
2021-02-12 13:21:00 +00:00
2021-03-01 09:23:05 +00:00
let backend = CrosstermBackend::new(&mut stdout);
let mut term = Terminal::new(backend)?;
let mut mail_state = MailTabState::new();
2021-02-27 06:27:53 +00:00
2021-02-12 08:54:19 +00:00
loop {
term.draw(|f| {
2021-03-01 08:20:21 +00:00
let chunks = Layout::default()
.direction(Direction::Vertical)
2021-03-01 09:23:05 +00:00
.margin(0)
2021-03-01 09:35:14 +00:00
.constraints([
Constraint::Length(1),
Constraint::Max(5000),
// Constraint::Percentage(10),
// Constraint::Percentage(80),
// Constraint::Percentage(10),
])
2021-03-01 08:20:21 +00:00
.split(f.size());
// let chunks2 = Layout::default()
// .direction(Direction::Horizontal)
// .margin(0)
// .constraints([
// Constraint::Length(20),
// Constraint::Max(5000),
// //
// ])
// .split(chunks[1]);
2021-03-01 09:35:14 +00:00
// this is the title bar
let titles = vec!["hellosu"].into_iter().map(Spans::from).collect();
let tabs = Tabs::new(titles);
2021-03-01 09:35:14 +00:00
f.render_widget(tabs, chunks[0]);
let mail_tab = MailTab;
f.render_stateful_widget(mail_tab, chunks[1], &mut mail_state);
2021-03-01 09:35:14 +00:00
// TODO: check active tab
// let items = [
// ListItem::new("Osu"),
// ListItem::new("Game").style(Style::default().add_modifier(Modifier::BOLD)),
// ];
// let dirlist = List::new(items)
// .block(Block::default().title("List").borders(Borders::ALL))
// .style(Style::default().fg(Color::White))
// .highlight_style(Style::default().add_modifier(Modifier::ITALIC))
// .highlight_symbol(">>");
// f.render_widget(dirlist, chunks2[0]);
// let block = Block::default().title("Block").borders(Borders::ALL);
// f.render_widget(block, chunks2[1]);
2021-03-01 09:35:14 +00:00
// let block = Block::default().title("Block 2").borders(Borders::ALL);
// f.render_widget(block, chunks[1]);
2021-03-01 08:20:21 +00:00
})?;
2021-03-01 09:23:05 +00:00
let event = if event::poll(FRAME_DURATION)? {
let event = event::read()?;
// table.update(&event);
if let Event::Key(KeyEvent {
code: KeyCode::Char('q'),
..
}) = event
{
break;
}
Some(event)
} else {
None
};
// approx 60fps
time::sleep(FRAME_DURATION).await;
// if let Event::Input(input) = events.next()? {
// match input {
// Key::Char('q') => {
// break;
// }
// _ => {}
// }
// }
2021-02-12 08:12:43 +00:00
}
mem::drop(term);
2021-03-01 09:23:05 +00:00
execute!(
stdout,
style::ResetColor,
cursor::Show,
terminal::LeaveAlternateScreen
)?;
terminal::disable_raw_mode()?;
exit_tx.send(()).await?;
debug!("sent exit");
2021-02-12 08:54:19 +00:00
Ok(())
2021-02-12 08:12:43 +00:00
}