diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index e4aaa7a61..77251f700 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_element.h" #include "history/view/history_view_item_preview.h" +#include "history/view/history_view_translate_tracker.h" #include "dialogs/dialogs_indexed_list.h" #include "history/history_inner_widget.h" #include "history/history_item.h" diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index f297ad57f..90dd68bc6 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -555,6 +555,9 @@ void HistoryInner::messagesReceived( const QVector &messages) { if (_history->peer == peer) { _history->addOlderSlice(messages); + if (!messages.isEmpty()) { + _translateTracker->addBunchFromBlocks(); + } } else if (_migrated && _migrated->peer == peer) { const auto newLoaded = _migrated && _migrated->isEmpty() diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index 2145504db..85347cb76 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -523,6 +523,9 @@ void ListWidget::refreshRows(const Data::MessagesSlice &old) { } } } + if (_translateTracker) { + _translateTracker->addBunchFrom(_items); + } for (auto e = end(_items), i = e - addedToEndCount; i != e; ++i) { _itemRevealPending.emplace(*i); } diff --git a/Telegram/SourceFiles/history/view/history_view_translate_bar.cpp b/Telegram/SourceFiles/history/view/history_view_translate_bar.cpp index a9e01fd57..9797a69d4 100644 --- a/Telegram/SourceFiles/history/view/history_view_translate_bar.cpp +++ b/Telegram/SourceFiles/history/view/history_view_translate_bar.cpp @@ -279,7 +279,7 @@ void TranslateBar::setup(not_null history) { } }; const auto button = static_cast(_wrap.entity()); - button->resize(0, st::historyComposeButton.height); + button->resize(0, st::historyTranslateBarHeight); button->setAttribute(Qt::WA_OpaquePaintEvent); button->paintRequest( @@ -619,7 +619,7 @@ int TranslateBar::height() const { return !_forceHidden ? _wrap.height() : _shouldBeShown - ? st::historyComposeButton.height + ? st::historyTranslateBarHeight : 0; } diff --git a/Telegram/SourceFiles/history/view/history_view_translate_tracker.cpp b/Telegram/SourceFiles/history/view/history_view_translate_tracker.cpp index 77fcbfa10..5a3ca97bc 100644 --- a/Telegram/SourceFiles/history/view/history_view_translate_tracker.cpp +++ b/Telegram/SourceFiles/history/view/history_view_translate_tracker.cpp @@ -26,6 +26,7 @@ namespace { constexpr auto kEnoughForRecognition = 10; constexpr auto kEnoughForTranslation = 6; +constexpr auto kMaxCheckInBunch = 100; constexpr auto kRequestLengthLimit = 24 * 1024; constexpr auto kRequestCountLimit = 20; @@ -82,20 +83,20 @@ void TranslateTracker::startBunch() { ++_generation; } -void TranslateTracker::add(not_null view) { +bool TranslateTracker::add(not_null view) { const auto item = view->data(); const auto only = view->isOnlyEmojiAndSpaces(); if (only != OnlyEmojiAndSpaces::Unknown) { item->cacheOnlyEmojiAndSpaces(only == OnlyEmojiAndSpaces::Yes); } - add(item, false); + return add(item, false); } -void TranslateTracker::add(not_null item) { - add(item, false); +bool TranslateTracker::add(not_null item) { + return add(item, false); } -void TranslateTracker::add( +bool TranslateTracker::add( not_null item, bool skipDependencies) { Expects(_addedInBunch >= 0); @@ -104,7 +105,7 @@ void TranslateTracker::add( || item->isService() || !item->isRegular() || item->isOnlyEmojiAndSpaces()) { - return; + return false; } if (item->translationShowRequiresCheck(_bunchTranslatedTo)) { _switchTranslations[item] = _bunchTranslatedTo; @@ -131,7 +132,7 @@ void TranslateTracker::add( const auto i = _itemsForRecognize.find(id); if (i != end(_itemsForRecognize)) { i->second.generation = _generation; - return; + return true; } const auto &text = item->originalText().text; _itemsForRecognize.emplace(id, ItemForRecognize{ @@ -141,6 +142,7 @@ void TranslateTracker::add( : MaybeLanguageId{ text }), }); ++_addedInBunch; + return true; } void TranslateTracker::switchTranslation( @@ -173,6 +175,43 @@ void TranslateTracker::finishBunch() { requestSome(); } +void TranslateTracker::addBunchFromBlocks() { + if (enoughForRecognition()) { + return; + } + startBunch(); + const auto guard = gsl::finally([&] { + finishBunch(); + }); + + auto check = kMaxCheckInBunch; + for (const auto &block : _history->blocks) { + for (const auto &view : block->messages) { + if (!check-- || (add(view.get()) && enoughForRecognition())) { + return; + } + } + } +} + +void TranslateTracker::addBunchFrom( + const std::vector> &views) { + if (enoughForRecognition()) { + return; + } + startBunch(); + const auto guard = gsl::finally([&] { + finishBunch(); + }); + + auto check = kMaxCheckInBunch; + for (const auto &view : views) { + if (!check-- || (add(view.get()) && enoughForRecognition())) { + return; + } + } +} + void TranslateTracker::cancelToRequest() { if (!_itemsToRequest.empty()) { const auto owner = &_history->owner(); diff --git a/Telegram/SourceFiles/history/view/history_view_translate_tracker.h b/Telegram/SourceFiles/history/view/history_view_translate_tracker.h index 77f9c4e32..9e2ca8b69 100644 --- a/Telegram/SourceFiles/history/view/history_view_translate_tracker.h +++ b/Telegram/SourceFiles/history/view/history_view_translate_tracker.h @@ -23,10 +23,13 @@ public: [[nodiscard]] bool enoughForRecognition() const; void startBunch(); - void add(not_null view); - void add(not_null item); + bool add(not_null view); + bool add(not_null item); void finishBunch(); + void addBunchFromBlocks(); + void addBunchFrom(const std::vector> &views); + [[nodiscard]] rpl::producer trackingLanguage() const; private: @@ -40,7 +43,7 @@ private: }; void setup(); - void add(not_null item, bool skipDependencies); + bool add(not_null item, bool skipDependencies); void recognizeCollected(); void trackSkipLanguages(); void checkRecognized(); diff --git a/Telegram/SourceFiles/ui/chat/chat.style b/Telegram/SourceFiles/ui/chat/chat.style index 7e70cce7b..104c72181 100644 --- a/Telegram/SourceFiles/ui/chat/chat.style +++ b/Telegram/SourceFiles/ui/chat/chat.style @@ -1237,18 +1237,19 @@ historyTranslateLabel: FlatLabel(defaultFlatLabel) { minWidth: 80px; } historyTranslateIcon: icon{{ "menu/translate", windowActiveTextFg }}; +historyTranslateBarHeight: 36px; historyTranslateSettings: IconButton(defaultIconButton) { width: 46px; - height: 46px; + height: 36px; icon: icon{{ "menu/customize", windowActiveTextFg }}; iconOver: icon{{ "menu/customize", windowActiveTextFg }}; - rippleAreaPosition: point(4px, 4px); - rippleAreaSize: 38px; + rippleAreaPosition: point(6px, 2px); + rippleAreaSize: 32px; ripple: RippleAnimation(defaultRippleAnimation) { color: lightButtonBgOver; } } -historyTranslateMenuPosition: point(-6px, 40px); +historyTranslateMenuPosition: point(-6px, 30px); historySendDisabled: FlatLabel(defaultFlatLabel) { minWidth: 10px;