@@ -3787,6 +3787,9 @@ void OverlayWidget::displayPhoto(
37873787 if (photoChanged) {
37883788 _showRecognitionResults = false ;
37893789 _recognitionResult = {};
3790+ _recognitionPendingSessionUniqueId = 0 ;
3791+ _recognitionPendingPhotoId = 0 ;
3792+ _recognitionRetryOnLarge = false ;
37903793 }
37913794
37923795 refreshMediaViewer ();
@@ -3796,41 +3799,7 @@ void OverlayWidget::displayPhoto(
37963799 initStreaming ();
37973800 }
37983801
3799- if (!_stories && Platform::TextRecognition::IsAvailable ()) {
3800- const auto cache = RecognitionCache ();
3801- const auto id = RecognitionId{
3802- .sessionUniqueId = _session->uniqueId (),
3803- .photoId = _photo->id ,
3804- };
3805- if (const auto cached = cache->find (id)
3806- ; cached != cache->end ()) {
3807- _recognitionResult = cached->second ;
3808- updateControls ();
3809- } else {
3810- const auto weak = base::make_weak (_widget);
3811- const auto photoMedia = _photoMedia;
3812- crl::async ([=] {
3813- const auto image = photoMedia->image (
3814- Data::PhotoSize::Large);
3815- if (!image) {
3816- return ;
3817- }
3818- const auto original = image->original ();
3819- if (original.isNull ()) {
3820- return ;
3821- }
3822- auto result = Platform::TextRecognition::RecognizeText (
3823- original);
3824- crl::on_main (weak, [=, result = std::move (result)]() mutable {
3825- const auto cache = RecognitionCache ();
3826- _recognitionResult = std::move (result);
3827- auto &cached = (*cache)[id];
3828- cached = _recognitionResult;
3829- updateControls ();
3830- });
3831- });
3832- }
3833- }
3802+ tryStartTextRecognition ();
38343803
38353804 initSponsoredButton ();
38363805
@@ -3896,6 +3865,9 @@ void OverlayWidget::displayDocument(
38963865 if (documentChanged) {
38973866 _showRecognitionResults = false ;
38983867 _recognitionResult = {};
3868+ _recognitionPendingSessionUniqueId = 0 ;
3869+ _recognitionPendingPhotoId = 0 ;
3870+ _recognitionRetryOnLarge = false ;
38993871 }
39003872
39013873 _touchbarDisplay.fire (TouchBarItemType::None);
@@ -5159,6 +5131,85 @@ void OverlayWidget::validatePhotoCurrentImage() {
51595131 if (_staticContent.isNull ()) {
51605132 _photoMedia->wanted (Data::PhotoSize::Small, fileOrigin ());
51615133 }
5134+ if (_recognitionRetryOnLarge) {
5135+ if (const auto image = _photoMedia->image (Data::PhotoSize::Large)
5136+ ; image && !image->original ().isNull ()) {
5137+ tryStartTextRecognition ();
5138+ }
5139+ }
5140+ }
5141+
5142+ void OverlayWidget::tryStartTextRecognition () {
5143+ if (_stories
5144+ || !_session
5145+ || !_photo
5146+ || !_photoMedia
5147+ || !Platform::TextRecognition::IsAvailable ()) {
5148+ return ;
5149+ }
5150+ const auto cache = RecognitionCache ();
5151+ if (!cache) {
5152+ return ;
5153+ }
5154+ const auto id = RecognitionId{
5155+ .sessionUniqueId = _session->uniqueId (),
5156+ .photoId = _photo->id ,
5157+ };
5158+ if (const auto cached = cache->find (id); cached != cache->end ()) {
5159+ _recognitionRetryOnLarge = false ;
5160+ const auto changed = (_recognitionResult.success != cached->second .success )
5161+ || (_recognitionResult.items .size () != cached->second .items .size ());
5162+ _recognitionResult = cached->second ;
5163+ if (changed) {
5164+ updateControls ();
5165+ }
5166+ return ;
5167+ }
5168+ if (_recognitionPendingSessionUniqueId == id.sessionUniqueId
5169+ && _recognitionPendingPhotoId == id.photoId ) {
5170+ _recognitionRetryOnLarge = false ;
5171+ return ;
5172+ }
5173+ _photoMedia->wanted (Data::PhotoSize::Large, fileOrigin ());
5174+ const auto image = _photoMedia->image (Data::PhotoSize::Large);
5175+ if (!image) {
5176+ _recognitionRetryOnLarge = true ;
5177+ return ;
5178+ }
5179+ const auto original = image->original ();
5180+ if (original.isNull ()) {
5181+ _recognitionRetryOnLarge = true ;
5182+ return ;
5183+ }
5184+ _recognitionRetryOnLarge = false ;
5185+ _recognitionPendingSessionUniqueId = id.sessionUniqueId ;
5186+ _recognitionPendingPhotoId = id.photoId ;
5187+ const auto weak = base::make_weak (_widget);
5188+ crl::async ([=] {
5189+ auto result = Platform::TextRecognition::RecognizeText (original);
5190+ crl::on_main (weak, [=, result = std::move (result)]() mutable {
5191+ const auto cache = RecognitionCache ();
5192+ if (!cache) {
5193+ return ;
5194+ }
5195+ const auto pendingMatches = (_recognitionPendingSessionUniqueId
5196+ == id.sessionUniqueId )
5197+ && (_recognitionPendingPhotoId == id.photoId );
5198+ if (pendingMatches) {
5199+ _recognitionPendingSessionUniqueId = 0 ;
5200+ _recognitionPendingPhotoId = 0 ;
5201+ }
5202+ (*cache)[id] = result;
5203+ if (!_session
5204+ || !_photo
5205+ || (_session->uniqueId () != id.sessionUniqueId )
5206+ || (_photo->id != id.photoId )) {
5207+ return ;
5208+ }
5209+ _recognitionResult = std::move (result);
5210+ updateControls ();
5211+ });
5212+ });
51625213}
51635214
51645215Ui::GL::ChosenRenderer OverlayWidget::chooseRenderer (
@@ -6953,6 +7004,9 @@ void OverlayWidget::clearBeforeHide() {
69537004
69547005void OverlayWidget::clearAfterHide () {
69557006 _recognitionResult = {};
7007+ _recognitionPendingSessionUniqueId = 0 ;
7008+ _recognitionPendingPhotoId = 0 ;
7009+ _recognitionRetryOnLarge = false ;
69567010 _body->hide ();
69577011 clearStreaming ();
69587012 destroyThemePreview ();
0 commit comments