From 20feb50475ac17a7f7595f5c96b6b45d40fe423e Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 18 Sep 2021 11:36:17 +0300 Subject: [PATCH] view/thread: open the latest email in the thread by default --- src/components/mail/view/thread.rs | 56 +++++++++++++++++++----------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/components/mail/view/thread.rs b/src/components/mail/view/thread.rs index 147ab6ff..56efe3f2 100644 --- a/src/components/mail/view/thread.rs +++ b/src/components/mail/view/thread.rs @@ -33,6 +33,7 @@ struct ThreadEntry { dirty: bool, hidden: bool, heading: String, + timestamp: UnixTimestamp, } #[derive(Debug, Default, Clone)] @@ -162,6 +163,26 @@ impl ThreadView { } fn initiate(&mut self, expanded_hash: Option, context: &Context) { + #[inline(always)] + fn make_entry( + i: (usize, ThreadNodeHash, usize), + msg_hash: EnvelopeHash, + seen: bool, + timestamp: UnixTimestamp, + ) -> ThreadEntry { + let (ind, _, _) = i; + ThreadEntry { + index: i, + indentation: ind, + msg_hash, + seen, + dirty: true, + hidden: false, + heading: String::new(), + timestamp, + } + } + let account = &context.accounts[&self.coordinates.0]; let threads = account.collection.get_threads(self.coordinates.1); @@ -174,8 +195,13 @@ impl ThreadView { for (line, (ind, thread_node_hash)) in thread_iter.enumerate() { let entry = if let Some(msg_hash) = threads.thread_nodes()[&thread_node_hash].message() { - let seen: bool = account.collection.get_env(msg_hash).is_seen(); - self.make_entry((ind, thread_node_hash, line), msg_hash, seen) + let env_ref = account.collection.get_env(msg_hash); + make_entry( + (ind, thread_node_hash, line), + msg_hash, + env_ref.is_seen(), + env_ref.timestamp, + ) } else { continue; }; @@ -189,7 +215,13 @@ impl ThreadView { } } if expanded_hash.is_none() { - self.new_expanded_pos = self.entries.len().saturating_sub(1); + self.new_expanded_pos = self + .entries + .iter() + .enumerate() + .reduce(|a, b| if a.1.timestamp > b.1.timestamp { a } else { b }) + .map(|el| el.0) + .unwrap_or(0); self.expanded_pos = self.new_expanded_pos + 1; } @@ -392,24 +424,6 @@ impl ThreadView { self.visible_entries = vec![(0..self.entries.len()).collect()]; } - fn make_entry( - &mut self, - i: (usize, ThreadNodeHash, usize), - msg_hash: EnvelopeHash, - seen: bool, - ) -> ThreadEntry { - let (ind, _, _) = i; - ThreadEntry { - index: i, - indentation: ind, - msg_hash, - seen, - dirty: true, - hidden: false, - heading: String::new(), - } - } - fn highlight_line( &self, grid: &mut CellBuffer,