Skip to content

Commit

Permalink
Merge pull request #2109 from fpabl0/fix/2075
Browse files Browse the repository at this point in the history
Do not double update color channels when their associated entry text is changed programmatically, fixes #2075
  • Loading branch information
fpabl0 committed Mar 26, 2021
2 parents 9cc38f1 + 2c55b2a commit 1a6c38f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
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

0 comments on commit 1a6c38f

Please sign in to comment.