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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::event::Outcome;
use crate::{TableSelection, TableState};
use crossterm::event::KeyModifiers;
use rat_event::{ct_event, flow, HandleEvent, MouseOnly, Regular};
use rat_focus::HasFocusFlag;
use rat_scrolled::event::ScrollOutcome;
use rat_scrolled::ScrollAreaState;
use std::cmp::{max, min};
use std::collections::HashSet;
use std::mem;

/// Allows selection an active range of rows.
///
/// The current range can be retired to a set of selected rows,
/// and a new range be started. This allows multiple interval
/// selection and deselection of certain rows.
///
/// This one only supports row-selection.
#[derive(Debug, Default, Clone)]
pub struct RowSetSelection {
    /// Start of the active selection.
    pub anchor_row: Option<usize>,
    /// Current end of the active selection.
    pub lead_row: Option<usize>,
    /// Retired rows. This doesn't contain the rows
    /// between anchor and lead.
    ///
    /// You can call [RowSetSelection::retire_selection] to
    /// add the anchor-lead range. This resets anchor and lead though.
    /// Or iterate the complete range and call [RowSetSelection::is_selected_row].
    pub selected: HashSet<usize>,
}

impl TableSelection for RowSetSelection {
    #[allow(clippy::collapsible_else_if)]
    fn is_selected_row(&self, row: usize) -> bool {
        if let Some(mut anchor) = self.anchor_row {
            if let Some(mut lead) = self.lead_row {
                if lead < anchor {
                    mem::swap(&mut lead, &mut anchor);
                }
                if row >= anchor && row <= lead {
                    return true;
                }
            }
        } else {
            if let Some(lead) = self.lead_row {
                if row == lead {
                    return true;
                }
            }
        }

        self.selected.contains(&row)
    }

    fn is_selected_column(&self, _column: usize) -> bool {
        false
    }

    fn is_selected_cell(&self, _column: usize, _row: usize) -> bool {
        false
    }

    fn lead_selection(&self) -> Option<(usize, usize)> {
        self.lead_row.map(|srow| (0, srow))
    }
}

impl RowSetSelection {
    /// New selection.
    pub fn new() -> RowSetSelection {
        RowSetSelection {
            anchor_row: None,
            lead_row: None,
            selected: HashSet::new(),
        }
    }

    /// Clear the selection.
    pub fn clear(&mut self) {
        self.anchor_row = None;
        self.lead_row = None;
        self.selected.clear();
    }

    /// Current lead.
    pub fn lead(&self) -> Option<usize> {
        self.lead_row
    }

    /// Current anchor.
    pub fn anchor(&self) -> Option<usize> {
        self.anchor_row
    }

    /// Set of all selected rows. Clones the retired set and adds the current anchor..lead range.
    pub fn selected(&self) -> HashSet<usize> {
        let mut selected = self.selected.clone();
        Self::fill(self.anchor_row, self.lead_row, &mut selected);
        selected
    }

    /// Has some selection.
    pub fn has_selection(&self) -> bool {
        self.lead_row.is_some() || !self.selected.is_empty()
    }

    /// Set a new lead. Maybe extend the range.
    pub fn set_lead(&mut self, lead: Option<usize>, extend: bool) -> bool {
        let old_selection = (self.anchor_row, self.lead_row);
        self.extend(extend);
        self.lead_row = lead;
        old_selection != (self.anchor_row, self.lead_row)
    }

    /// Transfers the range anchor to lead to the selection set and reset both.
    pub fn retire_selection(&mut self) {
        Self::fill(self.anchor_row, self.lead_row, &mut self.selected);
        self.anchor_row = None;
        self.lead_row = None;
    }

    /// Add to selection. Only works for retired selections, not for the
    /// active anchor-lead range.
    pub fn add(&mut self, idx: usize) {
        self.selected.insert(idx);
    }

    /// Remove from selection. Only works for retired selections, not for the
    /// active anchor-lead range.
    pub fn remove(&mut self, idx: usize) {
        self.selected.remove(&idx);
    }

