giowrap/main.go

570 lines
12 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/gesture"
"gioui.org/ui/paint"
"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.Dimensions
container Widget
}
func NewContext(w *app.Window) *Context {
return &Context{
w: w,
ops: new(ui.Ops),
q: w.Queue(),
}
}
func (ctx *Context) Reset(e *app.UpdateEvent) {
Mallocs("ctx.Reset() ctx.c")
ctx.c = &e.Config
ctx.ops.Reset()
ctx.cs = layout.RigidConstraints(e.Size)
ctx.Faces.Reset(ctx.c)
Mallocs("ctx.Reset() done")
}
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) {
Mallocs("Label.Layout()")
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) {
Mallocs("Editor.Layout()")
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 Stack WidgetCombinator
type stackWidget struct {
s layout.Stack
scs []layout.StackChild
ws []Widget
}
func NewStack() Stack {
ret := &stackWidget{}
ret.s = layout.Stack{Alignment: layout.Center}
ret.scs = make([]layout.StackChild, 0)
return func(ws ...Widget) Widget {
ret.ws = ws
return ret
}
}
func (sw *stackWidget) Layout(ctx *Context) {
sw.s.Init(ctx.ops, ctx.cs)
for _, w := range sw.ws {
ctx.cs = sw.s.Rigid()
w.Layout(ctx)
sw.scs = append(sw.scs, sw.s.End(ctx.dims))
}
ctx.dims = sw.s.Layout(sw.scs...)
sw.scs = sw.scs[0:0]
}
type List = WidgetCombinator
type AxisOpt struct{ axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{x} }
var Horizontal = AxisOpt{layout.Horizontal}
var Vertical = AxisOpt{layout.Vertical}
type ListOpts struct {
AxisOpt
}
type ListOption interface{ DoListOption(*ListOpts) }
func (x AxisOpt) DoListOption(o *ListOpts) { o.axis = x.axis }
type listWidget struct {
l layout.List
ws []Widget
}
func NewList(los ...ListOption) List {
ret := &listWidget{}
opts := &ListOpts{}
for _, o := range los {
o.DoListOption(opts)
}
ret.l = layout.List{Axis: opts.axis}
return func(ws ...Widget) Widget {
ret.ws = ws
return ret
}
}
func (lw *listWidget) Layout(ctx *Context) {
for lw.l.Init(ctx.c, ctx.q, ctx.ops, ctx.cs, len(lw.ws)); lw.l.More(); lw.l.Next() {
ctx.cs = lw.l.Constraints()
lw.ws[lw.l.Index()].Layout(ctx)
lw.l.End(ctx.dims)
}
ctx.dims = lw.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
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 }
type flexWidget struct {
f func(*Context)
flexible float32
}
type flexOptWidget struct {
flexible float32
}
func (fo flexOptWidget) Layout(ctx *Context) {
Mallocs("flexOptWidget.Layout()")
ctx.container.(*flexWidget).flexible = fo.flexible
}
// This "Widget" does nothing except set the Flexible field of a FlexOpts
// struct within the Extra data structure
func Flexible(v float32) Widget {
return flexOptWidget{v}
}
func Rigid() Widget {
return flexOptWidget{0}
}
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
func (fw *flexWidget) Layout(ctx *Context) {
fw.f(ctx)
}
func NewFlex(fos ...FlexOption) Flex {
opts := &FlexOpts{}
for _, o := range fos {
o.DoFlexOption(opts)
}
f := layout.Flex{
Axis: opts.axis,
Alignment: opts.alignment,
}
fcs := make([]layout.FlexChild, 0)
return func(ws ...Widget) Widget {
ret := &flexWidget{}
ret.f = func(ctx *Context) {
oldContainer := ctx.container
ctx.container = ret
f.Init(ctx.ops, ctx.cs)
for _, w := range ws {
ctx.cs = f.Rigid()
w.Layout(ctx)
fcs = append(fcs, f.End(ctx.dims))
}
ctx.dims = f.Layout(fcs...)
fcs = fcs[0:0]
ctx.container = oldContainer
}
return ret
}
}
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
}
type insetWidget func(*Context)
func (iw insetWidget) Layout(ctx *Context) {
iw(ctx)
}
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
func NewInset(insos ...InsetOption) func(...Widget) Widget {
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 {
Mallocs("NewInset() return")
var ret insetWidget
ret = func(ctx *Context) {
oldContainer := ctx.container
ctx.container = ret
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
for _, w := range ws {
w.Layout(ctx)
}
ctx.dims = ins.End(ctx.dims)
ctx.container = oldContainer
}
return ret
}
}
type Background struct {
Color color.RGBA
Radius ui.Value
Inset layout.Inset
macro ui.MacroOp
}
type Enclosure interface {
Begin(*Context)
End(*Context)
}
type encloseWidget struct {
enc Enclosure
ws []Widget
}
func Enclose(e Enclosure, ws ...Widget) Widget {
return &encloseWidget{e, ws}
}
func (e *encloseWidget) Layout(ctx *Context) {
oldContainer := ctx.container
ctx.container = e
e.enc.Begin(ctx)
for _, w := range e.ws {
w.Layout(ctx)
}
e.enc.End(ctx)
ctx.container = oldContainer
}
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) {
Mallocs("Background.Begin()")
bg.macro.Record(ctx.ops)
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
Mallocs("Background.Begin() done")
}
func (bg *Background) End(ctx *Context) {
Mallocs("Background.End()")
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()
Mallocs("Background.End() done")
}
// https://pomax.github.io/bezierinfo/#circles_cubic.
func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
Mallocs("Rrect() start")
w, h := float32(width), float32(height)
Mallocs("Rrect() const")
const c = 0.55228475 // 4*(sqrt(2)-1)/3
var b paint.PathBuilder
Mallocs("Rrect() init")
b.Init(ops)
Mallocs("Rrect() move")
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()
Mallocs("Rrect() end")
}
type fillWidget struct {
axis layout.Axis
}
func FillWidth(ws ...Widget) Widget {
return Enclose(fillWidget{layout.Horizontal}, ws...)
}
func FillHeight(ws ...Widget) Widget {
return Enclose(fillWidget{layout.Vertical}, ws...)
}
func (fw fillWidget) Begin(ctx *Context) {
switch fw.axis {
case layout.Horizontal:
ctx.cs.Width.Min = ctx.cs.Width.Max
case layout.Vertical:
ctx.cs.Height.Min = ctx.cs.Height.Max
}
}
func (fw fillWidget) End(*Context) {
}
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) {
Mallocs("cWidget.Layout()")
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 {
Mallocs("cWidget.Clicked()")
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)}
}