Skip to content

Commit

Permalink
Add a float to string converter binding (two-way)
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Nov 7, 2020
1 parent 85566a6 commit ea43b6f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions binding/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (f *floatBind) Get() float64 {
}

func (f *floatBind) Set(val float64) {
if *f.val == val {
return
}
*f.val = val

f.trigger(f)
Expand Down
7 changes: 7 additions & 0 deletions binding/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package binding

type String interface {
DataItem
Get() string
Set(string)
}
44 changes: 44 additions & 0 deletions binding/tostring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package binding

import (
"fmt"
"strconv"

"fyne.io/fyne"
)

type floatToString struct {
base

from Float
}

func FloatToString(f Float) String {
str := &floatToString{from: f}
f.AddListener(str)
return str
}

func (f *floatToString) Get() string {
val := f.from.Get()

return fmt.Sprintf("%0.2f", val) // TODO format string
}

func (f *floatToString) Set(val string) {
fVal, err := strconv.ParseFloat(val, 0)
if err != nil {
fyne.LogError("Float parse error", err)
return
}
if fVal == f.from.Get() {
return
}
f.from.Set(fVal)

f.trigger(f)
}

func (f *floatToString) DataChanged(_ DataItem) {
f.trigger(f)
}
19 changes: 19 additions & 0 deletions binding/tostring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package binding

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFloatToString(t *testing.T) {
f := NewFloat()
s := FloatToString(f)
assert.Equal(t, "0.00", s.Get())

f.Set(0.3)
assert.Equal(t, "0.30", s.Get())

s.Set("5.00")
assert.Equal(t, 5.0, f.Get())
}
17 changes: 17 additions & 0 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"unicode"

"fyne.io/fyne"
"fyne.io/fyne/binding"
"fyne.io/fyne/canvas"
"fyne.io/fyne/driver/desktop"
"fyne.io/fyne/driver/mobile"
Expand Down Expand Up @@ -77,6 +78,22 @@ func NewEntry() *Entry {
return e
}

// NewEntryWithData returns an Entry widget connected to the specified data source.
func NewEntryWithData(data binding.String) *Entry {
entry := NewEntry()
entry.Text = data.Get()

data.AddListener(binding.NewDataItemListener(func(binding.DataItem) {
entry.Text = data.Get()
entry.Refresh()
}))
entry.OnChanged = func(s string) {
data.Set(s)
}

return entry
}

// NewMultiLineEntry creates a new entry that allows multiple lines
func NewMultiLineEntry() *Entry {
e := &Entry{MultiLine: true}
Expand Down
13 changes: 13 additions & 0 deletions widget/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"image/color"

"fyne.io/fyne"
"fyne.io/fyne/binding"
"fyne.io/fyne/theme"
)

Expand All @@ -22,6 +23,18 @@ func NewLabel(text string) *Label {
return NewLabelWithStyle(text, fyne.TextAlignLeading, fyne.TextStyle{})
}

// NewLabelWithData returns an Label widget connected to the specified data source.
func NewLabelWithData(data binding.String) *Label {
label := NewLabel(data.Get())

data.AddListener(binding.NewDataItemListener(func(binding.DataItem) {
label.Text = data.Get()
label.Refresh()
}))

return label
}

// NewLabelWithStyle creates a new label widget with the set text content
func NewLabelWithStyle(text string, alignment fyne.TextAlign, style fyne.TextStyle) *Label {
l := &Label{
Expand Down

0 comments on commit ea43b6f

Please sign in to comment.