diff --git a/cove-tui/src/chat/tree/layout.rs b/cove-tui/src/chat/tree/layout.rs index ad6da2d..c5daa96 100644 --- a/cove-tui/src/chat/tree/layout.rs +++ b/cove-tui/src/chat/tree/layout.rs @@ -7,69 +7,61 @@ use super::blocks::{Block, Blocks}; use super::util::{self, MIN_CONTENT_WIDTH}; use super::TreeView; +fn msg_to_block(frame: &mut Frame, size: Size, msg: &M, indent: usize) -> Block { + let nick = msg.nick(); + let content = msg.content(); + + let content_width = size.width as i32 - util::after_nick(frame, indent, &nick); + if content_width < MIN_CONTENT_WIDTH as i32 { + Block::placeholder(msg.id(), indent).time(msg.time()) + } else { + let content_width = content_width as usize; + let lines = toss::split_at_indices(&content, &frame.wrap(&content, content_width)); + let lines = lines.into_iter().map(|s| s.to_string()).collect::>(); + Block::msg(msg.id(), indent, msg.time(), nick, lines) + } +} + +fn layout_subtree( + frame: &mut Frame, + size: Size, + tree: &Tree, + indent: usize, + id: &M::Id, + result: &mut Blocks, +) { + let block = if let Some(msg) = tree.msg(id) { + msg_to_block(frame, size, msg, indent) + } else { + Block::placeholder(id.clone(), indent) + }; + result.push_back(block); + + if let Some(children) = tree.children(id) { + for child in children { + layout_subtree(frame, size, tree, indent + 1, child, result); + } + } +} + +fn layout_tree(frame: &mut Frame, size: Size, tree: Tree) -> Blocks { + let mut blocks = Blocks::new(); + layout_subtree(frame, size, &tree, 0, tree.root(), &mut blocks); + blocks +} + impl TreeView { - fn msg_to_block( - &mut self, - frame: &mut Frame, - size: Size, - msg: &M, - indent: usize, - ) -> Block { - let nick = msg.nick(); - let content = msg.content(); - - let content_width = size.width as i32 - util::after_nick(frame, indent, &nick); - if content_width < MIN_CONTENT_WIDTH as i32 { - Block::placeholder(msg.id(), indent).time(msg.time()) - } else { - let content_width = content_width as usize; - let lines = toss::split_at_indices(&content, &frame.wrap(&content, content_width)); - let lines = lines.into_iter().map(|s| s.to_string()).collect::>(); - Block::msg(msg.id(), indent, msg.time(), nick, lines) - } - } - - fn layout_subtree( - &mut self, - tree: &Tree, - frame: &mut Frame, - size: Size, - indent: usize, - id: &M::Id, - layout: &mut Blocks, - ) { - let block = if let Some(msg) = tree.msg(id) { - self.msg_to_block(frame, size, msg, indent) - } else { - Block::placeholder(id.clone(), indent) - }; - layout.push_back(block); - - if let Some(children) = tree.children(id) { - for child in children { - self.layout_subtree(tree, frame, size, indent + 1, child, layout); - } - } - } - - fn layout_tree(&mut self, tree: Tree, frame: &mut Frame, size: Size) -> Blocks { - let mut layout = Blocks::new(); - self.layout_subtree(&tree, frame, size, 0, tree.root(), &mut layout); - layout - } - - async fn expand_layout_upwards>( - &mut self, + pub async fn expand_blocks_up>( room: &str, store: &S, frame: &mut Frame, size: Size, - layout: &mut Blocks, + blocks: &mut Blocks, mut tree_id: M::Id, ) { - while layout.top_line > 0 { + while blocks.top_line > 0 { let tree = store.tree(room, &tree_id).await; - layout.prepend(self.layout_tree(tree, frame, size)); + blocks.prepend(layout_tree(frame, size, tree)); if let Some(prev_tree_id) = store.prev_tree(room, &tree_id).await { tree_id = prev_tree_id; } else { @@ -78,18 +70,17 @@ impl TreeView { } } - async fn expand_layout_downwards>( - &mut self, + pub async fn expand_blocks_down>( room: &str, store: &S, frame: &mut Frame, size: Size, - layout: &mut Blocks, + blocks: &mut Blocks, mut tree_id: M::Id, ) { - while layout.bottom_line < size.height as i32 { + while blocks.bottom_line < size.height as i32 { let tree = store.tree(room, &tree_id).await; - layout.append(self.layout_tree(tree, frame, size)); + blocks.append(layout_tree(frame, size, tree)); if let Some(next_tree_id) = store.next_tree(room, &tree_id).await { tree_id = next_tree_id; } else { @@ -111,35 +102,32 @@ impl TreeView { // TODO Ensure focus lies on cursor path, otherwise unfocus // TODO Unfold all messages on path to cursor - // Produce layout of cursor subtree (with correct offsets) + // Layout cursor subtree (with correct offsets) let cursor_path = store.path(room, &cursor.id).await; let cursor_tree_id = cursor_path.first(); let cursor_tree = store.tree(room, cursor_tree_id).await; - let mut layout = self.layout_tree(cursor_tree, frame, size); + let mut layout = layout_tree(frame, size, cursor_tree); layout.calculate_offsets_with_cursor(cursor, height); - // Expand layout upwards and downwards + // Expand upwards and downwards // TODO Don't do this if there is a focus if let Some(prev_tree) = store.prev_tree(room, cursor_tree_id).await { - self.expand_layout_upwards(room, store, frame, size, &mut layout, prev_tree) - .await; + Self::expand_blocks_up(room, store, frame, size, &mut layout, prev_tree).await; } if let Some(next_tree) = store.next_tree(room, cursor_tree_id).await { - self.expand_layout_downwards(room, store, frame, size, &mut layout, next_tree) - .await; + Self::expand_blocks_down(room, store, frame, size, &mut layout, next_tree).await; } layout } else { // TODO Ensure there is no focus - // Start layout at the bottom of the screen + // Start at the bottom of the screen let mut layout = Blocks::new_below(height - 1); - // Expand layout upwards until the edge of the screen + // Expand upwards until the edge of the screen if let Some(last_tree) = store.last_tree(room).await { - self.expand_layout_upwards(room, store, frame, size, &mut layout, last_tree) - .await; + Self::expand_blocks_up(room, store, frame, size, &mut layout, last_tree).await; } layout