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
|
#ifndef NEWSBOAT_LISTMOVEMENTCONTROL_H_
#define NEWSBOAT_LISTMOVEMENTCONTROL_H_
#include "listwidgetbackend.h"
#include <string>
namespace newsboat {
// Backend is required to implement:
// std::uint32_t get_height()
// std::uint32_t get_num_lines()
// void update_position(std::uint32_t pos, std::uint32_t scroll_offset)
// virtual void on_list_changed()
template<class Backend>
class ListMovementControl : public Backend {
public:
ListMovementControl(const std::string& list_name, Stfl::Form& form,
std::uint32_t scrolloff)
: Backend(list_name, form)
{
num_context_lines = scrolloff;
set_position(current_position);
}
// Make sure the backend's destructor is marked virtual
~ListMovementControl() override = default;
bool move_up(bool wrap_scroll)
{
if (current_position > 0) {
set_position(current_position - 1);
return true;
} else if (wrap_scroll) {
move_to_last();
return true;
}
return false;
}
bool move_down(bool wrap_scroll)
{
const auto num_lines = Backend::get_num_lines();
if (num_lines == 0) {
// Ignore if list is empty
return false;
}
const std::uint32_t maxpos = num_lines - 1;
if (current_position + 1 <= maxpos) {
set_position(current_position + 1);
return true;
} else if (wrap_scroll) {
move_to_first();
return true;
}
return false;
}
void move_to_first()
{
set_position(0);
}
void move_to_last()
{
const auto num_lines = Backend::get_num_lines();
if (num_lines == 0) {
// Ignore if list is empty
return;
}
const std::uint32_t maxpos = num_lines - 1;
set_position(maxpos);
}
void move_page_up(bool wrap_scroll)
{
const std::uint32_t list_height = Backend::get_height();
if (current_position > list_height) {
set_position(current_position - list_height);
} else if (wrap_scroll && current_position == 0) {
move_to_last();
} else {
set_position(0);
}
}
void move_page_down(bool wrap_scroll)
{
const auto num_lines = Backend::get_num_lines();
if (num_lines == 0) {
// Ignore if list is empty
return;
}
const std::uint32_t maxpos = num_lines - 1;
const std::uint32_t list_height = Backend::get_height();
if (current_position + list_height < maxpos) {
set_position(current_position + list_height);
} else if (wrap_scroll && current_position == maxpos) {
move_to_first();
} else {
set_position(maxpos);
}
}
void scroll_halfpage_up(bool wrap_scroll)
{
const std::uint32_t list_height = Backend::get_height();
const std::uint32_t scroll_amount = (list_height + 1) / 2;
if (current_scroll_offset >= scroll_amount) {
current_scroll_offset -= scroll_amount;
} else {
current_scroll_offset = 0;
}
if (wrap_scroll && current_position == 0) {
move_to_last();
} else if (current_position >= scroll_amount) {
set_position(current_position - scroll_amount);
} else {
set_position(0);
}
}
void scroll_halfpage_down(bool wrap_scroll)
{
const auto num_lines = Backend::get_num_lines();
if (num_lines == 0) {
// Ignore if list is empty
return;
}
const std::uint32_t maxpos = num_lines - 1;
const std::uint32_t list_height = Backend::get_height();
const std::uint32_t scroll_amount = (list_height + 1) / 2;
if (current_scroll_offset + scroll_amount <= max_offset()) {
current_scroll_offset += scroll_amount;
} else {
current_scroll_offset = max_offset();
}
if (wrap_scroll && current_position == maxpos) {
move_to_first();
} else if (current_position + scroll_amount <= maxpos) {
set_position(current_position + scroll_amount);
} else {
set_position(maxpos);
}
}
std::uint32_t get_position()
{
return current_position;
}
void set_position(std::uint32_t pos)
{
// TODO: Check if in valid range?
current_position = pos;
current_scroll_offset = std::min(get_new_scroll_offset(pos), max_offset());
Backend::update_position(current_position, current_scroll_offset);
}
private:
void on_list_changed() override
{
const auto num_lines = Backend::get_num_lines();
if (current_position >= num_lines) {
if (num_lines > 0) {
set_position(num_lines - 1);
} else {
set_position(0);
}
}
}
std::uint32_t max_offset()
{
const auto h = Backend::get_height();
const auto num_lines = Backend::get_num_lines();
// An offset at which the last item of the list is at the bottom of the
// widget. We shouldn't set "offset" to more than this value, otherwise
// we'll have an empty "gap" at the bottom of the list. That's only
// acceptable if the list is shorter than the widget's height.
if (num_lines >= h) {
return num_lines - h;
} else {
return 0;
}
}
std::uint32_t get_new_scroll_offset(std::uint32_t pos)
{
// In STFL, "offset" is how many items at the beginning of the list are
// hidden off-screen. That's how scrolling is implemented: to scroll down,
// you increase "offset", hiding items at the top and showing more at the
// bottom. By manipulating "offset" here, we can keep the cursor within the
// bounds we set.
//
// All the lines that are visible because of "scrolloff" setting are called
// "context" here. They include the current line under cursor (which has
// position "pos"), "num_context_lines" lines above it, and
// "num_context_lines" lines below it.
const auto h = Backend::get_height();
if (2 * num_context_lines < h) {
// Check if items at the bottom of the "context" are visible. If not,
// we'll have to scroll down.
if (pos + num_context_lines >= current_scroll_offset + h) {
if (pos + num_context_lines >= h) {
const std::uint32_t target_offset = pos + num_context_lines - h + 1;
return target_offset;
} else { // "pos" is towards the beginning of the list; don't scroll
return 0;
}
}
// Check if items at the top of the "context" are visible. If not,
// we'll have to scroll up.
if (pos < current_scroll_offset + num_context_lines) {
if (pos >= num_context_lines) {
const std::uint32_t target_offset = pos - num_context_lines;
return target_offset;
} else { // "pos" is towards the beginning of the list; don't scroll
return 0;
}
}
return current_scroll_offset;
} else { // Keep selected item in the middle
if (pos > h / 2) {
const std::uint32_t target_offset = pos - h / 2;
return target_offset;
} else { // "pos" is towards the beginning of the list; don't scroll
return 0;
}
}
}
private:
std::uint32_t num_context_lines {};
std::uint32_t current_position {};
std::uint32_t current_scroll_offset {};
};
} // namespace newsboat
#endif /* NEWSBOAT_LISTWIDGETBACKEND_H_ */
|