    /// Set a new lead, at the same time limit the lead to max.
    pub fn move_to(&mut self, lead: usize, max: usize, extend: bool) -> bool {
        let old_selection = (self.anchor_row, self.lead_row);
        self.extend(extend);
        if lead <= max {
            self.lead_row = Some(lead);
        } else {
            self.lead_row = Some(max);
        }
        old_selection != (self.anchor_row, self.lead_row)
    }

    /// Select next. Maybe extend the range.
    pub fn move_down(&mut self, n: usize, maximum: usize, extend: bool) -> bool {
        let old_selection = (self.anchor_row, self.lead_row);
        self.extend(extend);
        self.lead_row = Some(self.lead_row.map_or(0, |v| min(v + n, maximum)));
        old_selection != (self.anchor_row, self.lead_row)
    }

    /// Select next. Maybe extend the range.
    pub fn move_up(&mut self, n: usize, maximum: usize, extend: bool) -> bool {
        let old_selection = (self.anchor_row, self.lead_row);
        self.extend(extend);
        self.lead_row = Some(self.lead_row.map_or(maximum, |v| v.saturating_sub(n)));
        old_selection != (self.anchor_row, self.lead_row)
    }

    fn extend(&mut self, extend: bool) {
        if extend {
            if self.anchor_row.is_none() {
                self.anchor_row = self.lead_row;
            }
        } else {
            self.anchor_row = None;
            self.selected.clear();
        }
    }

    #[allow(clippy::collapsible_else_if)]
    fn fill(anchor: Option<usize>, lead: Option<usize>, selection: &mut HashSet<usize>) {
        if let Some(mut anchor) = anchor {
            if let Some(mut lead) = lead {
                if lead < anchor {
                    mem::swap(&mut lead, &mut anchor);
                }

                for n in anchor..=lead {
                    selection.insert(n);
                }
            }
        } else {
            if let Some(lead) = lead {
                selection.insert(lead);
            }
        }
    }
}

impl HandleEvent<crossterm::event::Event, Regular, Outcome> for TableState<RowSetSelection> {
    fn handle(&mut self, event: &crossterm::event::Event, _: Regular) -> Outcome {
        let res = if self.is_focused() {
            match event {
                ct_event!(keycode press Up) => self.move_up(1, false).into(),
                ct_event!(keycode press Down) => self.move_down(1, false).into(),
                ct_event!(keycode press CONTROL-Up)
                | ct_event!(keycode press CONTROL-Home)
                | ct_event!(keycode press Home) => self.move_to(0, false).into(),
                ct_event!(keycode press CONTROL-Down)
                | ct_event!(keycode press CONTROL-End)
                | ct_event!(keycode press End) => {
                    self.move_to(self.rows.saturating_sub(1), false).into()
                }
                ct_event!(keycode press PageUp) => self
                    .move_up(max(1, self.page_len().saturating_sub(1)), false)
                    .into(),
                ct_event!(keycode press PageDown) => self
                    .move_down(max(1, self.page_len().saturating_sub(1)), false)
                    .into(),

                ct_event!(keycode press SHIFT-Up) => self.move_up(1, true).into(),
                ct_event!(keycode press SHIFT-Down) => self.move_down(1, true).into(),
                ct_event!(keycode press CONTROL_SHIFT-Up)
                | ct_event!(keycode press CONTROL_SHIFT-Home)
                | ct_event!(keycode press SHIFT-Home) => self.move_to(0, true).into(),
                ct_event!(keycode press CONTROL_SHIFT-Down)
                | ct_event!(keycode press CONTROL_SHIFT-End)
                | ct_event!(keycode press SHIFT-End) => {
                    self.move_to(self.rows.saturating_sub(1), true).into()
                }
                ct_event!(keycode press SHIFT-PageUp) => self
                    .move_up(max(1, self.page_len().saturating_sub(1)), true)
                    .into(),
                ct_event!(keycode press SHIFT-PageDown) => self
                    .move_down(max(1, self.page_len().saturating_sub(1)), true)
                    .into(),

                ct_event!(keycode press Left) => self.scroll_left(1).into(),
                ct_event!(keycode press Right) => self.scroll_right(1).into(),
                ct_event!(keycode press CONTROL-Left) => self.scroll_to_x(0).into(),
                ct_event!(keycode press CONTROL-Right) => {
                    self.scroll_to_x(self.x_max_offset()).into()
                }
                _ => Outcome::Continue,
            }
        } else {
            Outcome::Continue
        };

        if res == Outcome::Continue {
            self.handle(event, MouseOnly)
        } else {
            res
        }
    }
}

