From 4b3a9fac67f61e37b678e98139fa3d74297d987c Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 29 Mar 2019 18:23:15 +0400 Subject: [PATCH] Use several LRU input languages. --- .../chat_helpers/emoji_keywords.cpp | 73 ++++++++++++------- .../SourceFiles/chat_helpers/emoji_keywords.h | 3 + 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp b/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp index c2447548c2..0762a17f77 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp @@ -20,7 +20,8 @@ namespace ChatHelpers { namespace { constexpr auto kRefreshEach = 60 * 60 * crl::time(1000); // 1 hour. -constexpr auto kKeepNotUsedLangPacksCount = 10; +constexpr auto kKeepNotUsedLangPacksCount = 4; +constexpr auto kKeepNotUsedInputLanguagesCount = 4; using namespace Ui::Emoji; @@ -45,30 +46,6 @@ struct LangPackData { return (code == 0x2122U) || (code == 0xA9U) || (code == 0xAEU); } -[[nodiscard]] std::vector KeywordLanguages() { - if (!AuthSession::Exists()) { - return {}; - } - auto result = std::vector(); - const auto yield = [&](const QString &language) { - result.push_back(language); - }; - const auto yieldLocale = [&](const QLocale &locale) { - for (const auto &language : locale.uiLanguages()) { - yield(language); - } - }; - yield(Lang::Current().id()); - yield(Lang::DefaultLanguageId()); - yield(Lang::CurrentCloudManager().suggestedLanguage()); - yield(Platform::SystemLanguage()); - yieldLocale(QLocale::system()); - if (const auto method = QGuiApplication::inputMethod()) { - yieldLocale(method->locale()); - } - return result; -} - [[nodiscard]] QString CacheFilePath(QString id) { static const auto BadSymbols = QRegularExpression("[^a-zA-Z0-9_\\.\\-]"); id.replace(BadSymbols, QString()); @@ -461,7 +438,7 @@ void EmojiKeywords::apiChanged(ApiWrap *api) { } void EmojiKeywords::refresh() { - auto list = KeywordLanguages(); + auto list = languages(); if (_localList != list) { _localList = std::move(list); refreshRemoteList(); @@ -470,6 +447,48 @@ void EmojiKeywords::refresh() { } } +std::vector EmojiKeywords::languages() { + if (!AuthSession::Exists()) { + return {}; + } + refreshInputLanguages(); + + auto result = std::vector(); + const auto yield = [&](const QString &language) { + result.push_back(language); + }; + const auto yieldList = [&](const QStringList &list) { + result.insert(end(result), list.begin(), list.end()); + }; + yield(Lang::Current().id()); + yield(Lang::DefaultLanguageId()); + yield(Lang::CurrentCloudManager().suggestedLanguage()); + yield(Platform::SystemLanguage()); + yieldList(QLocale::system().uiLanguages()); + for (const auto &list : _inputLanguages) { + yieldList(list); + } + ranges::sort(result); + return result; +} + +void EmojiKeywords::refreshInputLanguages() { + const auto method = QGuiApplication::inputMethod(); + if (!method) { + return; + } + const auto list = method->locale().uiLanguages(); + const auto i = ranges::find(_inputLanguages, list); + if (i != end(_inputLanguages)) { + std::rotate(i, i + 1, end(_inputLanguages)); + } else { + if (_inputLanguages.size() >= kKeepNotUsedInputLanguagesCount) { + _inputLanguages.pop_front(); + } + _inputLanguages.push_back(list); + } +} + rpl::producer<> EmojiKeywords::refreshed() const { return _refreshed.events(); } @@ -553,7 +572,7 @@ void EmojiKeywords::setRemoteList(std::vector &&list) { if (ranges::find(_remoteList, i->first) != end(_remoteList)) { ++i; } else { - if (_notUsedData.size() > kKeepNotUsedLangPacksCount) { + if (_notUsedData.size() >= kKeepNotUsedLangPacksCount) { _notUsedData.pop_front(); } _notUsedData.push_back(std::move(i->second)); diff --git a/Telegram/SourceFiles/chat_helpers/emoji_keywords.h b/Telegram/SourceFiles/chat_helpers/emoji_keywords.h index d69ec24d2f..2d31ea8aa1 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_keywords.h +++ b/Telegram/SourceFiles/chat_helpers/emoji_keywords.h @@ -54,6 +54,8 @@ private: void handleAuthSessionChanges(); void apiChanged(ApiWrap *api); + void refreshInputLanguages(); + [[nodiscard]] std::vector languages(); void refreshRemoteList(); void setRemoteList(std::vector &&list); void refreshFromRemoteList(); @@ -64,6 +66,7 @@ private: mtpRequestId _langsRequestId = 0; base::flat_map> _data; std::deque> _notUsedData; + std::deque _inputLanguages; rpl::event_stream<> _refreshed; rpl::lifetime _suggestedChangeLifetime;