New style for options.

This commit is contained in:
Greg 2019-08-20 12:39:28 -04:00
parent 5bef5fc95a
commit 491f26dcfe
3 changed files with 108 additions and 74 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
cmd/hello/hello

View File

@ -35,8 +35,8 @@ var (
) )
func NewButton(face text.Face, t string, c color.RGBA) giowrap.Clickable { func NewButton(face text.Face, t string, c color.RGBA) giowrap.Clickable {
lbl := giowrap.NewLabel(t, giowrap.LabelFace(face), giowrap.LabelAlignment(text.Center)) lbl := giowrap.NewLabel(t, giowrap.Face(face), giowrap.Align(text.Center))
bg := giowrap.NewBackground(giowrap.BgColor(c), giowrap.BgRadius(ui.Dp(4))) bg := giowrap.NewBackground(giowrap.Color(c), giowrap.Radius(ui.Dp(4)))
return giowrap.AsClickable(bg(lbl)) return giowrap.AsClickable(bg(lbl))
} }
@ -86,16 +86,16 @@ func main1() {
} }
ctx := giowrap.NewContext(w) ctx := giowrap.NewContext(w)
t := time.NewTicker(time.Second/time.Duration(FPS)) t := time.NewTicker(time.Second/time.Duration(FPS))
e1 := giowrap.NewEditor("text 1",giowrap.EditorFace(ctx.Faces.For(regular, ui.Sp(24)))) e1 := giowrap.NewEditor("text 1",giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
e1.Focus() e1.Focus()
e2 := giowrap.NewEditor("text 2",giowrap.EditorFace(ctx.Faces.For(regular, ui.Sp(24)))) e2 := giowrap.NewEditor("text 2",giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
f1 := giowrap.NewFlex(giowrap.FlexAxis(layout.Vertical)) f1 := giowrap.NewFlex(giowrap.Axis(layout.Vertical))
OuterInset := giowrap.NewInset(giowrap.InsetSize(ui.Dp(10))) OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
InnerInset := giowrap.NewInset(giowrap.InsetSize(ui.Dp(10))) InnerInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
f2 := giowrap.NewFlex(giowrap.FlexAxis(layout.Horizontal)) f2 := giowrap.NewFlex(giowrap.Axis(layout.Horizontal))
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)), btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}) "push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})

165
main.go
View File

