-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChatPhotoWindow.hpp
More file actions
116 lines (102 loc) · 3.42 KB
/
ChatPhotoWindow.hpp
File metadata and controls
116 lines (102 loc) · 3.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
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
#pragma once
#include "managers/FileManager.hpp"
#include "common-windows/MenuWindow.hpp"
#include "managers/GlobalParameters.hpp"
#include "td/telegram/td_api.h"
#include "td/tl/TlObject.h"
#include "windows/Input.hpp"
#include "windows/Output.hpp"
#include "windows/TextEdit.hpp"
#include "windows/Window.hpp"
#include <memory>
namespace tdcurses {
class ChatPhotoWindow
: public MenuWindow
, public windows::Window {
public:
ChatPhotoWindow(Tdcurses *root, td::ActorId<Tdcurses> root_actor, td::tl_object_ptr<td::td_api::file> photo,
td::int32 height, td::int32 width)
: MenuWindow(root, root_actor), photo_(std::move(photo)), height_(height), width_(width) {
update();
}
~ChatPhotoWindow() {
if (download_id_ > 0) {
file_manager().unsubscribe_from_file_updates(photo_->id_, download_id_);
download_id_ = -1;
}
}
void download() {
if (download_id_ > 0) {
return;
}
auto file_id = photo_->id_;
download_id_ = file_manager().subscribe_to_file_updates(
file_id, [root = root(), self = this, self_id = window_unique_id()](const td::td_api::updateFile &upd) {
if (!root->window_exists(self_id)) {
return;
}
auto file = clone_td_file(*upd.file_);
self->photo_ = std::move(file);
self->update();
});
send_request(
td::make_tl_object<td::td_api::downloadFile>(file_id, 16, 0, 0, true),
[root = root(), self = this, self_id = window_unique_id()](td::Result<td::tl_object_ptr<td::td_api::file>> R) {
if (!root->window_exists(self_id)) {
return;
}
if (R.is_error()) {
return;
}
self->photo_ = R.move_as_ok();
self->update();
});
}
void update() {
set_need_refresh();
}
void render(windows::WindowOutputter &rb, bool force) override {
rb.erase_rect(0, 0, height(), width());
auto dir = windows::SavedRenderedImagesDirectory(std::move(saved_images_));
Outputter out;
if (photo_->local_->is_downloading_completed_) {
if (!global_parameters().image_path_is_allowed(photo_->local_->path_)) {
out << "IMAGES ARE DISABLED IN SETTINGS MENU";
} else {
Outputter::Photo p;
p.path = photo_->local_->path_;
p.height = height_;
p.width = width_;
p.allow_pixel = global_parameters().show_pixel_images();
out << p;
}
} else {
out << "LOADING ";
download();
}
windows::TextEdit::render(rb, width(), out.as_cslice(), 0, out.markup(), false, false, &dir);
saved_images_ = dir.release();
}
void handle_input(const windows::InputEvent &info) override {
menu_window_handle_input(info);
set_need_refresh();
}
std::shared_ptr<windows::Window> get_window(std::shared_ptr<MenuWindow> w) override {
return std::static_pointer_cast<ChatPhotoWindow>(std::move(w));
}
td::int32 best_width() override {
auto r = windows::empty_window_outputter().rendered_image_height(20, 40, height_, width_, "");
return r.second;
}
td::int32 best_height() override {
auto r = windows::empty_window_outputter().rendered_image_height(20, 40, height_, width_, "");
return r.first;
}
private:
td::tl_object_ptr<td::td_api::file> photo_;
td::int32 height_;
td::int32 width_;
td::int64 download_id_{-1};
windows::SavedRenderedImages saved_images_;
};
} // namespace tdcurses