From 18573e5a3742f3190d0a5fae057f7716daf48c9f Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 2 Aug 2022 01:19:40 +0200 Subject: [PATCH] Fix rendering of editor and pseudo message --- src/ui/chat/tree/layout.rs | 28 ++++++++++++++-------------- src/ui/chat/tree/widgets/time.rs | 22 ++++++++++++---------- src/ui/room.rs | 2 +- src/ui/widgets/empty.rs | 28 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/ui/chat/tree/layout.rs b/src/ui/chat/tree/layout.rs index 7a324dd..41317c6 100644 --- a/src/ui/chat/tree/layout.rs +++ b/src/ui/chat/tree/layout.rs @@ -3,7 +3,6 @@ use toss::frame::Frame; 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}; @@ -73,7 +72,7 @@ impl> InnerTreeViewState { ) { // Ghost cursor in front, for positioning according to last cursor line if self.last_cursor.refers_to(id) { - let block = Block::new(frame, BlockId::LastCursor, Empty); + let block = Block::new(frame, BlockId::LastCursor, Empty::new()); blocks.blocks_mut().push_back(block); } @@ -96,24 +95,25 @@ impl> InnerTreeViewState { // Trailing ghost cursor, for positioning according to last cursor line if self.last_cursor.refers_to_last_child_of(id) { - let block = Block::new(frame, BlockId::LastCursor, Empty); + let block = Block::new(frame, BlockId::LastCursor, Empty::new()); blocks.blocks_mut().push_back(block); } // Trailing editor or pseudomessage if self.cursor.refers_to_last_child_of(id) { match self.cursor { - Cursor::Editor { .. } => blocks - .blocks_mut() - .push_back(self.editor_block(nick, frame, indent)), - Cursor::Pseudo { .. } => blocks - .blocks_mut() - .push_back(self.pseudo_block(nick, frame, indent)), + Cursor::Editor { .. } => { + blocks + .blocks_mut() + .push_back(self.editor_block(nick, frame, indent + 1)) + } + Cursor::Pseudo { .. } => { + blocks + .blocks_mut() + .push_back(self.pseudo_block(nick, frame, indent + 1)) + } _ => {} } - // TODO Render proper editor or pseudocursor - let block = Block::new(frame, BlockId::Cursor, Text::new("TODO")); - blocks.blocks_mut().push_back(block); } } @@ -131,13 +131,13 @@ impl> InnerTreeViewState { if let Cursor::Editor { parent: None, .. } | Cursor::Pseudo { parent: None, .. } = self.last_cursor { - let block = Block::new(frame, BlockId::LastCursor, Empty); + let block = Block::new(frame, BlockId::LastCursor, Empty::new()); blocks.blocks_mut().push_back(block); } match self.cursor { Cursor::Bottom => { - let block = Block::new(frame, BlockId::Cursor, Empty); + let block = Block::new(frame, BlockId::Cursor, Empty::new()); blocks.blocks_mut().push_back(block); } Cursor::Editor { parent: None, .. } => blocks diff --git a/src/ui/chat/tree/widgets/time.rs b/src/ui/chat/tree/widgets/time.rs index 037e39f..30c626e 100644 --- a/src/ui/chat/tree/widgets/time.rs +++ b/src/ui/chat/tree/widgets/time.rs @@ -4,11 +4,12 @@ use time::macros::format_description; use time::OffsetDateTime; use crate::ui::widgets::background::Background; +use crate::ui::widgets::empty::Empty; use crate::ui::widgets::text::Text; use crate::ui::widgets::BoxedWidget; const TIME_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day] [hour]:[minute]"); -const TIME_EMPTY: &str = " "; +const TIME_WIDTH: u16 = 16; fn style() -> ContentStyle { ContentStyle::default().grey() @@ -19,19 +20,20 @@ fn style_inverted() -> ContentStyle { } pub fn widget(time: Option, highlighted: bool) -> BoxedWidget { - let text = if let Some(time) = time { - time.format(TIME_FORMAT).expect("could not format time") - } else { - TIME_EMPTY.to_string() - }; - let style = if highlighted { style_inverted() } else { style() }; - Background::new(Text::new((text, style))) - .style(style) - .into() + if let Some(time) = time { + let text = time.format(TIME_FORMAT).expect("could not format time"); + Background::new(Text::new((text, style))) + .style(style) + .into() + } else { + Background::new(Empty::new().width(TIME_WIDTH)) + .style(style) + .into() + } } diff --git a/src/ui/room.rs b/src/ui/room.rs index b38db8a..ba7a776 100644 --- a/src/ui/room.rs +++ b/src/ui/room.rs @@ -229,7 +229,7 @@ impl EuphRoom { let heading_style = ContentStyle::new().bold(); if !list.is_empty() { - list.add_unsel(Empty); + list.add_unsel(Empty::new()); } let row = Styled::new(name, heading_style).then_plain(format!(" ({})", sessions.len())); diff --git a/src/ui/widgets/empty.rs b/src/ui/widgets/empty.rs index 6f95369..40ff3bf 100644 --- a/src/ui/widgets/empty.rs +++ b/src/ui/widgets/empty.rs @@ -3,12 +3,36 @@ use toss::frame::{Frame, Size}; use super::Widget; -pub struct Empty; +#[derive(Debug, Default, Clone, Copy)] +pub struct Empty { + size: Size, +} + +impl Empty { + pub fn new() -> Self { + Self { size: Size::ZERO } + } + + pub fn width(mut self, width: u16) -> Self { + self.size.width = width; + self + } + + pub fn height(mut self, height: u16) -> Self { + self.size.height = height; + self + } + + pub fn size(mut self, size: Size) -> Self { + self.size = size; + self + } +} #[async_trait] impl Widget for Empty { fn size(&self, _frame: &mut Frame, _max_width: Option, _max_height: Option) -> Size { - Size::ZERO + self.size } async fn render(self: Box, _frame: &mut Frame) {}