package giowrap import ( "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" gdraw "gioui.org/ui/draw" "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.DrawEvent) { ctx.c = &e.Config ctx.ops.Reset() ctx.cs = layout.RigidConstraints(e.Size) ctx.Faces.Reset(ctx.c) } func (ctx *Context) Draw() { ctx.w.Draw(ctx.ops) } type Layout func(*Context) type Widget interface { Layout(*Context) } type WidgetCombinator func(...Widget) Widget type Label struct { l *text.Label } type LabelOpts struct { face text.Face alignment text.Alignment } type LabelOption func(*LabelOpts) func LabelFace(x text.Face) LabelOption { return func(o *LabelOpts) { o.face = x } } func LabelAlignment(x text.Alignment) LabelOption { return func(o *LabelOpts) { o.alignment = x } } func NewLabel(t string, lops ...LabelOption) *Label { ret := &Label{} opts := &LabelOpts{} for _,o := range lops { o(opts) } ret.l = &text.Label{ Face: opts.face, Text: t, Alignment: opts.alignment, } return ret } func (l *Label) Layout(ctx *Context) { ctx.dims = l.l.Layout(ctx.ops, ctx.cs) } type Editor struct { e *text.Editor } type EditorOpts struct { face text.Face singleline bool } type EditorOption func(*EditorOpts) func EditorFace(x text.Face) EditorOption { return func(o *EditorOpts) { o.face = x } } func EditorSingleline() EditorOption { return func(o *EditorOpts) { o.singleline = true } } func NewEditor(t string, eops ...EditorOption) *Editor { ret := &Editor{} opts := &EditorOpts{} for _,o := range eops { o(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 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 }) } type FlexOpts struct { axis layout.Axis mainAxisAlignment layout.MainAxisAlignment crossAxisAlignment layout.CrossAxisAlignment flexible float32 } type FlexOption func(*FlexOpts) func FlexAxis(x layout.Axis) FlexOption { return func(o *FlexOpts) { o.axis = x } } func MainAxisAlignment(x layout.MainAxisAlignment) FlexOption { return func(o *FlexOpts) { o.mainAxisAlignment = x } } func CrossAxisAlignment(x layout.CrossAxisAlignment) FlexOption { return func(o *FlexOpts) { o.crossAxisAlignment = x } } // NewFlex returns a WidgetCombinator that wraps the layout.Flex element. func NewFlex(fos ...FlexOption) Flex { opts := &FlexOpts{} for _,o := range fos { o(opts) } f := layout.Flex{ Axis: opts.axis, MainAxisAlignment: opts.mainAxisAlignment, CrossAxisAlignment: opts.crossAxisAlignment, } 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)) } f.Layout(fcs...) fcs = fcs[0:0] // truncate }) } } type InsetOpts struct { top, right, bottom, left ui.Value } type InsetOption func(*InsetOpts) func InsetTop(x ui.Value) InsetOption { return func(o *InsetOpts) { o.top = x } } func InsetRight(x ui.Value) InsetOption { return func(o *InsetOpts) { o.right = x } } func InsetBottom(x ui.Value) InsetOption { return func(o *InsetOpts) { o.bottom = x } } func InsetLeft(x ui.Value) InsetOption { return func(o *InsetOpts) { o.left = x } } func InsetSize(x ui.Value) InsetOption { return func(o *InsetOpts) { o.top = x o.right = x o.bottom = x o.left = x } } //NewInset returns a WidgetCombinator that wraps the layout.Inset element. func NewInset(insos ...InsetOption) WidgetCombinator { opts := &InsetOpts{} for _,o := range insos { o(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 func(*BackgroundOpts) func BgColor(c color.RGBA) BackgroundOption { return func(o *BackgroundOpts) { o.c = c } } func BgRadius(x ui.Value) BackgroundOption { return func(o *BackgroundOpts) { o.radius = x } } func NewBackground(bos ...BackgroundOption) WidgetCombinator { opts := &BackgroundOpts{} for _,o := range bos { o(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 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) } gdraw.ColorOp{Color: bg.Color}.Add(ctx.ops) gdraw.DrawOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops) bg.macro.Add(ctx.ops) } // 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 gdraw.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{Size: ctx.dims.Size}.Add(ctx.ops) w.click.Add(ctx.ops) } func (w cWidget) Clicked(ctx *Context) bool { for _,e := range w.click.Events(ctx.q) { if e.Type == gesture.TypeClick { 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) } }