Skip to content

feat: PdfTextSelection.selectedTextIfLoaded returns the selection without waiting - #733

Open
palmoni5 wants to merge 2 commits into
espresso3389:masterfrom
palmoni5:feat/selected-text-if-loaded
Open

palmoni5 wants to merge 2 commits into
espresso3389:masterfrom
palmoni5:feat/selected-text-if-loaded

Conversation

@palmoni5

@palmoni5 palmoni5 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

PdfTextSelection.getSelectedText() is async, so code that builds UI synchronously can't show the selected text, for example a context-menu builder that puts the selection into a tooltip such as Search "…". The only real wait is for a selection that spans three or more pages, where the text of the pages in between may not be loaded yet. The text of both ends is always loaded, and the pages in between are almost always cached already, because the user dragged the selection through them.

Change

  • New PdfTextSelection.selectedTextIfLoaded: returns the selected text right away from the viewer's text cache, or null when a page in between has not loaded its text yet. Callers can fall back to getSelectedText().
  • getSelectedTextRanges() and getSelectedText() are unchanged. They keep the selection ends they started with while the pages in between load, so clearing or replacing the selection meanwhile doesn't change the result of an extraction that is already running.

Adding an abstract member to PdfTextSelection would break third-party implementers. The only implementer in the package is _PdfViewerState.

Tests

New tests in pdf_viewer_selection_drag_test.dart:

  • selectedTextIfLoaded on a single-page selection and on a three-page selection, where it equals await getSelectedText().
  • On a 30-page document, selectedTextIfLoaded is null until the pages in between are loaded, and then returns the full selection.
  • A pending getSelectedText() keeps its selection when the selection is cleared or replaced.

flutter analyze and flutter test in packages/pdfrx pass.

…hout waiting

getSelectedText() is async only because a selection spanning three or more pages may need the text of the pages in between. The text of both ends is always loaded, and the pages in between are usually cached, so a synchronous menu or tooltip builder can show the selection right away. selectedTextIfLoaded returns it from the cache, or null when a page in between is not cached yet.

getSelectedTextRanges() now loads the pages in between and then reads the same cache, so both paths share one implementation.
palmoni5 added a commit to palmoni5/otzaria that referenced this pull request Oct 5, 2026
pdfrx החזיר את הטקסט המסומן רק אחרי המתנה (Future), ולכן התפריט, שנבנה בלי לחכות, לא יכול היה לצטט אותו. בפורק נוסף selectedTextIfLoaded, שמחזיר את הבחירה מיד מתוך מטמון הטקסט, או null אם עמוד באמצע הבחירה טרם נטען (espresso3389/pdfrx#733).

- buildSearchAllBooksIconAction מקבל ReaderMenuSelection בשתי התצוגות, והרמז נבנה במקום אחד.
- ב-PDF הבחירה נקראת בפתיחת התפריט ומשמשת גם לחיפוש עצמו. רק כשהיא עוד לא טעונה ממתינים ל-getSelectedText.
- pdfrx ו-pdfrx_engine מוצמדים ל-ff9785dd בפורק, ו-Otzaria#733 נוסף לרשימת ה-PR שאחריהם חוזרים ל-pub.dev.
Y-PLONI pushed a commit to palmoni5/otzaria that referenced this pull request Oct 5, 2026
pdfrx החזיר את הטקסט המסומן רק אחרי המתנה (Future), ולכן התפריט, שנבנה בלי לחכות, לא יכול היה לצטט אותו. בפורק נוסף selectedTextIfLoaded, שמחזיר את הבחירה מיד מתוך מטמון הטקסט, או null אם עמוד באמצע הבחירה טרם נטען (espresso3389/pdfrx#733).

- buildSearchAllBooksIconAction מקבל ReaderMenuSelection בשתי התצוגות, והרמז נבנה במקום אחד.
- ב-PDF הבחירה נקראת בפתיחת התפריט ומשמשת גם לחיפוש עצמו. רק כשהיא עוד לא טעונה ממתינים ל-getSelectedText.
- pdfrx ו-pdfrx_engine מוצמדים ל-ff9785dd בפורק, ו-Otzaria#733 נוסף לרשימת ה-PR שאחריהם חוזרים ל-pub.dev.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants