/*
* 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 crate::components::utilities::PageMovement;
use std::cmp;
const INDENTATION_COLORS: &'static [u8] = &[
69, // CornflowerBlue
196, // Red1
175, // Pink3
220, // Gold1
172, // Orange3
072, // CadetBlue
];
#[derive(Debug, Clone)]
struct ThreadEntry {
index: (usize, ThreadNodeHash, 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, Clone)]
pub struct ThreadView {
new_cursor_pos: usize,
cursor_pos: usize,
expanded_pos: usize,
new_expanded_pos: usize,
reversed: bool,
coordinates: (usize, usize, usize),
thread_group: ThreadHash,
mailview: MailView,
show_mailview: bool,
show_thread: bool,
entries: Vec<ThreadEntry>,
visible_entries: Vec<Vec<usize>>,
movement: Option<PageMovement>,
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_hash: 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),
thread_group: ThreadHash,
expanded_hash: Option<ThreadNodeHash>,
context: &Context,
) -> Self {
let mut view = ThreadView {
reversed: false,
initiated: false,
coordinates,
thread_group,
mailview: MailView::default(),
show_mailview: true,
show_thread: true,
entries: Vec::new(),
cursor_pos: 1,
new_cursor_pos: 0,
dirty: true,
id: ComponentId::new_v4(),
..Default::default()
};
view.initiate(expanded_hash, 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_hash = old_expanded_entry.as_ref().map(|e| e.index.1);
self.initiate(expanded_hash, context);
let mut old_cursor = 0;
let mut new_cursor = 0;
loop {
if old_cursor >= old_entries