Changing some element when moving the cursor over it #5229
-
Fyne v2.5.1 Initially I wanted to add a fade in effect when hovering over an element (e.g. a button). I found out that there is Fyne API “desktop.Hoverable” | Fyne.io, tried to implement changing the icon of the button when hovering, but nothing worked. The logs show that the functions are triggered, but the button icon does not change. package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type HoverableButton struct {
widget.Button
normalIcon fyne.Resource
hoverIcon fyne.Resource
}
func NewHoverableButton(label string, tapped func(), normalIcon, hoverIcon fyne.Resource) *HoverableButton {
btn := &HoverableButton{
Button: *widget.NewButtonWithIcon(label, normalIcon, tapped),
normalIcon: normalIcon,
hoverIcon: hoverIcon,
}
btn.ExtendBaseWidget(btn)
return btn
}
func (b *HoverableButton) MouseIn(*desktop.MouseEvent) {
println("Mouse In")
b.Icon = b.hoverIcon
b.Refresh()
}
func (b *HoverableButton) MouseOut() {
println("Mouse Out")
b.Icon = b.normalIcon
b.Refresh()
}
func (b *HoverableButton) MouseMoved(*desktop.MouseEvent) {}
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Hoverable Button Example")
normalIcon := theme.HomeIcon()
hoverIcon := theme.CancelIcon()
hoverableButton := NewHoverableButton("Hover Me", func() {
println("Button clicked!")
}, normalIcon, hoverIcon)
content := container.NewVBox(hoverableButton)
myWindow.SetContent(content)
myWindow.Resize(fyne.NewSize(200, 100))
myWindow.ShowAndRun()
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Buttons already have a hover effect on them - just like Check / Radio and other input types. With regards to your code snippet you have created two buttons, your one and an embedded one. Don't copy a new one in on the constructor. (i.e. remove the line including |
Beta Was this translation helpful? Give feedback.
Buttons already have a hover effect on them - just like Check / Radio and other input types.
With regards to your code snippet you have created two buttons, your one and an embedded one. Don't copy a new one in on the constructor. (i.e. remove the line including
*widget.NewButtonWithIcon(label, normalIcon, tapped),
because you are not manipulating the same button that is rendered).