-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileSelectionWindow.hpp
215 lines (191 loc) · 6.19 KB
/
FileSelectionWindow.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
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
#pragma once
#include "MenuWindowPad.hpp"
#include "GlobalParameters.hpp"
#include "td/telegram/PromoDataManager.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice-decl.h"
#include "td/utils/Status.h"
#include "td/utils/format.h"
#include "td/utils/port/path.h"
#include "port/dirlist.h"
#include "windows/Input.hpp"
#include "windows/Output.hpp"
#include <memory>
namespace tdcurses {
class FileSelectionWindow : public MenuWindowPad {
public:
enum SortMode : td::uint32 { Name, Mtime, Count };
class Callback {
public:
virtual ~Callback() = default;
virtual void on_abort(FileSelectionWindow &) = 0;
virtual void on_answer(FileSelectionWindow &, std::string answer) = 0;
};
FileSelectionWindow(Tdcurses *root, td::ActorId<Tdcurses> root_actor, std::shared_ptr<Callback> callback = nullptr)
: MenuWindowPad(root, root_actor), callback_(std::move(callback)) {
change_folder("/");
}
class Element : public windows::PadWindowElement {
public:
Element(std::string name, std::string display_name, const FileInfo &info, SortMode sort_mode)
: name_(std::move(name)), display_name_(std::move(display_name)), info_(info), sort_mode_(sort_mode) {
}
bool is_less(const PadWindowElement &other) const override {
const auto &o = static_cast<const Element &>(other);
if (info_.is_dir) {
if (o.info_.is_dir) {
// FALLTHROUGH
} else {
return true;
}
} else {
if (o.info_.is_dir) {
return false;
} else {
// FALLTHROUGH
}
}
switch (sort_mode_) {
case SortMode::Name:
return display_name_ < o.display_name_;
case SortMode::Mtime:
return info_.modified_at > o.info_.modified_at;
default:
UNREACHABLE();
};
}
void handle_input(PadWindow &root, const windows::InputEvent &info) override {
auto &w = static_cast<FileSelectionWindow &>(root);
if (info == "T-Enter") {
if (info_.is_dir) {
w.change_folder(name_);
} else {
w.on_result(name_);
}
} else if (info == "T-Escape") {
if (!w.sent_answer_) {
w.sent_answer_ = true;
w.callback_->on_abort(w);
}
} else if (info == "s") {
w.set_sort_mode((SortMode)((w.sort_mode_ + 1) % SortMode::Count));
}
}
td::int32 render(PadWindow &root, windows::WindowOutputter &rb, windows::SavedRenderedImagesDirectory &dir,
bool is_selected) override {
std::string s;
if (!info_.has_access) {
s = "<INACCESSIBLE>";
} else if (info_.is_dir) {
s = "<DIR>";
} else {
s = PSTRING() << td::format::as_size(info_.size);
}
Outputter out;
out << display_name_ << Outputter::RightPad{s};
return render_plain_text(rb, out.as_cslice(), out.markup(), width(), 1, is_selected, &dir);
}
private:
std::string name_;
std::string display_name_;
FileInfo info_;
SortMode sort_mode_;
};
void add_file(std::string path, const FileInfo &info) {
auto x = path.rfind('/');
std::string display_path = (x == std::string::npos) ? path : path.substr(x + 1);
auto e = std::make_shared<Element>(std::move(path), std::move(display_path), info, sort_mode_);
add_element(std::move(e));
}
void on_result(std::string name) {
if (!sent_answer_) {
sent_answer_ = true;
callback_->on_answer(*this, std::move(name));
}
}
void change_folder(std::string name) {
cur_folder_ = name;
clear();
set_title(name);
iterate_files_in_directory(name, [&](const std::string &path, const FileInfo &info) { add_file(path, info); });
set_need_refresh();
}
void install_callback(std::shared_ptr<Callback> callback) {
callback_ = std::move(callback);
}
auto sort_mode() const {
return sort_mode_;
}
void set_sort_mode(SortMode sort_mode) {
sort_mode_ = sort_mode;
change_folder(cur_folder_);
}
/*static MenuWindowSpawnFunction spawn_function(std::string text, td::Promise<std::string> promise) {
class Cb : public Callback {
public:
Cb(td::Promise<std::string> promise) : promise_(std::move(promise)) {
}
void on_abort(FileSelectionWindow &) override {
promise_.set_error(td::Status::Error("aborted"));
}
void on_answer(FileSelectionWindow &, std::string answer) override {
promise_.set_value(std::move(answer));
}
private:
td::Promise<std::string> promise_;
};
return [callback = std::make_shared<Cb>(std::move(promise))](
Tdcurses *root, td::ActorId<Tdcurses> root_id) -> std::shared_ptr<MenuWindow> {
return std::make_shared<FileSelectionWindow>(root, root_id, callback);
};
}*/
void handle_rollback() override {
if (!sent_answer_) {
sent_answer_ = true;
callback_->on_abort(*this);
}
}
void handle_exit() override {
sent_answer_ = true;
exit();
}
~FileSelectionWindow() {
if (!sent_answer_) {
sent_answer_ = true;
callback_->on_abort(*this);
}
}
private:
std::shared_ptr<Callback> callback_;
bool sent_answer_{false};
SortMode sort_mode_{SortMode::Name};
std::string cur_folder_;
};
template <typename T, typename F>
std::enable_if_t<std::is_base_of<MenuWindow, T>::value, std::shared_ptr<FileSelectionWindow>>
spawn_file_selection_window(T &cur_window, std::string caption, std::string initial_dir, F &&cb) {
class Cb : public FileSelectionWindow::Callback {
public:
Cb(F &&cb, T *win) : cb_(std::move(cb)), self_(win), self_id_(win->window_unique_id()) {
}
void on_abort(FileSelectionWindow &w) override {
if (w.root()->window_exists(self_id_)) {
w.rollback();
cb_(td::Status::Error("aborted"));
}
}
void on_answer(FileSelectionWindow &w, std::string answer) override {
if (w.root()->window_exists(self_id_)) {
w.rollback();
cb_(std::move(answer));
}
}
private:
F cb_;
T *self_;
td::int64 self_id_;
};
auto callback = std::make_unique<Cb>(std::move(cb), &cur_window);
return cur_window.template spawn_submenu<FileSelectionWindow>(std::move(callback));
}
} // namespace tdcurses