Make new ChatMsg trait for Chat message rendering

This commit is contained in:
Joscha 2022-08-01 19:57:05 +02:00
parent 5c9c6e9d98
commit 4ac0b5f074
13 changed files with 122 additions and 69 deletions

View file

@ -6,7 +6,9 @@ use std::sync::Arc;
use async_trait::async_trait;
use crossterm::event::KeyEvent;
use parking_lot::FairMutex;
use time::OffsetDateTime;
use toss::frame::{Frame, Size};
use toss::styled::Styled;
use toss::terminal::Terminal;
use crate::store::{Msg, MsgStore};
@ -15,6 +17,17 @@ use self::tree::{TreeView, TreeViewState};
use super::widgets::Widget;
///////////
// Trait //
///////////
pub trait ChatMsg {
fn time(&self) -> OffsetDateTime;
fn styled(&self) -> (Styled, Styled);
fn edit(nick: &str, content: &str) -> (Styled, Styled);
fn pseudo(nick: &str, content: &str) -> (Styled, Styled);
}
///////////
// State //
///////////
@ -87,7 +100,7 @@ pub enum Chat<M: Msg, S: MsgStore<M>> {
#[async_trait]
impl<M, S> Widget for Chat<M, S>
where
M: Msg,
M: Msg + ChatMsg,
M::Id: Send + Sync,
S: MsgStore<M> + Send + Sync,
{

View file

@ -18,6 +18,8 @@ use crate::ui::widgets::Widget;
use self::cursor::Cursor;
use super::ChatMsg;
///////////
// State //
///////////
@ -115,7 +117,7 @@ pub struct TreeView<M: Msg, S: MsgStore<M>>(Arc<Mutex<InnerTreeViewState<M, S>>>
#[async_trait]
impl<M, S> Widget for TreeView<M, S>
where
M: Msg,
M: Msg + ChatMsg,
M::Id: Send + Sync,
S: MsgStore<M> + Send + Sync,
{

View file

@ -4,11 +4,12 @@ use crate::store::{Msg, MsgStore, Path, Tree};
use crate::ui::chat::blocks::Block;
use crate::ui::widgets::empty::Empty;
use crate::ui::widgets::text::Text;
use crate::ui::ChatMsg;
use super::tree_blocks::{BlockId, Root, TreeBlocks};
use super::{widgets, Cursor, InnerTreeViewState};
impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
async fn cursor_path(&self, cursor: &Cursor<M::Id>) -> Path<M::Id> {
match cursor {
Cursor::Msg(id) => self.store.path(id).await,

View file

@ -5,6 +5,7 @@ mod time;
use crossterm::style::{ContentStyle, Stylize};
use super::super::ChatMsg;
use crate::store::Msg;
use crate::ui::widgets::join::{HJoin, Segment};
use crate::ui::widgets::padding::Padding;
@ -19,7 +20,8 @@ pub fn style_placeholder() -> ContentStyle {
ContentStyle::default().dark_grey()
}
pub fn msg<M: Msg>(highlighted: bool, indent: usize, msg: &M) -> BoxedWidget {
pub fn msg<M: Msg + ChatMsg>(highlighted: bool, indent: usize, msg: &M) -> BoxedWidget {
let (nick, content) = msg.styled();
HJoin::new(vec![
Segment::new(
Padding::new(time::widget(Some(msg.time()), highlighted))
@ -27,10 +29,10 @@ pub fn msg<M: Msg>(highlighted: bool, indent: usize, msg: &M) -> BoxedWidget {
.right(1),
),
Segment::new(Indent::new(indent, highlighted)),
Segment::new(Padding::new(Text::new(msg.nick())).right(1)),
Segment::new(Padding::new(Text::new(nick)).right(1)),
// TODO Minimum content width
// TODO Minimizing and maximizing messages
Segment::new(Text::new(msg.content()).wrap(true)).priority(1),
Segment::new(Text::new(content).wrap(true)).priority(1),
])
.into()
}

View file

@ -10,7 +10,7 @@ use toss::terminal::Terminal;
use crate::euph::api::{SessionType, SessionView};
use crate::euph::{self, Joined, Status};
use crate::vault::{EuphMsg, EuphVault};
use crate::vault::EuphVault;
use super::chat::ChatState;
use super::widgets::background::Background;
@ -37,7 +37,7 @@ pub struct EuphRoom {
state: State,
room: Option<euph::Room>,
chat: ChatState<EuphMsg, EuphVault>,
chat: ChatState<euph::Message, EuphVault>,
nick_list: ListState<String>,
}