all: move Now from system.Config to system.FrameEvent

Then, make layout.Context.Now a field, copied from FrameEvent.Now.

API change:

	gofmt -r 'gtx.Now() -> gtx.Now'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-09 23:18:04 +02:00
parent 81a84d874b
commit 6380baacb6
14 changed files with 25 additions and 34 deletions
+1 -1
View File
@@ -384,6 +384,7 @@ func (w *window) draw(sync bool) {
ppdp := float32(w.dpi) * inchPrDp ppdp := float32(w.dpi) * inchPrDp
w.callbacks.Event(FrameEvent{ w.callbacks.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: int(width), X: int(width),
Y: int(height), Y: int(height),
@@ -392,7 +393,6 @@ func (w *window) draw(sync bool) {
Config: &config{ Config: &config{
pxPerDp: ppdp, pxPerDp: ppdp,
pxPerSp: w.fontScale * ppdp, pxPerSp: w.fontScale * ppdp,
now: time.Now(),
}, },
}, },
Sync: sync, Sync: sync,
+1 -1
View File
@@ -108,6 +108,7 @@ func (w *window) draw(sync bool) {
const inchPrDp = 1.0 / 163 const inchPrDp = 1.0 / 163
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: int(params.width + .5), X: int(params.width + .5),
Y: int(params.height + .5), Y: int(params.height + .5),
@@ -121,7 +122,6 @@ func (w *window) draw(sync bool) {
Config: &config{ Config: &config{
pxPerDp: float32(params.dpi) * inchPrDp, pxPerDp: float32(params.dpi) * inchPrDp,
pxPerSp: float32(params.sdpi) * inchPrDp, pxPerSp: float32(params.sdpi) * inchPrDp,
now: time.Now(),
}, },
}, },
Sync: sync, Sync: sync,
+1 -1
View File
@@ -413,9 +413,9 @@ func (w *window) draw(sync bool) {
w.mu.Lock() w.mu.Lock()
w.scale = float32(scale) w.scale = float32(scale)
w.mu.Unlock() w.mu.Unlock()
cfg.now = time.Now()
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: width, X: width,
Y: height, Y: height,
+1 -1
View File
@@ -228,10 +228,10 @@ func (w *window) draw() {
width := int(wf*w.scale + .5) width := int(wf*w.scale + .5)
height := int(hf*w.scale + .5) height := int(hf*w.scale + .5)
cfg := configFor(w.scale) cfg := configFor(w.scale)
cfg.now = time.Now()
w.setStage(system.StageRunning) w.setStage(system.StageRunning)
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: width, X: width,
Y: height, Y: height,
+1 -1
View File
@@ -1385,9 +1385,9 @@ func (w *window) draw(sync bool) {
// Use the surface as listener data for gio_onFrameDone. // Use the surface as listener data for gio_onFrameDone.
C.wl_callback_add_listener(w.lastFrameCallback, &C.gio_callback_listener, unsafe.Pointer(w.surf)) C.wl_callback_add_listener(w.lastFrameCallback, &C.gio_callback_listener, unsafe.Pointer(w.surf))
} }
cfg.now = time.Now()
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: width, X: width,
Y: height, Y: height,
+1 -1
View File
@@ -370,9 +370,9 @@ func (w *window) draw(sync bool) {
return return
} }
cfg := configForDC() cfg := configForDC()
cfg.now = time.Now()
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: w.width, X: w.width,
Y: w.height, Y: w.height,
+1 -1
View File
@@ -184,9 +184,9 @@ loop:
} }
if anim || syn { if anim || syn {
w.cfg.now = time.Now()
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
Now: time.Now(),
Size: image.Point{ Size: image.Point{
X: w.width, X: w.width,
Y: w.height, Y: w.height,
-6
View File
@@ -7,7 +7,6 @@ package window
import ( import (
"errors" "errors"
"math" "math"
"time"
"gioui.org/gpu/backend" "gioui.org/gpu/backend"
"gioui.org/io/event" "gioui.org/io/event"
@@ -78,11 +77,6 @@ type config struct {
pxPerDp float32 pxPerDp float32
// Device pixels per sp. // Device pixels per sp.
pxPerSp float32 pxPerSp float32
now time.Time
}
func (c *config) Now() time.Time {
return c.now
} }
func (c *config) Px(v unit.Value) int { func (c *config) Px(v unit.Value) int {
+3 -3
View File
@@ -18,6 +18,9 @@ import (
// input. // input.
type FrameEvent struct { type FrameEvent struct {
Config Config Config Config
// Now is the current animation. Use Now instead of time.Now to
// synchronize animation and to avoid the time.Now call overhead.
Now time.Time
// Size is the dimensions of the window. // Size is the dimensions of the window.
Size image.Point Size image.Point
// Insets is the insets to apply. // Insets is the insets to apply.
@@ -51,9 +54,6 @@ type FrameEvent struct {
// Config defines the essential properties of // Config defines the essential properties of
// the environment. // the environment.
type Config interface { type Config interface {
// Now returns the current animation time.
Now() time.Time
unit.Converter unit.Converter
} }
+5 -8
View File
@@ -24,6 +24,9 @@ type Context struct {
// By convention, a nil Queue is a signal to widgets to draw themselves // By convention, a nil Queue is a signal to widgets to draw themselves
// in a disabled state. // in a disabled state.
Queue event.Queue Queue event.Queue
// Now is the animation time.
Now time.Time
*op.Ops *op.Ops
} }
@@ -31,6 +34,7 @@ type Context struct {
// //
// Context{ // Context{
// Ops: ops, // Ops: ops,
// Now: e.Now,
// Queue: e.Queue, // Queue: e.Queue,
// Config: e.Config, // Config: e.Config,
// Constraints: Exact(e.Size), // Constraints: Exact(e.Size),
@@ -41,20 +45,13 @@ func NewContext(ops *op.Ops, e system.FrameEvent) Context {
ops.Reset() ops.Reset()
return Context{ return Context{
Ops: ops, Ops: ops,
Now: e.Now,
Queue: e.Queue, Queue: e.Queue,
Config: e.Config, Config: e.Config,
Constraints: Exact(e.Size), Constraints: Exact(e.Size),
} }
} }
// Now returns the configuration time or the zero time.
func (c Context) Now() time.Time {
if c.Config == nil {
return time.Time{}
}
return c.Config.Now()
}
// Px maps the value to pixels. If no configuration is set, // Px maps the value to pixels. If no configuration is set,
// Px returns the rounded value of v. // Px returns the rounded value of v.
func (c Context) Px(v unit.Value) int { func (c Context) Px(v unit.Value) int {
+1 -1
View File
@@ -121,7 +121,7 @@ func (l *List) Dragging() bool {
} }
func (l *List) update() { func (l *List) update() {
d := l.scroll.Scroll(l.ctx, l.ctx, l.ctx.Now(), gesture.Axis(l.Axis)) d := l.scroll.Scroll(l.ctx, l.ctx, l.ctx.Now, gesture.Axis(l.Axis))
l.scrollDelta = d l.scrollDelta = d
l.Position.Offset += d l.Position.Offset += d
} }
+4 -4
View File
@@ -80,7 +80,7 @@ func (b *Clickable) Layout(gtx layout.Context) layout.Dimensions {
stack.Pop() stack.Pop()
for len(b.history) > 0 { for len(b.history) > 0 {
c := b.history[0] c := b.history[0]
if c.End.IsZero() || gtx.Now().Sub(c.End) < 1*time.Second { if c.End.IsZero() || gtx.Now.Sub(c.End) < 1*time.Second {
break break
} }
n := copy(b.history, b.history[1:]) n := copy(b.history, b.history[1:])
@@ -104,19 +104,19 @@ func (b *Clickable) update(gtx layout.Context) {
NumClicks: e.NumClicks, NumClicks: e.NumClicks,
}) })
if l := len(b.history); l > 0 { if l := len(b.history); l > 0 {
b.history[l-1].End = gtx.Now() b.history[l-1].End = gtx.Now
} }
case gesture.TypeCancel: case gesture.TypeCancel:
for i := range b.history { for i := range b.history {
b.history[i].Cancelled = true b.history[i].Cancelled = true
if b.history[i].End.IsZero() { if b.history[i].End.IsZero() {
b.history[i].End = gtx.Now() b.history[i].End = gtx.Now
} }
} }
case gesture.TypePress: case gesture.TypePress:
b.history = append(b.history, Press{ b.history = append(b.history, Press{
Position: e.Position, Position: e.Position,
Start: gtx.Now(), Start: gtx.Now,
}) })
} }
} }
+4 -4
View File
@@ -129,7 +129,7 @@ func (e *Editor) processPointer(gtx layout.Context) {
axis = gesture.Vertical axis = gesture.Vertical
smin, smax = sbounds.Min.Y, sbounds.Max.Y smin, smax = sbounds.Min.Y, sbounds.Max.Y
} }
sdist := e.scroller.Scroll(gtx, gtx, gtx.Now(), axis) sdist := e.scroller.Scroll(gtx, gtx, gtx.Now, axis)
var soff int var soff int
if e.SingleLine { if e.SingleLine {
e.scrollRel(sdist, 0) e.scrollRel(sdist, 0)
@@ -142,7 +142,7 @@ func (e *Editor) processPointer(gtx layout.Context) {
switch { switch {
case evt.Type == gesture.TypePress && evt.Source == pointer.Mouse, case evt.Type == gesture.TypePress && evt.Source == pointer.Mouse,
evt.Type == gesture.TypeClick && evt.Source == pointer.Touch: evt.Type == gesture.TypeClick && evt.Source == pointer.Touch:
e.blinkStart = gtx.Now() e.blinkStart = gtx.Now
e.moveCoord(gtx, image.Point{ e.moveCoord(gtx, image.Point{
X: int(math.Round(float64(evt.Position.X))), X: int(math.Round(float64(evt.Position.X))),
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
@@ -163,7 +163,7 @@ func (e *Editor) processKey(gtx layout.Context) {
e.events = append(e.events, ChangeEvent{}) e.events = append(e.events, ChangeEvent{})
} }
for _, ke := range gtx.Events(&e.eventKey) { for _, ke := range gtx.Events(&e.eventKey) {
e.blinkStart = gtx.Now() e.blinkStart = gtx.Now
switch ke := ke.(type) { switch ke := ke.(type) {
case key.FocusEvent: case key.FocusEvent:
e.focused = ke.Focus e.focused = ke.Focus
@@ -309,7 +309,7 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
e.clicker.Add(gtx.Ops) e.clicker.Add(gtx.Ops)
e.caretOn = false e.caretOn = false
if e.focused { if e.focused {
now := gtx.Now() now := gtx.Now
dt := now.Sub(e.blinkStart) dt := now.Sub(e.blinkStart)
blinking := dt < maxBlinkDuration blinking := dt < maxBlinkDuration
const timePerBlink = time.Second / blinksPerSecond const timePerBlink = time.Second / blinksPerSecond
+1 -1
View File
@@ -198,7 +198,7 @@ func drawInk(gtx layout.Context, c widget.Press) {
fadeDuration = float32(0.9) fadeDuration = float32(0.9)
) )
now := gtx.Now() now := gtx.Now
t := float32(now.Sub(c.Start).Seconds()) t := float32(now.Sub(c.Start).Seconds())