forked from joejulian/gio
936c266b03
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.
Before this change, op.Save and Load were used to save and restore the
state:
ops := new(op.Ops)
// Save state.
state := op.Save(ops)
// Apply offset.
op.Offset(...).Add(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
state.Load()
A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.
This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:
ops := new(op.Ops)
// Push offset to the transformation stack.
stack := op.Offset(...).Push(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
stack.Pop()
For convenience, transformation also be Add'ed if the stack operation is
not required.
Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.
API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.
To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package widget
|
|
|
|
import (
|
|
"image"
|
|
|
|
"gioui.org/gesture"
|
|
"gioui.org/io/pointer"
|
|
"gioui.org/layout"
|
|
)
|
|
|
|
// Float is for selecting a value in a range.
|
|
type Float struct {
|
|
Value float32
|
|
Axis layout.Axis
|
|
|
|
drag gesture.Drag
|
|
pos float32 // position normalized to [0, 1]
|
|
length float32
|
|
changed bool
|
|
}
|
|
|
|
// Dragging returns whether the value is being interacted with.
|
|
func (f *Float) Dragging() bool { return f.drag.Dragging() }
|
|
|
|
// Layout updates the value according to drag events along the f's main axis.
|
|
//
|
|
// The range of f is set by the minimum constraints main axis value.
|
|
func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32) layout.Dimensions {
|
|
size := gtx.Constraints.Min
|
|
f.length = float32(f.Axis.Convert(size).X)
|
|
|
|
var de *pointer.Event
|
|
for _, e := range f.drag.Events(gtx.Metric, gtx, gesture.Axis(f.Axis)) {
|
|
if e.Type == pointer.Press || e.Type == pointer.Drag {
|
|
de = &e
|
|
}
|
|
}
|
|
|
|
value := f.Value
|
|
if de != nil {
|
|
xy := de.Position.X
|
|
if f.Axis == layout.Vertical {
|
|
xy = de.Position.Y
|
|
}
|
|
f.pos = xy / f.length
|
|
value = min + (max-min)*f.pos
|
|
} else if min != max {
|
|
f.pos = (value - min) / (max - min)
|
|
}
|
|
// Unconditionally call setValue in case min, max, or value changed.
|
|
f.setValue(value, min, max)
|
|
|
|
if f.pos < 0 {
|
|
f.pos = 0
|
|
} else if f.pos > 1 {
|
|
f.pos = 1
|
|
}
|
|
|
|
margin := f.Axis.Convert(image.Pt(pointerMargin, 0))
|
|
rect := image.Rectangle{
|
|
Min: margin.Mul(-1),
|
|
Max: size.Add(margin),
|
|
}
|
|
defer pointer.Rect(rect).Push(gtx.Ops).Pop()
|
|
f.drag.Add(gtx.Ops)
|
|
|
|
return layout.Dimensions{Size: size}
|
|
}
|
|
|
|
func (f *Float) setValue(value, min, max float32) {
|
|
if min > max {
|
|
min, max = max, min
|
|
}
|
|
if value < min {
|
|
value = min
|
|
} else if value > max {
|
|
value = max
|
|
}
|
|
if f.Value != value {
|
|
f.Value = value
|
|
f.changed = true
|
|
}
|
|
}
|
|
|
|
// Pos reports the selected position.
|
|
func (f *Float) Pos() float32 {
|
|
return f.pos * f.length
|
|
}
|
|
|
|
// Changed reports whether the value has changed since
|
|
// the last call to Changed.
|
|
func (f *Float) Changed() bool {
|
|
changed := f.changed
|
|
f.changed = false
|
|
return changed
|
|
}
|