Files
gio-patched/widget/bool.go
T
Elias Naur d017c722f5 widget,widget/material: only process events in Layout methods
Before this change, events were typically processed twice or more per
widget: once in the Layout method for refreshing the visual state, and
once per method that queries for state changes.

One example is widget.Clickable that processed events in both its Layout
and Clicked method.

This change establishes the convention that events are processed once, in
the Layout method. There are several advantages to that approach:

- Query methods such as Clickable.Clicked no longer need a layout.Context.
- State updates from events only occur in Layout.
- Widgets are simplified because they won't need a separate processEvents
(or similar) method and won't forget to call it from methods other than Layout.
- Useless calls to gtx.Events are avoided (gtx.Events only returns events
for the first call each frame for a given event.Tag).

The disadvantage is that state updates from input events will not appear
before Layout. For example, in the call sequence

	var btn *widget.Clickable

	if btn.Clicked() {...}
	btn.Layout(...)

the Clicked call will not detect an incoming click until the frame after it
happened.

This is ok because

- The Gio event router automatically dispatches an extra frame after events
arrive, bounding the latency from events to queries such as Clicked to
at most one frame (~17 ms).
- The potential extra frame of latency does not apply to Layout methods as long
as they process events before drawing. In other words, the visual feedback
from input events are not delayed because of this change.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-24 13:03:23 +02:00

40 lines
710 B
Go

package widget
import (
"gioui.org/gesture"
"gioui.org/layout"
)
type Bool struct {
Value bool
// Last is the last registered click.
Last Click
// changeVal tracks Value from the most recent call to Changed.
changeVal bool
gesture gesture.Click
}
// Changed reports whether Value has changed since the last
// call to Changed.
func (b *Bool) Changed() bool {
changed := b.Value != b.changeVal
b.changeVal = b.Value
return changed
}
func (b *Bool) Layout(gtx layout.Context) {
for _, e := range b.gesture.Events(gtx) {
switch e.Type {
case gesture.TypeClick:
b.Last = Click{
Time: gtx.Now(),
Position: e.Position,
}
b.Value = !b.Value
}
}
b.gesture.Add(gtx.Ops)
}