Extract cursor proportion calculations

This commit is contained in:
Joscha 2022-06-14 16:41:01 +02:00
parent eeb350aef4
commit bec12917d6
3 changed files with 19 additions and 6 deletions

View file

@ -6,6 +6,8 @@ use chrono::{DateTime, Utc};
use crate::chat::Cursor;
use super::util;
pub struct Block<I> {
pub id: I,
pub line: i32,
@ -118,9 +120,9 @@ impl<I: PartialEq> Blocks<I> {
cursor.expect("no cursor in layout")
}
pub fn calculate_offsets_with_cursor(&mut self, cursor: &Cursor<I>, height: i32) {
pub fn calculate_offsets_with_cursor(&mut self, cursor: &Cursor<I>, 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;

View file

@ -99,7 +99,6 @@ impl<M: Msg> TreeView<M> {
frame: &mut Frame,
size: Size,
) -> Blocks<M::Id> {
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<M: Msg> TreeView<M> {
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<M: Msg> TreeView<M> {
// 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 {

View file

@ -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
}
}