widget: [API] simplify Selectable event processing

Now (*widget.Selectable).Update() returns whether the selection changed during
event processing, rather than requiring a separate call to (*widget.Selectable).Events().

The Events() method has been removed as redundant.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2024-01-17 13:25:04 -05:00
committed by Elias Naur
parent c645c2ec8e
commit 297c03925d
+11 -24
View File
@@ -79,10 +79,6 @@ type Selectable struct {
dragger gesture.Drag
clicker gesture.Click
// events is the list of events not yet processed.
events []EditorEvent
// prevEvents is the number of events from the previous frame.
prevEvents int
}
// initialize must be called at the beginning of any exported method that
@@ -175,10 +171,11 @@ func (l *Selectable) Truncated() bool {
return l.text.Truncated()
}
// Update the state of the selectable in response to input events.
func (l *Selectable) Update(gtx layout.Context) {
// Update the state of the selectable in response to input events. It returns whether the
// text selection changed during event processing.
func (l *Selectable) Update(gtx layout.Context) bool {
l.initialize()
l.handleEvents(gtx)
return l.handleEvents(gtx)
}
// Layout clips to the dimensions of the selectable, updates the shaped text, configures input handling, and paints
@@ -206,18 +203,16 @@ func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font,
return dims
}
func (l *Selectable) handleEvents(gtx layout.Context) {
// Flush events from before the previous Layout.
n := copy(l.events, l.events[l.prevEvents:])
l.events = l.events[:n]
l.prevEvents = n
func (l *Selectable) handleEvents(gtx layout.Context) (selectionChanged bool) {
oldStart, oldLen := min(l.text.Selection()), l.text.SelectionLen()
defer func() {
if newStart, newLen := min(l.text.Selection()), l.text.SelectionLen(); oldStart != newStart || oldLen != newLen {
selectionChanged = true
}
}()
l.processPointer(gtx)
l.processKey(gtx)
// Queue a SelectEvent if the selection changed, including if it went away.
if newStart, newLen := min(l.text.Selection()), l.text.SelectionLen(); oldStart != newStart || oldLen != newLen {
l.events = append(l.events, SelectEvent{})
}
return selectionChanged
}
func (e *Selectable) processPointer(gtx layout.Context) {
@@ -389,14 +384,6 @@ func (e *Selectable) command(gtx layout.Context, k key.Event) {
}
}
// Events returns available text events.
func (l *Selectable) Events() []EditorEvent {
events := l.events
l.events = nil
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()