meli/ui/src/components/mail/listing.rs

192 lines
6.3 KiB
Rust
Raw Normal View History

2018-08-07 15:01:15 +03:00
/*
* 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::*;
2018-08-03 13:46:08 +03:00
mod compact;
pub use self::compact::*;
mod thread;
pub use self::thread::*;
2018-07-11 17:07:51 +03:00
mod plain;
pub use self::plain::*;
2018-07-11 17:07:51 +03:00
trait ListingTrait {
fn coordinates(&self) -> (usize, usize, Option<EnvelopeHash>);
fn set_coordinates(&mut self, (usize, usize, Option<EnvelopeHash>));
}
#[derive(Debug)]
pub enum Listing {
Plain(PlainListing),
Threaded(ThreadListing),
Compact(CompactListing),
2018-08-07 15:01:15 +03:00
}
impl fmt::Display for Listing {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Listing::Compact(l) => write!(f, "{}", l),
Listing::Plain(l) => write!(f, "{}", l),
Listing::Threaded(l) => write!(f, "{}", l),
2018-07-15 15:35:57 +03:00
}
}
}
impl Default for Listing {
fn default() -> Self {
2018-10-14 19:49:16 +03:00
Listing::Threaded(Default::default())
}
2018-07-11 17:07:51 +03:00
}
impl Component for Listing {
2018-07-14 15:04:42 +03:00
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
match self {
Listing::Compact(l) => l.draw(grid, area, context),
Listing::Plain(l) => l.draw(grid, area, context),
Listing::Threaded(l) => l.draw(grid, area, context),
2018-07-11 17:07:51 +03:00
}
}
2019-02-26 17:50:47 +02:00
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
if match self {
Listing::Plain(l) => l.process_event(event, context),
Listing::Compact(l) => l.process_event(event, context),
Listing::Threaded(l) => l.process_event(event, context),
} {
return true;
}
2019-04-10 23:37:20 +03:00
match *event {
UIEvent::Resize => self.set_dirty(),
UIEvent::Action(ref action) => match action {
Action::Listing(ListingAction::SetPlain) => {
let new_l = match self {
Listing::Plain(_) => {
return true;
}
2019-04-04 14:24:05 +03:00
Listing::Threaded(l) => {
let mut new_l = PlainListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
Listing::Compact(l) => {
let mut new_l = PlainListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
};
*self = Listing::Plain(new_l);
return true;
2018-08-07 15:01:15 +03:00
}
Action::Listing(ListingAction::SetThreaded) => {
let new_l = match self {
Listing::Threaded(_) => {
return true;
}
2019-04-04 14:24:05 +03:00
Listing::Plain(l) => {
let mut new_l = ThreadListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
Listing::Compact(l) => {
let mut new_l = ThreadListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
};
*self = Listing::Threaded(new_l);
return true;
2018-08-14 17:36:45 +03:00
}
Action::Listing(ListingAction::SetCompact) => {
let new_l = match self {
Listing::Compact(_) => {
return true;
}
2019-04-04 14:24:05 +03:00
Listing::Threaded(l) => {
let mut new_l = CompactListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
Listing::Plain(l) => {
let mut new_l = CompactListing::default();
new_l.set_coordinates(l.coordinates());
new_l
}
};
*self = Listing::Compact(new_l);
return true;
}
_ => {}
},
2018-07-27 21:37:56 +03:00
_ => {}
2018-07-11 17:07:51 +03:00
}
false
2018-07-11 17:07:51 +03:00
}
2018-07-14 15:04:42 +03:00
fn is_dirty(&self) -> bool {
match self {
Listing::Compact(l) => l.is_dirty(),
Listing::Plain(l) => l.is_dirty(),
Listing::Threaded(l) => l.is_dirty(),
}
2018-07-14 15:04:42 +03:00
}
fn set_dirty(&mut self) {
match self {
Listing::Compact(l) => l.set_dirty(),
Listing::Plain(l) => l.set_dirty(),
Listing::Threaded(l) => l.set_dirty(),
}
}
2019-03-02 19:25:11 +02:00
fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
2019-03-02 19:25:11 +02:00
match self {
Listing::Compact(l) => l.get_shortcuts(context),
Listing::Plain(l) => l.get_shortcuts(context),
Listing::Threaded(l) => l.get_shortcuts(context),
2019-03-02 19:25:11 +02:00
}
}
2019-04-10 22:01:02 +03:00
fn id(&self) -> ComponentId {
match self {
Listing::Compact(l) => l.id(),
Listing::Plain(l) => l.id(),
Listing::Threaded(l) => l.id(),
}
}
fn set_id(&mut self, id: ComponentId) {
match self {
Listing::Compact(l) => l.set_id(id),
Listing::Plain(l) => l.set_id(id),
Listing::Threaded(l) => l.set_id(id),
}
}
2018-07-11 17:07:51 +03:00
}
impl From<IndexStyle> for Listing {
fn from(index_style: IndexStyle) -> Self {
match index_style {
IndexStyle::Plain => Listing::Plain(Default::default()),
IndexStyle::Threaded => Listing::Threaded(Default::default()),
IndexStyle::Compact => Listing::Compact(Default::default()),
}
}
}