From 635655e76873e4cc6b264699982e64f5850aa455 Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 27 Aug 2019 09:49:35 -0400 Subject: [PATCH] Restructure main2 in cmd/hello (from Elias Naur) --- cmd/hello/main.go | 172 ++++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 83 deletions(-) diff --git a/cmd/hello/main.go b/cmd/hello/main.go index 017eb28..a74a2a8 100644 --- a/cmd/hello/main.go +++ b/cmd/hello/main.go @@ -64,19 +64,19 @@ func main() { mux.Unlock() } }() - if len(os.Args) > 1 { - switch os.Args[1] { - case "", "main1": - log.Print("main1()") - go main1() - case "main2": - log.Print("main2()") - go main2() - default: - log.Fatal(`Usage: + switch { + case len(os.Args) < 2: + fallthrough + default: + log.Fatal(`Usage: hello [main1|main2] `) - } + case os.Args[1] == "main1": + log.Print("main1()") + go main1() + case os.Args[1] == "main2": + log.Print("main2()") + go main2() } app.Main() } @@ -122,9 +122,9 @@ func main1() { OuterInset( f1( e1, - giowrap.Flexible(5), + giowrap.Flexible(0.33), InnerInset(e2), - giowrap.Flexible(10), + giowrap.Flexible(0.67), f2( InnerInset(btn1), giowrap.Flexible(0.5), @@ -193,25 +193,14 @@ func main2() { e2.Focus() e2.SetText("text 2") - f1 := layout.Flex{Axis: layout.Vertical} - OuterInset := layout.UniformInset(ui.Dp(10)) - InnerInset := layout.UniformInset(ui.Dp(10)) - - f2 := layout.Flex{Axis: layout.Horizontal} - - btn1 := text.Label{ - Face: faces.For(regular, ui.Sp(24)), - Text: "push1", + btn1 := &Button{ + Face: faces.For(regular, ui.Sp(24)), + Label: "push1", } - b1ins := layout.UniformInset(ui.Dp(4)) - btn2 := text.Label{ - Face: faces.For(regular, ui.Sp(24)), - Text: "push2", + btn2 := &Button{ + Face: faces.For(regular, ui.Sp(24)), + Label: "push2", } - b2ins := layout.UniformInset(ui.Dp(4)) - var bg1, bg2 ui.MacroOp - click1 := new(gesture.Click) - click2 := new(gesture.Click) profiled := false startTime := time.Now() @@ -229,67 +218,45 @@ func main2() { c := &e.Config ops.Reset() faces.Reset(c) - cs := layout.RigidConstraints(e.Size) - cs = OuterInset.Begin(c, ops, cs) - f1.Init(ops, cs) - cs = f1.Rigid() - dims := e1.Layout(c, q, ops, cs) - f1c1 := f1.End(dims) - cs = f1.Flexible(5) - cs = InnerInset.Begin(c, ops, cs) - dims = e2.Layout(c, q, ops, cs) - dims = InnerInset.End(dims) - f1c2 := f1.End(dims) - cs = f1.Flexible(10) - f2.Init(ops, cs) - cs = f2.Rigid() - cs = InnerInset.Begin(c, ops, cs) - bg1.Record(ops) - cs = b1ins.Begin(c, ops, cs) - dims = btn1.Layout(ops, cs) - dims = b1ins.End(dims) - pointer.RectAreaOp{image.Rect(0, 0, dims.Size.X, dims.Size.Y)}.Add(ops) - click1.Add(ops) - bg1.Stop() - wi, h := float32(dims.Size.X), float32(dims.Size.Y) - r := float32(c.Px(ui.Dp(4))) - giowrap.Rrect(ops, wi, h, r, r, r, r) - paint.ColorOp{Color: color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}}.Add(ops) - paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: wi, Y: h}}}.Add(ops) - bg1.Add(ops) - dims = InnerInset.End(dims) - f2c1 := f2.End(dims) - cs = f2.Flexible(0.5) - cs = InnerInset.Begin(c, ops, cs) - bg2.Record(ops) - cs = b2ins.Begin(c, ops, cs) - dims = btn2.Layout(ops, cs) - dims = b2ins.End(dims) - pointer.RectAreaOp{image.Rect(0, 0, dims.Size.X, dims.Size.Y)}.Add(ops) - click2.Add(ops) - bg2.Stop() - wi, h = float32(dims.Size.X), float32(dims.Size.Y) - giowrap.Rrect(ops, wi, h, r, r, r, r) - paint.ColorOp{Color: color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}}.Add(ops) - paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: wi, Y: h}}}.Add(ops) - bg2.Add(ops) - dims = InnerInset.End(dims) - f2c2 := f2.End(dims) - dims = f2.Layout(f2c1, f2c2) - f1c3 := f1.End(dims) - dims = f1.Layout(f1c1, f1c2, f1c3) - dims = OuterInset.End(dims) - w.Update(ops) - for ev, ok := click1.Next(q); ok; ev, ok = click1.Next(q) { + for ev, ok := btn1.Click.Next(q); ok; ev, ok = btn1.Click.Next(q) { if ev.Type == gesture.TypeClick { log.Print("Clicked: " + e1.Text()) } } - for ev, ok := click2.Next(q); ok; ev, ok = click2.Next(q) { + for ev, ok := btn2.Click.Next(q); ok; ev, ok = btn2.Click.Next(q) { if ev.Type == gesture.TypeClick { log.Print("Clicked: " + e2.Text()) } } + + var dims layout.Dimens + cs := layout.RigidConstraints(e.Size) + { + f1 := layout.Flex{Axis: layout.Vertical} + ins := layout.UniformInset(ui.Dp(10)) + f1.Init(ops, ins.Begin(c, ops, cs)) + c1 := f1.End(e1.Layout(c, q, ops, f1.Rigid())) + { + cs = f1.Flexible(0.33) + ins := layout.UniformInset(ui.Dp(10)) + dims = ins.End(e2.Layout(c, q, ops, ins.Begin(c, ops, cs))) + } + c2 := f1.End(dims) + { + cs = f1.Flexible(0.67) + f2 := layout.Flex{Axis: layout.Horizontal} + f2.Init(ops, cs) + + c1 := f2.End(btn1.Layout(c, ops, f2.Rigid())) + + c2 := f2.End(btn2.Layout(c, ops, f2.Flexible(0.5))) + + dims = f2.Layout(c1, c2) + } + c3 := f1.End(dims) + dims = ins.End(f1.Layout(c1, c2, c3)) + } + w.Update(ops) } dur := time.Since(stime).Nanoseconds() mux.Lock() @@ -320,3 +287,42 @@ func main2() { } } } + +func layoutRRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens { + r := float32(c.Px(ui.Dp(4))) + sz := image.Point{X: cs.Width.Min, Y: cs.Height.Min} + w, h := float32(sz.X), float32(sz.Y) + giowrap.Rrect(ops, w, h, r, r, r, r) + paint.ColorOp{Color: color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}}.Add(ops) + paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ops) + return layout.Dimens{Size: sz} +} + +type Button struct { + Face text.Face + Label string + Click gesture.Click +} + +func (b *Button) Layout(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens { + ins := layout.UniformInset(ui.Dp(10)) + cs = ins.Begin(c, ops, cs) + var dims layout.Dimens + st := layout.Stack{} + st.Init(ops, cs) + { + cs = st.Rigid() + l := text.Label{ + Face: b.Face, + Text: b.Label, + } + ins := layout.UniformInset(ui.Dp(4)) + dims = ins.End(l.Layout(ops, ins.Begin(c, ops, cs))) + pointer.RectAreaOp{image.Rect(0, 0, dims.Size.X, dims.Size.Y)}.Add(ops) + b.Click.Add(ops) + } + c2 := st.End(dims) + c1 := st.End(layoutRRect(c, ops, st.Expand())) + dims = st.Layout(c1, c2) + return ins.End(dims) +}