impl HandleEvent<crossterm::event::Event, MouseOnly, Outcome> for TableState<RowSetSelection> {
    fn handle(&mut self, event: &crossterm::event::Event, _: MouseOnly) -> Outcome {
        flow!(match event {
            ct_event!(mouse any for m) | ct_event!(mouse any CONTROL for m)
                if self.mouse.drag(self.table_area, m)
                    || self.mouse.drag2(self.table_area, m, KeyModifiers::CONTROL) =>
            {
                self.move_to(self.row_at_drag((m.column, m.row)), true)
                    .into()
            }
            ct_event!(mouse down Left for column, row) => {
                let pos = (*column, *row);
                if self.table_area.contains(pos.into()) {
                    if let Some(new_row) = self.row_at_clicked(pos) {
                        self.move_to(new_row, false).into()
                    } else {
                        Outcome::Continue
                    }
                } else {
                    Outcome::Continue
                }
            }
            ct_event!(mouse down ALT-Left for column, row) => {
                let pos = (*column, *row);
                if self.area.contains(pos.into()) {
                    if let Some(new_row) = self.row_at_clicked(pos) {
                        self.move_to(new_row, true).into()
                    } else {
                        Outcome::Continue
                    }
                } else {
                    Outcome::Continue
                }
            }
            ct_event!(mouse down CONTROL-Left for column, row) => {
                let pos = (*column, *row);
                if self.area.contains(pos.into()) {
                    if let Some(new_row) = self.row_at_clicked(pos) {
                        self.retire_selection();
                        if self.selection.is_selected_row(new_row) {
                            self.selection.remove(new_row);
                        } else {
                            self.move_to(new_row, true);
                        }
                        Outcome::Changed
                    } else {
                        Outcome::Continue
                    }
                } else {
                    Outcome::Continue
                }
            }
            _ => Outcome::Continue,
        });

        let mut sas = ScrollAreaState {
            area: self.inner,
            h_scroll: Some(&mut self.hscroll),
            v_scroll: Some(&mut self.vscroll),
        };
        let r = match sas.handle(event, MouseOnly) {
            ScrollOutcome::Up(v) => self.scroll_up(v),
            ScrollOutcome::Down(v) => self.scroll_down(v),
            ScrollOutcome::VPos(v) => self.set_row_offset(v),
            ScrollOutcome::Left(v) => self.scroll_left(v),
            ScrollOutcome::Right(v) => self.scroll_right(v),
            ScrollOutcome::HPos(v) => self.set_x_offset(v),

            ScrollOutcome::Continue => false,
            ScrollOutcome::Unchanged => false,
            ScrollOutcome::Changed => true,
        };
        if r {
            return Outcome::Changed;
        }

        Outcome::Unchanged
    }
}

/// Handle all events.
/// Table events are only processed if focus is true.
/// Mouse events are processed if they are in range.
pub fn handle_events(
    state: &mut TableState<RowSetSelection>,
    focus: bool,
    event: &crossterm::event::Event,
) -> Outcome {
    state.focus.set(focus);
    state.handle(event, Regular)
}

/// Handle only mouse-events.
pub fn handle_mouse_events(
    state: &mut TableState<RowSetSelection>,
    event: &crossterm::event::Event,
) -> Outcome {
    state.handle(event, MouseOnly)
}