From 74951e8ce517855695b43cfb693132ed0d2d3c23 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 15 Aug 2019 17:47:46 -0400 Subject: [PATCH] Add giowrap.Clickable(). --- cmd/hello/main.go | 14 ++++++------ main.go | 54 +++++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/cmd/hello/main.go b/cmd/hello/main.go index 7eb3e94..0ba352c 100644 --- a/cmd/hello/main.go +++ b/cmd/hello/main.go @@ -25,23 +25,23 @@ func main() { ctx := giowrap.NewContext(w) t := time.NewTicker(time.Second/30) e1 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true) - e1.SetText("hi there") + e1.SetText("text 1") e1.Focus() e2 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true) - e2.SetText("ok bye") + e2.SetText("text 2") f := giowrap.NewFlex(layout.Vertical, layout.Start, layout.Start) OuterInset := giowrap.NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10)) InnerInset := giowrap.NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10)) - lbl := giowrap.NewButton( + lbl := giowrap.NewLabel( ctx.Faces.For(regular, ui.Sp(24)), - "centered", + "push", text.Center, ) bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4)) - btn := bg(lbl) + btn := giowrap.Clickable(bg(lbl)) for { select { @@ -52,7 +52,7 @@ func main() { case app.DestroyEvent: return case app.DrawEvent: - ctx.Reset(e) + ctx = ctx.Reset(e) ctx = giowrap.LayoutWithContext(ctx, OuterInset( f( @@ -63,7 +63,7 @@ func main() { btn, ))) ctx.Draw() - if lbl.Clicked(ctx) { + if btn.Clicked(ctx) { log.Print("Clicked: " + e2.Text() ) } } diff --git a/main.go b/main.go index 5da3546..7a61c9c 100644 --- a/main.go +++ b/main.go @@ -38,11 +38,12 @@ func NewContext(w *app.Window) Context { } } -func (ctx *Context) Reset(e app.DrawEvent) { +func (ctx Context) Reset(e app.DrawEvent) Context { ctx.c = &e.Config ctx.ops.Reset() ctx.cs = layout.RigidConstraints(e.Size) ctx.Faces.Reset(ctx.c) + return ctx } func (ctx Context) Draw() { @@ -76,13 +77,12 @@ type Widget interface { type WidgetCombinator func(...Widget) Widget -type Button struct { +type Label struct { l *text.Label - click gesture.Click } -func NewButton(face text.Face, t string, alignment text.Alignment) *Button { - ret := &Button{} +func NewLabel(face text.Face, t string, alignment text.Alignment) *Label { + ret := &Label{} ret.l = &text.Label{ Face: face, Text: t, @@ -91,22 +91,11 @@ func NewButton(face text.Face, t string, alignment text.Alignment) *Button { return ret } -func (b *Button) Layout(ctx Context) Context { - ctx.dims = b.l.Layout(ctx.ops, ctx.cs) - pointer.RectAreaOp{Size: ctx.dims.Size}.Add(ctx.ops) - b.click.Add(ctx.ops) +func (l *Label) Layout(ctx Context) Context { + ctx.dims = l.l.Layout(ctx.ops, ctx.cs) return ctx } -func (b *Button) Clicked(ctx Context) bool { - for _,e := range b.click.Events(ctx.q) { - if e.Type == gesture.TypeClick { - return true - } - } - return false -} - type Editor struct { e *text.Editor } @@ -142,11 +131,6 @@ func Argb(c uint32) color.RGBA { return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)} } -type FlexChild struct { - x float32 - w Widget -} - type Flex WidgetCombinator func Flexible(v float32) Widget { @@ -280,3 +264,27 @@ func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) { b.End() } +type cWidget struct { + w Widget + click *gesture.Click +} + +func (w cWidget) Layout(ctx Context) Context { + ctx = w.w.Layout(ctx) + pointer.RectAreaOp{Size: ctx.dims.Size}.Add(ctx.ops) + w.click.Add(ctx.ops) + return ctx +} + +func (w cWidget) Clicked(ctx Context) bool { + for _,e := range w.click.Events(ctx.q) { + if e.Type == gesture.TypeClick { + return true + } + } + return false +} + +func Clickable(w Widget) cWidget { + return cWidget{ w: w, click: new(gesture.Click) } +}