Start restructuring chat as widget
This commit is contained in:
parent
2ee64c11be
commit
26e988114c
4 changed files with 231 additions and 149 deletions
144
src/ui/chat.rs
144
src/ui/chat.rs
|
|
@ -1,15 +1,17 @@
|
|||
mod tree;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crossterm::event::KeyEvent;
|
||||
use parking_lot::FairMutex;
|
||||
use toss::frame::{Frame, Pos, Size};
|
||||
use toss::terminal::Terminal;
|
||||
use async_trait::async_trait;
|
||||
use toss::frame::{Frame, Size};
|
||||
|
||||
use crate::store::{Msg, MsgStore};
|
||||
|
||||
use self::tree::TreeView;
|
||||
use self::tree::{TreeView, TreeViewState};
|
||||
|
||||
use super::widgets::Widget;
|
||||
|
||||
///////////
|
||||
// State //
|
||||
///////////
|
||||
|
||||
pub enum Mode {
|
||||
Tree,
|
||||
|
|
@ -34,74 +36,108 @@ impl<I> Cursor<I> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Chat<M: Msg, S: MsgStore<M>> {
|
||||
pub struct ChatState<M: Msg, S: MsgStore<M>> {
|
||||
store: S,
|
||||
cursor: Option<Cursor<M::Id>>,
|
||||
mode: Mode,
|
||||
tree: TreeView<M>,
|
||||
tree: TreeViewState<M, S>,
|
||||
// thread: ThreadView,
|
||||
// flat: FlatView,
|
||||
}
|
||||
|
||||
impl<M: Msg, S: MsgStore<M>> Chat<M, S> {
|
||||
impl<M: Msg, S: MsgStore<M> + Clone> ChatState<M, S> {
|
||||
pub fn new(store: S) -> Self {
|
||||
Self {
|
||||
store,
|
||||
cursor: None,
|
||||
mode: Mode::Tree,
|
||||
tree: TreeView::new(),
|
||||
tree: TreeViewState::new(store.clone()),
|
||||
store,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: Msg, S: MsgStore<M>> ChatState<M, S> {
|
||||
pub fn store(&self) -> &S {
|
||||
&self.store
|
||||
}
|
||||
|
||||
pub fn widget(&self) -> Chat<M, S> {
|
||||
match self.mode {
|
||||
Mode::Tree => Chat::Tree(self.tree.widget()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: Msg, S: MsgStore<M>> Chat<M, S> {
|
||||
pub async fn handle_navigation(
|
||||
&mut self,
|
||||
terminal: &mut Terminal,
|
||||
size: Size,
|
||||
event: KeyEvent,
|
||||
) {
|
||||
match self.mode {
|
||||
Mode::Tree => {
|
||||
self.tree
|
||||
.handle_navigation(&mut self.store, &mut self.cursor, terminal, size, event)
|
||||
.await
|
||||
}
|
||||
// pub async fn handle_navigation(
|
||||
// &mut self,
|
||||
// terminal: &mut Terminal,
|
||||
// size: Size,
|
||||
// event: KeyEvent,
|
||||
// ) {
|
||||
// match self.mode {
|
||||
// Mode::Tree => {
|
||||
// self.tree
|
||||
// .handle_navigation(&mut self.store, &mut self.cursor, terminal, size, event)
|
||||
// .await
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub async fn handle_messaging(
|
||||
// &mut self,
|
||||
// terminal: &mut Terminal,
|
||||
// crossterm_lock: &Arc<FairMutex<()>>,
|
||||
// event: KeyEvent,
|
||||
// ) -> Option<(Option<M::Id>, String)> {
|
||||
// match self.mode {
|
||||
// Mode::Tree => {
|
||||
// self.tree
|
||||
// .handle_messaging(
|
||||
// &mut self.store,
|
||||
// &mut self.cursor,
|
||||
// terminal,
|
||||
// crossterm_lock,
|
||||
// event,
|
||||
// )
|
||||
// .await
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub async fn render(&mut self, frame: &mut Frame, pos: Pos, size: Size) {
|
||||
// match self.mode {
|
||||
// Mode::Tree => {
|
||||
// self.tree
|
||||
// .render(&mut self.store, &self.cursor, frame, pos, size)
|
||||
// .await
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
////////////
|
||||
// Widget //
|
||||
////////////
|
||||
|
||||
pub enum Chat<M: Msg, S: MsgStore<M>> {
|
||||
Tree(TreeView<M, S>),
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<M, S> Widget for Chat<M, S>
|
||||
where
|
||||
M: Msg,
|
||||
M::Id: Send,
|
||||
S: MsgStore<M> + Send + Sync,
|
||||
{
|
||||
fn size(&self, frame: &mut Frame, max_width: Option<u16>, max_height: Option<u16>) -> Size {
|
||||
match self {
|
||||
Self::Tree(tree) => tree.size(frame, max_width, max_height),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_messaging(
|
||||
&mut self,
|
||||
terminal: &mut Terminal,
|
||||
crossterm_lock: &Arc<FairMutex<()>>,
|
||||
event: KeyEvent,
|
||||
) -> Option<(Option<M::Id>, String)> {
|
||||
match self.mode {
|
||||
Mode::Tree => {
|
||||
self.tree
|
||||
.handle_messaging(
|
||||
&mut self.store,
|
||||
&mut self.cursor,
|
||||
terminal,
|
||||
crossterm_lock,
|
||||
event,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn render(&mut self, frame: &mut Frame, pos: Pos, size: Size) {
|
||||
match self.mode {
|
||||
Mode::Tree => {
|
||||
self.tree
|
||||
.render(&mut self.store, &self.cursor, frame, pos, size)
|
||||
.await
|
||||
}
|
||||
async fn render(self: Box<Self>, frame: &mut Frame) {
|
||||
match *self {
|
||||
Self::Tree(tree) => Box::new(tree).render(frame).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue