ui/input: change Events to return all events at once

Single stepping events only makes sense for widgets with complex
state, e.g. the text.Editor. For the input.Events source, returning
all events in a single Events call is sufficient and more natural
for clients.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-07-11 23:39:25 +02:00
parent a9d4186bef
commit 1735d5ced8
4 changed files with 7 additions and 23 deletions
+2 -10
View File
@@ -82,11 +82,7 @@ func (c *Click) Add(ops *ui.Ops) {
} }
func (c *Click) Next(q input.Events) (ClickEvent, bool) { func (c *Click) Next(q input.Events) (ClickEvent, bool) {
for { for _, evt := range q.Events(c) {
evt, ok := q.Next(c)
if !ok {
break
}
e, ok := evt.(pointer.Event) e, ok := evt.(pointer.Event)
if !ok { if !ok {
continue continue
@@ -139,11 +135,7 @@ func (s *Scroll) Scroll(cfg ui.Config, q input.Events, axis Axis) int {
return 0 return 0
} }
total := 0 total := 0
for { for _, evt := range q.Events(s) {
evt, ok := q.Next(s)
if !ok {
break
}
e, ok := evt.(pointer.Event) e, ok := evt.(pointer.Event)
if !ok { if !ok {
continue continue
+1 -1
View File
@@ -7,7 +7,7 @@ package input
// Events maps an event handler key to the events // Events maps an event handler key to the events
// available to the handler. // available to the handler.
type Events interface { type Events interface {
Next(k Key) (Event, bool) Events(k Key) []Event
} }
// Key is the stable identifier for an event handler. For a handler h, the // Key is the stable identifier for an event handler. For a handler h, the
+3 -7
View File
@@ -19,14 +19,10 @@ type Queue struct {
type handlerEvents map[Key][]Event type handlerEvents map[Key][]Event
func (q *Queue) Next(k Key) (Event, bool) { func (q *Queue) Events(k Key) []Event {
events := q.handlers[k] events := q.handlers[k]
if len(events) == 0 { delete(q.handlers, k)
return nil, false return events
}
e := events[0]
q.handlers[k] = events[1:]
return e, true
} }
func (q *Queue) Frame(ops *ui.Ops) { func (q *Queue) Frame(ops *ui.Ops) {
+1 -5
View File
@@ -119,11 +119,7 @@ func (e *Editor) Next() (EditorEvent, bool) {
if (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin) { if (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin) {
e.scroller.Stop() e.scroller.Stop()
} }
for { for _, ke := range e.Inputs.Events(e) {
ke, ok := e.Inputs.Next(e)
if !ok {
break
}
e.blinkStart = e.Config.Now() e.blinkStart = e.Config.Now()
switch ke := ke.(type) { switch ke := ke.(type) {
case key.FocusEvent: case key.FocusEvent: