Skip to content
Repos

mologram

C++ 5.1 KB127 lines Opens in site

Directory Raw Download

#include "mologram/ui/FloatingChat.hpp"
#include "mologram/ui/ComposerSuggest.hpp"

#include <algorithm>

#include "mologram/ui/MologramApp.hpp"
#include "mologram/ui/dialogs/ChatDialogs.hpp"

namespace mologram::ui {
using namespace ttgui;

// ------------------------------------------------------------------ ChatPane

ChatPane::ChatPane(UiContext& ctx, const Peer&) : ctx_(ctx) {
  const ttgui::Config* cfg = &ctx.app.cfg;
  header = emplace<ChatHeader>();
  pinned = emplace<PinnedBar>();
  view = emplace<MessageView>();
  typing = emplace<TypingLine>();
  composer = emplace<Composer>();
  menu = emplace<Menu>();
  menu->visible = false;
  header->cfg = cfg; pinned->cfg = cfg; view->cfg = cfg; typing->cfg = cfg; composer->cfg = cfg; menu->cfg = cfg;
  header->showBack = false;
  header->setActions({"Search", "Profile"});
  focusable = true;
  prefSize = {50, 18};

  ChatSurface surface;
  surface.header = header; surface.view = view; surface.composer = composer; surface.typing = typing; surface.pinned = pinned;
  surface.relayout = [this]() { layout(); };
  session_ = std::make_unique<ChatSession>(ctx.client, ctx.premium, surface);
  session_->attach();
  session_->suggest = std::make_unique<ComposerSuggest>(ctx, *session_);
  session_->toast = [this](const std::string& t, const std::string& b) { if (ctx_.toast) ctx_.toast(t, b); };
  session_->copyToClipboard = [this](const std::string& s) { if (ctx_.controller.copyToClipboard) ctx_.controller.copyToClipboard(s); };
  session_->requestForward = [this](const std::vector<long long>& ids) { ForwardDialog::open(ctx_, session_->peer(), ids); };
  session_->requestReactionPicker = [this](long long id) { ReactionPicker::open(ctx_, id); };
  session_->requestDelete = [this](const std::vector<long long>& ids) { DeleteDialog::open(ctx_, ids); };
  session_->isBotUsername = [this](const std::string& n) { return ctx_.controller.isBotUsername(n); };

  composer->onSend.connect([this](std::string t) { session_->send(t); });
  view->onNeedOlder.connect([this](long long oldest) {
    session_->loadPage(session_->peer(), oldest, 30, [this](std::vector<TgMessage> page, bool more) { view->prepend(std::move(page), more); });
  });
  view->onContext.connect([this](long long id, Point at) { openMenu(id, at); });
  header->onAction.connect([this](std::string a) {
    if (a == "Search") { Peer p = session_->peer(); SearchDialog::open(ctx_, &p); }
    else if (a == "Profile") ctx_.actions.run("profile");
  });
}

void ChatPane::openMenu(long long id, Point at) {
  bool own = false;
  for (const auto& m : view->msgs) if (m.id == id) own = m.outgoing;
  auto made = makeMessageMenu(&ctx_.app.cfg, own, [this, id](const std::string& name) {
    menu->open = false;
    menu->visible = false;
    session_->onMenu(name, id);
  });
  menu->items = made->items;
  Rect a = absoluteBounds();
  int mw = 16, mh = (int)menu->items.size() + 2;
  menu->open = true;
  menu->visible = true;
  menu->selected = 0;
  menu->bounds = {std::clamp(at.x - a.x, 0, std::max(0, bounds.w - mw)), std::clamp(at.y - a.y, 0, std::max(0, bounds.h - mh)), mw, mh};
}

void ChatPane::layout() {
  int W = bounds.w, H = bounds.h;
  bool compact = compactHeight(H);
  int hh = compact ? 1 : 2;
  bool showPin = !pinned->pins.empty() && !compact;
  bool showTyping = !typing->who.empty();
  pinned->visible = showPin;
  typing->visible = showTyping;
  int chH = std::min(composer->desiredHeight(), std::max(3, H / 2));
  header->bounds = {0, 0, W, hh};
  int y = hh;
  pinned->bounds = {0, y, W, 1};
  if (showPin) y++;
  int bottom = H - chH;
  int ty = showTyping ? 1 : 0;
  typing->bounds = {0, bottom - ty, W, 1};
  composer->bounds = {0, bottom, W, chH};
  view->bounds = {0, y, W, std::max(1, bottom - ty - y)};
  menu->visible = menu->open;
  for (auto& c : children_) c->layout();
}

void ChatPane::draw(Canvas& c) {
  layout();
  Rect a = absoluteBounds();
  c.fill(a, " ", tgdetail::classStyle(&ctx_.app.cfg, "grey"));
  typing->step(0);
}

// ------------------------------------------------------------------ FloatingChat

FloatingChat::FloatingChat(MologramApp& app, UiContext& ctx, const Peer& peer, const Rect& where) : app_(app), ctx_(ctx), peer_(peer) {
  window_ = ctx.app.wm.createWindow(peer.key(), "lightblue", where);
  window_->appName = "ChatWindow";
  window_->popFrames = 0;
  auto pane = std::make_unique<ChatPane>(ctx, peer);
  pane_ = pane.get();
  window_->setContent(std::move(pane));
  pane_->bounds = window_->contentRect();
  int unread = 0;
  std::string draft;
  if (const Dialog* d = ctx.controller.dialog(peer)) { unread = d->unread; draft = d->draft; if (!d->name.empty()) window_->title = d->name; }
  pane_->session().now = [] { return (std::int64_t)std::time(nullptr); };
  pane_->session().open(peer, unread, draft, true);
  ctx.controller.addSession(&pane_->session());
  window_->onClose.connect([this]() { app_.floatingWindowClosed(this); });
  window_->layout();
  ctx.app.setFocused(pane_->composer);
}

FloatingChat::~FloatingChat() = default;

void FloatingChat::refreshTitle() {
  if (!window_ || !pane_) return;
  if (!pane_->header->name.empty() && window_->title != pane_->header->name) window_->title = pane_->header->name;
}

}  // namespace mologram::ui
42.9ms