#include "mologram/ui/dialogs/EmojiDialog.hpp" #include "tgwidgets.hpp" #include "mologram/ui/dialogs/DialogUtil.hpp" namespace mologram::ui { using namespace ttgui; const std::vector& EmojiDialog::tabs() { static const std::vector v = {"Emoji", "Recent", "Stickers", "Custom emoji"}; return v; } Window* EmojiDialog::open(UiContext& c) { UiContext* cp = &c; auto f = c.kit.open(c.t("title.emoji"), "EmojiDialog", 50, 22, false); auto alive = dlg::track(f.win); Window* win = f.win; struct Item { std::string label, id, fallback; std::int64_t doc = 0; bool premium = false; }; struct State { int tab = 0; std::vector items; int generation = 0; }; auto st = std::make_shared(); auto* tab = c.kit.combo(f, tabs(), 0); auto* picker = new EmojiPicker(); picker->cfg = &c.app.cfg; picker->flex = 1; f.box->addChild(std::unique_ptr(picker)); auto* search = c.kit.edit(f, c.t("label.search")); search->visible = false; auto* list = c.kit.list(f, {}); list->visible = false; auto insert = [cp](const std::string& glyph) { if (ChatSession* s = cp->activeSession ? cp->activeSession() : nullptr) s->insertText(glyph); cp->prefs.addRecentEmoji(glyph); std::string err; cp->prefs.save(err); }; picker->onPick.connect(insert); auto reload = [cp, st, list, search, alive, insert]() { int gen = ++st->generation; st->items.clear(); list->items.clear(); list->selected = -1; auto fill = [st, list]() { list->items.clear(); for (const auto& i : st->items) list->items.push_back(i.label); list->selected = list->items.empty() ? -1 : 0; }; if (st->tab == 1) { for (const auto& e : cp->prefs.recentEmoji()) st->items.push_back({e, e, e, 0, false}); fill(); return; } js::Object q; if (!search->text.empty()) q["query"] = js::Value(search->text); if (st->tab == 2) { cp->client.request("load stickers", "stickers.search", std::move(q), [cp, st, gen, alive, fill](const RpcResult& r) { if (!*alive || gen != st->generation) return; if (const js::Array* a = js::getArr(r.value, "stickers")) for (const auto& e : *a) { bool prem = js::getBool(e, "premium"); st->items.push_back({cp->premium.label(js::getStr(e, "emoji") + " " + js::getStr(e, "title"), prem && cp->cfg.flag("ascii_mode")), js::getStr(e, "id"), "", 0, prem}); } fill(); }, [](const RpcError&) {}); } else if (st->tab == 3) { cp->client.request("load custom emoji", "emoji.custom", std::move(q), [cp, st, gen, alive, fill](const RpcResult& r) { if (!*alive || gen != st->generation) return; if (const js::Array* a = js::getArr(r.value, "emoji")) for (const auto& e : *a) st->items.push_back({cp->premium.label(js::getStr(e, "fallback") + " " + js::getStr(e, "title"), cp->cfg.flag("ascii_mode")), "", js::getStr(e, "fallback"), js::getId(e, "id"), true}); fill(); }, [](const RpcError&) {}); } (void)insert; }; tab->onSelected.connect([=](int i) { st->tab = i; picker->visible = (i == 0); list->visible = (i != 0); search->visible = (i >= 2); win->layout(); if (i != 0) reload(); }); search->onChanged.connect([reload](std::string) { reload(); }); list->onActivated.connect([cp, st, insert](int i) { if (i < 0 || i >= (int)st->items.size()) return; Item it = st->items[(size_t)i]; ChatSession* s = cp->activeSession ? cp->activeSession() : nullptr; if (!s) return; if (st->tab == 1) { insert(it.id); return; } if (st->tab == 2) { auto go = [s, it]() { s->sendSticker(it.id); }; if (it.premium) cp->premium.use("premium_stickers", go); else go(); return; } cp->premium.use("custom_emoji_message", [s, it, insert]() { s->insertText(it.fallback); s->addCustomEmoji(it.fallback, it.doc); }); }); auto* row = c.kit.row(f); c.kit.rowButton(row, c.t("btn.close"), [cp, win]() { cp->kit.close(win); }); c.kit.show(f, picker->search); return f.win; } } // namespace mologram::ui