feature (UI): implemented the main UI

This commit is contained in:
antifallobst 2023-06-23 05:41:26 +02:00
parent a3cb8b8e20
commit 6e31c3e7c4
3 changed files with 171 additions and 34 deletions

View File

@ -4,16 +4,44 @@ use matrix_sdk::{Client};
use accounts::Account; use accounts::Account;
use crate::accounts::AccountManager; use crate::accounts::AccountManager;
pub struct Message {
pub author: String,
pub message: String,
}
pub struct Room {
pub messages: Vec<Message>,
}
pub struct App { pub struct App {
pub account_manager: accounts::AccountManager, pub account_manager: accounts::AccountManager,
pub client: Option<Client>, pub client: Option<Client>,
current_room_id: u32,
rooms: Vec<Room>,
} }
impl App { impl App {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
account_manager: AccountManager::new(), account_manager: AccountManager::new(),
client: None client: None,
current_room_id: 0,
rooms: Vec::new(),
} }
} }
pub fn fill_test_data(&mut self) {
let mut room = Room {
messages: Vec::new()
};
room.messages.push(Message {author: "someone".to_string(), message: "test".to_string()});
self.rooms.push(room);
}
pub fn room(&self) -> Option<&Room> {
self.rooms.get(self.current_room_id as usize)
}
} }

View File

@ -4,15 +4,22 @@ mod app;
use matrix_sdk::ruma::exports::serde_json; use matrix_sdk::ruma::exports::serde_json;
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
use crate::app::Message;
use crate::ui::UI;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let mut app = app::App::new(); let mut app = app::App::new();
app.fill_test_data();
let client = app.account_manager.add("https://nerdcult.net", "test", "abcd1234").await?; let mut ui = UI::new();
app.client = Some(client); ui.draw_main(&app)?;
// let client = app.account_manager.add("https://nerdcult.net", "test", "abcd1234").await?;
// app.client = Some(client);
// let client = app.account_manager.login(0).await?; // let client = app.account_manager.login(0).await?;
// app.client = Some(client); // app.client = Some(client);

View File

@ -1,48 +1,150 @@
use crate::ui; use crate::app::{App};
use crossterm::event::KeyCode::Null; use crossterm::event::KeyCode::Null;
use crossterm::{ use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
use anyhow::{Error, Result};
use std::io::Stdout; use std::io::Stdout;
use std::{io, thread, time::Duration}; use std::{io, thread, time::Duration};
use tui::{ use tui::{backend::CrosstermBackend, layout::{Constraint, Direction, Layout, Rect}, widgets::{Block, Borders, Widget}, Terminal, Frame};
backend::CrosstermBackend, use tui::layout::Alignment;
layout::{Constraint, Direction, Layout}, use tui::style::{Color, Style};
widgets::{Block, Borders, Widget}, use tui::text::{Spans, Span};
Terminal, use tui::widgets::{Paragraph, Wrap};
};
pub struct UI { pub struct UI {
backend: CrosstermBackend<Stdout>,
terminal: Terminal<CrosstermBackend<Stdout>>, terminal: Terminal<CrosstermBackend<Stdout>>,
} }
pub fn ui_create() -> Result<(), io::Error> { fn terminal_prepare() -> Result<Stdout> {
// setup terminal
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout); return Ok(stdout);
let mut terminal = Terminal::new(backend)?; }
terminal.draw(|f| { fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let size = f.size(); let panel = Block::default()
let block = Block::default().title("Layout design in work...").borders(Borders::ALL); .title("Status")
f.render_widget(block, size); .borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_rooms_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Rooms")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_messages_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect, app: &App) {
let messages = match app.room() {
None => return,
Some(r) => {
r.messages
.iter()
.rev()
.map(|msg| {
Spans::from(vec![
Span::styled(&msg.author, Style::default().fg(Color::Cyan)),
Span::styled(": ", Style::default().fg(Color::Cyan)),
Span::styled(&msg.message, Style::default().fg(Color::White)),
])
})
.collect::<Vec<_>>()
},
};
let panel = Paragraph::new(messages)
.block(
Block::default()
.title("Messages")
.borders(Borders::ALL))
.alignment(Alignment::Left)
.wrap(Wrap {trim: false});
frame.render_widget(panel, area);
}
fn draw_main_message_compose_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Message Compose")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_room_info_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Room Info")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
impl UI {
pub fn new() -> Self {
let stdout = terminal_prepare().expect("failed to prepare terminal");
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).expect("failed to initialize terminal");
terminal.clear().expect("failed to clear screen");
Self {
terminal
}
}
pub fn draw_main(&mut self, app: &App) -> Result<()> {
let chunk = self.terminal.size()?;
let main_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Length(32), Constraint::Min(16), Constraint::Length(32)].as_ref())
.split(chunk);
let left_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(5), Constraint::Min(4)].as_ref())
.split(main_chunks[0]);
let middle_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(4), Constraint::Length(3)].as_ref())
.split(main_chunks[1]);
let right_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(4)].as_ref())
.split(main_chunks[2]);
self.terminal.draw(|frame| {
draw_main_status_block(frame, left_chunks[0]);
draw_main_rooms_block(frame, left_chunks[1]);
draw_main_messages_block(frame, middle_chunks[0], app);
draw_main_message_compose_block(frame, middle_chunks[1]);
draw_main_room_info_block(frame, right_chunks[0]);
})?; })?;
thread::sleep(Duration::from_millis(5000)); thread::sleep(Duration::from_millis(10000));
Ok(())
}
// restore terminal pub fn close(&mut self) -> Result<()>{
disable_raw_mode()?; disable_raw_mode()?;
execute!( execute!(
terminal.backend_mut(), self.terminal.backend_mut(),
LeaveAlternateScreen, LeaveAlternateScreen,
DisableMouseCapture DisableMouseCapture
)?; )?;
terminal.show_cursor()?; self.terminal.show_cursor()?;
Ok(()) Ok(())
} }
}