Cleanups and comments.

This commit is contained in:
Greg 2019-08-15 18:32:01 -04:00
parent 74951e8ce5
commit 92cb4d20a4
2 changed files with 19 additions and 37 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"git.wow.st/gmp/giowrap"
"image/color"
"log"
"time"
@ -40,7 +41,7 @@ func main() {
"push",
text.Center,
)
bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4))
bg := giowrap.NewBackground(color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}, ui.Dp(4))
btn := giowrap.Clickable(bg(lbl))
for {
@ -53,15 +54,14 @@ func main() {
return
case app.DrawEvent:
ctx = ctx.Reset(e)
ctx = giowrap.LayoutWithContext(ctx,
OuterInset(
ctx = OuterInset(
f(
e1,
giowrap.Flexible(5),
InnerInset(e2),
giowrap.Flexible(10),
btn,
)))
)).Layout(ctx)
ctx.Draw()
if btn.Clicked(ctx) {
log.Print("Clicked: " + e2.Text() )

48
main.go
View File

@ -52,25 +52,6 @@ func (ctx Context) Draw() {
type Layout func(Context) Context
func LayoutWithContext(ctx Context, ws ...Widget) Context {
for _, w := range ws {
ctx = w.Layout(ctx)
}
return ctx
}
type fWidget struct {
l Layout
}
func NewfWidget(l Layout) fWidget {
return fWidget{ l: l }
}
func (fw fWidget) Layout(ctx Context) Context {
return fw.l(ctx)
}
type Widget interface {
Layout(Context) Context
}
@ -111,28 +92,25 @@ func (e *Editor) Layout(ctx Context) Context {
return ctx
}
func (e *Editor) Text() string {
return e.e.Text()
func (e *Editor) Text() string { return e.e.Text() }
func (e *Editor) SetText(s string) { e.e.SetText(s) }
func (e *Editor) Focus() { e.e.Focus() }
type fWidget struct {
l Layout
}
func (e *Editor) SetText(s string) {
e.e.SetText(s)
func NewfWidget(l Layout) fWidget {
return fWidget{ l: l }
}
func (e *Editor) Focus() {
e.e.Focus()
}
func Rgb(c uint32) color.RGBA {
return Argb((0xff << 24) | c)
}
func Argb(c uint32) color.RGBA {
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
func (fw fWidget) Layout(ctx Context) Context {
return fw.l(ctx)
}
type Flex WidgetCombinator
// This "Widget" does nothing except set the Flexible key of ctx.extra.
func Flexible(v float32) Widget {
return NewfWidget(func(ctx Context) Context {
ctx.extra["Flexible"] = v
@ -140,6 +118,7 @@ func Flexible(v float32) Widget {
})
}
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) Flex {
f := layout.Flex{
Axis: axis,
@ -171,6 +150,7 @@ func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, cross
}
}
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
func NewInset(top, right, bottom, left ui.Value) WidgetCombinator {
ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left }
return func(ws ...Widget) Widget {
@ -264,6 +244,7 @@ func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
b.End()
}
//cWidget is a clickable Widget that provides the Clicked() method.
type cWidget struct {
w Widget
click *gesture.Click
@ -285,6 +266,7 @@ func (w cWidget) Clicked(ctx Context) bool {
return false
}
//Clickable converts any Widget into a clickable Widget.
func Clickable(w Widget) cWidget {
return cWidget{ w: w, click: new(gesture.Click) }
}