mirror of
https://github.com/kotatogram/kotatogram-desktop
synced 2025-08-31 06:35:14 +00:00
Provide custom emoji factory through MarkedTextContext.
This commit is contained in:
@@ -65,6 +65,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "data/data_histories.h"
|
||||
#include "data/data_peer_values.h"
|
||||
#include "data/data_premium_limits.h"
|
||||
#include "data/stickers/data_custom_emoji.h"
|
||||
#include "base/platform/base_platform_info.h"
|
||||
#include "base/unixtime.h"
|
||||
#include "base/call_delayed.h"
|
||||
@@ -246,7 +247,8 @@ Session::Session(not_null<Main::Session*> session)
|
||||
, _stickers(std::make_unique<Stickers>(this))
|
||||
, _sponsoredMessages(std::make_unique<SponsoredMessages>(this))
|
||||
, _reactions(std::make_unique<Reactions>(this))
|
||||
, _notifySettings(std::make_unique<NotifySettings>(this)) {
|
||||
, _notifySettings(std::make_unique<NotifySettings>(this))
|
||||
, _customEmojiManager(std::make_unique<CustomEmojiManager>(this)) {
|
||||
_cache->open(_session->local().cacheKey());
|
||||
_bigFileCache->open(_session->local().cacheBigFileKey());
|
||||
|
||||
|
@@ -62,6 +62,7 @@ class PhotoMedia;
|
||||
class Stickers;
|
||||
class GroupCall;
|
||||
class NotifySettings;
|
||||
class CustomEmojiManager;
|
||||
|
||||
class Session final {
|
||||
public:
|
||||
@@ -120,6 +121,9 @@ public:
|
||||
[[nodiscard]] NotifySettings ¬ifySettings() const {
|
||||
return *_notifySettings;
|
||||
}
|
||||
[[nodiscard]] CustomEmojiManager &customEmojiManager() const {
|
||||
return *_customEmojiManager;
|
||||
}
|
||||
|
||||
[[nodiscard]] MsgId nextNonHistoryEntryId() {
|
||||
return ++_nonHistoryEntryId;
|
||||
@@ -978,6 +982,7 @@ private:
|
||||
std::unique_ptr<SponsoredMessages> _sponsoredMessages;
|
||||
const std::unique_ptr<Reactions> _reactions;
|
||||
const std::unique_ptr<NotifySettings> _notifySettings;
|
||||
const std::unique_ptr<CustomEmojiManager> _customEmojiManager;
|
||||
|
||||
MsgId _nonHistoryEntryId = ServerMaxMsgId.bare + ScheduledMsgIdsRange;
|
||||
|
||||
|
182
Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp
Normal file
182
Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop application for the Telegram messaging service.
|
||||
|
||||
For license and copyright information please follow this link:
|
||||
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#include "data/stickers/data_custom_emoji.h"
|
||||
|
||||
#include "chat_helpers/stickers_emoji_pack.h"
|
||||
#include "main/main_session.h"
|
||||
#include "data/data_session.h"
|
||||
#include "data/data_document.h"
|
||||
#include "ui/text/text_block.h"
|
||||
|
||||
namespace Data {
|
||||
namespace {
|
||||
|
||||
struct CustomEmojiId {
|
||||
StickerSetIdentifier set;
|
||||
uint64 id = 0;
|
||||
};
|
||||
[[nodiscard]] QString SerializeCustomEmojiId(const CustomEmojiId &id) {
|
||||
const auto &info = id.set;
|
||||
const auto set = info.id
|
||||
? (QString::number(info.id) + ':' + QString::number(info.accessHash))
|
||||
: info.shortName;
|
||||
return QString::number(id.id) + '@' + set;
|
||||
}
|
||||
|
||||
[[nodiscard]] CustomEmojiId ParseCustomEmojiData(const QString &data) {
|
||||
const auto parts = data.split('@');
|
||||
if (parts.size() != 2) {
|
||||
return {};
|
||||
}
|
||||
const auto id = parts[0].toULongLong();
|
||||
if (!id) {
|
||||
return {};
|
||||
}
|
||||
const auto second = parts[1].split(':');
|
||||
if (const auto set = second[0].toULongLong()) {
|
||||
return {
|
||||
.set = {.id = set, .accessHash = second[1].toULongLong() },
|
||||
.id = id
|
||||
};
|
||||
}
|
||||
return {
|
||||
.set = {.shortName = second[1] },
|
||||
.id = id
|
||||
};
|
||||
}
|
||||
|
||||
class CustomEmojiWithData : public Ui::Text::CustomEmoji {
|
||||
public:
|
||||
explicit CustomEmojiWithData(const QString &data);
|
||||
|
||||
QString entityData() final override;
|
||||
|
||||
private:
|
||||
const QString _data;
|
||||
|
||||
};
|
||||
|
||||
CustomEmojiWithData::CustomEmojiWithData(const QString &data) : _data(data) {
|
||||
}
|
||||
|
||||
QString CustomEmojiWithData::entityData() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
class DocumentCustomEmoji final : public CustomEmojiWithData {
|
||||
public:
|
||||
DocumentCustomEmoji(
|
||||
const QString &data,
|
||||
not_null<DocumentData*> document);
|
||||
|
||||
void paint(QPainter &p, int x, int y) override;
|
||||
|
||||
private:
|
||||
not_null<DocumentData*> _document;
|
||||
|
||||
};
|
||||
|
||||
DocumentCustomEmoji::DocumentCustomEmoji(
|
||||
const QString &data,
|
||||
not_null<DocumentData*> document)
|
||||
: CustomEmojiWithData(data)
|
||||
, _document(document) {
|
||||
}
|
||||
|
||||
void DocumentCustomEmoji::paint(QPainter &p, int x, int y) {
|
||||
const auto size = Ui::Emoji::GetSizeNormal() / style::DevicePixelRatio();
|
||||
p.fillRect(QRect{ x, y, size, size }, Qt::red);
|
||||
}
|
||||
|
||||
class ResolvingCustomEmoji final : public CustomEmojiWithData {
|
||||
public:
|
||||
explicit ResolvingCustomEmoji(const QString &data);
|
||||
|
||||
void paint(QPainter &p, int x, int y) override;
|
||||
|
||||
private:
|
||||
std::optional<DocumentCustomEmoji> _resolved;
|
||||
|
||||
};
|
||||
|
||||
ResolvingCustomEmoji::ResolvingCustomEmoji(const QString &data)
|
||||
: CustomEmojiWithData(data) {
|
||||
}
|
||||
|
||||
void ResolvingCustomEmoji::paint(QPainter &p, int x, int y) {
|
||||
if (_resolved) {
|
||||
_resolved->paint(p, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CustomEmojiManager::CustomEmojiManager(not_null<Session*> owner)
|
||||
: _owner(owner) {
|
||||
}
|
||||
|
||||
CustomEmojiManager::~CustomEmojiManager() = default;
|
||||
|
||||
std::unique_ptr<Ui::Text::CustomEmoji> CustomEmojiManager::create(
|
||||
const QString &data) {
|
||||
const auto parsed = ParseCustomEmojiData(data);
|
||||
if (!parsed.id) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto document = _owner->document(parsed.id);
|
||||
if (!document->isNull()) {
|
||||
return std::make_unique<DocumentCustomEmoji>(data, document);
|
||||
}
|
||||
return std::make_unique<ResolvingCustomEmoji>(data);
|
||||
}
|
||||
|
||||
Main::Session &CustomEmojiManager::session() const {
|
||||
return _owner->session();
|
||||
}
|
||||
|
||||
Session &CustomEmojiManager::owner() const {
|
||||
return *_owner;
|
||||
}
|
||||
|
||||
void FillTestCustomEmoji(
|
||||
not_null<Main::Session*> session,
|
||||
TextWithEntities &text) {
|
||||
const auto pack = &session->emojiStickersPack();
|
||||
const auto begin = text.text.constData(), end = begin + text.text.size();
|
||||
for (auto ch = begin; ch != end;) {
|
||||
auto length = 0;
|
||||
if (const auto emoji = Ui::Emoji::Find(ch, end, &length)) {
|
||||
if (const auto found = pack->stickerForEmoji(emoji)) {
|
||||
Expects(found.document->sticker() != nullptr);
|
||||
|
||||
text.entities.push_back({
|
||||
EntityType::CustomEmoji,
|
||||
(ch - begin),
|
||||
length,
|
||||
SerializeCustomEmojiId({
|
||||
found.document->sticker()->set,
|
||||
found.document->id,
|
||||
}),
|
||||
});
|
||||
}
|
||||
ch += length;
|
||||
} else if (ch->isHighSurrogate()
|
||||
&& (ch + 1 != end)
|
||||
&& (ch + 1)->isLowSurrogate()) {
|
||||
ch += 2;
|
||||
} else {
|
||||
++ch;
|
||||
}
|
||||
}
|
||||
ranges::stable_sort(
|
||||
text.entities,
|
||||
ranges::less(),
|
||||
&EntityInText::offset);
|
||||
}
|
||||
|
||||
} // namespace Data
|
38
Telegram/SourceFiles/data/stickers/data_custom_emoji.h
Normal file
38
Telegram/SourceFiles/data/stickers/data_custom_emoji.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop application for the Telegram messaging service.
|
||||
|
||||
For license and copyright information please follow this link:
|
||||
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
namespace Main {
|
||||
class Session;
|
||||
} // namespace Main
|
||||
|
||||
namespace Data {
|
||||
|
||||
class Session;
|
||||
|
||||
class CustomEmojiManager final {
|
||||
public:
|
||||
CustomEmojiManager(not_null<Session*> owner);
|
||||
~CustomEmojiManager();
|
||||
|
||||
[[nodiscard]] std::unique_ptr<Ui::Text::CustomEmoji> create(
|
||||
const QString &data);
|
||||
|
||||
[[nodiscard]] Main::Session &session() const;
|
||||
[[nodiscard]] Session &owner() const;
|
||||
|
||||
private:
|
||||
const not_null<Session*> _owner;
|
||||
|
||||
};
|
||||
|
||||
void FillTestCustomEmoji(
|
||||
not_null<Main::Session*> session,
|
||||
TextWithEntities &text);
|
||||
|
||||
} // namespace Data
|
Reference in New Issue
Block a user