[Add] Terms UI, [WIP] Main UI
This commit is contained in:
parent
a0fddf723f
commit
bac1c608fe
13 changed files with 450 additions and 112 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ACTIVE_TASKS;
|
||||
use crate::gui::ui::UI;
|
||||
use crate::gui::ui::{UI, UNIQUE};
|
||||
use crate::{RELOAD, SHUTDOWN};
|
||||
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, poll, read};
|
||||
use std::sync::Arc;
|
||||
|
|
@ -31,8 +31,9 @@ pub fn setup_input_handler(ui: Arc<UI>) {
|
|||
Ok(event) => {
|
||||
if let Event::Key(key_event) = event {
|
||||
if key_event.kind == KeyEventKind::Press {
|
||||
let ui_clone = ui.clone();
|
||||
handle_input(key_event, ui_clone).await;
|
||||
let uic = ui.clone();
|
||||
handle_input(key_event, uic).await;
|
||||
*UNIQUE.write().await = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
use std::fmt::Debug;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use crate::gui::screens::screens::Screen;
|
||||
|
||||
#[allow(unused)]
|
||||
pub enum InteractionResult {
|
||||
CloseScreen,
|
||||
OpenScreen {
|
||||
|
|
@ -17,7 +18,7 @@ pub enum InteractionResult {
|
|||
}
|
||||
|
||||
impl Debug for InteractionResult {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
InteractionResult::OpenScreen { screen: _ } => write!(f, "OpenScreen"),
|
||||
InteractionResult::OpenFutureScreen { screen: _ } => write!(f, "OpenFutureScreen"),
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ pub mod elements {
|
|||
pub mod log_card;
|
||||
}
|
||||
pub mod screens {
|
||||
pub mod main_screen;
|
||||
pub mod screens;
|
||||
pub mod terms_checker;
|
||||
pub mod terms_updater;
|
||||
|
|
|
|||
41
src/gui/screens/main_screen.rs
Normal file
41
src/gui/screens/main_screen.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use std::{any::Any, sync::Arc};
|
||||
|
||||
use crossterm::event::KeyEvent;
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::Rect,
|
||||
widgets::{Block, Borders},
|
||||
};
|
||||
|
||||
use crate::gui::{interaction_result::InteractionResult, screens::screens::Screen, ui::UI};
|
||||
|
||||
pub struct MainScreen {
|
||||
ui: Arc<UI>,
|
||||
}
|
||||
|
||||
impl MainScreen {
|
||||
pub async fn new(ui: Arc<UI>) -> Self {
|
||||
MainScreen { ui }
|
||||
}
|
||||
}
|
||||
impl Screen for MainScreen {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn get_ui(&self) -> &Arc<UI> {
|
||||
&self.ui
|
||||
}
|
||||
|
||||
fn render(&self, f: &mut Frame, rect: Rect) {
|
||||
f.render_widget(Block::default().title("Main").borders(Borders::ALL), rect);
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, event: KeyEvent) -> InteractionResult {
|
||||
InteractionResult::Unhandled
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,47 @@
|
|||
use crate::gui::{interaction_result::InteractionResult, screens::screens::Screen};
|
||||
use crate::{
|
||||
SHUTDOWN,
|
||||
gui::{
|
||||
input_handler::setup_input_handler, interaction_result::InteractionResult,
|
||||
screens::screens::Screen,
|
||||
},
|
||||
};
|
||||
use crossterm::event::KeyEvent;
|
||||
use once_cell::sync::Lazy;
|
||||
use ratatui::{Terminal, backend::CrosstermBackend, init};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{
|
||||
io::Stdout,
|
||||
sync::{Arc, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
/// UI state and rendering
|
||||
pub static UNIQUE: Lazy<RwLock<bool>> = Lazy::new(|| RwLock::new(true));
|
||||
pub struct UI {
|
||||
pub terminal: Arc<Mutex<Terminal<CrosstermBackend<std::io::Stdout>>>>,
|
||||
pub terminal: Arc<Mutex<Terminal<CrosstermBackend<Stdout>>>>,
|
||||
screen: Arc<RwLock<Option<Box<dyn Screen>>>>,
|
||||
}
|
||||
|
||||
pub fn start_tui() -> Arc<UI> {
|
||||
let ui = Arc::new(UI::new());
|
||||
let uic = ui.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
if *SHUTDOWN.read().await {
|
||||
break;
|
||||
}
|
||||
|
||||
if *UNIQUE.read().await {
|
||||
uic.render().await;
|
||||
} else {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
ratatui::restore();
|
||||
});
|
||||
setup_input_handler(ui.clone());
|
||||
ui
|
||||
}
|
||||
impl UI {
|
||||
pub fn new() -> Self {
|
||||
let terminal = init();
|
||||
|
|
@ -42,9 +74,7 @@ impl UI {
|
|||
ui.set_screen(screen).await;
|
||||
}
|
||||
InteractionResult::CloseScreen => {
|
||||
/* TODO
|
||||
self.set_screen();
|
||||
*/
|
||||
*self.screen.write().await = None;
|
||||
}
|
||||
InteractionResult::Handled => {}
|
||||
InteractionResult::Unhandled => {}
|
||||
|
|
|
|||
Loading…
Reference in a new issue