Skip to content
Repos

mologram

C++ 2.7 KB71 lines Opens in site

Directory Raw Download

#include "mologram/ui/media/SendMediaDialog.hpp"

#include <filesystem>

#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<std::string>& 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<std::string>& 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<std::string>& files, const std::string& chatName,
                              std::function<void(const SendChoice&)> 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<void()> fn) {
    auto* b = new PushButton(label);
    b->cfg = &app.cfg;
    b->onPressed.connect(std::move(fn));
    row->addChild(std::unique_ptr<Widget>(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<Widget>(body));
  box->addChild(std::unique_ptr<Widget>(caption));
  box->addChild(std::unique_ptr<Widget>(row));
  win->setContent(std::unique_ptr<Widget>(box));
  box->bounds = win->contentRect();
  box->layout();
  app.setFocused(caption);
  (void)send;
  return win;
}

}  // namespace mologram::ui::media
30.2ms