#include "mologram/ui/media/SendMediaDialog.hpp" #include #include "mologram/ui/TgAdapter.hpp" #include "mologram/ui/media/MediaFormat.hpp" namespace mologram::ui::media { using namespace ttgui; namespace fs = std::filesystem; bool SendMediaDialog::albumPossible(const std::vector& files) { if (files.size() < 2) return false; for (const auto& f : files) { std::string k = sendKindForPath(f); if (k != "photo" && k != "video") return false; } return true; } std::string SendMediaDialog::describe(const std::vector& files, const std::string& chatName) { std::string t = "Send " + std::to_string(files.size()) + (files.size() == 1 ? " file" : " files") + " to " + chatName + "?\n\n"; for (const auto& f : files) { std::error_code ec; auto size = fs::file_size(f, ec); t += " " + fs::path(f).filename().string() + " (" + sendKindForPath(f) + ", " + TgAdapter::humanSize(ec ? 0 : (std::int64_t)size) + ")\n"; } return t; } Window* SendMediaDialog::open(Application& app, const std::vector& files, const std::string& chatName, std::function onSend) { Window* win = app.wm.createWindow("Send files", "lightblue", {8, 3, 64, 16}); win->appName = "SendMediaDialog"; win->modal = true; auto* box = new VBox(); auto* body = new TextWidget(); body->cfg = &app.cfg; body->text = describe(files, chatName); body->flex = 1; auto* caption = new EditBox(); caption->cfg = &app.cfg; caption->placeholder = "Caption (optional)"; auto* row = new HBox(); auto choose = [&app, win, caption, onSend](bool album, bool asDocument) { SendChoice c; c.album = album; c.asDocument = asDocument; c.caption = caption->text; app.wm.closeWindow(win); if (onSend) onSend(c); }; auto addButton = [&](const std::string& label, std::function fn) { auto* b = new PushButton(label); b->cfg = &app.cfg; b->onPressed.connect(std::move(fn)); row->addChild(std::unique_ptr(b)); return b; }; auto* send = addButton("Send", [choose]() { choose(false, false); }); addButton("As files", [choose]() { choose(false, true); }); if (albumPossible(files)) addButton("As album", [choose]() { choose(true, false); }); addButton("Cancel", [&app, win]() { app.wm.closeWindow(win); }); caption->onEnter.connect([choose]() { choose(false, false); }); box->addChild(std::unique_ptr(body)); box->addChild(std::unique_ptr(caption)); box->addChild(std::unique_ptr(row)); win->setContent(std::unique_ptr(box)); box->bounds = win->contentRect(); box->layout(); app.setFocused(caption); (void)send; return win; } } // namespace mologram::ui::media