Skip to content

Commit

Permalink
tap animation should be created in widget's CreateRenderer method
Browse files Browse the repository at this point in the history
  • Loading branch information
fpabl0 committed Apr 19, 2021
1 parent 7c9f320 commit cf18f64
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
22 changes: 9 additions & 13 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type Button struct {

hovered bool
tapAnim *fyne.Animation
tapBG *canvas.Rectangle
}

// NewButton creates a new button widget with the set label and tap handler
Expand Down Expand Up @@ -99,10 +98,12 @@ func (b *Button) CreateRenderer() fyne.WidgetRenderer {
text.TextStyle.Bold = true

background := canvas.NewRectangle(theme.ButtonColor())
b.tapBG = canvas.NewRectangle(color.Transparent)
tapBG := canvas.NewRectangle(color.Transparent)
b.tapAnim = newButtonTapAnimation(tapBG, b)
b.tapAnim.Curve = fyne.AnimationEaseOut
objects := []fyne.CanvasObject{
background,
b.tapBG,
tapBG,
text,
}
shadowLevel := widget.ButtonLevel
Expand All @@ -112,6 +113,7 @@ func (b *Button) CreateRenderer() fyne.WidgetRenderer {
r := &buttonRenderer{
ShadowingRenderer: widget.NewShadowingRenderer(objects, shadowLevel),
background: background,
tapBG: tapBG,
button: b,
label: text,
layout: layout.NewHBoxLayout(),
Expand Down Expand Up @@ -177,17 +179,10 @@ func (b *Button) Tapped(*fyne.PointEvent) {
}

func (b *Button) tapAnimation() {
if b.tapBG == nil { // not rendered yet? (tests)
return
}

if b.tapAnim == nil {
b.tapAnim = newButtonTapAnimation(b.tapBG, b)
b.tapAnim.Curve = fyne.AnimationEaseOut
} else {
b.tapAnim.Stop()
return
}

b.tapAnim.Stop()
b.tapAnim.Start()
}

Expand All @@ -197,6 +192,7 @@ type buttonRenderer struct {
icon *canvas.Image
label *canvas.Text
background *canvas.Rectangle
tapBG *canvas.Rectangle
button *Button
layout fyne.Layout
}
Expand Down Expand Up @@ -351,7 +347,7 @@ func (r *buttonRenderer) updateIconAndText() {
if r.icon == nil {
r.icon = canvas.NewImageFromResource(r.button.Icon)
r.icon.FillMode = canvas.ImageFillContain
r.SetObjects([]fyne.CanvasObject{r.background, r.button.tapBG, r.label, r.icon})
r.SetObjects([]fyne.CanvasObject{r.background, r.tapBG, r.label, r.icon})
}
if r.button.Disabled() {
r.icon.Resource = theme.NewDisabledResource(r.button.Icon)
Expand Down
31 changes: 31 additions & 0 deletions widget/button_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -157,3 +158,33 @@ func TestButtonRenderer_ApplyTheme(t *testing.T) {

assert.NotEqual(t, textSize, customTextSize)
}

func TestButtonRenderer_TapAnimation(t *testing.T) {
test.NewApp()
defer test.NewApp()

button := NewButton("Hi", func() {})
w := test.NewWindow(button)
defer w.Close()
w.Resize(fyne.NewSize(50, 50).Add(fyne.NewSize(10, 10)))
button.Resize(fyne.NewSize(50, 50))

test.ApplyTheme(t, test.NewTheme())
button.Refresh()

render1 := test.WidgetRenderer(button).(*buttonRenderer)
test.Tap(button)
button.tapAnim.Tick(0.5)
test.AssertImageMatches(t, "button/tap_animation.png", w.Canvas().Capture())

cache.DestroyRenderer(button)
button.Refresh()

render2 := test.WidgetRenderer(button).(*buttonRenderer)

assert.NotEqual(t, render1, render2)

test.Tap(button)
button.tapAnim.Tick(0.5)
test.AssertImageMatches(t, "button/tap_animation.png", w.Canvas().Capture())
}
18 changes: 6 additions & 12 deletions widget/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Select struct {
hovered bool
popUp *PopUpMenu
tapAnim *fyne.Animation
tapBG *canvas.Rectangle
}

var _ fyne.Widget = (*Select)(nil)
Expand Down Expand Up @@ -66,8 +65,10 @@ func (s *Select) CreateRenderer() fyne.WidgetRenderer {

background := &canvas.Rectangle{}
line := canvas.NewRectangle(theme.ShadowColor())
s.tapBG = canvas.NewRectangle(color.Transparent)
objects := []fyne.CanvasObject{background, line, s.tapBG, txtProv, icon}
tapBG := canvas.NewRectangle(color.Transparent)
s.tapAnim = newButtonTapAnimation(tapBG, s)
s.tapAnim.Curve = fyne.AnimationEaseOut
objects := []fyne.CanvasObject{background, line, tapBG, txtProv, icon}
r := &selectRenderer{icon, txtProv, background, line, objects, s}
background.FillColor, line.FillColor = r.bgLineColor()
r.updateIcon()
Expand Down Expand Up @@ -246,17 +247,10 @@ func (s *Select) showPopUp() {
}

func (s *Select) tapAnimation() {
if s.tapBG == nil { // not rendered yet? (tests)
return
}

if s.tapAnim == nil {
s.tapAnim = newButtonTapAnimation(s.tapBG, s)
s.tapAnim.Curve = fyne.AnimationEaseOut
} else {
s.tapAnim.Stop()
return
}

s.tapAnim.Stop()
s.tapAnim.Start()
}

Expand Down
42 changes: 42 additions & 0 deletions widget/select_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package widget

import (
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/test"
"github.com/stretchr/testify/assert"
)

func TestSelectRenderer_TapAnimation(t *testing.T) {
test.NewApp()
defer test.NewApp()

sel := NewSelect([]string{"one"}, func(s string) {})
w := test.NewWindow(sel)
defer w.Close()
w.Resize(sel.MinSize().Add(fyne.NewSize(10, 10)))
sel.Resize(sel.MinSize())

test.ApplyTheme(t, test.NewTheme())
sel.Refresh()

render1 := test.WidgetRenderer(sel).(*selectRenderer)
test.Tap(sel)
sel.popUp.Hide()
sel.tapAnim.Tick(0.5)
test.AssertImageMatches(t, "select/tap_animation.png", w.Canvas().Capture())

cache.DestroyRenderer(sel)
sel.Refresh()

render2 := test.WidgetRenderer(sel).(*selectRenderer)

assert.NotEqual(t, render1, render2)

test.Tap(sel)
sel.popUp.Hide()
sel.tapAnim.Tick(0.5)
test.AssertImageMatches(t, "select/tap_animation.png", w.Canvas().Capture())
}
Binary file added widget/testdata/button/tap_animation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added widget/testdata/select/tap_animation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cf18f64

Please sign in to comment.