From dc6fbf07f0adfff7719aad0584078ee32b9e3188 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Thu, 22 Dec 2022 14:51:13 -0500 Subject: [PATCH] widget: expose text region resolution This commit adds exported methods to both LabelState and Editor allowing callers to locate the text regions representing a range of runes. This can be used to build interactive subregions of text, like (for instance) hyperlinks. Signed-off-by: Chris Waldon --- widget/editor.go | 6 ++++++ widget/index.go | 4 ++++ widget/selectable.go | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/widget/editor.go b/widget/editor.go index ad124da1..3c11464d 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -984,6 +984,12 @@ func (e *Editor) Read(p []byte) (int, error) { return e.text.Read(p) } +// Regions returns visible regions covering the rune range [start,end). +func (e *Editor) Regions(start, end int, regions []Region) []Region { + e.initBuffer() + return e.text.Regions(start, end, regions) +} + func max(a, b int) int { if a > b { return a diff --git a/widget/index.go b/widget/index.go index af092d7a..58308c48 100644 --- a/widget/index.go +++ b/widget/index.go @@ -307,6 +307,10 @@ type Region struct { Baseline int } +// region is identical to Region except that its coordinates are in document +// space instead of a widget coordinate space. +type region = Region + // locate returns highlight regions covering the glyphs that represent the runes in // [startRune,endRune). If the rects parameter is non-nil, locate will use it to // return results instead of allocating, provided that there is enough capacity. diff --git a/widget/selectable.go b/widget/selectable.go index d8411fff..9cdd1fc6 100644 --- a/widget/selectable.go +++ b/widget/selectable.go @@ -341,3 +341,9 @@ func (l *Selectable) Events() []EditorEvent { l.prevEvents = 0 return events } + +// Regions returns visible regions covering the rune range [start,end). +func (l *Selectable) Regions(start, end int, regions []Region) []Region { + l.initialize() + return l.text.Regions(start, end, regions) +}