/*
* meli - ui crate.
*
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use super::*;
use std::cmp;
use std::ops::Index;
#[derive(Debug, Clone)]
struct ThreadEntry {
index: (usize, usize, usize),
/// (indentation, thread_node index, line number in listing)
indentation: usize,
msg_hash: EnvelopeHash,
seen: bool,
dirty: bool,
hidden: bool,
heading: String,
}
#[derive(Debug, Default)]
pub struct ThreadView {
new_cursor_pos: usize,
cursor_pos: usize,
expanded_pos: usize,
new_expanded_pos: usize,
reversed: bool,
coordinates: (usize, usize, usize),
mailview: MailView,
show_mailview: bool,
entries: Vec<ThreadEntry>,
visible_entries: Vec<Vec<usize>>,
dirty: bool,
content: CellBuffer,
initiated: bool,
id: ComponentId,
}
impl ThreadView {
const DESCRIPTION: &'static str = "thread view";
/*
* coordinates: (account index, mailbox index, root set thread_node index)
* expanded_idx: optional position of expanded entry when we render the threadview. Default
* expanded message is the last one.
* context: current context
*/
pub fn new(
coordinates: (usize, usize, usize),
expanded_idx: Option<usize>,
context: &Context,
) -> Self {
let mut view = ThreadView {
reversed: false,
initiated: false,
coordinates,
mailview: MailView::default(),
show_mailview: true,
entries: Vec::new(),
cursor_pos: 1,
new_cursor_pos: 0,
dirty: true,
id: ComponentId::new_v4(),
..Default::default()
};
view.initiate(expanded_idx, context);
view.new_cursor_pos = view.new_expanded_pos;
view
}
pub fn update(&mut self, context: &Context) {
if self.entries.is_empty() {
return;
}
let old_entries = self.entries.clone();
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
};
let expanded_pos = self.expanded_pos;
self.initiate(Some(expanded_pos), context);
let mut old_cursor = 0;
let mut new_cursor = 0;
loop {
if old_cursor >= old_entries.len() || new_cursor >= self.entries.len() {
break;
}
if old_entries[old_cursor].msg_hash == self.entries[new_cursor].msg_hash
|| old_entries[old_cursor].index == self.entries[new_cursor].index
|| old_entries[old_cursor].heading == self.entries[new_cursor].heading
{
self.entries[new_cursor].hidden = old_entries[old_cursor].hidden;
old_cursor += 1;
new_cursor += 1;
} else {
new_cursor += 1;
}
self.recalc_visible_entries();
}
if let Some(old_focused_entry) = old_focused_entry {
if let Some(new_entry_idx) = self.entries.iter().position(|e| {
e.msg_hash == old_focused_entry.msg_hash
|| (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_hash == old_expanded_entry.msg_hash
|| (e.index.1 == old_expanded_entry.