Implement Insets.
This commit is contained in:
parent
9869efc448
commit
396ee09d9f
75
main.go
75
main.go
|
@ -10,13 +10,13 @@ import (
|
|||
"gioui.org/ui/layout"
|
||||
"gioui.org/ui/measure"
|
||||
"gioui.org/ui/text"
|
||||
//"gioui.org/ui/input"
|
||||
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
w *app.Window
|
||||
c *app.Config
|
||||
q input.Queue
|
||||
ops *ui.Ops
|
||||
|
@ -25,13 +25,15 @@ type Context struct {
|
|||
faces measure.Faces
|
||||
}
|
||||
|
||||
func NewContext() *Context {
|
||||
return &Context{ops: new(ui.Ops)}
|
||||
func NewContext(w *app.Window) *Context {
|
||||
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.q = w.Queue()
|
||||
ctx.ops.Reset()
|
||||
ctx.cs = layout.RigidConstraints(e.Size)
|
||||
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 LayoutCombinator func(...Layout) Layout
|
||||
|
||||
func (ctx *Context) Do(ls ...Layout) {
|
||||
for _, l := range ls {
|
||||
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() {
|
||||
go func() {
|
||||
w := app.NewWindow(nil)
|
||||
|
@ -66,7 +78,7 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := NewContext()
|
||||
ctx := NewContext(w)
|
||||
t := time.NewTicker(time.Second/30)
|
||||
e1 := &text.Editor{
|
||||
Face: ctx.faces.For(regular, ui.Sp(24)),
|
||||
|
@ -80,6 +92,16 @@ func main() {
|
|||
SingleLine: true,
|
||||
}
|
||||
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 {
|
||||
select {
|
||||
|
@ -90,11 +112,13 @@ func main() {
|
|||
case app.DestroyEvent:
|
||||
return
|
||||
case app.DrawEvent:
|
||||
ctx.Reset(w,e)
|
||||
|
||||
f := Flex(layout.Vertical, layout.Start, layout.Start)
|
||||
//f(e1,e2).Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
|
||||
ctx.Do(f(e1,e2).Layout)
|
||||
ctx.Reset(e)
|
||||
ctx.Do(
|
||||
ins10a(f(
|
||||
FlexChild{10,e1},
|
||||
FlexChild{5,ins10b(e2)},
|
||||
FlexChild{1,btn},
|
||||
)).Layout)
|
||||
w.Draw(ctx.ops)
|
||||
}
|
||||
}
|
||||
|
@ -103,13 +127,18 @@ func 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{
|
||||
Axis: axis,
|
||||
MainAxisAlignment: mainAxisAlignment,
|
||||
CrossAxisAlignment: crossAxisAlignment,
|
||||
}
|
||||
return func(ws ...Widget) Widget {
|
||||
return func(ws ...FlexChild) Widget {
|
||||
ret := fWidget{}
|
||||
ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
f.Init(ops, cs)
|
||||
|
@ -118,9 +147,9 @@ func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxi
|
|||
fcs := make([]layout.FlexChild,len(ws))
|
||||
for i, w := range ws {
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user