add hover + click behavior
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
package widgets
|
package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"supersonic/res"
|
"supersonic/res"
|
||||||
"supersonic/ui/layouts"
|
"supersonic/ui/layouts"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/canvas"
|
"fyne.io/fyne/v2/canvas"
|
||||||
"fyne.io/fyne/v2/container"
|
"fyne.io/fyne/v2/container"
|
||||||
|
"fyne.io/fyne/v2/driver/desktop"
|
||||||
"fyne.io/fyne/v2/theme"
|
"fyne.io/fyne/v2/theme"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
)
|
)
|
||||||
@@ -17,7 +19,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
starRatingBetweenStarPad = 2
|
starRatingBetweenStarPad = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
type StarRating struct {
|
type StarRating struct {
|
||||||
@@ -26,6 +28,10 @@ type StarRating struct {
|
|||||||
Rating int
|
Rating int
|
||||||
StarSize float32
|
StarSize float32
|
||||||
|
|
||||||
|
OnRatingChanged func(int)
|
||||||
|
|
||||||
|
holdRating bool // don't render mouseHoverRating
|
||||||
|
mouseHoverRating int // zero if not hovered
|
||||||
container *fyne.Container
|
container *fyne.Container
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,15 +58,58 @@ func (s *StarRating) createContainer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ desktop.Hoverable = (*StarRating)(nil)
|
||||||
|
|
||||||
|
func (s *StarRating) MouseIn(e *desktop.MouseEvent) {
|
||||||
|
s.MouseMoved(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StarRating) MouseMoved(e *desktop.MouseEvent) {
|
||||||
|
hoverRating := int(math.Ceil(5 * float64(e.Position.X/s.Size().Width)))
|
||||||
|
if s.mouseHoverRating != hoverRating {
|
||||||
|
s.holdRating = false
|
||||||
|
s.mouseHoverRating = hoverRating
|
||||||
|
s.Refresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StarRating) MouseOut() {
|
||||||
|
s.mouseHoverRating = 0
|
||||||
|
s.holdRating = false
|
||||||
|
s.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ fyne.Tappable = (*StarRating)(nil)
|
||||||
|
|
||||||
|
func (s *StarRating) Tapped(*fyne.PointEvent) {
|
||||||
|
if s.mouseHoverRating <= 0 {
|
||||||
|
return //shouldn't happen
|
||||||
|
}
|
||||||
|
if s.Rating == s.mouseHoverRating {
|
||||||
|
s.Rating = 0
|
||||||
|
} else {
|
||||||
|
s.Rating = s.mouseHoverRating
|
||||||
|
}
|
||||||
|
s.holdRating = true
|
||||||
|
s.Refresh()
|
||||||
|
if s.OnRatingChanged != nil {
|
||||||
|
s.OnRatingChanged(s.Rating)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StarRating) Refresh() {
|
func (s *StarRating) Refresh() {
|
||||||
// widget has not had renderer created yet
|
// widget has not had renderer created yet
|
||||||
if s.container == nil {
|
if s.container == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
rating := s.Rating
|
||||||
|
if !s.holdRating && s.mouseHoverRating > 0 {
|
||||||
|
rating = s.mouseHoverRating
|
||||||
|
}
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
im := s.container.Objects[i].(*canvas.Image)
|
im := s.container.Objects[i].(*canvas.Image)
|
||||||
im.SetMinSize(fyne.NewSize(s.StarSize, s.StarSize))
|
im.SetMinSize(fyne.NewSize(s.StarSize, s.StarSize))
|
||||||
if s.Rating > i {
|
if rating > i {
|
||||||
im.Resource = themedResStarFilled
|
im.Resource = themedResStarFilled
|
||||||
} else {
|
} else {
|
||||||
im.Resource = themedResStarOutline
|
im.Resource = themedResStarOutline
|
||||||
|
|||||||
Reference in New Issue
Block a user