mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
widget: redefine Enum.Changed and Bool.Changed to consider only user interaction
Ignore programmatic value changes to avoid feedback loops. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+4
-4
@@ -14,8 +14,7 @@ type Bool struct {
|
|||||||
// Last is the last registered click.
|
// Last is the last registered click.
|
||||||
Last Press
|
Last Press
|
||||||
|
|
||||||
// changeVal tracks Value from the most recent call to Changed.
|
changed bool
|
||||||
changeVal bool
|
|
||||||
|
|
||||||
gesture gesture.Click
|
gesture gesture.Click
|
||||||
}
|
}
|
||||||
@@ -23,8 +22,8 @@ type Bool struct {
|
|||||||
// Changed reports whether Value has changed since the last
|
// Changed reports whether Value has changed since the last
|
||||||
// call to Changed.
|
// call to Changed.
|
||||||
func (b *Bool) Changed() bool {
|
func (b *Bool) Changed() bool {
|
||||||
changed := b.Value != b.changeVal
|
changed := b.changed
|
||||||
b.changeVal = b.Value
|
b.changed = false
|
||||||
return changed
|
return changed
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +36,7 @@ func (b *Bool) Layout(gtx layout.Context) layout.Dimensions {
|
|||||||
Position: e.Position,
|
Position: e.Position,
|
||||||
}
|
}
|
||||||
b.Value = !b.Value
|
b.Value = !b.Value
|
||||||
|
b.changed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var st op.StackOp
|
var st op.StackOp
|
||||||
|
|||||||
+5
-4
@@ -12,7 +12,7 @@ import (
|
|||||||
type Enum struct {
|
type Enum struct {
|
||||||
Value string
|
Value string
|
||||||
|
|
||||||
changeVal string
|
changed bool
|
||||||
|
|
||||||
clicks []gesture.Click
|
clicks []gesture.Click
|
||||||
values []string
|
values []string
|
||||||
@@ -27,11 +27,11 @@ func index(vs []string, t string) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Changed reports whether Value has changed since the last
|
// Changed reports whether Value has changed by user interactino since the last
|
||||||
// call to Changed.
|
// call to Changed.
|
||||||
func (e *Enum) Changed() bool {
|
func (e *Enum) Changed() bool {
|
||||||
changed := e.changeVal != e.Value
|
changed := e.changed
|
||||||
e.changeVal = e.Value
|
e.changed = true
|
||||||
return changed
|
return changed
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +53,7 @@ func (e *Enum) Layout(gtx layout.Context, key string) layout.Dimensions {
|
|||||||
switch ev.Type {
|
switch ev.Type {
|
||||||
case gesture.TypeClick:
|
case gesture.TypeClick:
|
||||||
e.Value = e.values[idx]
|
e.Value = e.values[idx]
|
||||||
|
e.changed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clk.Add(gtx.Ops)
|
clk.Add(gtx.Ops)
|
||||||
|
|||||||
Reference in New Issue
Block a user