giowrap/main.go

210 lines
4.0 KiB
Go
Raw Normal View History

2019-08-08 18:11:20 -04:00
package main
import (
"log"
"time"
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/input"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
)
type Context struct {
2019-08-12 08:47:16 -04:00
w *app.Window
2019-08-08 18:11:20 -04:00
c *app.Config
q input.Queue
ops *ui.Ops
cs layout.Constraints
dims layout.Dimens
faces measure.Faces
extra map[string]interface{}
2019-08-08 18:11:20 -04:00
}
func NewContext(w *app.Window) Context {
return Context{
2019-08-12 08:47:16 -04:00
ops: new(ui.Ops),
q: w.Queue(),
extra: make(map[string]interface{}),
2019-08-12 08:47:16 -04:00
}
2019-08-08 18:11:20 -04:00
}
2019-08-12 08:47:16 -04:00
func (ctx *Context) Reset(e app.DrawEvent) {
2019-08-08 18:11:20 -04:00
ctx.c = &e.Config
ctx.ops.Reset()
ctx.cs = layout.RigidConstraints(e.Size)
ctx.faces.Reset(ctx.c)
}
func (fw fWidget) Layout(ctx Context) Context {
return fw.l(ctx)
2019-08-08 18:11:20 -04:00
}
type Widget interface {
Layout(Context) Context
2019-08-08 18:11:20 -04:00
}
type WidgetCombinator func(...Widget) Widget
2019-08-08 18:11:20 -04:00
type fWidget struct {
l Layout
}
type Layout func(Context) Context
2019-08-08 18:11:20 -04:00
2019-08-12 08:47:16 -04:00
type LayoutCombinator func(...Layout) Layout
func LayoutWithContext(ctx Context, ls ...Layout) Context {
2019-08-08 18:11:20 -04:00
for _, l := range ls {
ctx = l(ctx)
2019-08-08 18:11:20 -04:00
}
return ctx
2019-08-08 18:11:20 -04:00
}
2019-08-12 08:47:16 -04:00
type Label struct {
l *text.Label
}
func (l *Label) Layout(ctx Context) Context {
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
return ctx
}
type Editor struct {
e *text.Editor
}
func NewEditor(face text.Face, singleline bool) *Editor {
ret := &Editor{}
ret.e = &text.Editor{ Face: face, SingleLine: singleline }
return ret
}
func (e *Editor) Layout(ctx Context) Context {
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
return ctx
2019-08-12 08:47:16 -04:00
}
2019-08-08 18:11:20 -04:00
func main() {
go func() {
w := app.NewWindow(nil)
regular, err := sfnt.Parse(goregular.TTF)
if err != nil {
log.Fatal("Cannot parse font.")
}
2019-08-12 08:47:16 -04:00
ctx := NewContext(w)
2019-08-08 18:11:20 -04:00
t := time.NewTicker(time.Second/30)
e1 := NewEditor(ctx.faces.For(regular, ui.Sp(24)), true)
e1.e.SetText("hi there")
e1.e.Focus()
2019-08-08 18:11:20 -04:00
e2 := NewEditor( ctx.faces.For(regular, ui.Sp(24)), true)
e2.e.SetText("ok bye")
f := NewFlex(layout.Vertical, layout.Start, layout.Start)
ins10a := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
ins10b := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
2019-08-12 08:47:16 -04:00
btn := &Label{}
btn.l = &text.Label{
Face: ctx.faces.For(regular, ui.Sp(24)),
Text: "centered",
Alignment: text.Center,
}
2019-08-08 18:11:20 -04:00
for {
select {
case <-t.C:
w.Invalidate()
case e := <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
return
case app.DrawEvent:
2019-08-12 08:47:16 -04:00
ctx.Reset(e)
LayoutWithContext(ctx,
2019-08-12 08:47:16 -04:00
ins10a(f(
e1,
Flexible(5),
ins10b(e2),
Flexible(10),
btn,
2019-08-12 08:47:16 -04:00
)).Layout)
2019-08-08 18:11:20 -04:00
w.Draw(ctx.ops)
}
}
}
}()
app.Main()
}
2019-08-12 08:47:16 -04:00
type FlexChild struct {
x float32
w Widget
}
func Flexible(v float32) Widget {
ret := fWidget{}
ret.l = func(ctx Context) Context {
ctx.extra["Flexible"] = v
return ctx
}
return ret
}
func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) WidgetCombinator {
2019-08-08 18:11:20 -04:00
f := layout.Flex{
Axis: axis,
MainAxisAlignment: mainAxisAlignment,
CrossAxisAlignment: crossAxisAlignment,
}
return func(ws ...Widget) Widget {
2019-08-08 18:11:20 -04:00
ret := fWidget{}
ret.l = func(ctx Context) Context {
f.Init(ctx.ops, ctx.cs)
2019-08-08 18:11:20 -04:00
fcs := make([]layout.FlexChild,len(ws))
for i, w := range ws {
if v,ok := ctx.extra["Flexible"]; ok {
switch v := v.(type) {
case float32:
ctx.cs = f.Flexible(v)
default:
log.Fatal("Type error")
}
} else {
ctx.cs = f.Rigid()
2019-08-08 18:11:20 -04:00
}
ctx = w.Layout(ctx)
fcs[i] = f.End(ctx.dims)
2019-08-08 18:11:20 -04:00
}
ctx.dims = f.Layout(fcs...)
delete(ctx.extra, "Flexible")
return ctx
2019-08-08 18:11:20 -04:00
}
return ret
}
}
func NewInset(top, right, bottom, left ui.Value) WidgetCombinator {
2019-08-12 08:47:16 -04:00
ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left }
return func(ws ...Widget) Widget {
ret := fWidget{}
ret.l = func(ctx Context) Context {
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
2019-08-12 08:47:16 -04:00
var dims layout.Dimens
for _, w := range ws {
ctx = w.Layout(ctx)
2019-08-12 08:47:16 -04:00
}
ctx.dims = ins.End(dims)
return ctx
2019-08-12 08:47:16 -04:00
}
return ret
}
}