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

Added custom binding 'DisableableBinding' and added it to README.md. #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Optionally, Each canvas object can be encapsulated with `Responsive()` function
```go
layout := NewResponsiveLayout(
Responsive(object1), // all sizes to 100%
Responsive(object2, 0.5, 0.75), // small to 50%, medium to 75%, all others to 100%
Responsive(object2, 0.5, 0.75), // small to 50%, medium to 75%, all others to 100%
)
```

Expand All @@ -59,7 +59,7 @@ Community contributed widgets.

### Calendar



A date picker which returns a [time](https://pkg.go.dev/time) object with the selected date.

Expand All @@ -69,7 +69,7 @@ A date picker which returns a [time](https://pkg.go.dev/time) object with the se

</p>



To use create a new calendar with a given time and a callback function:

Expand Down Expand Up @@ -224,6 +224,16 @@ if err := token.Error(); err != nil {
s, err := binding.NewMqttString(client, "fyne.io/x/string")
```

### DisableableBinding

A `DisableableBinding` creates a `Bool` data binding which accepts `Disableable` targets to control.
When the `bool` binding is changed, all targets `Enable` or `Disable` methods will be executed depending on the settings.

The binding accepts targets when created with `NewDisableableBinding` or via the method `AddTargets`.
The behaviour of the binding can be inverted, so true disables and false enables. This is done through method `Invert`.

All targets are updated when either `AddTargets` or `Invert` is executed.

## Data Validation

Community contributed validators.
Expand Down
61 changes: 61 additions & 0 deletions data/binding/disableableBinding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package binding

import (
"log"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
)

type disableableBinding struct {
binding.Bool

inverted bool
targets []fyne.Disableable
}

// NewDisableableBinding returns a `Bool` binding which accepts Disableable targets.
// When the Bool changes, the targets Enable or Disable method will be executed.
func NewDisableableBinding(targets ...fyne.Disableable) *disableableBinding {
newBinding := &disableableBinding{
Bool: binding.NewBool(),
targets: targets,
}

// Add default listener
newBinding.AddListener(binding.NewDataListener(newBinding.update))

return newBinding
}

// Adding targets to the binding.
// This will update the Disable status of the targets immediately.
func (b *disableableBinding) AddTargets(targets ...fyne.Disableable) {
b.targets = append(b.targets, targets...)
b.update()
}

// Invert will switch the behavior of when the targets will be Enabled or Disabled.
// This will update the Disable status of the targets immediately.
func (b *disableableBinding) Invert(inverted bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be SetInverted? The name Invert applies action, which is to take something and make it the opposite state - which isn't really what the method does...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea with SetInverted. I had it as an action first, but then realized that it wasn't a good solution so I changed it. I'll change the name.
Isn't Widgets to generalized since it's only widgets that implements Disableable that are accepted? I agree that "targets" isn't a good name either. I guess "disableableWidgets" too long?

Not really sure what similar scenarios there are in Fyne and how they are named.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Go we don't typically put the type in the naming do we? For example Form.AddItem takes a widget.FormItem etc.
So the type ensures safety and the "Disablable" context is kindof provided by the object you are working with.
Happy to get others thoughts of course.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the name to Widget and also a SetInverted.
Do you feel that this issue is resolved or was there something more about name?
I've been thinking about maybe changing the name of the binding. Fells a bit to much to say binding.DisableableBinding.
Either change the name to Disableable or DisableableBool.
Any opinions on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you it could have a smoother name. binding.NewDisableable is a pretty good suggestion.

Apologies for the delay

b.inverted = inverted
b.update()
}

func (b *disableableBinding) update() {
val, err := b.Get()
if err != nil {
log.Println(err)
return
}

if (!b.inverted && val) || (b.inverted && !val) {
for _, target := range b.targets {
target.Enable()
}
} else {
for _, target := range b.targets {
target.Disable()
}
}
}