Fold subtrees
This commit is contained in:
parent
26923745ad
commit
0ad3432141
4 changed files with 69 additions and 10 deletions
|
|
@ -38,6 +38,12 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
}
|
||||
}
|
||||
|
||||
fn make_path_visible(&mut self, path: &Path<M::Id>) {
|
||||
for segment in path.parent_segments() {
|
||||
self.folded.remove(segment);
|
||||
}
|
||||
}
|
||||
|
||||
fn cursor_line(&self, blocks: &TreeBlocks<M::Id>) -> i32 {
|
||||
if let Cursor::Bottom = self.cursor {
|
||||
// The value doesn't matter as it will always be ignored.
|
||||
|
|
@ -84,18 +90,25 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
blocks.blocks_mut().push_back(block);
|
||||
}
|
||||
|
||||
let folded = self.folded.contains(id);
|
||||
let children = tree.children(id);
|
||||
let folded_info = children
|
||||
.filter(|_| folded)
|
||||
.map(|c| c.len())
|
||||
.filter(|c| *c > 0);
|
||||
|
||||
// Main message body
|
||||
let highlighted = self.cursor.refers_to(id);
|
||||
let widget = if let Some(msg) = tree.msg(id) {
|
||||
widgets::msg(highlighted, indent, msg)
|
||||
widgets::msg(highlighted, indent, msg, folded_info)
|
||||
} else {
|
||||
widgets::msg_placeholder(highlighted, indent)
|
||||
widgets::msg_placeholder(highlighted, indent, folded_info)
|
||||
};
|
||||
let block = Block::new(frame, BlockId::Msg(id.clone()), widget);
|
||||
blocks.blocks_mut().push_back(block);
|
||||
|
||||
// Children, recursively
|
||||
if let Some(children) = tree.children(id) {
|
||||
// Children recursively (if not folded)
|
||||
if let Some(children) = children.filter(|_| !folded) {
|
||||
for child in children {
|
||||
self.layout_subtree(nick, frame, tree, indent + 1, child, blocks);
|
||||
}
|
||||
|
|
@ -458,6 +471,7 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
|
||||
let last_cursor_path = self.cursor_path(&self.last_cursor).await;
|
||||
let cursor_path = self.cursor_path(&self.cursor).await;
|
||||
self.make_path_visible(&cursor_path);
|
||||
|
||||
let mut blocks = self
|
||||
.layout_initial_seed(nick, frame, &last_cursor_path, &cursor_path)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ mod time;
|
|||
|
||||
use crossterm::style::{ContentStyle, Stylize};
|
||||
use toss::frame::Frame;
|
||||
use toss::styled::Styled;
|
||||
|
||||
use super::super::ChatMsg;
|
||||
use crate::store::Msg;
|
||||
|
|
@ -40,6 +41,10 @@ fn style_indent(highlighted: bool) -> ContentStyle {
|
|||
}
|
||||
}
|
||||
|
||||
fn style_info() -> ContentStyle {
|
||||
ContentStyle::default().italic().dark_grey()
|
||||
}
|
||||
|
||||
fn style_editor_highlight() -> ContentStyle {
|
||||
ContentStyle::default().black().on_cyan()
|
||||
}
|
||||
|
|
@ -48,8 +53,20 @@ fn style_pseudo_highlight() -> ContentStyle {
|
|||
ContentStyle::default().black().on_yellow()
|
||||
}
|
||||
|
||||
pub fn msg<M: Msg + ChatMsg>(highlighted: bool, indent: usize, msg: &M) -> BoxedWidget {
|
||||
let (nick, content) = msg.styled();
|
||||
pub fn msg<M: Msg + ChatMsg>(
|
||||
highlighted: bool,
|
||||
indent: usize,
|
||||
msg: &M,
|
||||
folded_info: Option<usize>,
|
||||
) -> BoxedWidget {
|
||||
let (nick, mut content) = msg.styled();
|
||||
|
||||
if let Some(amount) = folded_info {
|
||||
content = content
|
||||
.then_plain("\n")
|
||||
.then(format!("[{amount} more]"), style_info());
|
||||
}
|
||||
|
||||
HJoin::new(vec![
|
||||
Segment::new(seen::widget(msg.seen())),
|
||||
Segment::new(
|
||||
|
|
@ -69,7 +86,19 @@ pub fn msg<M: Msg + ChatMsg>(highlighted: bool, indent: usize, msg: &M) -> Boxed
|
|||
.into()
|
||||
}
|
||||
|
||||
pub fn msg_placeholder(highlighted: bool, indent: usize) -> BoxedWidget {
|
||||
pub fn msg_placeholder(
|
||||
highlighted: bool,
|
||||
indent: usize,
|
||||
folded_info: Option<usize>,
|
||||
) -> BoxedWidget {
|
||||
let mut content = Styled::new(PLACEHOLDER, style_placeholder());
|
||||
|
||||
if let Some(amount) = folded_info {
|
||||
content = content
|
||||
.then_plain("\n")
|
||||
.then(format!("[{amount} more]"), style_info());
|
||||
}
|
||||
|
||||
HJoin::new(vec![
|
||||
Segment::new(seen::widget(true)),
|
||||
Segment::new(
|
||||
|
|
@ -78,7 +107,7 @@ pub fn msg_placeholder(highlighted: bool, indent: usize) -> BoxedWidget {
|
|||
.right(1),
|
||||
),
|
||||
Segment::new(Indent::new(indent, style_indent(highlighted))),
|
||||
Segment::new(Text::new((PLACEHOLDER, style_placeholder()))),
|
||||
Segment::new(Text::new(content)),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue