-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMenuWindowCommon.hpp
167 lines (147 loc) · 6.76 KB
/
MenuWindowCommon.hpp
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
#pragma once
#include "MenuWindowPad.hpp"
#include "windows/Output.hpp"
#include <functional>
#include <memory>
#include <utility>
namespace tdcurses {
class MenuWindowCommon;
using MenuWindowSpawnFunction = std::function<std::shared_ptr<MenuWindow>(MenuWindow &parent)>;
template <typename T, typename... ArgsT>
MenuWindowSpawnFunction create_menu_window_spawn_function(ArgsT &&...args) {
return [args = std::make_tuple(std::forward<ArgsT>(args)...)](MenuWindow &parent) mutable {
return std::apply([&](auto &&...args) { return parent.spawn_submenu<T>(std::forward<ArgsT>(args)...); },
std::move(args));
};
}
class MenuWindowElement {
public:
MenuWindowElement(std::string name, std::string data, std::vector<windows::MarkupElement> markup)
: name(std::move(name)), data(std::move(data)), markup(std::move(markup)) {
}
virtual ~MenuWindowElement() = default;
virtual void handle_input(MenuWindowCommon &root, const windows::InputEvent &info) {
}
std::string name;
std::string data;
std::vector<windows::MarkupElement> markup;
};
class MenuWindowElementSpawn : public MenuWindowElement {
public:
MenuWindowElementSpawn(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
MenuWindowSpawnFunction cb)
: MenuWindowElement(std::move(name), std::move(data), std::move(markup)), cb_(std::move(cb)) {
}
void handle_input(MenuWindowCommon &root, const windows::InputEvent &info) override;
private:
MenuWindowSpawnFunction cb_;
};
class MenuWindowElementRun : public MenuWindowElement {
public:
MenuWindowElementRun(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool()> cb)
: MenuWindowElement(std::move(name), std::move(data), std::move(markup))
, cb_([cb = std::move(cb)](MenuWindowCommon &, MenuWindowElementRun &) { return cb(); }) {
}
MenuWindowElementRun(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool(MenuWindowCommon &)> cb)
: MenuWindowElement(std::move(name), std::move(data), std::move(markup))
, cb_([cb = std::move(cb)](MenuWindowCommon &w, MenuWindowElementRun &) { return cb(w); }) {
}
MenuWindowElementRun(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool(MenuWindowElementRun &)> cb)
: MenuWindowElement(std::move(name), std::move(data), std::move(markup))
, cb_([cb = std::move(cb)](MenuWindowCommon &, MenuWindowElementRun &r) { return cb(r); }) {
}
void handle_input(MenuWindowCommon &root, const windows::InputEvent &info) override;
void replace_text(std::string data, std::vector<windows::MarkupElement> markup = {}) {
}
private:
std::function<bool(MenuWindowCommon &, MenuWindowElementRun &)> cb_;
};
class MenuWindowCommon : public MenuWindowPad {
public:
MenuWindowCommon(Tdcurses *root, td::ActorId<Tdcurses> root_actor) : MenuWindowPad(root, root_actor) {
}
class Element : public windows::PadWindowElement {
public:
Element(size_t idx, std::shared_ptr<MenuWindowElement> element) : idx_(idx), element_(std::move(element)) {
}
void handle_input(windows::PadWindow &pad, const windows::InputEvent &info) override {
element_->handle_input(static_cast<MenuWindowCommon &>(pad), info);
}
bool is_less(const PadWindowElement &other) const override {
return idx_ < static_cast<const Element &>(other).idx_;
}
td::int32 render(PadWindow &root, windows::WindowOutputter &rb, windows::SavedRenderedImagesDirectory &dir,
bool is_selected) override {
auto markup = element_->markup;
auto text = element_->name;
while (text.size() < static_cast<MenuWindowCommon &>(root).pad_size()) {
text += " ";
}
if (element_->data.size() != 0) {
text += ": ";
}
auto size = text.size();
text += element_->data;
for (auto &e : markup) {
e.first_pos += size;
e.last_pos += size;
}
markup.push_back(windows::MarkupElement::bold(0, size));
markup.push_back(windows::MarkupElement::fg_color(0, size, windows::Color::White));
return render_plain_text(rb, text, std::move(markup), width(), std::numeric_limits<td::int32>::max(), is_selected,
&dir);
}
void update_element(std::shared_ptr<MenuWindowElement> element) {
element_ = std::move(element);
}
auto &menu_element() {
return element_;
}
private:
size_t idx_;
std::shared_ptr<MenuWindowElement> element_;
};
std::shared_ptr<Element> add_element(std::shared_ptr<MenuWindowElement> element) {
if (element->name.size() > max_size_) {
max_size_ = element->name.size();
}
auto el = std::make_shared<Element>(last_idx_++, std::move(element));
PadWindow::add_element(el);
return el;
}
std::shared_ptr<Element> add_element(std::string name, std::string data,
std::vector<windows::MarkupElement> markup = {}) {
return add_element(std::make_shared<MenuWindowElement>(std::move(name), std::move(data), std::move(markup)));
}
std::shared_ptr<Element> add_element(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
MenuWindowSpawnFunction spawn) {
return add_element(std::make_shared<MenuWindowElementSpawn>(std::move(name), std::move(data), std::move(markup),
std::move(spawn)));
}
std::shared_ptr<Element> add_element(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool()> cb) {
return add_element(
std::make_shared<MenuWindowElementRun>(std::move(name), std::move(data), std::move(markup), std::move(cb)));
}
std::shared_ptr<Element> add_element(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool(MenuWindowCommon &)> cb) {
return add_element(
std::make_shared<MenuWindowElementRun>(std::move(name), std::move(data), std::move(markup), std::move(cb)));
}
std::shared_ptr<Element> add_element(std::string name, std::string data, std::vector<windows::MarkupElement> markup,
std::function<bool(MenuWindowElementRun &)> cb) {
return add_element(
std::make_shared<MenuWindowElementRun>(std::move(name), std::move(data), std::move(markup), std::move(cb)));
}
size_t pad_size() const {
return max_size_;
}
private:
size_t last_idx_ = 0;
size_t max_size_ = 0;
};
using ElInfo = MenuWindowCommon::Element;
} // namespace tdcurses