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
//! Moving around without the mouse: Ctrl+1…Ctrl+8 to a tab (Ctrl+9 the last one),
//! Ctrl+PageDown / Ctrl+PageUp to the next / previous tab in the bar, and F4 / Shift+F4 to the
//! next / previous entry of the list under the text (Find All, the filter, Find in files).
use std::ops::Range;
use super::i18n::trf;
use super::BigTextApp;
/// Keeps the current row of a `show_rows` list in view. Such a list draws only its visible rows,
/// so `scroll_to_me` cannot reach the others: when the current row changes to one that was not
/// wholly drawn last frame (F4, the arrow keys, the caret moving in the text), the list is given
/// an offset that puts it a third of the way down. Clicking a visible row, or scrolling the list
/// by hand, moves nothing.
#[derive(Default)]
pub struct RowReveal {
last: Option<usize>,
shown: Range<usize>,
view_h: f32,
}
impl RowReveal {
/// The offset to give the list this frame, if it has to move. `step` is a row's height
/// with the spacing below it.
pub fn offset(&mut self, cur: Option<usize>, step: f32) -> Option<f32> {
let changed = cur != self.last;
self.last = cur;
let c = cur?;
// The last drawn row may be cut by the bottom edge.
let in_view = c >= self.shown.start && c + 1 < self.shown.end;
(changed && !in_view).then(|| (c as f32 * step - self.view_h / 3.0).max(0.0))
}
/// After the list was drawn: the rows it drew and the height it had.
pub fn seen(&mut self, shown: Range<usize>, view_h: f32) {
self.shown = shown;
self.view_h = view_h;
}
/// The rows drawn last frame.
pub fn shown(&self) -> Range<usize> {
self.shown.clone()
}
}
impl BigTextApp {
/// Ctrl+N: tab `n` (1-based); 9 is the last tab.
pub(crate) fn goto_tab(&mut self, n: u8) {
let count = self.docs.len();
if count == 0 {
return;
}
let i = if n >= 9 { count - 1 } else { n as usize - 1 };
if i < count {
self.active = i;
}
}
/// The next (or previous) tab in the order of the tab bar, around the ends.
pub(crate) fn step_tab_bar(&mut self, forward: bool) {
let n = self.docs.len();
if n > 1 {
self.active = if forward {
(self.active + 1) % n
} else {
(self.active + n - 1) % n
};
}
}
/// F4 / Shift+F4: the next or previous entry of the list under the text, around the ends.
pub(crate) fn step_result(&mut self, forward: bool) {
let show_results = self.search.show_results;
let show_filter = self.search.show_filter;
let Some(d) = self.docs.get_mut(self.active) else {
return;
};
let caret = d.view.caret;
// Find All.
if let Some(r) = d
.results
.as_ref()
.filter(|r| show_results && r.version == d.version)
{
let g = r.data.lock().unwrap();
let n = g.matches.len();
if n > 0 {
let i = if forward {
let i = g.matches.partition_point(|&(s, _)| s <= caret);
if i < n {
i
} else {
0
}
} else {
let i = g
.matches
.partition_point(|&(s, _)| s < d.view.anchor.min(caret));
if i > 0 {
i - 1
} else {
n - 1
}
};
let (s, e) = g.matches[i];
drop(g);
d.select(s, e);
d.view.center();
let msg = trf(
"Match {i} of {n}",
&[("i", &(i + 1).to_string()), ("n", &n.to_string())],
);
self.notify(msg, None);
return;
}
}
// The filter's lines with a match.
if let Some(r) = d
.filter
.as_ref()
.filter(|r| show_filter && r.version == d.version)
{
let here = d
.line_bounds(caret, 64 * 1024)
.map(|b| b.0)
.unwrap_or(caret);
let g = r.data.lock().unwrap();
let hits: Vec<usize> = (0..g.lines.len()).filter(|&i| !g.is_context(i)).collect();
if !hits.is_empty() {
let pos = if forward {
hits.iter().position(|&i| g.lines[i] > here).unwrap_or(0)
} else {
hits.iter()
.rposition(|&i| g.lines[i] < here)
.unwrap_or(hits.len() - 1)
};
let ls = g.lines[hits[pos]];
drop(g);
d.set_caret(ls.max(d.min_caret()), false);
d.view.center();
return;
}
}
// Find in files.
let Some(r) = &self.files.results else { return };
let n = r.data.lock().unwrap().hits.len();
if n == 0 {
return;
}
let i = match self.files.current {
Some(c) if forward => (c + 1) % n,
Some(c) => (c + n - 1) % n,
None if forward => 0,
None => n - 1,
};
self.open_files_hit(i);
}
}
#[cfg(test)]
mod tests {
use super::RowReveal;
#[test]
fn a_row_out_of_view_is_revealed_once_and_a_visible_one_never() {
let mut r = RowReveal::default();
r.seen(0..20, 400.0);
// A row in view: nothing moves, also when it changes.
assert_eq!(r.offset(Some(5), 20.0), None);
assert_eq!(r.offset(Some(6), 20.0), None);
// The last drawn row may be cut off: it counts as out of view.
assert_eq!(r.offset(Some(19), 20.0), Some(19.0 * 20.0 - 400.0 / 3.0));
// F4 to row 50: a third of the way down, and only once, so the list can be scrolled.
assert_eq!(r.offset(Some(50), 20.0), Some(50.0 * 20.0 - 400.0 / 3.0));
r.seen(43..63, 400.0);
assert_eq!(r.offset(Some(50), 20.0), None);
r.seen(0..20, 400.0);
assert_eq!(r.offset(Some(50), 20.0), None);
// Near the top the offset stops at 0; no current row, no move.
assert_eq!(r.offset(Some(1), 20.0), None);
r.seen(30..50, 400.0);
assert_eq!(r.offset(Some(2), 20.0), Some(0.0));
assert_eq!(r.offset(None, 20.0), None);
}
}