2018-09-07 09:44:37 +03:00
|
|
|
/*
|
2020-02-04 15:52:12 +02:00
|
|
|
* meli
|
2018-09-07 09:44:37 +03:00
|
|
|
*
|
|
|
|
* 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::*;
|
2019-06-18 21:13:58 +03:00
|
|
|
use crate::components::utilities::PageMovement;
|
2018-09-07 09:44:37 +03:00
|
|
|
|
|
|
|
const MAX_COLS: usize = 500;
|
|
|
|
|
|
|
|
/// A list of all mail (`Envelope`s) in a `Mailbox`. On `\n` it opens the `Envelope` content in a
|
|
|
|
/// `MailView`.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ThreadListing {
|
|
|
|
/// (x, y, z): x is accounts, y is folders, z is index inside a folder.
|
2020-02-08 23:42:31 +02:00
|
|
|
cursor_pos: (usize, FolderHash, usize),
|
|
|
|
new_cursor_pos: (usize, FolderHash, usize),
|
2018-09-07 09:44:37 +03:00
|
|
|
length: usize,
|
|
|
|
sort: (SortField, SortOrder),
|
|
|
|
subsort: (SortField, SortOrder),
|
|
|
|
/// Cache current view.
|
|
|
|
content: CellBuffer,
|
2020-02-08 13:40:47 +02:00
|
|
|
color_cache: ColorCache,
|
2018-10-14 19:49:16 +03:00
|
|
|
|
2020-01-18 02:00:39 +02:00
|
|
|
row_updates: SmallVec<[ThreadHash; 8]>,
|
2019-02-10 21:06:30 +02:00
|
|
|
locations: Vec<EnvelopeHash>,
|
2018-09-07 09:44:37 +03:00
|
|
|
/// If we must redraw on next redraw event
|
|
|
|
dirty: bool,
|
2019-06-05 01:26:23 +03:00
|
|
|
/// If `self.view` is focused or not.
|
2018-09-07 09:44:37 +03:00
|
|
|
unfocused: bool,
|
2018-10-14 19:49:16 +03:00
|
|
|
initialised: bool,
|
2018-09-07 09:44:37 +03:00
|
|
|
view: Option<MailView>,
|
2019-05-16 00:58:40 +03:00
|
|
|
movement: Option<PageMovement>,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
|
|
|
|
2019-12-01 16:09:39 +02:00
|
|
|
impl MailListingTrait for ThreadListing {
|
2020-01-18 02:00:39 +02:00
|
|
|
fn row_updates(&mut self) -> &mut SmallVec<[ThreadHash; 8]> {
|
2019-12-01 16:09:39 +02:00
|
|
|
&mut self.row_updates
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:00:39 +02:00
|
|
|
fn get_focused_items(&self, _context: &Context) -> SmallVec<[ThreadHash; 8]> {
|
2020-01-08 17:04:44 +02:00
|
|
|
SmallVec::new()
|
2019-12-01 22:28:50 +02:00
|
|
|
}
|
2020-02-06 01:51:20 +02:00
|
|
|
|
|
|
|
/// Fill the `self.content` `CellBuffer` with the contents of the account folder the user has
|
|
|
|
/// chosen.
|
|
|
|
fn refresh_mailbox(&mut self, context: &mut Context, _force: bool) {
|
|
|
|
self.dirty = true;
|
|
|
|
if !(self.cursor_pos.0 == self.new_cursor_pos.0
|
|
|
|
&& self.cursor_pos.1 == self.new_cursor_pos.1)
|
|
|
|
{
|
|
|
|
self.cursor_pos.2 = 0;
|
|
|
|
self.new_cursor_pos.2 = 0;
|
|
|
|
}
|
|
|
|
self.cursor_pos.1 = self.new_cursor_pos.1;
|
|
|
|
self.cursor_pos.0 = self.new_cursor_pos.0;
|
|
|
|
|
2020-02-08 23:33:18 +02:00
|
|
|
self.color_cache = ColorCache {
|
|
|
|
unseen: crate::conf::value(context, "mail.listing.plain.unseen"),
|
|
|
|
highlighted: crate::conf::value(context, "mail.listing.plain.highlighted"),
|
|
|
|
even: crate::conf::value(context, "mail.listing.plain.even"),
|
|
|
|
odd: crate::conf::value(context, "mail.listing.plain.odd"),
|
|
|
|
selected: crate::conf::value(context, "mail.listing.plain.selected"),
|
|
|
|
attachment_flag: crate::conf::value(context, "mail.listing.attachment_flag"),
|
|
|
|
thread_snooze_flag: crate::conf::value(context, "mail.listing.thread_snooze_flag"),
|
|
|
|
..self.color_cache
|
|
|
|
};
|
2020-02-09 20:47:36 +02:00
|
|
|
if !context.settings.terminal.use_color() {
|
2020-02-08 23:33:18 +02:00
|
|
|
self.color_cache.highlighted.attrs |= Attr::Reverse;
|
|
|
|
}
|
|
|
|
|
2020-02-06 01:51:20 +02:00
|
|
|
// Get mailbox as a reference.
|
|
|
|
//
|
2020-02-08 23:42:31 +02:00
|
|
|
match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) {
|
2020-02-06 01:51:20 +02:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(_) => {
|
2020-02-08 23:33:18 +02:00
|
|
|
let default_cell = {
|
|
|
|
let mut ret = Cell::with_char(' ');
|
|
|
|
ret.set_fg(self.color_cache.theme_default.fg)
|
|
|
|
.set_bg(self.color_cache.theme_default.bg)
|
|
|
|
.set_attrs(self.color_cache.theme_default.attrs);
|
|
|
|
ret
|
|
|
|
};
|
2020-02-06 01:51:20 +02:00
|
|
|
let message: String =
|
2020-02-08 23:33:18 +02:00
|
|
|
context.accounts[self.cursor_pos.0][self.cursor_pos.1].to_string();
|
|
|
|
self.content =
|
|
|
|
CellBuffer::new_with_context(message.len(), 1, default_cell, context);
|
2020-02-06 01:51:20 +02:00
|
|
|
self.length = 0;
|
|
|
|
write_string_to_grid(
|
|
|
|
message.as_str(),
|
|
|
|
&mut self.content,
|
2020-02-08 23:33:18 +02:00
|
|
|
self.color_cache.theme_default.fg,
|
|
|
|
self.color_cache.theme_default.bg,
|
|
|
|
self.color_cache.theme_default.attrs,
|
2020-02-06 01:51:20 +02:00
|
|
|
((0, 0), (MAX_COLS - 1, 0)),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let account = &context.accounts[self.cursor_pos.0];
|
|
|
|
let mailbox = account[self.cursor_pos.1].unwrap();
|
|
|
|
|
|
|
|
let threads = &account.collection.threads[&mailbox.folder.hash()];
|
|
|
|
self.length = threads.len();
|
|
|
|
self.locations.clear();
|
2020-02-08 23:33:18 +02:00
|
|
|
let default_cell = {
|
|
|
|
let mut ret = Cell::with_char(' ');
|
|
|
|
ret.set_fg(self.color_cache.theme_default.fg)
|
|
|
|
.set_bg(self.color_cache.theme_default.bg)
|
|
|
|
.set_attrs(self.color_cache.theme_default.attrs);
|
|
|
|
ret
|
|
|
|
};
|
2020-02-06 01:51:20 +02:00
|
|
|
if self.length == 0 {
|
|
|
|
let message = format!("Folder `{}` is empty.", mailbox.folder.name());
|
2020-02-08 23:33:18 +02:00
|
|
|
self.content = CellBuffer::new_with_context(message.len(), 1, default_cell, context);
|
2020-02-06 01:51:20 +02:00
|
|
|
write_string_to_grid(
|
|
|
|
&message,
|
|
|
|
&mut self.content,
|
2020-02-08 23:33:18 +02:00
|
|
|
self.color_cache.theme_default.fg,
|
|
|
|
self.color_cache.theme_default.bg,
|
|
|
|
self.color_cache.theme_default.attrs,
|
2020-02-06 01:51:20 +02:00
|
|
|
((0, 0), (message.len() - 1, 0)),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-08 23:33:18 +02:00
|
|
|
self.content =
|
|
|
|
CellBuffer::new_with_context(MAX_COLS, self.length + 1, default_cell, context);
|
2020-02-06 01:51:20 +02:00
|
|
|
|
|
|
|
let mut indentations: Vec<bool> = Vec::with_capacity(6);
|
|
|
|
let mut thread_idx = 0; // needed for alternate thread colors
|
|
|
|
/* Draw threaded view. */
|
|
|
|
threads.sort_by(self.sort, self.subsort, &account.collection.envelopes);
|
|
|
|
let thread_nodes: &FnvHashMap<ThreadNodeHash, ThreadNode> = &threads.thread_nodes();
|
|
|
|
let mut iter = threads.threads_iter().peekable();
|
|
|
|
/* This is just a desugared for loop so that we can use .peek() */
|
|
|
|
let mut idx = 0;
|
|
|
|
while let Some((indentation, thread_node_hash, has_sibling)) = iter.next() {
|
|
|
|
let thread_node = &thread_nodes[&thread_node_hash];
|
|
|
|
|
|
|
|
if indentation == 0 {
|
|
|
|
thread_idx += 1;
|
|
|
|
}
|
|
|
|
if thread_node.has_message() {
|
|
|
|
let envelope: EnvelopeRef =
|
|
|
|
account.collection.get_env(thread_node.message().unwrap());
|
|
|
|
self.locations.push(envelope.hash());
|
|
|
|
let fg_color = if !envelope.is_seen() {
|
|
|
|
Color::Byte(0)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
let bg_color = if !envelope.is_seen() {
|
|
|
|
Color::Byte(251)
|
|
|
|
} else if thread_idx % 2 == 0 {
|
|
|
|
Color::Byte(236)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
let (x, _) = write_string_to_grid(
|
|
|
|
&ThreadListing::make_thread_entry(
|
|
|
|
&envelope,
|
|
|
|
idx,
|
|
|
|
indentation,
|
|
|
|
thread_node_hash,
|
|
|
|
threads,
|
|
|
|
&indentations,
|
|
|
|
has_sibling,
|
|
|
|
),
|
|
|
|
&mut self.content,
|
|
|
|
fg_color,
|
|
|
|
bg_color,
|
|
|
|
Attr::Default,
|
|
|
|
((0, idx), (MAX_COLS - 1, idx)),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
for x in x..MAX_COLS {
|
|
|
|
self.content[(x, idx)].set_ch(' ');
|
|
|
|
self.content[(x, idx)].set_bg(bg_color);
|
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match iter.peek() {
|
|
|
|
Some((x, _, _)) if *x > indentation => {
|
|
|
|
if has_sibling {
|
|
|
|
indentations.push(true);
|
|
|
|
} else {
|
|
|
|
indentations.push(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some((x, _, _)) if *x < indentation => {
|
|
|
|
for _ in 0..(indentation - *x) {
|
|
|
|
indentations.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 16:09:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:58:42 +03:00
|
|
|
impl ListingTrait for ThreadListing {
|
2020-02-08 23:42:31 +02:00
|
|
|
fn coordinates(&self) -> (usize, FolderHash) {
|
2019-12-01 15:28:15 +02:00
|
|
|
(self.new_cursor_pos.0, self.new_cursor_pos.1)
|
2019-04-03 16:58:42 +03:00
|
|
|
}
|
2020-02-08 23:42:31 +02:00
|
|
|
fn set_coordinates(&mut self, coordinates: (usize, FolderHash)) {
|
2019-04-03 16:58:42 +03:00
|
|
|
self.new_cursor_pos = (coordinates.0, coordinates.1, 0);
|
2019-10-07 16:47:05 +03:00
|
|
|
self.unfocused = false;
|
2020-02-06 01:51:20 +02:00
|
|
|
self.view = None;
|
2019-10-07 16:47:05 +03:00
|
|
|
self.locations.clear();
|
2019-12-01 16:09:39 +02:00
|
|
|
self.row_updates.clear();
|
2019-10-07 16:47:05 +03:00
|
|
|
self.initialised = false;
|
2019-04-03 16:58:42 +03:00
|
|
|
}
|
2019-06-21 12:02:11 +03:00
|
|
|
fn draw_list(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
if self.cursor_pos.1 != self.new_cursor_pos.1 || self.cursor_pos.0 != self.new_cursor_pos.0
|
|
|
|
{
|
2020-02-06 01:51:20 +02:00
|
|
|
self.refresh_mailbox(context, false);
|
2019-06-21 12:02:11 +03:00
|
|
|
}
|
|
|
|
let upper_left = upper_left!(area);
|
|
|
|
let bottom_right = bottom_right!(area);
|
|
|
|
if self.length == 0 {
|
2020-02-08 13:40:47 +02:00
|
|
|
clear_area(grid, area, self.color_cache.theme_default);
|
2019-06-21 12:02:11 +03:00
|
|
|
copy_area(grid, &self.content, area, ((0, 0), (MAX_COLS - 1, 0)));
|
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let rows = get_y(bottom_right) - get_y(upper_left) + 1;
|
2020-02-06 21:49:21 +02:00
|
|
|
if rows == 0 {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-02 00:15:23 +03:00
|
|
|
if let Some(mvm) = self.movement.take() {
|
|
|
|
match mvm {
|
2019-11-12 22:14:44 +02:00
|
|
|
PageMovement::Up(amount) => {
|
|
|
|
self.new_cursor_pos.2 = self.new_cursor_pos.2.saturating_sub(amount);
|
2019-08-02 00:15:23 +03:00
|
|
|
}
|
2019-11-12 22:14:44 +02:00
|
|
|
PageMovement::PageUp(multiplier) => {
|
|
|
|
self.new_cursor_pos.2 = self.new_cursor_pos.2.saturating_sub(rows * multiplier);
|
|
|
|
}
|
|
|
|
PageMovement::Down(amount) => {
|
|
|
|
if self.new_cursor_pos.2 + amount + 1 < self.length {
|
|
|
|
self.new_cursor_pos.2 += amount;
|
|
|
|
} else {
|
|
|
|
self.new_cursor_pos.2 = self.length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PageMovement::PageDown(multiplier) => {
|
|
|
|
if self.new_cursor_pos.2 + rows * multiplier + 1 < self.length {
|
|
|
|
self.new_cursor_pos.2 += rows * multiplier;
|
|
|
|
} else if self.new_cursor_pos.2 + rows * multiplier > self.length {
|
2019-08-02 00:15:23 +03:00
|
|
|
self.new_cursor_pos.2 = self.length - 1;
|
|
|
|
} else {
|
|
|
|
self.new_cursor_pos.2 = (self.length / rows) * rows;
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 22:14:44 +02:00
|
|
|
PageMovement::Right(_) | PageMovement::Left(_) => {}
|
2019-08-02 00:15:23 +03:00
|
|
|
PageMovement::Home => {
|
|
|
|
self.new_cursor_pos.2 = 0;
|
|
|
|
}
|
|
|
|
PageMovement::End => {
|
2019-11-12 22:14:44 +02:00
|
|
|
self.new_cursor_pos.2 = self.length - 1;
|
2019-08-02 00:15:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:02:11 +03:00
|
|
|
let prev_page_no = (self.cursor_pos.2).wrapping_div(rows);
|
|
|
|
let page_no = (self.new_cursor_pos.2).wrapping_div(rows);
|
|
|
|
|
|
|
|
let top_idx = page_no * rows;
|
|
|
|
if !self.initialised {
|
|
|
|
self.initialised = false;
|
|
|
|
copy_area(
|
|
|
|
grid,
|
|
|
|
&self.content,
|
|
|
|
area,
|
|
|
|
((0, top_idx), (MAX_COLS - 1, self.length)),
|
|
|
|
);
|
|
|
|
self.highlight_line(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
set_y(upper_left, get_y(upper_left) + (self.cursor_pos.2 % rows)),
|
|
|
|
set_y(bottom_right, get_y(upper_left) + (self.cursor_pos.2 % rows)),
|
|
|
|
),
|
|
|
|
self.cursor_pos.2,
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
/* If cursor position has changed, remove the highlight from the previous position and
|
|
|
|
* apply it in the new one. */
|
|
|
|
if self.cursor_pos.2 != self.new_cursor_pos.2 && prev_page_no == page_no {
|
|
|
|
let old_cursor_pos = self.cursor_pos;
|
|
|
|
self.cursor_pos = self.new_cursor_pos;
|
|
|
|
for idx in &[old_cursor_pos.2, self.new_cursor_pos.2] {
|
|
|
|
if *idx >= self.length {
|
|
|
|
continue; //bounds check
|
|
|
|
}
|
|
|
|
let new_area = (
|
|
|
|
set_y(upper_left, get_y(upper_left) + (*idx % rows)),
|
|
|
|
set_y(bottom_right, get_y(upper_left) + (*idx % rows)),
|
|
|
|
);
|
|
|
|
self.highlight_line(grid, new_area, *idx, context);
|
|
|
|
context.dirty_areas.push_back(new_area);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if self.cursor_pos != self.new_cursor_pos {
|
|
|
|
self.cursor_pos = self.new_cursor_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Page_no has changed, so draw new page */
|
|
|
|
copy_area(
|
|
|
|
grid,
|
|
|
|
&self.content,
|
|
|
|
area,
|
|
|
|
((0, top_idx), (MAX_COLS - 1, self.length)),
|
|
|
|
);
|
|
|
|
self.highlight_line(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
set_y(upper_left, get_y(upper_left) + (self.cursor_pos.2 % rows)),
|
|
|
|
set_y(bottom_right, get_y(upper_left) + (self.cursor_pos.2 % rows)),
|
|
|
|
),
|
|
|
|
self.cursor_pos.2,
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_line(&mut self, grid: &mut CellBuffer, area: Area, idx: usize, context: &Context) {
|
2019-07-28 18:52:45 +03:00
|
|
|
let mailbox = if context.accounts[self.cursor_pos.0][self.cursor_pos.1].is_available() {
|
|
|
|
context.accounts[self.cursor_pos.0][self.cursor_pos.1].unwrap()
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2019-06-21 12:02:11 +03:00
|
|
|
if mailbox.is_empty() || mailbox.len() <= idx {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.locations[idx] != 0 {
|
2019-11-02 12:14:31 +02:00
|
|
|
let envelope: EnvelopeRef = context.accounts[self.cursor_pos.0]
|
|
|
|
.collection
|
|
|
|
.get_env(self.locations[idx]);
|
2019-06-21 12:02:11 +03:00
|
|
|
|
|
|
|
let fg_color = if !envelope.is_seen() {
|
|
|
|
Color::Byte(0)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
let bg_color = if self.cursor_pos.2 == idx {
|
|
|
|
Color::Byte(246)
|
|
|
|
} else if !envelope.is_seen() {
|
|
|
|
Color::Byte(251)
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
Color::Byte(236)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
change_colors(grid, area, fg_color, bg_color);
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 17:41:06 +03:00
|
|
|
|
|
|
|
fn set_movement(&mut self, mvm: PageMovement) {
|
|
|
|
self.movement = Some(mvm);
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2019-10-12 17:41:06 +03:00
|
|
|
}
|
2019-04-03 16:58:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
impl fmt::Display for ThreadListing {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "mail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ThreadListing {
|
2020-02-08 23:42:31 +02:00
|
|
|
pub fn new(coordinates: (usize, FolderHash)) -> Self {
|
2018-09-07 09:44:37 +03:00
|
|
|
ThreadListing {
|
|
|
|
cursor_pos: (0, 1, 0),
|
2020-02-08 23:42:31 +02:00
|
|
|
new_cursor_pos: (coordinates.0, coordinates.1, 0),
|
2018-09-07 09:44:37 +03:00
|
|
|
length: 0,
|
|
|
|
sort: (Default::default(), Default::default()),
|
|
|
|
subsort: (Default::default(), Default::default()),
|
2020-02-08 23:33:18 +02:00
|
|
|
content: CellBuffer::new(0, 0, Cell::with_char(' ')),
|
2020-02-08 13:40:47 +02:00
|
|
|
color_cache: ColorCache::default(),
|
2020-01-08 17:04:44 +02:00
|
|
|
row_updates: SmallVec::new(),
|
2018-10-14 19:49:16 +03:00
|
|
|
locations: Vec::new(),
|
2018-09-07 09:44:37 +03:00
|
|
|
dirty: true,
|
|
|
|
unfocused: false,
|
|
|
|
view: None,
|
2018-10-14 19:49:16 +03:00
|
|
|
initialised: false,
|
2019-05-16 00:58:40 +03:00
|
|
|
movement: None,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_line_self(&mut self, idx: usize, context: &Context) {
|
2019-07-28 18:52:45 +03:00
|
|
|
let mailbox = if context.accounts[self.cursor_pos.0][self.cursor_pos.1].is_available() {
|
|
|
|
context.accounts[self.cursor_pos.0][self.cursor_pos.1].unwrap()
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2019-02-25 11:11:56 +02:00
|
|
|
if mailbox.is_empty() {
|
2018-10-14 19:49:16 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-02-10 21:06:30 +02:00
|
|
|
if self.locations[idx] != 0 {
|
2019-11-02 12:14:31 +02:00
|
|
|
let envelope: EnvelopeRef = context.accounts[self.cursor_pos.0]
|
|
|
|
.collection
|
|
|
|
.get_env(self.locations[idx]);
|
2018-09-07 09:44:37 +03:00
|
|
|
|
2018-10-14 19:49:16 +03:00
|
|
|
let fg_color = if !envelope.is_seen() {
|
|
|
|
Color::Byte(0)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
let bg_color = if !envelope.is_seen() {
|
|
|
|
Color::Byte(251)
|
|
|
|
} else if idx % 2 == 0 {
|
|
|
|
Color::Byte(236)
|
|
|
|
} else {
|
|
|
|
Color::Default
|
|
|
|
};
|
|
|
|
change_colors(
|
|
|
|
&mut self.content,
|
|
|
|
((0, idx), (MAX_COLS - 1, idx)),
|
|
|
|
fg_color,
|
|
|
|
bg_color,
|
|
|
|
);
|
|
|
|
}
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_thread_entry(
|
|
|
|
envelope: &Envelope,
|
|
|
|
idx: usize,
|
|
|
|
indent: usize,
|
2020-01-18 02:00:39 +02:00
|
|
|
node_idx: ThreadNodeHash,
|
2018-09-07 09:44:37 +03:00
|
|
|
threads: &Threads,
|
|
|
|
indentations: &[bool],
|
2019-05-13 01:09:23 +03:00
|
|
|
has_sibling: bool,
|
2018-09-07 09:44:37 +03:00
|
|
|
//op: Box<BackendOp>,
|
|
|
|
) -> String {
|
2019-05-14 21:47:47 +03:00
|
|
|
let thread_node = &threads[&node_idx];
|
2018-09-07 09:44:37 +03:00
|
|
|
let has_parent = thread_node.has_parent();
|
|
|
|
let show_subject = thread_node.show_subject();
|
|
|
|
|
2019-05-13 01:09:23 +03:00
|
|
|
let mut s = format!("{}{}{} ", idx, " ", ThreadListing::format_date(&envelope));
|
2018-09-07 09:44:37 +03:00
|
|
|
for i in 0..indent {
|
|
|
|
if indentations.len() > i && indentations[i] {
|
|
|
|
s.push('│');
|
2019-05-13 01:09:23 +03:00
|
|
|
} else if indentations.len() > i {
|
2018-09-07 09:44:37 +03:00
|
|
|
s.push(' ');
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
s.push(' ');
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 01:09:23 +03:00
|
|
|
if indent > 0 && (has_sibling || has_parent) {
|
2018-09-07 09:44:37 +03:00
|
|
|
if has_sibling && has_parent {
|
|
|
|
s.push('├');
|
|
|
|
} else if has_sibling {
|
|
|
|
s.push('┬');
|
|
|
|
} else {
|
|
|
|
s.push('└');
|
|
|
|
}
|
|
|
|
s.push('─');
|
|
|
|
s.push('>');
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:59:00 +02:00
|
|
|
s.push_str(if envelope.has_attachments() {
|
|
|
|
"📎"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
});
|
2018-09-07 09:44:37 +03:00
|
|
|
if show_subject {
|
|
|
|
s.push_str(&format!("{:.85}", envelope.subject()));
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
fn format_date(envelope: &Envelope) -> String {
|
|
|
|
let d = std::time::UNIX_EPOCH + std::time::Duration::from_secs(envelope.date());
|
2019-03-14 12:19:25 +02:00
|
|
|
let now: std::time::Duration = std::time::SystemTime::now()
|
|
|
|
.duration_since(d)
|
|
|
|
.unwrap_or_else(|_| std::time::Duration::new(std::u64::MAX, 0));
|
2018-09-07 09:44:37 +03:00
|
|
|
match now.as_secs() {
|
|
|
|
n if n < 10 * 60 * 60 => format!("{} hours ago{}", n / (60 * 60), " ".repeat(8)),
|
|
|
|
n if n < 24 * 60 * 60 => format!("{} hours ago{}", n / (60 * 60), " ".repeat(7)),
|
|
|
|
n if n < 4 * 24 * 60 * 60 => {
|
|
|
|
format!("{} days ago{}", n / (24 * 60 * 60), " ".repeat(9))
|
|
|
|
}
|
2020-01-06 16:10:36 +02:00
|
|
|
_ => melib::datetime::timestamp_to_string(envelope.datetime(), None),
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for ThreadListing {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
if !self.unfocused {
|
|
|
|
if !self.is_dirty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.dirty = false;
|
|
|
|
/* Draw the entire list */
|
|
|
|
self.draw_list(grid, area, context);
|
|
|
|
} else {
|
2019-02-18 23:14:06 +02:00
|
|
|
self.cursor_pos = self.new_cursor_pos;
|
2018-09-07 09:44:37 +03:00
|
|
|
let upper_left = upper_left!(area);
|
|
|
|
let bottom_right = bottom_right!(area);
|
|
|
|
if self.length == 0 && self.dirty {
|
2020-02-08 13:40:47 +02:00
|
|
|
clear_area(grid, area, self.color_cache.theme_default);
|
2018-09-07 09:44:37 +03:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Render the mail body in a pager, basically copy what HSplit does */
|
|
|
|
let total_rows = get_y(bottom_right) - get_y(upper_left);
|
|
|
|
let pager_ratio = context.runtime_settings.pager.pager_ratio;
|
|
|
|
let bottom_entity_rows = (pager_ratio * total_rows) / 100;
|
|
|
|
|
|
|
|
if bottom_entity_rows > total_rows {
|
2020-02-08 13:40:47 +02:00
|
|
|
clear_area(grid, area, self.color_cache.theme_default);
|
2018-09-07 09:44:37 +03:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-14 19:49:16 +03:00
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
let idx = self.cursor_pos.2;
|
2018-10-14 19:49:16 +03:00
|
|
|
|
2019-02-10 21:06:30 +02:00
|
|
|
let has_message: bool = self.locations[self.new_cursor_pos.2] > 0;
|
|
|
|
if !has_message {
|
|
|
|
self.dirty = false;
|
|
|
|
/* Draw the entire list */
|
|
|
|
return self.draw_list(grid, area, context);
|
2018-10-14 19:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark message as read */
|
2018-09-07 09:44:37 +03:00
|
|
|
let must_highlight = {
|
|
|
|
if self.length == 0 {
|
|
|
|
false
|
|
|
|
} else {
|
2019-11-02 12:14:31 +02:00
|
|
|
let account = &context.accounts[self.cursor_pos.0];
|
|
|
|
let envelope: EnvelopeRef = account
|
|
|
|
.collection
|
|
|
|
.get_env(self.locations[self.cursor_pos.2]);
|
2019-09-09 11:54:47 +03:00
|
|
|
envelope.is_seen()
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
|
|
|
};
|
2019-07-18 20:16:51 +03:00
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
if must_highlight {
|
|
|
|
self.highlight_line_self(idx, context);
|
|
|
|
}
|
2019-07-18 20:16:51 +03:00
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
let mid = get_y(upper_left) + total_rows - bottom_entity_rows;
|
|
|
|
self.draw_list(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left,
|
|
|
|
(get_x(bottom_right), get_y(upper_left) + mid - 1),
|
|
|
|
),
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
if self.length == 0 {
|
|
|
|
self.dirty = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
/* TODO: Move the box drawing business in separate functions */
|
|
|
|
if get_x(upper_left) > 0 && grid[(get_x(upper_left) - 1, mid)].ch() == VERT_BOUNDARY
|
|
|
|
{
|
|
|
|
grid[(get_x(upper_left) - 1, mid)].set_ch(LIGHT_VERTICAL_AND_RIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in get_x(upper_left)..=get_x(bottom_right) {
|
|
|
|
grid[(i, mid)].set_ch(HORZ_BOUNDARY);
|
|
|
|
}
|
|
|
|
context
|
|
|
|
.dirty_areas
|
|
|
|
.push_back((set_y(upper_left, mid), set_y(bottom_right, mid)));
|
|
|
|
}
|
|
|
|
// TODO: Make headers view configurable
|
|
|
|
|
|
|
|
if !self.dirty {
|
|
|
|
if let Some(v) = self.view.as_mut() {
|
|
|
|
v.draw(grid, (set_y(upper_left, mid + 1), bottom_right), context);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-02-11 14:55:29 +02:00
|
|
|
|
|
|
|
let coordinates = (
|
|
|
|
self.cursor_pos.0,
|
|
|
|
self.cursor_pos.1,
|
|
|
|
self.locations[self.cursor_pos.2],
|
2019-03-14 12:19:25 +02:00
|
|
|
);
|
2019-02-11 14:55:29 +02:00
|
|
|
|
2019-06-05 01:26:23 +03:00
|
|
|
if let Some(ref mut v) = self.view {
|
|
|
|
v.update(coordinates);
|
|
|
|
} else {
|
2020-02-08 13:40:47 +02:00
|
|
|
self.view = Some(MailView::new(coordinates, None, None, context));
|
2019-06-05 01:26:23 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
self.view.as_mut().unwrap().draw(
|
|
|
|
grid,
|
|
|
|
(set_y(upper_left, mid + 1), bottom_right),
|
|
|
|
context,
|
|
|
|
);
|
2019-02-11 14:55:29 +02:00
|
|
|
|
2018-09-07 09:44:37 +03:00
|
|
|
self.dirty = false;
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2018-09-07 09:44:37 +03:00
|
|
|
if let Some(ref mut v) = self.view {
|
|
|
|
if v.process_event(event, context) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
match *event {
|
|
|
|
UIEvent::Input(Key::Char('\n')) if !self.unfocused => {
|
2018-09-07 09:44:37 +03:00
|
|
|
self.unfocused = true;
|
|
|
|
self.dirty = true;
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(Key::Char('i')) if self.unfocused => {
|
2018-09-07 09:44:37 +03:00
|
|
|
self.unfocused = false;
|
|
|
|
self.dirty = true;
|
|
|
|
self.view = None;
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::MailboxUpdate((ref idxa, ref idxf))
|
2020-02-08 23:42:31 +02:00
|
|
|
if (*idxa, *idxf) == (self.new_cursor_pos.0, self.cursor_pos.1) =>
|
2019-04-04 14:24:05 +03:00
|
|
|
{
|
2020-02-06 01:51:20 +02:00
|
|
|
self.refresh_mailbox(context, false);
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2019-03-30 13:41:32 +02:00
|
|
|
}
|
2020-02-08 23:42:31 +02:00
|
|
|
UIEvent::StartupCheck(ref f) if *f == self.cursor_pos.1 => {
|
2020-02-06 01:51:20 +02:00
|
|
|
self.refresh_mailbox(context, false);
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::ChangeMode(UIMode::Normal) => {
|
2018-09-07 09:44:37 +03:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Resize => {
|
2018-09-07 09:44:37 +03:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Action(ref action) => match action {
|
2018-09-07 09:44:37 +03:00
|
|
|
Action::SubSort(field, order) => {
|
2019-05-01 19:20:33 +03:00
|
|
|
debug!("SubSort {:?} , {:?}", field, order);
|
2018-09-07 09:44:37 +03:00
|
|
|
self.subsort = (*field, *order);
|
|
|
|
self.dirty = true;
|
2020-02-06 01:51:20 +02:00
|
|
|
self.refresh_mailbox(context, false);
|
2018-09-07 09:44:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Action::Sort(field, order) => {
|
2019-05-01 19:20:33 +03:00
|
|
|
debug!("Sort {:?} , {:?}", field, order);
|
2018-09-07 09:44:37 +03:00
|
|
|
self.sort = (*field, *order);
|
|
|
|
self.dirty = true;
|
2020-02-06 01:51:20 +02:00
|
|
|
self.refresh_mailbox(context, false);
|
2018-09-07 09:44:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.dirty || self.view.as_ref().map(|p| p.is_dirty()).unwrap_or(false)
|
|
|
|
}
|
2019-12-14 18:50:05 +02:00
|
|
|
fn set_dirty(&mut self, value: bool) {
|
2018-09-07 09:44:37 +03:00
|
|
|
if let Some(p) = self.view.as_mut() {
|
2019-12-14 18:50:05 +02:00
|
|
|
p.set_dirty(value);
|
2018-09-07 09:44:37 +03:00
|
|
|
};
|
2019-12-14 18:50:05 +02:00
|
|
|
self.dirty = value;
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|
2019-05-10 22:00:56 +03:00
|
|
|
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
|
2019-03-26 19:53:39 +02:00
|
|
|
self.view
|
2019-03-23 00:28:17 +02:00
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.get_shortcuts(context))
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-09-07 09:44:37 +03:00
|
|
|
}
|