Restructure main2 in cmd/hello (from Elias Naur)

This commit is contained in:
Greg 2019-08-27 09:49:35 -04:00
parent 9f60b99984
commit 635655e768

View File

@ -64,19 +64,19 @@ func main() {
mux.Unlock() mux.Unlock()
} }
}() }()
if len(os.Args) > 1 { switch {
switch os.Args[1] { case len(os.Args) < 2:
case "", "main1": fallthrough
log.Print("main1()") default:
go main1() log.Fatal(`Usage:
case "main2":
log.Print("main2()")
go main2()
default:
log.Fatal(`Usage:
hello [main1|main2] 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() app.Main()
} }
@ -122,9 +122,9 @@ func main1() {
OuterInset( OuterInset(
f1( f1(
e1, e1,
giowrap.Flexible(5), giowrap.Flexible(0.33),
InnerInset(e2), InnerInset(e2),
giowrap.Flexible(10), giowrap.Flexible(0.67),
f2( f2(
InnerInset(btn1), InnerInset(btn1),
giowrap.Flexible(0.5), giowrap.Flexible(0.5),
@ -193,25 +193,14 @@ func main2() {
e2.Focus() e2.Focus()
e2.SetText("text 2") e2.SetText("text 2")
f1 := layout.Flex{Axis: layout.Vertical} btn1 := &Button{
OuterInset := layout.UniformInset(ui.Dp(10)) Face: faces.For(regular, ui.Sp(24)),
InnerInset := layout.UniformInset(ui.Dp(10)) Label: "push1",
f2 := layout.Flex{Axis: layout.Horizontal}
btn1 := text.Label{
Face: faces.For(regular, ui.Sp(24)),
Text: "push1",
} }
b1ins := layout.UniformInset(ui.Dp(4)) btn2 := &Button{
btn2 := text.Label{ Face: faces.For(regular, ui.Sp(24)),
Face: faces.For(regular, ui.Sp(24)), Label: "push2",
Text: "push2",
} }
b2ins := layout.UniformInset(ui.Dp(4))
var bg1, bg2 ui.MacroOp
click1 := new(gesture.Click)
click2 := new(gesture.Click)
profiled := false profiled := false
startTime := time.Now() startTime := time.Now()
@ -229,67 +218,45 @@ func main2() {
c := &e.Config c := &e.Config
ops.Reset() ops.Reset()
faces.Reset(c) faces.Reset(c)
cs := layout.RigidConstraints(e.Size) for ev, ok := btn1.Click.Next(q); ok; ev, ok = btn1.Click.Next(q) {
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) {
if ev.Type == gesture.TypeClick { if ev.Type == gesture.TypeClick {
log.Print("Clicked: " + e1.Text()) 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 { if ev.Type == gesture.TypeClick {
log.Print("Clicked: " + e2.Text()) 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() dur := time.Since(stime).Nanoseconds()
mux.Lock() 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)
}