added button feedback
This commit is contained in:
@@ -67,6 +67,7 @@ func main() {
|
|||||||
|
|
||||||
clearHistory := func() {
|
clearHistory := func() {
|
||||||
calculations = nil
|
calculations = nil
|
||||||
|
diceInputEntry.SetText("")
|
||||||
historyList.Refresh()
|
historyList.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"math"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/canvas"
|
"fyne.io/fyne/v2/canvas"
|
||||||
"fyne.io/fyne/v2/driver/desktop"
|
"fyne.io/fyne/v2/driver/desktop"
|
||||||
|
"fyne.io/fyne/v2/driver/mobile"
|
||||||
"fyne.io/fyne/v2/theme"
|
"fyne.io/fyne/v2/theme"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
)
|
)
|
||||||
@@ -84,6 +86,8 @@ type customButton2 struct {
|
|||||||
TextSize float32
|
TextSize float32
|
||||||
Background color.Color
|
Background color.Color
|
||||||
Foreground color.Color
|
Foreground color.Color
|
||||||
|
hovered bool
|
||||||
|
pressed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *customButton2) CreateRenderer() fyne.WidgetRenderer {
|
func (b *customButton2) CreateRenderer() fyne.WidgetRenderer {
|
||||||
@@ -108,14 +112,39 @@ func (b *customButton2) TappedSecondary(*fyne.PointEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *customButton2) MouseIn(*desktop.MouseEvent) {
|
func (b *customButton2) MouseIn(*desktop.MouseEvent) {
|
||||||
|
b.hovered = true
|
||||||
|
b.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *customButton2) MouseOut() {
|
func (b *customButton2) MouseOut() {
|
||||||
|
b.hovered = false
|
||||||
|
b.pressed = false
|
||||||
|
b.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *customButton2) MouseMoved(*desktop.MouseEvent) {
|
func (b *customButton2) MouseMoved(*desktop.MouseEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *customButton2) MouseDown(*desktop.MouseEvent) {
|
||||||
|
b.pressed = true
|
||||||
|
b.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *customButton2) MouseUp(*desktop.MouseEvent) {
|
||||||
|
b.pressed = false
|
||||||
|
b.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *customButton2) TouchDown(*mobile.TouchEvent) {
|
||||||
|
b.pressed = true
|
||||||
|
b.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *customButton2) TouchUp(*mobile.TouchEvent) {
|
||||||
|
b.pressed = false
|
||||||
|
b.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
type customButton2Renderer struct {
|
type customButton2Renderer struct {
|
||||||
text *canvas.Text
|
text *canvas.Text
|
||||||
background *canvas.Rectangle
|
background *canvas.Rectangle
|
||||||
@@ -157,6 +186,18 @@ func (r *customButton2Renderer) Destroy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *customButton2) backgroundColor() color.Color {
|
func (b *customButton2) backgroundColor() color.Color {
|
||||||
|
base := b.baseBackgroundColor()
|
||||||
|
switch {
|
||||||
|
case b.pressed:
|
||||||
|
return adjustColorBrightness(base, 0.82)
|
||||||
|
case b.hovered:
|
||||||
|
return adjustColorBrightness(base, 1.12)
|
||||||
|
default:
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *customButton2) baseBackgroundColor() color.Color {
|
||||||
if b.Background != nil {
|
if b.Background != nil {
|
||||||
return b.Background
|
return b.Background
|
||||||
}
|
}
|
||||||
@@ -212,6 +253,21 @@ func newCustomButton2WithColors(label string, background, foreground color.Color
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func adjustColorBrightness(c color.Color, factor float64) color.Color {
|
||||||
|
r, g, b, a := c.RGBA()
|
||||||
|
return color.NRGBA{
|
||||||
|
R: scaleColorChannel(r, factor),
|
||||||
|
G: scaleColorChannel(g, factor),
|
||||||
|
B: scaleColorChannel(b, factor),
|
||||||
|
A: uint8(a >> 8),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func scaleColorChannel(channel uint32, factor float64) uint8 {
|
||||||
|
value := float64(channel>>8) * factor
|
||||||
|
return uint8(math.Max(0, math.Min(255, value)))
|
||||||
|
}
|
||||||
|
|
||||||
// customEntry is a custom entry widget with configurable text size
|
// customEntry is a custom entry widget with configurable text size
|
||||||
type customEntry struct {
|
type customEntry struct {
|
||||||
widget.Entry
|
widget.Entry
|
||||||
|
|||||||
Reference in New Issue
Block a user