giowrap/main.go
Greg 70e5529a9b Add List widget combinator. Add HScroll and VScroll functions. Add
scroll demo to see a 2-way scrolling field of selectable labels.
2019-08-23 10:46:59 -04:00

582 lines
13 KiB
Go

package giowrap
import (
"image"
"image/color"
//"log"
"sync"
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/input"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
"gioui.org/ui/f32"
"gioui.org/ui/paint"
"gioui.org/ui/gesture"
"gioui.org/ui/pointer"
)
type Extra struct {
cur, max int
sync.Mutex
data []interface{}
}
var extra Extra
func (e *Extra) New() int {
e.Lock()
if e.data == nil { e.data = make([]interface{},0) }
ret := e.max
e.max = e.max + 1
e.data = append(e.data,nil)
e.Unlock()
return ret
}
type Context struct {
Faces measure.Faces
w *app.Window
c *app.Config
q input.Queue
ops *ui.Ops
cs layout.Constraints
dims layout.Dimens
}
func NewContext(w *app.Window) *Context {
return &Context{
w: w,
ops: new(ui.Ops),
q: w.Queue(),
}
}
func (ctx *Context) Reset(e app.UpdateEvent) {
ctx.c = &e.Config
ctx.ops.Reset()
ctx.cs = layout.RigidConstraints(e.Size)
ctx.Faces.Reset(ctx.c)
}
func (ctx *Context) Update() {
ctx.w.Update(ctx.ops)
}
type Layout func(*Context)
type Widget interface {
Layout(*Context)
}
type WidgetCombinator func(...Widget) Widget
type Label struct {
l *text.Label
}
type FaceOpt struct { face text.Face }
func Face(x text.Face) FaceOpt { return FaceOpt { x } }
type AlignOpt struct { alignment text.Alignment }
func Align(x text.Alignment) AlignOpt { return AlignOpt { x } }
type LabelOpts struct {
FaceOpt
c *color.RGBA
AlignOpt
}
type LabelOption interface { DoLabelOption(*LabelOpts) }
func (x FaceOpt) DoLabelOption(o *LabelOpts) { o.face = x.face }
func (x AlignOpt) DoLabelOption(o *LabelOpts) { o.alignment = x.alignment }
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c; }
func NewLabel(t string, lops ...LabelOption) *Label {
ret := &Label{}
opts := &LabelOpts{}
for _,o := range lops { o.DoLabelOption(opts) }
ret.l = &text.Label{
Face: opts.face,
Text: t,
Alignment: opts.alignment,
}
if opts.c != nil { // got a color option...
ops := new(ui.Ops)
ret.l.Material.Record(ops)
paint.ColorOp{Color: *opts.c}.Add(ops)
ret.l.Material.Stop()
}
return ret
}
func (l *Label) Layout(ctx *Context) {
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
}
func (l *Label) SetText(t string) {
l.l.Text = t
}
type Editor struct {
e *text.Editor
}
type SinglelineOpt struct { singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{ x } }
type EditorOpts struct {
FaceOpt
SinglelineOpt
}
type EditorOption interface { DoEditorOption(*EditorOpts) }
func (x FaceOpt) DoEditorOption(o *EditorOpts) { o.face = x.face }
func (x SinglelineOpt) DoEditorOption(o *EditorOpts) { o.singleline = x.singleline }
func NewEditor(t string, eops ...EditorOption) *Editor {
ret := &Editor{}
opts := &EditorOpts{}
for _,o := range eops { o.DoEditorOption(opts) }
ret.e = &text.Editor{ Face: opts.face, SingleLine: opts.singleline }
ret.SetText(t)
return ret
}
func (e *Editor) Layout(ctx *Context) {
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
}
func (e *Editor) Text() string { return e.e.Text() }
func (e *Editor) SetText(s string) { e.e.SetText(s) }
func (e *Editor) Focus() { e.e.Focus() }
type fWidget struct {
l Layout
}
func NewfWidget(l Layout) fWidget {
return fWidget{ l: l }
}
func (fw fWidget) Layout(ctx *Context) {
fw.l(ctx)
}
type Stack WidgetCombinator
func NewStack() Stack {
s := layout.Stack{ Alignment: layout.Center }
scs := make([]layout.StackChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
s.Init(ctx.ops, ctx.cs)
for _,w := range ws {
ctx.cs = s.Rigid()
w.Layout(ctx)
scs = append(scs, s.End(ctx.dims))
}
ctx.dims = s.Layout(scs...)
scs = scs[0:0]
})
}
}
type List = WidgetCombinator
type AxisOpt struct { axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{ x } }
type ListOpts struct {
AxisOpt
}
type ListOption interface { DoListOption(*ListOpts) }
func (x AxisOpt) DoListOption(o *ListOpts) { o.axis = x.axis }
func NewList(los ...ListOption) List {
opts := &ListOpts{}
for _,o := range los { o.DoListOption(opts) }
l := layout.List { Axis: opts.axis }
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
for l.Init(ctx.c, ctx.q, ctx.ops, ctx.cs, len(ws)); l.More(); l.Next() {
ctx.cs = l.Constraints()
ws[l.Index()].Layout(ctx)
l.End(ctx.dims)
}
ctx.dims = l.Layout()
})
}
}
func HScroll(c WidgetCombinator) WidgetCombinator {
return NewList(Axis(layout.Horizontal))
}
func VScroll(c WidgetCombinator) WidgetCombinator {
return NewList(Axis(layout.Vertical))
}
type Flex = WidgetCombinator
// This "Widget" does nothing except set the Flexible field of a FlexOpts
// struct within the Extra data structure
func Flexible(v float32) Widget {
return NewfWidget(func(ctx *Context) {
extra.data[extra.cur].(*FlexOpts).flexible = v
})
}
func Rigid() Widget {
return NewfWidget(func(ctx *Context) {
extra.data[extra.cur].(*FlexOpts).flexible = 0
})
}
type AlignmentOpt struct { alignment layout.Alignment }
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{ x } }
type FlexOpts struct {
AxisOpt
AlignmentOpt
flexible float32
}
type FlexOption interface { DoFlexOption(*FlexOpts) }
func (x AxisOpt) DoFlexOption(o *FlexOpts) { o.axis = x.axis }
func (x AlignmentOpt) DoFlexOption(o *FlexOpts) { o.alignment = x.alignment }
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
func NewFlex(fos ...FlexOption) Flex {
opts := &FlexOpts{}
for _,o := range fos { o.DoFlexOption(opts) }
f := layout.Flex{
Axis: opts.axis,
Alignment: opts.alignment,
}
index := extra.New()
extra.data[index] = opts
// do not call "make" inside the Layout function
fcs := make([]layout.FlexChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
// ensure child widgets write options to the right place
extra.cur = index
opts := extra.data[index].(*FlexOpts)
f.Init(ctx.ops, ctx.cs)
for _, w := range ws {
if opts.flexible != 0 {
ctx.cs = f.Flexible(opts.flexible)
} else {
ctx.cs = f.Rigid()
}
w.Layout(ctx)
fcs = append(fcs, f.End(ctx.dims))
}
ctx.dims = f.Layout(fcs...)
fcs = fcs[0:0] // truncate
})
}
}
type InsetOpts struct {
top, right, bottom, left ui.Value
}
type InsetOption interface { DoInsetOption(*InsetOpts) }
type TopOpt struct { top ui.Value }
func Top(x ui.Value) TopOpt { return TopOpt{ x } }
type RightOpt struct { right ui.Value }
func Right(x ui.Value) RightOpt { return RightOpt{ x } }
type BottomOpt struct { bottom ui.Value }
func Bottom(x ui.Value) BottomOpt { return BottomOpt{ x } }
type LeftOpt struct { left ui.Value }
func Left(x ui.Value) LeftOpt { return LeftOpt{ x } }
func (x TopOpt) DoInsetOption(o *InsetOpts) { o.top = x.top }
func (x RightOpt) DoInsetOption(o *InsetOpts) { o.right = x.right }
func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
type SizeOpt struct { size ui.Value }
func Size(x ui.Value) SizeOpt { return SizeOpt{ x } }
func (x SizeOpt) DoInsetOption(o *InsetOpts) {
o.top = x.size
o.right = x.size
o.bottom = x.size
o.left = x.size
}
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
func NewInset(insos ...InsetOption) WidgetCombinator {
opts := &InsetOpts{}
for _,o := range insos { o.DoInsetOption(opts) }
ins := layout.Inset{ Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left }
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
for _, w := range ws {
w.Layout(ctx)
}
ctx.dims = ins.End(ctx.dims)
})
}
}
type Background struct {
Color color.RGBA
Radius ui.Value
Inset layout.Inset
macro ui.MacroOp
}
type Enclosure interface {
Begin(*Context)
End(*Context)
}
func Enclose(e Enclosure, ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
e.Begin(ctx)
for _,w := range ws {
w.Layout(ctx)
}
e.End(ctx)
})
}
type BackgroundOpts struct {
c color.RGBA
radius ui.Value
}
type BackgroundOption interface { DoBackgroundOption(*BackgroundOpts) }
type ColorOpt struct { c color.RGBA }
func Color(x color.RGBA) ColorOpt { return ColorOpt{ x } }
func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
type RadiusOpt struct { radius ui.Value }
func Radius(x ui.Value) RadiusOpt { return RadiusOpt { x } }
func (x RadiusOpt) DoBackgroundOption(o *BackgroundOpts) { o.radius = x.radius }
func NewBackground(bos ...BackgroundOption) WidgetCombinator {
opts := &BackgroundOpts{}
for _,o := range bos { o.DoBackgroundOption(opts) }
bg := &Background{
Color: opts.c,
Radius: opts.radius,
Inset: layout.UniformInset(opts.radius),
}
return func(ws ...Widget) Widget {
return Enclose(bg, ws...)
}
}
func (bg *Background) Begin(ctx *Context) {
bg.macro.Record(ctx.ops)
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
}
func (bg *Background) End(ctx *Context) {
ctx.dims = bg.Inset.End(ctx.dims)
bg.macro.Stop()
var stack ui.StackOp
stack.Push(ctx.ops)
w, h := float32(ctx.dims.Size.X), float32(ctx.dims.Size.Y)
if r := float32(ctx.c.Px(bg.Radius)); r > 0 {
if r > w / 2 {
r = w / 2
}
if r > h / 2 {
r = h / 2
}
Rrect(ctx.ops, w, h, r, r, r, r)
}
paint.ColorOp{Color: bg.Color}.Add(ctx.ops)
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops)
bg.macro.Add(ctx.ops)
stack.Pop()
}
// https://pomax.github.io/bezierinfo/#circles_cubic.
func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
w, h := float32(width), float32(height)
const c = 0.55228475 // 4*(sqrt(2)-1)/3
var b paint.PathBuilder
b.Init(ops)
b.Move(f32.Point{X: w, Y: h - se})
b.Cube(f32.Point{X: 0, Y: se * c}, f32.Point{X: -se + se*c, Y: se}, f32.Point{X: -se, Y: se}) // SE
b.Line(f32.Point{X: sw - w + se, Y: 0})
b.Cube(f32.Point{X: -sw * c, Y: 0}, f32.Point{X: -sw, Y: -sw + sw*c}, f32.Point{X: -sw, Y: -sw}) // SW
b.Line(f32.Point{X: 0, Y: nw - h + sw})
b.Cube(f32.Point{X: 0, Y: -nw * c}, f32.Point{X: nw - nw*c, Y: -nw}, f32.Point{X: nw, Y: -nw}) // NW
b.Line(f32.Point{X: w - ne - nw, Y: 0})
b.Cube(f32.Point{X: ne * c, Y: 0}, f32.Point{X: ne, Y: ne - ne*c}, f32.Point{X: ne, Y: ne}) // NE
b.End()
}
type Clickable interface {
Widget
Clicked(*Context) bool
}
//cWidget is a clickable Widget that provides the Clicked() method.
type cWidget struct {
w Widget
click *gesture.Click
}
func (w cWidget) Layout(ctx *Context) {
w.w.Layout(ctx)
pointer.RectAreaOp{image.Rect(0,0,ctx.dims.Size.X,ctx.dims.Size.Y)}.Add(ctx.ops)
w.click.Add(ctx.ops)
}
func (w cWidget) Clicked(ctx *Context) bool {
for e, ok := w.click.Next(ctx.q); ok; e, ok = w.click.Next(ctx.q) {
if e.Type == gesture.TypeClick {
ctx.w.Invalidate()
return true
}
}
return false
}
//Clickable converts any Widget into a clickable Widget.
func AsClickable(w Widget) cWidget {
return cWidget{ w: w, click: new(gesture.Click) }
}
type Grid struct {
Cols int
Height, Width int
macro ui.MacroOp
ops *ui.Ops
cs layout.Constraints
mode gridMode
row, col int
}
type GridChild struct {
dims layout.Dimens
macro ui.MacroOp
}
type gridMode uint8
const (
modeNone gridMode = iota
modeBegun
)
func (g *Grid) Init(ops *ui.Ops, cs layout.Constraints) layout.Constraints {
g.mode = modeBegun
g.ops = ops
g.cs = cs
g.row, g.col = 0, 0
cs.Height.Min = 0
if g.Height != 0 { g.cs.Height.Max = g.Height }
if g.Width != 0 { g.cs.Width.Max = g.Width }
cs.Width.Max = g.cs.Width.Max / g.Cols
if g.Cols > 1 {
cs.Width.Min = cs.Width.Max
}
return cs
}
func (g *Grid) Begin() {
g.macro.Record(g.ops)
}
func (g *Grid) End(dims layout.Dimens) GridChild {
if g.mode != modeBegun {
panic("Must call Grid.Begin() before adding children.")
}
g.macro.Stop()
return GridChild{ dims: dims, macro: g.macro }
}
func (g *Grid) Layout(cs ...GridChild) layout.Dimens {
rowheight := 0
height := 0
var width float32
var maxwidth float32
for _,c := range cs {
var stack ui.StackOp
stack.Push(g.ops)
c.macro.Add(g.ops)
stack.Pop()
if c.dims.Size.Y > rowheight {
rowheight = c.dims.Size.Y
}
g.col = g.col+1
var x float32
if g.Cols == 1 {
x = float32(c.dims.Size.X)
if x > maxwidth { maxwidth = x }
} else {
x = float32(g.cs.Width.Max / g.Cols)
}
if g.col < g.Cols {
ui.TransformOp{}.Offset(f32.Point{X: x}).Add(g.ops)
width = width + x
} else {
g.col = 0
ui.TransformOp{}.Offset(f32.Point{ X: -width, Y: float32(rowheight) }).Add(g.ops)
g.row = g.row + 1
height = height + rowheight
width = 0
rowheight = 0
}
}
if height == 0 { height = rowheight }
g.mode = modeNone
var dwidth int
if g.Cols == 1 {
dwidth = int(maxwidth)
} else {
dwidth = g.cs.Width.Max
}
return layout.Dimens{ Size: image.Point{ dwidth, height } }
}
func toPointF(p image.Point) f32.Point {
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}
type GridOption interface { DoGridOption(*Grid) }
type HeightOpt struct { height int }
func Height(x int) HeightOpt { return HeightOpt{ x } }
func (x HeightOpt) DoGridOption(g *Grid) { g.Height = x.height }
func NewGrid(cols int, gops ...GridOption) WidgetCombinator {
g := &Grid{ Cols: cols }
for _, gop := range gops {
gop.DoGridOption(g)
}
gcs := make([]GridChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
cs := g.Init(ctx.ops, ctx.cs)
ctx.cs = cs
for _,w := range ws {
g.Begin()
w.Layout(ctx)
ctx.cs = cs // widget layout can modify constraints...
gcs = append(gcs,g.End(ctx.dims))
}
ctx.dims = g.Layout(gcs...)
gcs = gcs[0:0]
})
}
}