summaryrefslogtreecommitdiffstats
path: root/ui/src/components/mail/listing.rs
blob: 9e2371d753315624e05033c9fd5d3f6d7370b3f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * 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::*;

mod compact;
pub use self::compact::*;

mod thread;
pub use self::thread::*;

mod plain;
pub use self::plain::*;

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),
}

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),
        }
    }
}

impl Default for Listing {
    fn default() -> Self {
        Listing::Threaded(Default::default())
    }
}

impl Component for Listing {
    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),
        }
    }
    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;
        }

        match event.event_type {
            UIEventType::Resize => self.set_dirty(),
            UIEventType::Action(ref action) => match action {
                Action::Listing(ListingAction::SetPlain) => {
                    let new_l = match self {
                        Listing::Plain(_) => {
                            return true;
                        }
                        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;
                }
                Action::Listing(ListingAction::SetThreaded) => {
                    let new_l = match self {
                        Listing::Threaded(_) => {
                            return true;
                        }
                        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;
                }
                Action::Listing(ListingAction::SetCompact) => {
                    let new_l = match self {
                        Listing::Compact(_) => {
                            return true;
                        }
                        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;
                }
                _ => {}
            },
            _ => {}
        }
        false
    }
    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(),
        }
    }
    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(),
        }
    }

    fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
        match self {
            Listing::Compact(l) => l.get_shortcuts(context),
            Listing::Plain(l) => l.get_shortcuts(context),
            Listing::Threaded(l) => l.get_shortcuts(context),
        }
    }
}

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()),
        }
    }
}