-
-
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.
First pass for latest data binding API proposal
This commit is float value to test concept. Includes a simple demo :)
- Loading branch information
1 parent
808a4b1
commit 7154e4c
Showing
8 changed files
with
251 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package binding | ||
|
||
// DataItemListener is any object that can register for changes in a bindable DataItem. | ||
// See NewNotifyFunction to define a new listener using just an inline function. | ||
type DataItemListener interface { | ||
DataChanged(DataItem) | ||
} | ||
|
||
// DataItem is the base interface for all bindable data items. | ||
type DataItem interface { | ||
AddListener(DataItemListener) | ||
RemoveListener(DataItemListener) | ||
} | ||
|
||
type base struct { | ||
listeners []DataItemListener | ||
} | ||
|
||
// AddListener allows a data listener to be informed of changes to this item. | ||
func (b *base) AddListener(l DataItemListener) { | ||
b.listeners = append(b.listeners, l) | ||
} | ||
|
||
// RemoveListener should be called if the listener is no longer interested in being informed of data change events. | ||
func (b *base) RemoveListener(l DataItemListener) { | ||
for i, listen := range b.listeners { | ||
if listen != l { | ||
continue | ||
} | ||
|
||
if i == len(b.listeners)-1 { | ||
b.listeners = b.listeners[:len(b.listeners)-1] | ||
} else { | ||
b.listeners = append(b.listeners[:i], b.listeners[i+1:]...) | ||
} | ||
} | ||
} | ||
|
||
func (b *base) trigger(i DataItem) { | ||
for _, listen := range b.listeners { | ||
listen.DataChanged(i) | ||
} | ||
} | ||
|
||
type listener struct { | ||
callback func(DataItem) | ||
} | ||
|
||
// NewNotifyFunction is a helper function that creates a new listener type from a simple callback function. | ||
func NewNotifyFunction(fn func(DataItem)) DataItemListener { | ||
return &listener{fn} | ||
} | ||
|
||
func (l *listener) DataChanged(i DataItem) { | ||
l.callback(i) | ||
} |
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,52 @@ | ||
package binding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type simpleItem struct { | ||
base | ||
} | ||
|
||
func TestBase_AddListener(t *testing.T) { | ||
data := &simpleItem{} | ||
assert.Equal(t, 0, len(data.listeners)) | ||
|
||
called := false | ||
fn := NewNotifyFunction(func(DataItem) { | ||
called = true | ||
}) | ||
data.AddListener(fn) | ||
assert.Equal(t, 1, len(data.listeners)) | ||
|
||
data.trigger(data) | ||
assert.True(t, called) | ||
} | ||
|
||
func TestBase_RemoveListener(t *testing.T) { | ||
called := false | ||
fn := NewNotifyFunction(func(DataItem) { | ||
called = true | ||
}) | ||
data := &simpleItem{} | ||
data.listeners = []DataItemListener{fn} | ||
|
||
assert.Equal(t, 1, len(data.listeners)) | ||
data.RemoveListener(fn) | ||
assert.Equal(t, 0, len(data.listeners)) | ||
|
||
data.trigger(data) | ||
assert.False(t, called) | ||
} | ||
|
||
func TestNewNotifyFunction(t *testing.T) { | ||
called := false | ||
fn := NewNotifyFunction(func(DataItem) { | ||
called = true | ||
}) | ||
|
||
fn.DataChanged(nil) | ||
assert.True(t, called) | ||
} |
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,42 @@ | ||
package binding | ||
|
||
// Float supports binding a float64 value in a Fyne application | ||
type Float interface { | ||
DataItem | ||
Get() float64 | ||
Set(float64) | ||
} | ||
|
||
// NewFloat returns a bindable float value that is managed internally. | ||
func NewFloat() Float { | ||
blank := 0.0 | ||
return &floatBind{val: &blank} | ||
} | ||
|
||
// BindFloat returns a new bindable value that controls the contents of the provided float64 variable. | ||
func BindFloat(f *float64) Float { | ||
if f == nil { | ||
return NewFloat() // never allow a nil value pointer | ||
} | ||
|
||
return &floatBind{val: f} | ||
} | ||
|
||
type floatBind struct { | ||
base | ||
|
||
val *float64 | ||
} | ||
|
||
func (f *floatBind) Get() float64 { | ||
if f.val == nil { | ||
return 0.0 | ||
} | ||
return *f.val | ||
} | ||
|
||
func (f *floatBind) Set(val float64) { | ||
*f.val = val | ||
|
||
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,24 @@ | ||
package binding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBindFloat(t *testing.T) { | ||
val := 0.5 | ||
f := BindFloat(&val) | ||
assert.Equal(t, float64(0.5), f.Get()) | ||
|
||
f.Set(0.3) | ||
assert.Equal(t, float64(0.3), val) | ||
} | ||
|
||
func TestNewFloat(t *testing.T) { | ||
f := NewFloat() | ||
assert.Equal(t, float64(0.0), f.Get()) | ||
|
||
f.Set(0.3) | ||
assert.Equal(t, float64(0.3), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tutorials | ||
|
||
import ( | ||
"fmt" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/binding" | ||
"fyne.io/fyne/container" | ||
"fyne.io/fyne/widget" | ||
) | ||
|
||
func bindingScreen(_ fyne.Window) fyne.CanvasObject { | ||
data := binding.NewFloat() | ||
label := widget.NewLabel(floatLabel(data)) | ||
data.AddListener(binding.NewNotifyFunction(func(binding.DataItem) { | ||
label.SetText(floatLabel(data)) | ||
})) | ||
|
||
slide := widget.NewSliderWithData(0, 1, data) | ||
slide.Step = 0.1 | ||
bar := widget.NewProgressBarWithData(data) | ||
|
||
buttons := container.NewGridWithColumns(4, | ||
widget.NewButton("0%", func() { | ||
data.Set(0) | ||
}), | ||
widget.NewButton("30%", func() { | ||
data.Set(0.3) | ||
}), | ||
widget.NewButton("70%", func() { | ||
data.Set(0.7) | ||
}), | ||
widget.NewButton("100%", func() { | ||
data.Set(1) | ||
})) | ||
|
||
return container.NewVBox(label, slide, bar, buttons) | ||
} | ||
|
||
func floatLabel(data binding.Float) string { // TODO add type conversion | ||
return fmt.Sprintf("Float current value: %0.1f", data.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
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