diff --git a/cove-tui/src/chat/tree/blocks.rs b/cove-tui/src/chat/tree/blocks.rs index 6b5a221..a479947 100644 --- a/cove-tui/src/chat/tree/blocks.rs +++ b/cove-tui/src/chat/tree/blocks.rs @@ -6,6 +6,8 @@ use chrono::{DateTime, Utc}; use crate::chat::Cursor; +use super::util; + pub struct Block { pub id: I, pub line: i32, @@ -118,9 +120,9 @@ impl Blocks { cursor.expect("no cursor in layout") } - pub fn calculate_offsets_with_cursor(&mut self, cursor: &Cursor, height: i32) { + pub fn calculate_offsets_with_cursor(&mut self, cursor: &Cursor, height: u16) { let cursor_index = self.mark_cursor(&cursor.id); - let cursor_line = ((height - 1) as f32 * cursor.proportion).floor() as i32; + let cursor_line = util::proportion_to_line(height, cursor.proportion); // Propagate lines from cursor to both ends self.blocks[cursor_index].line = cursor_line; diff --git a/cove-tui/src/chat/tree/layout.rs b/cove-tui/src/chat/tree/layout.rs index 55b4a84..7cc1cf7 100644 --- a/cove-tui/src/chat/tree/layout.rs +++ b/cove-tui/src/chat/tree/layout.rs @@ -99,7 +99,6 @@ impl TreeView { frame: &mut Frame, size: Size, ) -> Blocks { - let height: i32 = size.height.into(); if let Some(cursor) = cursor { // TODO Ensure focus lies on cursor path, otherwise unfocus // TODO Unfold all messages on path to cursor @@ -109,7 +108,7 @@ impl TreeView { let cursor_tree_id = cursor_path.first(); let cursor_tree = store.tree(room, cursor_tree_id).await; let mut layout = layout_tree(frame, size, cursor_tree); - layout.calculate_offsets_with_cursor(cursor, height); + layout.calculate_offsets_with_cursor(cursor, size.height); // Expand upwards and downwards // TODO Don't do this if there is a focus @@ -125,7 +124,7 @@ impl TreeView { // TODO Ensure there is no focus // Start at the bottom of the screen - let mut layout = Blocks::new_below(height - 1); + let mut layout = Blocks::new_below(size.height as i32 - 1); // Expand upwards until the edge of the screen if let Some(last_tree) = store.last_tree(room).await { diff --git a/cove-tui/src/chat/tree/util.rs b/cove-tui/src/chat/tree/util.rs index f1e7482..61fd56f 100644 --- a/cove-tui/src/chat/tree/util.rs +++ b/cove-tui/src/chat/tree/util.rs @@ -1,7 +1,7 @@ //! Constants and helper functions. use crossterm::style::{ContentStyle, Stylize}; -use toss::frame::Frame; +use toss::frame::{Frame, Size}; pub const TIME_FORMAT: &str = "%H:%M "; pub const TIME_EMPTY: &str = " "; @@ -42,3 +42,15 @@ pub fn after_indent(indent: usize) -> i32 { pub fn after_nick(frame: &mut Frame, indent: usize, nick: &str) -> i32 { after_indent(indent) + 1 + frame.width(nick) as i32 + 2 } + +pub fn proportion_to_line(height: u16, proportion: f32) -> i32 { + ((height - 1) as f32 * proportion).ceil() as i32 +} + +pub fn line_to_proportion(height: u16, line: i32) -> f32 { + if height > 1 { + line as f32 / (height - 1) as f32 + } else { + 0.0 + } +}