-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpending_attachment_selection.cpp
More file actions
68 lines (59 loc) · 2.42 KB
/
Copy pathpending_attachment_selection.cpp
File metadata and controls
68 lines (59 loc) · 2.42 KB
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
#include "pending_attachment_selection.hpp"
#include <algorithm>
namespace acecode::tui {
bool has_pending_attachment_focus(int selected_index, std::size_t attachment_count) {
return selected_index >= 0 &&
static_cast<std::size_t>(selected_index) < attachment_count;
}
bool clamp_pending_attachment_focus(int& selected_index, std::size_t attachment_count) {
const int before = selected_index;
if (attachment_count == 0) {
selected_index = kNoPendingAttachmentFocus;
} else if (selected_index >= 0) {
const int last = static_cast<int>(attachment_count - 1);
selected_index = std::clamp(selected_index, 0, last);
} else {
selected_index = kNoPendingAttachmentFocus;
}
return selected_index != before;
}
bool toggle_pending_attachment_focus(int& selected_index, std::size_t attachment_count) {
const int before = selected_index;
if (attachment_count == 0) {
selected_index = kNoPendingAttachmentFocus;
} else if (has_pending_attachment_focus(selected_index, attachment_count)) {
selected_index = kNoPendingAttachmentFocus;
} else {
selected_index = static_cast<int>(attachment_count - 1);
}
return selected_index != before;
}
bool move_pending_attachment_focus(int& selected_index,
std::size_t attachment_count,
int delta) {
if (!has_pending_attachment_focus(selected_index, attachment_count)) {
clamp_pending_attachment_focus(selected_index, attachment_count);
return false;
}
const int before = selected_index;
const int last = static_cast<int>(attachment_count - 1);
selected_index = std::clamp(selected_index + delta, 0, last);
return selected_index != before;
}
std::optional<std::size_t> remove_focused_pending_attachment_index(
int& selected_index,
std::size_t attachment_count) {
if (!has_pending_attachment_focus(selected_index, attachment_count)) {
clamp_pending_attachment_focus(selected_index, attachment_count);
return std::nullopt;
}
const std::size_t removed = static_cast<std::size_t>(selected_index);
const std::size_t remaining = attachment_count - 1;
if (remaining == 0) {
selected_index = kNoPendingAttachmentFocus;
} else if (removed >= remaining) {
selected_index = static_cast<int>(remaining - 1);
}
return removed;
}
} // namespace acecode::tui