ui: do not recreate ThreadView on envelope update event

This commit is contained in:
Manos Pitsidianakis 2019-04-03 22:21:29 +03:00
parent ba6c259820
commit d7a4bd24c3
No known key found for this signature in database
GPG Key ID: 73627C2F690DF710
2 changed files with 54 additions and 25 deletions

View File

@ -42,7 +42,7 @@ pub struct CompactListing {
dirty: bool, dirty: bool,
/// If `self.view` exists or not. /// If `self.view` exists or not.
unfocused: bool, unfocused: bool,
view: Option<ThreadView>, view: ThreadView,
movement: Option<PageMovement>, movement: Option<PageMovement>,
} }
@ -101,7 +101,7 @@ impl CompactListing {
content, content,
dirty: true, dirty: true,
unfocused: false, unfocused: false,
view: None, view: ThreadView::default(),
movement: None, movement: None,
} }
@ -110,6 +110,12 @@ impl CompactListing {
/// chosen. /// chosen.
fn refresh_mailbox(&mut self, context: &mut Context) { fn refresh_mailbox(&mut self, context: &mut Context) {
self.dirty = true; self.dirty = true;
if self.cursor_pos == self.new_cursor_pos {
self.view.update(context);
} else {
self.view = ThreadView::new(self.cursor_pos, None, context);
}
if !(self.cursor_pos.0 == self.new_cursor_pos.0 if !(self.cursor_pos.0 == self.new_cursor_pos.0
&& self.cursor_pos.1 == self.new_cursor_pos.1) && self.cursor_pos.1 == self.new_cursor_pos.1)
{ {
@ -384,23 +390,13 @@ impl Component for CompactListing {
return; return;
} }
/* Render the mail body in a pager */ self.view.draw(grid, area, context);
if !self.dirty {
if let Some(v) = self.view.as_mut() {
v.draw(grid, area, context);
}
return;
}
self.view = Some(ThreadView::new(self.cursor_pos, None, context));
self.view.as_mut().unwrap().draw(grid, area, context);
} }
self.dirty = false; self.dirty = false;
} }
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool { fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
if let Some(ref mut v) = self.view { if self.unfocused && self.view.process_event(event, context) {
if v.process_event(event, context) { return true;
return true;
}
} }
let shortcuts = self.get_shortcuts(context); let shortcuts = self.get_shortcuts(context);
@ -420,6 +416,7 @@ impl Component for CompactListing {
return true; return true;
} }
UIEventType::Input(ref k) if !self.unfocused && *k == shortcuts["open_thread"] => { UIEventType::Input(ref k) if !self.unfocused && *k == shortcuts["open_thread"] => {
self.view = ThreadView::new(self.cursor_pos, None, context);
self.unfocused = true; self.unfocused = true;
self.dirty = true; self.dirty = true;
return true; return true;
@ -435,7 +432,6 @@ impl Component for CompactListing {
UIEventType::Input(ref k) if self.unfocused && *k == shortcuts["exit_thread"] => { UIEventType::Input(ref k) if self.unfocused && *k == shortcuts["exit_thread"] => {
self.unfocused = false; self.unfocused = false;
self.dirty = true; self.dirty = true;
self.view = None;
return true; return true;
} }
UIEventType::Input(Key::Char(k @ 'J')) | UIEventType::Input(Key::Char(k @ 'K')) => { UIEventType::Input(Key::Char(k @ 'J')) | UIEventType::Input(Key::Char(k @ 'K')) => {
@ -485,7 +481,6 @@ impl Component for CompactListing {
return true; return true;
} }
UIEventType::RefreshMailbox(_) => { UIEventType::RefreshMailbox(_) => {
self.view = None;
self.dirty = true; self.dirty = true;
} }
UIEventType::MailboxUpdate((ref idxa, ref idxf)) if *idxa == self.new_cursor_pos.0 && *idxf == self.new_cursor_pos.1 => { UIEventType::MailboxUpdate((ref idxa, ref idxf)) if *idxa == self.new_cursor_pos.0 && *idxf == self.new_cursor_pos.1 => {
@ -538,21 +533,21 @@ impl Component for CompactListing {
false false
} }
fn is_dirty(&self) -> bool { fn is_dirty(&self) -> bool {
self.dirty || self.view.as_ref().map(|p| p.is_dirty()).unwrap_or(false) self.dirty || if self.unfocused { self.view.is_dirty() } else { false }
} }
fn set_dirty(&mut self) { fn set_dirty(&mut self) {
if let Some(p) = self.view.as_mut() { if self.unfocused {
p.set_dirty(); self.view.set_dirty();
} }
self.dirty = true; self.dirty = true;
} }
fn get_shortcuts(&self, context: &Context) -> ShortcutMap { fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
let mut map = self let mut map = if self.unfocused {
.view self.view.get_shortcuts(context)
.as_ref() } else {
.map(|p| p.get_shortcuts(context)) ShortcutMap::default()
.unwrap_or_default(); };
let config_map = context.settings.shortcuts.compact_listing.key_values(); let config_map = context.settings.shortcuts.compact_listing.key_values();
map.insert( map.insert(

View File

@ -75,6 +75,40 @@ impl ThreadView {
view.new_cursor_pos = view.new_expanded_pos; view.new_cursor_pos = view.new_expanded_pos;
view view
} }
pub fn update(&mut self, context: &Context) {
if self.entries.is_empty() {
return;
}
let old_focused_entry = if self.entries.len() > self.cursor_pos {
Some(self.entries.remove(self.cursor_pos))
} else { None };
let old_expanded_entry = if self.entries.len() > self.expanded_pos {
Some(self.entries.remove(self.expanded_pos))
} else {
None
};
// FIXME 2018
let expanded_pos = self.expanded_pos;
self.initiate(Some(expanded_pos), context);
if let Some(old_focused_entry) = old_focused_entry {
if let Some(new_entry_idx) = self.entries.iter().position(
|e| e.msg_idx == old_focused_entry.msg_idx ||
(e.index.1 == old_focused_entry.index.1 && e.index.2 == old_focused_entry.index.2)) {
self.cursor_pos = new_entry_idx;
}
}
if let Some(old_expanded_entry) = old_expanded_entry {
if let Some(new_entry_idx) = self.entries.iter().position(
|e| e.msg_idx == old_expanded_entry.msg_idx ||
(e.index.1 == old_expanded_entry.index.1 && e.index.2 == old_expanded_entry.index.2)) {
self.expanded_pos = new_entry_idx;
}
}
self.set_dirty();
}
fn initiate(&mut self, expanded_idx: Option<usize>, context: &Context) { fn initiate(&mut self, expanded_idx: Option<usize>, context: &Context) {
/* stack to push thread messages in order in order to pop and print them later */ /* stack to push thread messages in order in order to pop and print them later */
let mailbox = &context.accounts[self.coordinates.0][self.coordinates.1] let mailbox = &context.accounts[self.coordinates.0][self.coordinates.1]