~eliasnaur/gio

b981ccf9eda43816ae9d809466fe96c8e0c2475b — Elias Naur 2 years ago 893b7f3
ui/input: support single stepping through events

Change input.Events interface to return one event at a time until
the queue is empty.

Change text.Editor and gestures to match.

Re-add Editor.Submit while we're here; we don't want to enable
submit mode always.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
4 files changed, 28 insertions(+), 9 deletions(-)

M ui/gesture/gestures.go
M ui/input/input.go
M ui/input/queue.go
M ui/text/editor.go
M ui/gesture/gestures.go => ui/gesture/gestures.go +12 -4
@@ 83,8 83,12 @@ func (c *Click) Add(ops *ui.Ops) {

func (c *Click) Update(q input.Events) []ClickEvent {
	var events []ClickEvent
	for _, e := range q.For(c) {
		e, ok := e.(pointer.Event)
	for {
		evt, ok := q.Next(c)
		if !ok {
			break
		}
		e, ok := evt.(pointer.Event)
		if !ok {
			continue
		}


@@ 135,8 139,12 @@ func (s *Scroll) Update(cfg *ui.Config, q input.Events, axis Axis) int {
		return 0
	}
	total := 0
	for _, e := range q.For(s) {
		e, ok := e.(pointer.Event)
	for {
		evt, ok := q.Next(s)
		if !ok {
			break
		}
		e, ok := evt.(pointer.Event)
		if !ok {
			continue
		}

M ui/input/input.go => ui/input/input.go +1 -1
@@ 7,7 7,7 @@ package input
// Events maps an event handler key to the events
// available to the handler.
type Events interface {
	For(k Key) []Event
	Next(k Key) (Event, bool)
}

// Key is the stable identifier for an event handler. For a handler h, the

M ui/input/queue.go => ui/input/queue.go +8 -2
@@ 19,8 19,14 @@ type Queue struct {

type handlerEvents map[Key][]Event

func (q *Queue) For(k Key) []Event {
	return q.handlers[k]
func (q *Queue) Next(k Key) (Event, bool) {
	events := q.handlers[k]
	if len(events) == 0 {
		return nil, false
	}
	e := events[0]
	q.handlers[k] = events[1:]
	return e, true
}

func (q *Queue) Frame(ops *ui.Ops) {

M ui/text/editor.go => ui/text/editor.go +7 -2
@@ 25,6 25,7 @@ type Editor struct {
	Face       Face
	Alignment  Alignment
	SingleLine bool
	Submit     bool

	oldCfg            ui.Config
	blinkStart        time.Time


@@ 103,7 104,11 @@ func (e *Editor) Next() (EditorEvent, bool) {
		}
	}
	stop := (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin)
	for _, ke := range e.Inputs.For(e) {
	for {
		ke, ok := e.Inputs.Next(e)
		if !ok {
			break
		}
		e.blinkStart = e.Config.Now
		switch ke := ke.(type) {
		case key.Focus:


@@ 112,7 117,7 @@ func (e *Editor) Next() (EditorEvent, bool) {
			if !e.focused {
				break
			}
			if ke.Name == key.NameReturn || ke.Name == key.NameEnter {
			if e.Submit && (ke.Name == key.NameReturn || ke.Name == key.NameEnter) {
				if !ke.Modifiers.Contain(key.ModShift) {
					return Submit{}, true
				}