Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calculations to apply hover over high importance buttons #2017

Merged
merged 4 commits into from Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified internal/driver/glfw/testdata/menu_bar_hovered_content.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions widget/button.go
Expand Up @@ -312,10 +312,25 @@ func (r *buttonRenderer) buttonColor() color.Color {
switch {
case r.button.Disabled():
return theme.DisabledButtonColor()
case r.button.hovered:
bg := theme.ButtonColor()
if r.button.Importance == HighImportance {
bg = theme.PrimaryColor()
}

dstR, dstG, dstB, dstA := bg.RGBA()
srcR, srcG, srcB, srcA := theme.HoverColor().RGBA()
srcAlpha := float32(srcA) / 0xFFFF
dstAlpha := float32(dstA) / 0xFFFF
targetAlpha := 1 - srcAlpha*dstAlpha

outAlpha := srcAlpha + targetAlpha
outR := (srcAlpha*float32(srcR) + targetAlpha*float32(dstR)) / outAlpha
outG := (srcAlpha*float32(srcG) + targetAlpha*float32(dstG)) / outAlpha
outB := (srcAlpha*float32(srcB) + targetAlpha*float32(dstB)) / outAlpha
return color.RGBA{R: uint8(uint32(outR) >> 8), G: uint8(uint32(outG) >> 8), B: uint8(uint32(outB) >> 8), A: uint8(outAlpha * 0xFF)}
case r.button.Importance == HighImportance:
return theme.PrimaryColor()
case r.button.hovered:
return theme.HoverColor()
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.