giowrap/main.go

488 lines
11 KiB
Go
Raw Normal View History

2019-08-15 09:16:00 -04:00
package giowrap
2019-08-08 18:11:20 -04:00
import (
"image"
2019-08-15 09:16:00 -04:00
"image/color"
2019-08-16 16:38:49 -04:00
//"log"
"sync"
2019-08-08 18:11:20 -04:00
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/input"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
2019-08-15 09:16:00 -04:00
"gioui.org/ui/f32"
"gioui.org/ui/gesture"
2019-08-27 09:24:30 -04:00
"gioui.org/ui/paint"
2019-08-15 09:16:00 -04:00
"gioui.org/ui/pointer"
2019-08-08 18:11:20 -04:00
)
2019-08-16 16:38:49 -04:00
type Extra struct {
cur, max int
sync.Mutex
data []interface{}
}
var extra Extra
func (e *Extra) New() int {
e.Lock()
2019-08-27 09:24:30 -04:00
if e.data == nil {
e.data = make([]interface{}, 0)
}
2019-08-16 16:38:49 -04:00
ret := e.max
e.max = e.max + 1
2019-08-27 09:24:30 -04:00
e.data = append(e.data, nil)
2019-08-16 16:38:49 -04:00
e.Unlock()
return ret
}
2019-08-08 18:11:20 -04:00
type Context struct {
2019-08-15 09:16:00 -04:00
Faces measure.Faces
2019-08-27 09:24:30 -04:00
w *app.Window
c *app.Config
q input.Queue
ops *ui.Ops
cs layout.Constraints
2019-08-08 18:11:20 -04:00
dims layout.Dimens
}
func NewContext(w *app.Window) *Context {
return &Context{
2019-08-27 09:24:30 -04:00
w: w,
2019-08-12 08:47:16 -04:00
ops: new(ui.Ops),
2019-08-27 09:24:30 -04:00
q: w.Queue(),
2019-08-12 08:47:16 -04:00
}
2019-08-08 18:11:20 -04:00
}
func (ctx *Context) Reset(e app.UpdateEvent) {
2019-08-08 18:11:20 -04:00
ctx.c = &e.Config
ctx.ops.Reset()
ctx.cs = layout.RigidConstraints(e.Size)
2019-08-15 09:16:00 -04:00
ctx.Faces.Reset(ctx.c)
}
func (ctx *Context) Update() {
ctx.w.Update(ctx.ops)
2019-08-15 09:16:00 -04:00
}
type Layout func(*Context)
2019-08-15 09:16:00 -04:00
2019-08-08 18:11:20 -04:00
type Widget interface {
Layout(*Context)
2019-08-08 18:11:20 -04:00
}
type WidgetCombinator func(...Widget) Widget
2019-08-15 17:47:46 -04:00
type Label struct {
2019-08-15 09:16:00 -04:00
l *text.Label
2019-08-08 18:11:20 -04:00
}
2019-08-27 09:24:30 -04:00
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} }
2019-08-20 12:39:28 -04:00
2019-08-16 16:38:49 -04:00
type LabelOpts struct {
2019-08-20 12:39:28 -04:00
FaceOpt
2019-08-22 18:15:14 -04:00
c *color.RGBA
2019-08-20 12:39:28 -04:00
AlignOpt
2019-08-16 16:38:49 -04:00
}
2019-08-27 09:24:30 -04:00
type LabelOption interface{ DoLabelOption(*LabelOpts) }
func (x FaceOpt) DoLabelOption(o *LabelOpts) { o.face = x.face }
2019-08-20 12:39:28 -04:00
func (x AlignOpt) DoLabelOption(o *LabelOpts) { o.alignment = x.alignment }
2019-08-27 09:24:30 -04:00
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c }
2019-08-16 16:38:49 -04:00
func NewLabel(t string, lops ...LabelOption) *Label {
2019-08-15 17:47:46 -04:00
ret := &Label{}
2019-08-16 16:38:49 -04:00
opts := &LabelOpts{}
2019-08-27 09:24:30 -04:00
for _, o := range lops {
o.DoLabelOption(opts)
}
2019-08-15 09:16:00 -04:00
ret.l = &text.Label{
2019-08-27 09:24:30 -04:00
Face: opts.face,
Text: t,
2019-08-16 16:38:49 -04:00
Alignment: opts.alignment,
2019-08-08 18:11:20 -04:00
}
2019-08-22 18:15:14 -04:00
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()
}
2019-08-15 09:16:00 -04:00
return ret
2019-08-08 18:11:20 -04:00
}
func (l *Label) Layout(ctx *Context) {
2019-08-15 17:47:46 -04:00
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
2019-08-12 08:47:16 -04:00
}
2019-08-20 12:39:28 -04:00
func (l *Label) SetText(t string) {
l.l.Text = t
}
type Editor struct {
e *text.Editor
}
2019-08-27 09:24:30 -04:00
type SinglelineOpt struct{ singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{x} }
2019-08-20 12:39:28 -04:00
2019-08-16 16:38:49 -04:00
type EditorOpts struct {
2019-08-20 12:39:28 -04:00
FaceOpt
SinglelineOpt
2019-08-16 16:38:49 -04:00
}
2019-08-27 09:24:30 -04:00
type EditorOption interface{ DoEditorOption(*EditorOpts) }
func (x FaceOpt) DoEditorOption(o *EditorOpts) { o.face = x.face }
2019-08-20 12:39:28 -04:00
func (x SinglelineOpt) DoEditorOption(o *EditorOpts) { o.singleline = x.singleline }
2019-08-16 16:38:49 -04:00
func NewEditor(t string, eops ...EditorOption) *Editor {
ret := &Editor{}
2019-08-16 16:38:49 -04:00
opts := &EditorOpts{}
2019-08-27 09:24:30 -04:00
for _, o := range eops {
o.DoEditorOption(opts)
}
ret.e = &text.Editor{Face: opts.face, SingleLine: opts.singleline}
2019-08-16 16:38:49 -04:00
ret.SetText(t)
return ret
}
func (e *Editor) Layout(ctx *Context) {
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
2019-08-12 08:47:16 -04:00
}
2019-08-27 09:24:30 -04:00
func (e *Editor) Text() string { return e.e.Text() }
2019-08-15 18:32:01 -04:00
func (e *Editor) SetText(s string) { e.e.SetText(s) }
2019-08-27 09:24:30 -04:00
func (e *Editor) Focus() { e.e.Focus() }
2019-08-15 09:16:00 -04:00
2019-08-15 18:32:01 -04:00
type fWidget struct {
l Layout
2019-08-15 09:16:00 -04:00
}
2019-08-15 18:32:01 -04:00
func NewfWidget(l Layout) fWidget {
2019-08-27 09:24:30 -04:00
return fWidget{l: l}
2019-08-15 09:16:00 -04:00
}
func (fw fWidget) Layout(ctx *Context) {
fw.l(ctx)
2019-08-08 18:11:20 -04:00
}
2019-08-20 12:39:28 -04:00
type Stack WidgetCombinator
func NewStack() Stack {
2019-08-27 09:24:30 -04:00
s := layout.Stack{Alignment: layout.Center}
scs := make([]layout.StackChild, 0)
2019-08-20 12:39:28 -04:00
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
s.Init(ctx.ops, ctx.cs)
2019-08-27 09:24:30 -04:00
for _, w := range ws {
2019-08-20 12:39:28 -04:00
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
2019-08-27 09:24:30 -04:00
type AxisOpt struct{ axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{x} }
type ListOpts struct {
AxisOpt
}
2019-08-27 09:24:30 -04:00
type ListOption interface{ DoListOption(*ListOpts) }
func (x AxisOpt) DoListOption(o *ListOpts) { o.axis = x.axis }
func NewList(los ...ListOption) List {
opts := &ListOpts{}
2019-08-27 09:24:30 -04:00
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
2019-08-15 09:16:00 -04:00
2019-08-16 16:38:49 -04:00
// This "Widget" does nothing except set the Flexible field of a FlexOpts
// struct within the Extra data structure
2019-08-15 09:27:04 -04:00
func Flexible(v float32) Widget {
return NewfWidget(func(ctx *Context) {
2019-08-16 16:38:49 -04:00
extra.data[extra.cur].(*FlexOpts).flexible = v
2019-08-15 09:16:00 -04:00
})
}
2019-08-20 12:39:28 -04:00
func Rigid() Widget {
return NewfWidget(func(ctx *Context) {
extra.data[extra.cur].(*FlexOpts).flexible = 0
})
2019-08-16 16:38:49 -04:00
}
2019-08-27 09:24:30 -04:00
type AlignmentOpt struct{ alignment layout.Alignment }
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{x} }
2019-08-16 16:38:49 -04:00
2019-08-20 12:39:28 -04:00
type FlexOpts struct {
AxisOpt
AlignmentOpt
2019-08-20 12:39:28 -04:00
flexible float32
2019-08-16 16:38:49 -04:00
}
2019-08-27 09:24:30 -04:00
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 }
2019-08-16 16:38:49 -04:00
2019-08-15 18:32:01 -04:00
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
2019-08-16 16:38:49 -04:00
func NewFlex(fos ...FlexOption) Flex {
opts := &FlexOpts{}
2019-08-27 09:24:30 -04:00
for _, o := range fos {
o.DoFlexOption(opts)
}
2019-08-08 18:11:20 -04:00
f := layout.Flex{
2019-08-27 09:24:30 -04:00
Axis: opts.axis,
Alignment: opts.alignment,
2019-08-08 18:11:20 -04:00
}
2019-08-16 16:38:49 -04:00
index := extra.New()
extra.data[index] = opts
// do not call "make" inside the Layout function
2019-08-27 09:24:30 -04:00
fcs := make([]layout.FlexChild, 0)
2019-08-16 16:38:49 -04:00
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
2019-08-27 09:24:30 -04:00
// ensure child widgets write options to the right place
2019-08-16 16:38:49 -04:00
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()
2019-08-08 18:11:20 -04:00
}
w.Layout(ctx)
fcs = append(fcs, f.End(ctx.dims))
2019-08-08 18:11:20 -04:00
}
2019-08-20 12:39:28 -04:00
ctx.dims = f.Layout(fcs...)
fcs = fcs[0:0] // truncate
2019-08-15 09:16:00 -04:00
})
2019-08-08 18:11:20 -04:00
}
}
2019-08-16 16:38:49 -04:00
type InsetOpts struct {
top, right, bottom, left ui.Value
}
2019-08-27 09:24:30 -04:00
type InsetOption interface{ DoInsetOption(*InsetOpts) }
2019-08-16 16:38:49 -04:00
2019-08-27 09:24:30 -04:00
type TopOpt struct{ top ui.Value }
2019-08-20 12:39:28 -04:00
2019-08-27 09:24:30 -04:00
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 }
2019-08-20 12:39:28 -04:00
func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
2019-08-27 09:24:30 -04:00
func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
2019-08-20 12:39:28 -04:00
2019-08-27 09:24:30 -04:00
type SizeOpt struct{ size ui.Value }
func Size(x ui.Value) SizeOpt { return SizeOpt{x} }
2019-08-20 12:39:28 -04:00
func (x SizeOpt) DoInsetOption(o *InsetOpts) {
o.top = x.size
o.right = x.size
o.bottom = x.size
o.left = x.size
2019-08-16 16:38:49 -04:00
}
2019-08-15 18:32:01 -04:00
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
2019-08-16 16:38:49 -04:00
func NewInset(insos ...InsetOption) WidgetCombinator {
opts := &InsetOpts{}
2019-08-27 09:24:30 -04:00
for _, o := range insos {
o.DoInsetOption(opts)
}
ins := layout.Inset{Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left}
2019-08-12 08:47:16 -04:00
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
2019-08-12 08:47:16 -04:00
for _, w := range ws {
w.Layout(ctx)
2019-08-12 08:47:16 -04:00
}
ctx.dims = ins.End(ctx.dims)
2019-08-15 09:16:00 -04:00
})
}
}
type Background struct {
2019-08-27 09:24:30 -04:00
Color color.RGBA
2019-08-15 09:16:00 -04:00
Radius ui.Value
2019-08-27 09:24:30 -04:00
Inset layout.Inset
2019-08-15 09:16:00 -04:00
macro ui.MacroOp
}
type Enclosure interface {
Begin(*Context)
End(*Context)
2019-08-15 09:16:00 -04:00
}
func Enclose(e Enclosure, ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
e.Begin(ctx)
2019-08-27 09:24:30 -04:00
for _, w := range ws {
w.Layout(ctx)
2019-08-15 09:16:00 -04:00
}
e.End(ctx)
2019-08-15 09:16:00 -04:00
})
}
2019-08-16 16:38:49 -04:00
type BackgroundOpts struct {
2019-08-27 09:24:30 -04:00
c color.RGBA
2019-08-16 16:38:49 -04:00
radius ui.Value
}
2019-08-27 09:24:30 -04:00
type BackgroundOption interface{ DoBackgroundOption(*BackgroundOpts) }
2019-08-20 12:39:28 -04:00
2019-08-27 09:24:30 -04:00
type ColorOpt struct{ c color.RGBA }
func Color(x color.RGBA) ColorOpt { return ColorOpt{x} }
2019-08-20 12:39:28 -04:00
func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
2019-08-27 09:24:30 -04:00
type RadiusOpt struct{ radius ui.Value }
func Radius(x ui.Value) RadiusOpt { return RadiusOpt{x} }
2019-08-20 12:39:28 -04:00
func (x RadiusOpt) DoBackgroundOption(o *BackgroundOpts) { o.radius = x.radius }
2019-08-16 16:38:49 -04:00
func NewBackground(bos ...BackgroundOption) WidgetCombinator {
opts := &BackgroundOpts{}
2019-08-27 09:24:30 -04:00
for _, o := range bos {
o.DoBackgroundOption(opts)
}
2019-08-15 09:16:00 -04:00
bg := &Background{
2019-08-27 09:24:30 -04:00
Color: opts.c,
2019-08-16 16:38:49 -04:00
Radius: opts.radius,
2019-08-27 09:24:30 -04:00
Inset: layout.UniformInset(opts.radius),
2019-08-16 16:38:49 -04:00
}
2019-08-15 09:16:00 -04:00
return func(ws ...Widget) Widget {
return Enclose(bg, ws...)
}
}
func (bg *Background) Begin(ctx *Context) {
2019-08-15 09:16:00 -04:00
bg.macro.Record(ctx.ops)
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
}
func (bg *Background) End(ctx *Context) {
2019-08-15 09:16:00 -04:00
ctx.dims = bg.Inset.End(ctx.dims)
bg.macro.Stop()
var stack ui.StackOp
stack.Push(ctx.ops)
2019-08-15 09:16:00 -04:00
w, h := float32(ctx.dims.Size.X), float32(ctx.dims.Size.Y)
if r := float32(ctx.c.Px(bg.Radius)); r > 0 {
2019-08-27 09:24:30 -04:00
if r > w/2 {
2019-08-15 09:16:00 -04:00
r = w / 2
2019-08-12 08:47:16 -04:00
}
2019-08-27 09:24:30 -04:00
if r > h/2 {
2019-08-15 09:16:00 -04:00
r = h / 2
}
Rrect(ctx.ops, w, h, r, r, r, r)
2019-08-12 08:47:16 -04:00
}
paint.ColorOp{Color: bg.Color}.Add(ctx.ops)
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops)
2019-08-15 09:16:00 -04:00
bg.macro.Add(ctx.ops)
stack.Pop()
2019-08-15 09:16:00 -04:00
}
// https://pomax.github.io/bezierinfo/#circles_cubic.
func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
2019-08-15 09:16:00 -04:00
w, h := float32(width), float32(height)
const c = 0.55228475 // 4*(sqrt(2)-1)/3
var b paint.PathBuilder
2019-08-15 09:16:00 -04:00
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()
2019-08-12 08:47:16 -04:00
}
2019-08-16 16:38:49 -04:00
type Clickable interface {
Widget
Clicked(*Context) bool
2019-08-16 16:38:49 -04:00
}
2019-08-15 18:32:01 -04:00
//cWidget is a clickable Widget that provides the Clicked() method.
2019-08-15 17:47:46 -04:00
type cWidget struct {
2019-08-27 09:24:30 -04:00
w Widget
2019-08-15 17:47:46 -04:00
click *gesture.Click
}
func (w cWidget) Layout(ctx *Context) {
w.w.Layout(ctx)
2019-08-27 09:24:30 -04:00
pointer.RectAreaOp{image.Rect(0, 0, ctx.dims.Size.X, ctx.dims.Size.Y)}.Add(ctx.ops)
2019-08-15 17:47:46 -04:00
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) {
2019-08-15 17:47:46 -04:00
if e.Type == gesture.TypeClick {
2019-08-20 12:39:28 -04:00
ctx.w.Invalidate()
2019-08-15 17:47:46 -04:00
return true
}
}
return false
}
2019-08-15 18:32:01 -04:00
//Clickable converts any Widget into a clickable Widget.
2019-08-16 16:38:49 -04:00
func AsClickable(w Widget) cWidget {
2019-08-27 09:24:30 -04:00
return cWidget{w: w, click: new(gesture.Click)}
}