mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-02-24 17:26:58 +00:00
Use several LRU input languages.
This commit is contained in:
parent
54ca5772f8
commit
4b3a9fac67
@ -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<QString> KeywordLanguages() {
|
||||
if (!AuthSession::Exists()) {
|
||||
return {};
|
||||
}
|
||||
auto result = std::vector<QString>();
|
||||
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<QString> EmojiKeywords::languages() {
|
||||
if (!AuthSession::Exists()) {
|
||||
return {};
|
||||
}
|
||||
refreshInputLanguages();
|
||||
|
||||
auto result = std::vector<QString>();
|
||||
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<QString> &&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));
|
||||
|
@ -54,6 +54,8 @@ private:
|
||||
|
||||
void handleAuthSessionChanges();
|
||||
void apiChanged(ApiWrap *api);
|
||||
void refreshInputLanguages();
|
||||
[[nodiscard]] std::vector<QString> languages();
|
||||
void refreshRemoteList();
|
||||
void setRemoteList(std::vector<QString> &&list);
|
||||
void refreshFromRemoteList();
|
||||
@ -64,6 +66,7 @@ private:
|
||||
mtpRequestId _langsRequestId = 0;
|
||||
base::flat_map<QString, std::unique_ptr<LangPack>> _data;
|
||||
std::deque<std::unique_ptr<LangPack>> _notUsedData;
|
||||
std::deque<QStringList> _inputLanguages;
|
||||
rpl::event_stream<> _refreshed;
|
||||
|
||||
rpl::lifetime _suggestedChangeLifetime;
|
||||
|
Loading…
Reference in New Issue
Block a user