pdfrx icon indicating copy to clipboard operation
pdfrx copied to clipboard

Is there an API to clear the current text selection?

Open MarcVanDaele90 opened this issue 1 year ago • 1 comments

Whenever a user selects some text, the callback onTextSelectionChange: (selection) { _selectedText = selection; }, gets called.

But I would also like to clear the selection (eg after calling _addCurrentSelectionToMarkers in the example app).

Is this possible?

MarcVanDaele90 avatar Sep 18 '24 14:09 MarcVanDaele90

No at the current released version. I'm still working on text selection and the git master has the changes to add the feature.

espresso3389 avatar Sep 20 '24 16:09 espresso3389

See the disucssion #265.

espresso3389 avatar Dec 12 '24 15:12 espresso3389

The following code seems to work fine to clear the selection (based on the info in #265 but without using a perPageSelectableRegionInjector)

  T? findChildWidget<T extends Widget>(BuildContext context) {
    T? foundWidget;
    void search(Element element) {
      if (element.widget is T) {
        foundWidget = element.widget as T;
      } else {
        element.visitChildren(search);
      }
    }

    context.visitChildElements(search);
    return foundWidget;
  }

  void clearSelection() {
    //find the internal SelectableRegion
    SelectableRegion? selectableRegion = findChildWidget<SelectableRegion>(context);
    if (selectableRegion?.key is GlobalKey) {
      final state = (selectableRegion!.key as GlobalKey<SelectableRegionState>).currentState;
      state?.clearSelection();
    }
  }



MarcVanDaele90 avatar Mar 12 '25 07:03 MarcVanDaele90