Skip to content

Commit

Permalink
Add calculations to apply hover over high importance buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Feb 23, 2021
1 parent 7305479 commit ffeca25
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions widget/button.go
Expand Up @@ -312,10 +312,20 @@ func (r *buttonRenderer) buttonColor() color.Color {
switch {
case r.button.Disabled():
return theme.DisabledButtonColor()
case r.button.Importance == HighImportance:
return theme.PrimaryColor()
case r.button.hovered:
if r.button.Importance == HighImportance {
srcR, srcG, srcB, srcA := theme.PrimaryColor().RGBA()
dstR, dstG, dstB, dstA := theme.HoverColor().RGBA()
alpha := float32(dstA) / 0xFFFF
outR := alpha*float32(dstR) + (1-alpha)*float32(srcR)
outG := alpha*float32(dstG) + (1-alpha)*float32(srcG)
outB := alpha*float32(dstB) + (1-alpha)*float32(srcB)
outA := alpha*float32(dstA) + (1-alpha)*float32(srcA)
return color.RGBA{R: uint8(uint32(outR) >> 8), G: uint8(uint32(outG) >> 8), B: uint8(uint32(outB) >> 8), A: uint8(uint32(outA) >> 8)}
}
return theme.HoverColor()
case r.button.Importance == HighImportance:
return theme.PrimaryColor()
default:
return theme.ButtonColor()
}
Expand Down
25 changes: 25 additions & 0 deletions widget/button_test.go
Expand Up @@ -121,6 +121,31 @@ func TestButton_Disabled(t *testing.T) {
assert.False(t, button.Disabled())
}

func TestButton_Hover(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
app.Settings().SetTheme(theme.LightTheme())

b := widget.NewButtonWithIcon("Test", theme.HomeIcon(), func() {})
w := test.NewWindow(b)
defer w.Close()

test.MoveMouse(w.Canvas(), fyne.NewPos(5, 5))
test.AssertImageMatches(t, "button/hovered.png", w.Canvas().Capture())

b.Importance = widget.HighImportance
b.Refresh()
test.AssertImageMatches(t, "button/high_importance_hovered.png", w.Canvas().Capture())

test.MoveMouse(w.Canvas(), fyne.NewPos(0, 0))
b.Refresh()
test.AssertImageMatches(t, "button/high_importance.png", w.Canvas().Capture())

b.Importance = widget.MediumImportance
b.Refresh()
test.AssertImageMatches(t, "button/initial.png", w.Canvas().Capture())
}

func TestButton_Layout(t *testing.T) {
test.NewApp()
defer test.NewApp()
Expand Down
Binary file added widget/testdata/button/high_importance.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/button/hovered.png
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 ffeca25

Please sign in to comment.