-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a float to string converter binding (two-way)
- Loading branch information
1 parent
85566a6
commit ea43b6f
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package binding | ||
|
||
type String interface { | ||
DataItem | ||
Get() string | ||
Set(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters