Implement Insets.

This commit is contained in:
Greg 2019-08-12 08:47:16 -04:00
parent 9869efc448
commit 396ee09d9f
1 changed files with 60 additions and 15 deletions

75
main.go
View File

@ -10,13 +10,13 @@ import (
"gioui.org/ui/layout" "gioui.org/ui/layout"
"gioui.org/ui/measure" "gioui.org/ui/measure"
"gioui.org/ui/text" "gioui.org/ui/text"
//"gioui.org/ui/input"
"golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt" "golang.org/x/image/font/sfnt"
) )
type Context struct { type Context struct {
w *app.Window
c *app.Config c *app.Config
q input.Queue q input.Queue
ops *ui.Ops ops *ui.Ops
@ -25,13 +25,15 @@ type Context struct {
faces measure.Faces faces measure.Faces
} }
func NewContext() *Context { func NewContext(w *app.Window) *Context {
return &Context{ops: new(ui.Ops)} return &Context{
ops: new(ui.Ops),
q: w.Queue(),
}
} }
func (ctx *Context) Reset(w *app.Window, e app.DrawEvent) { func (ctx *Context) Reset(e app.DrawEvent) {
ctx.c = &e.Config ctx.c = &e.Config
ctx.q = w.Queue()
ctx.ops.Reset() ctx.ops.Reset()
ctx.cs = layout.RigidConstraints(e.Size) ctx.cs = layout.RigidConstraints(e.Size)
ctx.faces.Reset(ctx.c) ctx.faces.Reset(ctx.c)
@ -53,12 +55,22 @@ type WidgetCombinator func(...Widget) Widget
type Layout func(ui.Config, input.Queue, *ui.Ops, layout.Constraints) layout.Dimens type Layout func(ui.Config, input.Queue, *ui.Ops, layout.Constraints) layout.Dimens
type LayoutCombinator func(...Layout) Layout
func (ctx *Context) Do(ls ...Layout) { func (ctx *Context) Do(ls ...Layout) {
for _, l := range ls { for _, l := range ls {
ctx.dims = l(ctx.c, ctx.q, ctx.ops, ctx.cs) ctx.dims = l(ctx.c, ctx.q, ctx.ops, ctx.cs)
} }
} }
type Label struct {
l *text.Label
}
func (l *Label) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
return l.l.Layout(ops, cs)
}
func main() { func main() {
go func() { go func() {
w := app.NewWindow(nil) w := app.NewWindow(nil)
@ -66,7 +78,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal("Cannot parse font.") log.Fatal("Cannot parse font.")
} }
ctx := NewContext() ctx := NewContext(w)
t := time.NewTicker(time.Second/30) t := time.NewTicker(time.Second/30)
e1 := &text.Editor{ e1 := &text.Editor{
Face: ctx.faces.For(regular, ui.Sp(24)), Face: ctx.faces.For(regular, ui.Sp(24)),
@ -80,6 +92,16 @@ func main() {
SingleLine: true, SingleLine: true,
} }
e2.SetText("ok bye") e2.SetText("ok bye")
f := Flex(layout.Vertical, layout.Start, layout.Start)
ins10a := Inset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
ins10b := Inset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
btn := &Label{}
btn.l = &text.Label{
Face: ctx.faces.For(regular, ui.Sp(24)),
Text: "centered",
Alignment: text.Center,
}
for { for {
select { select {
@ -90,11 +112,13 @@ func main() {
case app.DestroyEvent: case app.DestroyEvent:
return return
case app.DrawEvent: case app.DrawEvent:
ctx.Reset(w,e) ctx.Reset(e)
ctx.Do(
f := Flex(layout.Vertical, layout.Start, layout.Start) ins10a(f(
//f(e1,e2).Layout(ctx.c, ctx.q, ctx.ops, ctx.cs) FlexChild{10,e1},
ctx.Do(f(e1,e2).Layout) FlexChild{5,ins10b(e2)},
FlexChild{1,btn},
)).Layout)
w.Draw(ctx.ops) w.Draw(ctx.ops)
} }
} }
@ -103,13 +127,18 @@ func main() {
app.Main() app.Main()
} }
func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) WidgetCombinator { type FlexChild struct {
x float32
w Widget
}
func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) func(...FlexChild) Widget {
f := layout.Flex{ f := layout.Flex{
Axis: axis, Axis: axis,
MainAxisAlignment: mainAxisAlignment, MainAxisAlignment: mainAxisAlignment,
CrossAxisAlignment: crossAxisAlignment, CrossAxisAlignment: crossAxisAlignment,
} }
return func(ws ...Widget) Widget { return func(ws ...FlexChild) Widget {
ret := fWidget{} ret := fWidget{}
ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
f.Init(ops, cs) f.Init(ops, cs)
@ -118,9 +147,9 @@ func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxi
fcs := make([]layout.FlexChild,len(ws)) fcs := make([]layout.FlexChild,len(ws))
for i, w := range ws { for i, w := range ws {
if i > 0 { if i > 0 {
cs = f.Flexible(1) cs = f.Flexible(w.x)
} }
dims = w.Layout(c, q, ops, cs) dims = w.w.Layout(c, q, ops, cs)
fcs[i] = f.End(dims) fcs[i] = f.End(dims)
} }
return f.Layout(fcs...) return f.Layout(fcs...)
@ -129,3 +158,19 @@ func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxi
} }
} }
func Inset(top, right, bottom, left ui.Value) WidgetCombinator {
ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left }
return func(ws ...Widget) Widget {
ret := fWidget{}
ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
cs = ins.Begin(c, ops, cs)
var dims layout.Dimens
for _, w := range ws {
dims = w.Layout(c, q, ops, cs)
}
return ins.End(dims)
}
return ret
}
}