Feed initial messages slice to translation tracker.

This commit is contained in:
John Preston 2023-02-07 17:29:30 +04:00
parent 64f4e0dd52
commit 4a37846605
7 changed files with 66 additions and 16 deletions

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/view/history_view_element.h" #include "history/view/history_view_element.h"
#include "history/view/history_view_item_preview.h" #include "history/view/history_view_item_preview.h"
#include "history/view/history_view_translate_tracker.h"
#include "dialogs/dialogs_indexed_list.h" #include "dialogs/dialogs_indexed_list.h"
#include "history/history_inner_widget.h" #include "history/history_inner_widget.h"
#include "history/history_item.h" #include "history/history_item.h"

View file

@ -555,6 +555,9 @@ void HistoryInner::messagesReceived(
const QVector<MTPMessage> &messages) { const QVector<MTPMessage> &messages) {
if (_history->peer == peer) { if (_history->peer == peer) {
_history->addOlderSlice(messages); _history->addOlderSlice(messages);
if (!messages.isEmpty()) {
_translateTracker->addBunchFromBlocks();
}
} else if (_migrated && _migrated->peer == peer) { } else if (_migrated && _migrated->peer == peer) {
const auto newLoaded = _migrated const auto newLoaded = _migrated
&& _migrated->isEmpty() && _migrated->isEmpty()

View file

@ -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) { for (auto e = end(_items), i = e - addedToEndCount; i != e; ++i) {
_itemRevealPending.emplace(*i); _itemRevealPending.emplace(*i);
} }

View file

@ -279,7 +279,7 @@ void TranslateBar::setup(not_null<History*> history) {
} }
}; };
const auto button = static_cast<Ui::AbstractButton*>(_wrap.entity()); const auto button = static_cast<Ui::AbstractButton*>(_wrap.entity());
button->resize(0, st::historyComposeButton.height); button->resize(0, st::historyTranslateBarHeight);
button->setAttribute(Qt::WA_OpaquePaintEvent); button->setAttribute(Qt::WA_OpaquePaintEvent);
button->paintRequest( button->paintRequest(
@ -619,7 +619,7 @@ int TranslateBar::height() const {
return !_forceHidden return !_forceHidden
? _wrap.height() ? _wrap.height()
: _shouldBeShown : _shouldBeShown
? st::historyComposeButton.height ? st::historyTranslateBarHeight
: 0; : 0;
} }

View file

@ -26,6 +26,7 @@ namespace {
constexpr auto kEnoughForRecognition = 10; constexpr auto kEnoughForRecognition = 10;
constexpr auto kEnoughForTranslation = 6; constexpr auto kEnoughForTranslation = 6;
constexpr auto kMaxCheckInBunch = 100;
constexpr auto kRequestLengthLimit = 24 * 1024; constexpr auto kRequestLengthLimit = 24 * 1024;
constexpr auto kRequestCountLimit = 20; constexpr auto kRequestCountLimit = 20;
@ -82,20 +83,20 @@ void TranslateTracker::startBunch() {
++_generation; ++_generation;
} }
void TranslateTracker::add(not_null<Element*> view) { bool TranslateTracker::add(not_null<Element*> view) {
const auto item = view->data(); const auto item = view->data();
const auto only = view->isOnlyEmojiAndSpaces(); const auto only = view->isOnlyEmojiAndSpaces();
if (only != OnlyEmojiAndSpaces::Unknown) { if (only != OnlyEmojiAndSpaces::Unknown) {
item->cacheOnlyEmojiAndSpaces(only == OnlyEmojiAndSpaces::Yes); item->cacheOnlyEmojiAndSpaces(only == OnlyEmojiAndSpaces::Yes);
} }
add(item, false); return add(item, false);
} }
void TranslateTracker::add(not_null<HistoryItem*> item) { bool TranslateTracker::add(not_null<HistoryItem*> item) {
add(item, false); return add(item, false);
} }
void TranslateTracker::add( bool TranslateTracker::add(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
bool skipDependencies) { bool skipDependencies) {
Expects(_addedInBunch >= 0); Expects(_addedInBunch >= 0);
@ -104,7 +105,7 @@ void TranslateTracker::add(
|| item->isService() || item->isService()
|| !item->isRegular() || !item->isRegular()
|| item->isOnlyEmojiAndSpaces()) { || item->isOnlyEmojiAndSpaces()) {
return; return false;
} }
if (item->translationShowRequiresCheck(_bunchTranslatedTo)) { if (item->translationShowRequiresCheck(_bunchTranslatedTo)) {
_switchTranslations[item] = _bunchTranslatedTo; _switchTranslations[item] = _bunchTranslatedTo;
@ -131,7 +132,7 @@ void TranslateTracker::add(
const auto i = _itemsForRecognize.find(id); const auto i = _itemsForRecognize.find(id);
if (i != end(_itemsForRecognize)) { if (i != end(_itemsForRecognize)) {
i->second.generation = _generation; i->second.generation = _generation;
return; return true;
} }
const auto &text = item->originalText().text; const auto &text = item->originalText().text;
_itemsForRecognize.emplace(id, ItemForRecognize{ _itemsForRecognize.emplace(id, ItemForRecognize{
@ -141,6 +142,7 @@ void TranslateTracker::add(
: MaybeLanguageId{ text }), : MaybeLanguageId{ text }),
}); });
++_addedInBunch; ++_addedInBunch;
return true;
} }
void TranslateTracker::switchTranslation( void TranslateTracker::switchTranslation(
@ -173,6 +175,43 @@ void TranslateTracker::finishBunch() {
requestSome(); 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<not_null<Element*>> &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() { void TranslateTracker::cancelToRequest() {
if (!_itemsToRequest.empty()) { if (!_itemsToRequest.empty()) {
const auto owner = &_history->owner(); const auto owner = &_history->owner();

View file

@ -23,10 +23,13 @@ public:
[[nodiscard]] bool enoughForRecognition() const; [[nodiscard]] bool enoughForRecognition() const;
void startBunch(); void startBunch();
void add(not_null<Element*> view); bool add(not_null<Element*> view);
void add(not_null<HistoryItem*> item); bool add(not_null<HistoryItem*> item);
void finishBunch(); void finishBunch();
void addBunchFromBlocks();
void addBunchFrom(const std::vector<not_null<Element*>> &views);
[[nodiscard]] rpl::producer<bool> trackingLanguage() const; [[nodiscard]] rpl::producer<bool> trackingLanguage() const;
private: private:
@ -40,7 +43,7 @@ private:
}; };
void setup(); void setup();
void add(not_null<HistoryItem*> item, bool skipDependencies); bool add(not_null<HistoryItem*> item, bool skipDependencies);
void recognizeCollected(); void recognizeCollected();
void trackSkipLanguages(); void trackSkipLanguages();
void checkRecognized(); void checkRecognized();

View file

@ -1237,18 +1237,19 @@ historyTranslateLabel: FlatLabel(defaultFlatLabel) {
minWidth: 80px; minWidth: 80px;
} }
historyTranslateIcon: icon{{ "menu/translate", windowActiveTextFg }}; historyTranslateIcon: icon{{ "menu/translate", windowActiveTextFg }};
historyTranslateBarHeight: 36px;
historyTranslateSettings: IconButton(defaultIconButton) { historyTranslateSettings: IconButton(defaultIconButton) {
width: 46px; width: 46px;
height: 46px; height: 36px;
icon: icon{{ "menu/customize", windowActiveTextFg }}; icon: icon{{ "menu/customize", windowActiveTextFg }};
iconOver: icon{{ "menu/customize", windowActiveTextFg }}; iconOver: icon{{ "menu/customize", windowActiveTextFg }};
rippleAreaPosition: point(4px, 4px); rippleAreaPosition: point(6px, 2px);
rippleAreaSize: 38px; rippleAreaSize: 32px;
ripple: RippleAnimation(defaultRippleAnimation) { ripple: RippleAnimation(defaultRippleAnimation) {
color: lightButtonBgOver; color: lightButtonBgOver;
} }
} }
historyTranslateMenuPosition: point(-6px, 40px); historyTranslateMenuPosition: point(-6px, 30px);
historySendDisabled: FlatLabel(defaultFlatLabel) { historySendDisabled: FlatLabel(defaultFlatLabel) {
minWidth: 10px; minWidth: 10px;