@ -78,22 +78,23 @@ type Label struct {
l *text.Label l *text.Label
} }
type FaceOpt struct { face text.Face }
func Face(x text.Face) FaceOpt { return FaceOpt { x } }
type AlignOpt struct { alignment text.Alignment }
func Align(x text.Alignment) AlignOpt { return AlignOpt { x } }
type LabelOpts struct { type LabelOpts struct {
face text.Face FaceOpt
alignment text.Alignment AlignOpt
}
type LabelOption func(*LabelOpts)
func LabelFace(x text.Face) LabelOption {
return func(o *LabelOpts) { o.face = x }
}
func LabelAlignment(x text.Alignment) LabelOption {
return func(o *LabelOpts) { o.alignment = x }
} }
type LabelOption interface { DoLabelOption(*LabelOpts) }
func (x FaceOpt) DoLabelOption(o *LabelOpts) { o.face = x.face }
func (x AlignOpt) DoLabelOption(o *LabelOpts) { o.alignment = x.alignment }
func NewLabel(t string, lops ...LabelOption) *Label { func NewLabel(t string, lops ...LabelOption) *Label {
ret := &Label{} ret := &Label{}
opts := &LabelOpts{} opts := &LabelOpts{}
for _,o := range lops { o(opts) } for _,o := range lops { o.DoLabelOption(opts) }
ret.l = &text.Label{ ret.l = &text.Label{
Face: opts.face, Face: opts.face,
Text: t, Text: t,
@ -106,26 +107,29 @@ func (l *Label) Layout(ctx *Context) {
ctx.dims = l.l.Layout(ctx.ops, ctx.cs) ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
} }
func (l *Label) SetText(t string) {
l.l.Text = t
}
type Editor struct { type Editor struct {
e *text.Editor e *text.Editor
} }
type SinglelineOpt struct { singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{ x } }
type EditorOpts struct { type EditorOpts struct {
face text.Face FaceOpt
singleline bool SinglelineOpt
}
type EditorOption func(*EditorOpts)
func EditorFace(x text.Face) EditorOption {
return func(o *EditorOpts) { o.face = x }
}
func EditorSingleline() EditorOption {
return func(o *EditorOpts) { o.singleline = true }
} }
type EditorOption interface { DoEditorOption(*EditorOpts) }
func (x FaceOpt) DoEditorOption(o *EditorOpts) { o.face = x.face }
func (x SinglelineOpt) DoEditorOption(o *EditorOpts) { o.singleline = x.singleline }
func NewEditor(t string, eops ...EditorOption) *Editor { func NewEditor(t string, eops ...EditorOption) *Editor {
ret := &Editor{} ret := &Editor{}
opts := &EditorOpts{} opts := &EditorOpts{}
for _,o := range eops { o(opts) } for _,o := range eops { o.DoEditorOption(opts) }
ret.e = &text.Editor{ Face: opts.face, SingleLine: opts.singleline } ret.e = &text.Editor{ Face: opts.face, SingleLine: opts.singleline }
ret.SetText(t) ret.SetText(t)
return ret return ret
@ -151,6 +155,26 @@ func (fw fWidget) Layout(ctx *Context) {
fw.l(ctx) fw.l(ctx)
} }
type Stack WidgetCombinator
func NewStack() Stack {
s := layout.Stack{ Alignment: layout.Center }
scs := make([]layout.StackChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
s.Init(ctx.ops, ctx.cs)
for _,w := range ws {
ctx.cs = s.Rigid()
w.Layout(ctx)
scs = append(scs, s.End(ctx.dims))
}
ctx.dims = s.Layout(scs...)
scs = scs[0:0]
})
}
}
type Flex WidgetCombinator type Flex WidgetCombinator
// This "Widget" does nothing except set the Flexible field of a FlexOpts // This "Widget" does nothing except set the Flexible field of a FlexOpts
@ -160,31 +184,35 @@ func Flexible(v float32) Widget {
extra.data[extra.cur].(*FlexOpts).flexible = v extra.data[extra.cur].(*FlexOpts).flexible = v
}) })
} }
func Rigid() Widget {
return NewfWidget(func(ctx *Context) {
extra.data[extra.cur].(*FlexOpts).flexible = 0
})
}
type AxisOpt struct { axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{ x } }
type MainAxisAlignmentOpt struct { mainAxisAlignment layout.MainAxisAlignment }
func MainAxisAlignment(x layout.MainAxisAlignment) MainAxisAlignmentOpt { return MainAxisAlignmentOpt{ x } }
type CrossAxisAlignmentOpt struct { crossAxisAlignment layout.CrossAxisAlignment }
func CrossAxisAlignment(x layout.CrossAxisAlignment) CrossAxisAlignmentOpt { return CrossAxisAlignmentOpt{ x } }
type FlexOpts struct { type FlexOpts struct {
axis layout.Axis AxisOpt
mainAxisAlignment layout.MainAxisAlignment MainAxisAlignmentOpt
crossAxisAlignment layout.CrossAxisAlignment CrossAxisAlignmentOpt
flexible float32 flexible float32
} }
type FlexOption func(*FlexOpts) type FlexOption interface { DoFlexOption(*FlexOpts) }
func (x AxisOpt) DoFlexOption(o *FlexOpts) { o.axis = x.axis }
func FlexAxis(x layout.Axis) FlexOption { func (x MainAxisAlignmentOpt) DoFlexOption(o *FlexOpts) { o.mainAxisAlignment = x.mainAxisAlignment }
return func(o *FlexOpts) { o.axis = x } func (x CrossAxisAlignmentOpt) DoFlexOption(o *FlexOpts) { o.crossAxisAlignment = x.crossAxisAlignment }
}
func MainAxisAlignment(x layout.MainAxisAlignment) FlexOption {
return func(o *FlexOpts) { o.mainAxisAlignment = x }
}
func CrossAxisAlignment(x layout.CrossAxisAlignment) FlexOption {
return func(o *FlexOpts) { o.crossAxisAlignment = x }
}
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element. // NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
func NewFlex(fos ...FlexOption) Flex { func NewFlex(fos ...FlexOption) Flex {
opts := &FlexOpts{} opts := &FlexOpts{}
for _,o := range fos { o(opts) } for _,o := range fos { o.DoFlexOption(opts) }
f := layout.Flex{ f := layout.Flex{
Axis: opts.axis, Axis: opts.axis,
MainAxisAlignment: opts.mainAxisAlignment, MainAxisAlignment: opts.mainAxisAlignment,
@ -210,7 +238,7 @@ func NewFlex(fos ...FlexOption) Flex {
w.Layout(ctx) w.Layout(ctx)
fcs = append(fcs, f.End(ctx.dims)) fcs = append(fcs, f.End(ctx.dims))
} }
f.Layout(fcs...) ctx.dims = f.Layout(fcs...)
fcs = fcs[0:0] // truncate fcs = fcs[0:0] // truncate
}) })
} }
@ -220,33 +248,35 @@ type InsetOpts struct {
top, right, bottom, left ui.Value top, right, bottom, left ui.Value
} }
type InsetOption func(*InsetOpts) type InsetOption interface { DoInsetOption(*InsetOpts) }
func InsetTop(x ui.Value) InsetOption { type TopOpt struct { top ui.Value }
return func(o *InsetOpts) { o.top = x } func Top(x ui.Value) TopOpt { return TopOpt{ x } }
} type RightOpt struct { right ui.Value }
func InsetRight(x ui.Value) InsetOption { func Right(x ui.Value) RightOpt { return RightOpt{ x } }
return func(o *InsetOpts) { o.right = x } type BottomOpt struct { bottom ui.Value }
} func Bottom(x ui.Value) BottomOpt { return BottomOpt{ x } }
func InsetBottom(x ui.Value) InsetOption { type LeftOpt struct { left ui.Value }
return func(o *InsetOpts) { o.bottom = x } func Left(x ui.Value) LeftOpt { return LeftOpt{ x } }
}
func InsetLeft(x ui.Value) InsetOption { func (x TopOpt) DoInsetOption(o *InsetOpts) { o.top = x.top }
return func(o *InsetOpts) { o.left = x } func (x RightOpt) DoInsetOption(o *InsetOpts) { o.right = x.right }
} func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
func InsetSize(x ui.Value) InsetOption { func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
return func(o *InsetOpts) {
o.top = x type SizeOpt struct { size ui.Value }
o.right = x func Size(x ui.Value) SizeOpt { return SizeOpt{ x } }
o.bottom = x func (x SizeOpt) DoInsetOption(o *InsetOpts) {
o.left = x o.top = x.size
} o.right = x.size
o.bottom = x.size
o.left = x.size
} }
//NewInset returns a WidgetCombinator that wraps the layout.Inset element. //NewInset returns a WidgetCombinator that wraps the layout.Inset element.
func NewInset(insos ...InsetOption) WidgetCombinator { func NewInset(insos ...InsetOption) WidgetCombinator {
opts := &InsetOpts{} opts := &InsetOpts{}
for _,o := range insos { o(opts) } for _,o := range insos { o.DoInsetOption(opts) }
ins := layout.Inset{ Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left } ins := layout.Inset{ Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left }
return func(ws ...Widget) Widget { return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) { return NewfWidget(func(ctx *Context) {
@ -287,17 +317,19 @@ type BackgroundOpts struct {
radius ui.Value radius ui.Value
} }
type BackgroundOption func(*BackgroundOpts) type BackgroundOption interface { DoBackgroundOption(*BackgroundOpts) }
func BgColor(c color.RGBA) BackgroundOption {
return func(o *BackgroundOpts) { o.c = c } type ColorOpt struct { c color.RGBA }
} func Color(x color.RGBA) ColorOpt { return ColorOpt{ x } }
func BgRadius(x ui.Value) BackgroundOption { func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
return func(o *BackgroundOpts) { o.radius = x }
} type RadiusOpt struct { radius ui.Value }
func Radius(x ui.Value) RadiusOpt { return RadiusOpt { x } }
func (x RadiusOpt) DoBackgroundOption(o *BackgroundOpts) { o.radius = x.radius }
func NewBackground(bos ...BackgroundOption) WidgetCombinator { func NewBackground(bos ...BackgroundOption) WidgetCombinator {
opts := &BackgroundOpts{} opts := &BackgroundOpts{}
for _,o := range bos { o(opts) } for _,o := range bos { o.DoBackgroundOption(opts) }
bg := &Background{ bg := &Background{
Color: opts.c, Color: opts.c,
Radius: opts.radius, Radius: opts.radius,
@ -369,6 +401,7 @@ func (w cWidget) Layout(ctx *Context) {
func (w cWidget) Clicked(ctx *Context) bool { func (w cWidget) Clicked(ctx *Context) bool {
for _,e := range w.click.Events(ctx.q) { for _,e := range w.click.Events(ctx.q) {
if e.Type == gesture.TypeClick { if e.Type == gesture.TypeClick {
ctx.w.Invalidate()
return true return true
} }
} }