diff --git a/meli/src/mail/listing.rs b/meli/src/mail/listing.rs index c6fcadc1..5660841b 100644 --- a/meli/src/mail/listing.rs +++ b/meli/src/mail/listing.rs @@ -1308,6 +1308,7 @@ impl Component for Listing { env_hash, thread_hash, show_thread, + go_to_first_unread, }) => { let (a, m) = self.component.coordinates(); self.view.unrealize(context); @@ -1315,6 +1316,7 @@ impl Component for Listing { (a, m, env_hash), thread_hash, Some(env_hash), + go_to_first_unread, if show_thread { None } else { @@ -3068,6 +3070,7 @@ pub enum ListingMessage { env_hash: EnvelopeHash, thread_hash: ThreadHash, show_thread: bool, + go_to_first_unread: bool, }, UpdateView, } diff --git a/meli/src/mail/listing/compact.rs b/meli/src/mail/listing/compact.rs index b07387af..e330bb1c 100644 --- a/meli/src/mail/listing/compact.rs +++ b/meli/src/mail/listing/compact.rs @@ -331,6 +331,7 @@ impl MailListingTrait for CompactListing { thread_hash, env_hash, show_thread: true, + go_to_first_unread: true, }, context, ); @@ -930,6 +931,7 @@ impl ListingTrait for CompactListing { thread_hash, env_hash, show_thread: true, + go_to_first_unread: true, }, context, ); diff --git a/meli/src/mail/listing/conversations.rs b/meli/src/mail/listing/conversations.rs index b8bbbe55..b58fd48e 100644 --- a/meli/src/mail/listing/conversations.rs +++ b/meli/src/mail/listing/conversations.rs @@ -238,6 +238,7 @@ impl MailListingTrait for ConversationsListing { thread_hash, env_hash, show_thread: true, + go_to_first_unread: true, }, context, ); @@ -666,6 +667,7 @@ impl ListingTrait for ConversationsListing { thread_hash, env_hash, show_thread: true, + go_to_first_unread: true, }, context, ); diff --git a/meli/src/mail/listing/plain.rs b/meli/src/mail/listing/plain.rs index 970aadca..7d221256 100644 --- a/meli/src/mail/listing/plain.rs +++ b/meli/src/mail/listing/plain.rs @@ -297,6 +297,7 @@ impl MailListingTrait for PlainListing { thread_hash, env_hash, show_thread: false, + go_to_first_unread: false, }, context, ); @@ -648,6 +649,7 @@ impl ListingTrait for PlainListing { thread_hash, env_hash, show_thread: false, + go_to_first_unread: false, }, context, ); diff --git a/meli/src/mail/listing/thread.rs b/meli/src/mail/listing/thread.rs index e49deffd..87155a45 100644 --- a/meli/src/mail/listing/thread.rs +++ b/meli/src/mail/listing/thread.rs @@ -730,6 +730,7 @@ impl ListingTrait for ThreadListing { thread_hash, env_hash, show_thread: false, + go_to_first_unread: false, }, context, ); diff --git a/meli/src/mail/view/thread.rs b/meli/src/mail/view/thread.rs index 52634bae..8cb44943 100644 --- a/meli/src/mail/view/thread.rs +++ b/meli/src/mail/view/thread.rs @@ -81,6 +81,7 @@ impl ThreadView { coordinates: (AccountHash, MailboxHash, EnvelopeHash), thread_group: ThreadHash, expanded_hash: Option, + go_to_first_unread: bool, focus: Option, context: &mut Context, ) -> Self { @@ -105,7 +106,7 @@ impl ThreadView { use_color: context.settings.terminal.use_color(), ..Default::default() }; - view.initiate(expanded_hash, context); + view.initiate(expanded_hash, go_to_first_unread, context); view.new_cursor_pos = view.new_expanded_pos; view } @@ -130,7 +131,7 @@ impl ThreadView { }; let expanded_hash = old_expanded_entry.as_ref().map(|e| e.msg_hash); - self.initiate(expanded_hash, context); + self.initiate(expanded_hash, false, context); let mut old_cursor = 0; let mut new_cursor = 0; @@ -172,7 +173,12 @@ impl ThreadView { self.set_dirty(true); } - fn initiate(&mut self, expanded_hash: Option, context: &mut Context) { + fn initiate( + &mut self, + expanded_hash: Option, + go_to_first_unread: bool, + context: &mut Context, + ) { #[inline(always)] fn make_entry( i: (usize, ThreadNodeHash, usize), @@ -210,11 +216,19 @@ impl ThreadView { let thread_iter = threads.thread_iter(self.thread_group); self.entries.clear(); + let mut earliest_unread = 0; + let mut earliest_unread_entry = 0; 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 (is_seen, timestamp) = { let env_ref = collection.get_env(msg_hash); + if !env_ref.is_seen() + && (earliest_unread == 0 || env_ref.timestamp < earliest_unread) + { + earliest_unread = env_ref.timestamp; + earliest_unread_entry = self.entries.len(); + } (env_ref.is_seen(), env_ref.timestamp) }; make_entry( @@ -248,6 +262,10 @@ impl ThreadView { .unwrap_or(0); self.expanded_pos = self.new_expanded_pos + 1; } + if go_to_first_unread && earliest_unread > 0 { + self.new_expanded_pos = earliest_unread_entry; + self.expanded_pos = earliest_unread_entry + 1; + } let height = 2 * self.entries.len() + 1; let mut width = 0; @@ -1105,7 +1123,7 @@ impl Component for ThreadView { { self.reversed = !self.reversed; let expanded_hash = self.entries[self.expanded_pos].msg_hash; - self.initiate(Some(expanded_hash), context); + self.initiate(Some(expanded_hash), false, context); self.dirty = true; true }