Add giowrap.Clickable().

This commit is contained in:
Greg 2019-08-15 17:47:46 -04:00
parent 03740d940b
commit 74951e8ce5
2 changed files with 38 additions and 30 deletions

View File

@ -25,23 +25,23 @@ func main() {
ctx := giowrap.NewContext(w) ctx := giowrap.NewContext(w)
t := time.NewTicker(time.Second/30) t := time.NewTicker(time.Second/30)
e1 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true) e1 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true)
e1.SetText("hi there") e1.SetText("text 1")
e1.Focus() e1.Focus()
e2 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true) 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) f := giowrap.NewFlex(layout.Vertical, layout.Start, layout.Start)
OuterInset := giowrap.NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10)) 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)) 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)), ctx.Faces.For(regular, ui.Sp(24)),
"centered", "push",
text.Center, text.Center,
) )
bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4)) bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4))
btn := bg(lbl) btn := giowrap.Clickable(bg(lbl))
for { for {
select { select {
@ -52,7 +52,7 @@ func main() {
case app.DestroyEvent: case app.DestroyEvent:
return return
case app.DrawEvent: case app.DrawEvent:
ctx.Reset(e) ctx = ctx.Reset(e)
ctx = giowrap.LayoutWithContext(ctx, ctx = giowrap.LayoutWithContext(ctx,
OuterInset( OuterInset(
f( f(
@ -63,7 +63,7 @@ func main() {
btn, btn,
))) )))
ctx.Draw() ctx.Draw()
if lbl.Clicked(ctx) { if btn.Clicked(ctx) {
log.Print("Clicked: " + e2.Text() ) log.Print("Clicked: " + e2.Text() )
} }
} }

54
main.go
View File

@ -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.c = &e.Config
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)
return ctx
} }
func (ctx Context) Draw() { func (ctx Context) Draw() {
@ -76,13 +77,12 @@ type Widget interface {
type WidgetCombinator func(...Widget) Widget type WidgetCombinator func(...Widget) Widget
type Button struct { type Label struct {
l *text.Label l *text.Label
click gesture.Click
} }
func NewButton(face text.Face, t string, alignment text.Alignment) *Button { func NewLabel(face text.Face, t string, alignment text.Alignment) *Label {
ret := &Button{} ret := &Label{}
ret.l = &text.Label{ ret.l = &text.Label{
Face: face, Face: face,
Text: t, Text: t,
@ -91,22 +91,11 @@ func NewButton(face text.Face, t string, alignment text.Alignment) *Button {
return ret return ret
} }
func (b *Button) Layout(ctx Context) Context { func (l *Label) Layout(ctx Context) Context {
ctx.dims = b.l.Layout(ctx.ops, ctx.cs) ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
pointer.RectAreaOp{Size: ctx.dims.Size}.Add(ctx.ops)
b.click.Add(ctx.ops)
return ctx 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 { type Editor struct {
e *text.Editor 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)} 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 type Flex WidgetCombinator
func Flexible(v float32) Widget { func Flexible(v float32) Widget {
@ -280,3 +264,27 @@ func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
b.End() 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) }
}