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

Do not double update color channels when their associated entry text is changed programmatically, fixes #2075 #2109

Merged
merged 2 commits into from Mar 26, 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
73 changes: 58 additions & 15 deletions dialog/color_channel.go
Expand Up @@ -2,6 +2,7 @@ package dialog

import (
"strconv"
"sync"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand Down Expand Up @@ -124,25 +125,21 @@ func (r *colorChannelRenderer) updateObjects() {
}

type colorChannelEntry struct {
widget.Entry
userChangeEntry
}

func newColorChannelEntry(c *colorChannel) *colorChannelEntry {
e := &colorChannelEntry{
Entry: widget.Entry{
Text: "0",
OnChanged: func(text string) {
value, err := strconv.Atoi(text)
if err != nil {
fyne.LogError("Couldn't parse: "+text, err)
} else {
c.SetValue(value)
}
},
// TODO add number min/max validator
},
}
e := &colorChannelEntry{}
e.Text = "0"
e.ExtendBaseWidget(e)
e.setOnChanged(func(text string) {
value, err := strconv.Atoi(text)
if err != nil {
fyne.LogError("Couldn't parse: "+text, err)
return
}
c.SetValue(value)
})
return e
}

Expand All @@ -152,3 +149,49 @@ func (e *colorChannelEntry) MinSize() fyne.Size {
min = min.Add(fyne.NewSize(theme.Padding()*6, theme.Padding()*4))
return min.Max(e.Entry.MinSize())
}

type userChangeEntry struct {
widget.Entry

lock sync.RWMutex
userTyped bool
}

func newUserChangeEntry(text string) *userChangeEntry {
e := &userChangeEntry{}
e.Entry.Text = text
e.ExtendBaseWidget(e)
return e
}

func (e *userChangeEntry) setOnChanged(onChanged func(s string)) {
e.Entry.OnChanged = func(text string) {
e.lock.Lock()
userTyped := e.userTyped
if userTyped {
e.userTyped = false
}
e.lock.Unlock()
if !userTyped {
return
}
if onChanged != nil {
onChanged(text)
}
}
e.ExtendBaseWidget(e)
}

func (e *userChangeEntry) TypedRune(r rune) {
e.lock.Lock()
e.userTyped = true
e.lock.Unlock()
e.Entry.TypedRune(r)
}

func (e *userChangeEntry) TypedKey(ev *fyne.KeyEvent) {
e.lock.Lock()
e.userTyped = true
e.lock.Unlock()
e.Entry.TypedKey(ev)
}
23 changes: 11 additions & 12 deletions dialog/color_picker.go
Expand Up @@ -162,17 +162,16 @@ func (p *colorAdvancedPicker) CreateRenderer() fyne.WidgetRenderer {
})

// Hex
hex := &widget.Entry{
OnChanged: func(text string) {
c, err := stringToColor(text)
if err != nil {
fyne.LogError("Error parsing color: "+text, err)
// TODO trigger entry invalid state
} else {
p.SetColor(c)
}
},
}
hex := newUserChangeEntry("")
hex.setOnChanged(func(text string) {
c, err := stringToColor(text)
if err != nil {
fyne.LogError("Error parsing color: "+text, err)
// TODO trigger entry invalid state
} else {
p.SetColor(c)
}
})

contents := fyne.NewContainerWithLayout(layout.NewPaddedLayout(), container.NewVBox(
container.NewGridWithColumns(3,
Expand Down Expand Up @@ -264,7 +263,7 @@ type colorPickerRenderer struct {
wheel *colorWheel
preview *canvas.Rectangle
alphaChannel *colorChannel
hex *widget.Entry
hex *userChangeEntry
contents fyne.CanvasObject
}

Expand Down