Fix button timing and event decoding and avoid redundant menu refreshes
This commit is contained in:
8 files changed
+276
-44
No files matched your search
+5
-1
@@ -55,7 +55,7 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
||||
)
|
||||
|
||||
if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) {
|
||||
if w.buffer == nil {
|
||||
if w.buffer == nil || w.buffer.Bounds().Size() != size {
|
||||
w.buffer = app.BufferFor(ctx.Display().ColorModel(), size)
|
||||
w.Root.SetPageSize(size.Y / 16)
|
||||
}
|
||||
@@ -64,6 +64,8 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
||||
w.curr, w.next = w.next, nil
|
||||
}
|
||||
|
||||
w.curr.SetPageSize(size.Y / 16)
|
||||
|
||||
// Clear the buffer.
|
||||
app.ImageClear(w.buffer)
|
||||
|
||||
@@ -102,6 +104,8 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
||||
y := size.Y / len(menu.Items) * menu.Position
|
||||
draw.Box(w.buffer, image.Rect(size.X-3, y, size.X, y+size.Y/len(menu.Items)), color.Gray{Y: 0x7f})
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load()))
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"testing"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/menu"
|
||||
"github.com/BeatGlow/display"
|
||||
"github.com/BeatGlow/display/pixel"
|
||||
)
|
||||
|
||||
type menuTestDisplay struct {
|
||||
display.Display
|
||||
image *image.RGBA
|
||||
}
|
||||
|
||||
func (d *menuTestDisplay) Bounds() image.Rectangle { return d.image.Bounds() }
|
||||
func (d *menuTestDisplay) ColorModel() color.Model { return pixel.Gray4Model }
|
||||
func (d *menuTestDisplay) At(x, y int) color.Color { return d.image.At(x, y) }
|
||||
func (d *menuTestDisplay) Set(x, y int, c color.Color) { d.image.Set(x, y, c) }
|
||||
|
||||
type menuTestContext struct {
|
||||
app.Context
|
||||
display display.Display
|
||||
}
|
||||
|
||||
func (c *menuTestContext) Display() display.Display { return c.display }
|
||||
|
||||
func TestMenuRefreshOnlyOnChanges(t *testing.T) {
|
||||
data := struct{ Enabled bool }{}
|
||||
root, err := menu.New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := &menuTestContext{display: &menuTestDisplay{image: image.NewRGBA(image.Rect(0, 0, 256, 48))}}
|
||||
w := NewMenu(image.Pt(256, 48), root)
|
||||
w.SetStyles(*app.StylesFor(pixel.Gray4Model))
|
||||
w.Show(ctx)
|
||||
if !w.Refresh(ctx) {
|
||||
t.Fatal("initial menu was not drawn")
|
||||
}
|
||||
if w.Refresh(ctx) {
|
||||
t.Fatal("unchanged menu requested another hardware refresh")
|
||||
}
|
||||
root.Change(1)
|
||||
w.Redraw()
|
||||
if !w.Refresh(ctx) || w.Refresh(ctx) {
|
||||
t.Fatal("menu change should trigger exactly one refresh")
|
||||
}
|
||||
w.Hide(ctx)
|
||||
w.Show(ctx)
|
||||
if !w.Refresh(ctx) {
|
||||
t.Fatal("reopened menu was not drawn")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user