mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
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:
+11
-24
@@ -79,10 +79,6 @@ type Selectable struct {
|
|||||||
dragger gesture.Drag
|
dragger gesture.Drag
|
||||||
|
|
||||||
clicker gesture.Click
|
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
|
// 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()
|
return l.text.Truncated()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the state of the selectable in response to input events.
|
// Update the state of the selectable in response to input events. It returns whether the
|
||||||
func (l *Selectable) Update(gtx layout.Context) {
|
// text selection changed during event processing.
|
||||||
|
func (l *Selectable) Update(gtx layout.Context) bool {
|
||||||
l.initialize()
|
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
|
// 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
|
return dims
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Selectable) handleEvents(gtx layout.Context) {
|
func (l *Selectable) handleEvents(gtx layout.Context) (selectionChanged bool) {
|
||||||
// Flush events from before the previous Layout.
|
|
||||||
n := copy(l.events, l.events[l.prevEvents:])
|
|
||||||
l.events = l.events[:n]
|
|
||||||
l.prevEvents = n
|
|
||||||
oldStart, oldLen := min(l.text.Selection()), l.text.SelectionLen()
|
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.processPointer(gtx)
|
||||||
l.processKey(gtx)
|
l.processKey(gtx)
|
||||||
// Queue a SelectEvent if the selection changed, including if it went away.
|
return selectionChanged
|
||||||
if newStart, newLen := min(l.text.Selection()), l.text.SelectionLen(); oldStart != newStart || oldLen != newLen {
|
|
||||||
l.events = append(l.events, SelectEvent{})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Selectable) processPointer(gtx layout.Context) {
|
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).
|
// Regions returns visible regions covering the rune range [start,end).
|
||||||
func (l *Selectable) Regions(start, end int, regions []Region) []Region {
|
func (l *Selectable) Regions(start, end int, regions []Region) []Region {
|
||||||
l.initialize()
|
l.initialize()
|
||||||
|
|||||||
Reference in New Issue
Block a user