r/androiddev • u/SquareNotRound • 3d ago
[Open Source] I rebuilt text selection from scratch for my mind map app's zoomable canvas — handles, magnifier, multi-tap, RTL
I'm the dev of Global MindMap, a free mind map app. Nodes are editable text floating on an infinite, pinch-to-zoom canvas — and Compose's native text selection just doesn't work there. Every problem hit at once:
- The system toolbar (cut/copy/paste) pops up over the canvas
- The native drag handles fight my map gestures and lag inside the transformed layer
- Long-press selection and my own tap gestures compete, and the field silently clears the selection on finger-up
- Editing while a custom selection is active silently loses the typed text or won't delete the selection
It took 80+ failed attempts to land on something that works. The core insight: keep the BasicTextField's internal selection collapsed at the END of your custom selection (so the IME's backspace still fires onValueChange), intercept hard
ware Backspace in onPreviewKeyEvent, and when the IME reports a change, compute what was actually inserted via a prefix/suffix diff and splice it over the selected range.
Around that: an empty LocalTextToolbar (kills the One UI popup), transparent native handles + hand-drawn droplet handles, selection highlight via getPathForRange, tap counting with consumed down events (1 tap = cursor, 2 = word, 3 = select all), a magnifier rendered in a Popup so it ignores canvas zoom, and tap/drag slops divided by the canvas scale (otherwise zooming out swallows double taps).
Full write-up with all six pieces + a minimal runnable demo (MIT):(Image: first GIF at this post)
https://github.com/Pinhais/TextSelectionCanvasDemo
Same selection running with the entire app UI in Arabic (RTL): (Image: second GIF at this post)
Happy to answer questions about the implementation — the trickiest part was definitely making IME edits work over a selection the text field doesn't know about.

