forked from fyne-io/fyne
/
bool.go
118 lines (100 loc) · 2.28 KB
/
bool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package binding
type not struct {
Bool
}
var _ Bool = (*not)(nil)
// Not returns a Bool binding that invert the value of the given data binding.
// This is providing the logical Not boolean operation as a data binding.
//
// Since 2.4
func Not(data Bool) Bool {
return ¬{Bool: data}
}
func (n *not) Get() (bool, error) {
v, err := n.Bool.Get()
return !v, err
}
func (n *not) Set(value bool) error {
return n.Bool.Set(!value)
}
type and struct {
booleans
}
var _ Bool = (*and)(nil)
// And returns a Bool binding that return true when all the passed Bool binding are
// true and false otherwise. It does apply a logical and boolean operation on all passed
// Bool bindings. This binding is two way. In case of a Set, it will propagate the value
// identically to all the Bool bindings used for its construction.
//
// Since 2.4
func And(data ...Bool) Bool {
return &and{booleans: booleans{data: data}}
}
func (a *and) Get() (bool, error) {
for _, d := range a.data {
v, err := d.Get()
if err != nil {
return false, err
}
if !v {
return false, nil
}
}
return true, nil
}
func (a *and) Set(value bool) error {
for _, d := range a.data {
err := d.Set(value)
if err != nil {
return err
}
}
return nil
}
type or struct {
booleans
}
var _ Bool = (*or)(nil)
// Or returns a Bool binding that return true when at least one of the passed Bool binding
// is true and false otherwise. It does apply a logical or boolean operation on all passed
// Bool bindings. This binding is two way. In case of a Set, it will propagate the value
// identically to all the Bool bindings used for its construction.
//
// Since 2.4
func Or(data ...Bool) Bool {
return &or{booleans: booleans{data: data}}
}
func (o *or) Get() (bool, error) {
for _, d := range o.data {
v, err := d.Get()
if err != nil {
return false, err
}
if v {
return true, nil
}
}
return false, nil
}
func (o *or) Set(value bool) error {
for _, d := range o.data {
err := d.Set(value)
if err != nil {
return err
}
}
return nil
}
type booleans struct {
data []Bool
}
func (g *booleans) AddListener(listener DataListener) {
for _, d := range g.data {
d.AddListener(listener)
}
}
func (g *booleans) RemoveListener(listener DataListener) {
for _, d := range g.data {
d.RemoveListener(listener)
}
}