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 {
lbl := giowrap.NewLabel(t, giowrap.LabelFace(face), giowrap.LabelAlignment(text.Center))
bg := giowrap.NewBackground(giowrap.BgColor(c), giowrap.BgRadius(ui.Dp(4)))
lbl := giowrap.NewLabel(t, giowrap.Face(face), giowrap.Align(text.Center))
bg := giowrap.NewBackground(giowrap.Color(c), giowrap.Radius(ui.Dp(4)))
return giowrap.AsClickable(bg(lbl))
}
@ -86,16 +86,16 @@ func main1() {
}
ctx := giowrap.NewContext(w)
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()
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))
OuterInset := giowrap.NewInset(giowrap.InsetSize(ui.Dp(10)))
InnerInset := giowrap.NewInset(giowrap.InsetSize(ui.Dp(10)))
f1 := giowrap.NewFlex(giowrap.Axis(layout.Vertical))
OuterInset := giowrap.NewInset(giowrap.Size(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)),
"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
}
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 {
face text.Face
alignment text.Alignment
}
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 }
FaceOpt
AlignOpt
}
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 {
ret := &Label{}
opts := &LabelOpts{}
for _,o := range lops { o(opts) }
for _,o := range lops { o.DoLabelOption(opts) }
ret.l = &text.Label{
Face: opts.face,
Text: t,
@ -106,26 +107,29 @@ func (l *Label) Layout(ctx *Context) {
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
}
func (l *Label) SetText(t string) {
l.l.Text = t
}
type Editor struct {
e *text.Editor
}
type SinglelineOpt struct { singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{ x } }
type EditorOpts struct {
face text.Face
singleline bool
}
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 }
FaceOpt
SinglelineOpt
}
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 {
ret := &Editor{}
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.SetText(t)
return ret
@ -151,6 +155,26 @@ func (fw fWidget) Layout(ctx *Context) {
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
// 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
})
}
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 {
axis layout.Axis
mainAxisAlignment layout.MainAxisAlignment
crossAxisAlignment layout.CrossAxisAlignment
AxisOpt
MainAxisAlignmentOpt
CrossAxisAlignmentOpt
flexible float32
}
type FlexOption func(*FlexOpts)
func FlexAxis(x layout.Axis) FlexOption {
return func(o *FlexOpts) { o.axis = x }
}
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 }
}
type FlexOption interface { DoFlexOption(*FlexOpts) }
func (x AxisOpt) DoFlexOption(o *FlexOpts) { o.axis = x.axis }
func (x MainAxisAlignmentOpt) DoFlexOption(o *FlexOpts) { o.mainAxisAlignment = x.mainAxisAlignment }
func (x CrossAxisAlignmentOpt) DoFlexOption(o *FlexOpts) { o.crossAxisAlignment = x.crossAxisAlignment }
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
func NewFlex(fos ...FlexOption) Flex {
opts := &FlexOpts{}
for _,o := range fos { o(opts) }
for _,o := range fos { o.DoFlexOption(opts) }
f := layout.Flex{
Axis: opts.axis,
MainAxisAlignment: opts.mainAxisAlignment,
@ -210,7 +238,7 @@ func NewFlex(fos ...FlexOption) Flex {
w.Layout(ctx)
fcs = append(fcs, f.End(ctx.dims))
}
f.Layout(fcs...)
ctx.dims = f.Layout(fcs...)
fcs = fcs[0:0] // truncate
})
}
@ -220,33 +248,35 @@ type InsetOpts struct {
top, right, bottom, left ui.Value
}
type InsetOption func(*InsetOpts)
type InsetOption interface { DoInsetOption(*InsetOpts) }
func InsetTop(x ui.Value) InsetOption {
return func(o *InsetOpts) { o.top = x }
}
func InsetRight(x ui.Value) InsetOption {
return func(o *InsetOpts) { o.right = x }
}
func InsetBottom(x ui.Value) InsetOption {
return func(o *InsetOpts) { o.bottom = x }
}
func InsetLeft(x ui.Value) InsetOption {
return func(o *InsetOpts) { o.left = x }
}
func InsetSize(x ui.Value) InsetOption {
return func(o *InsetOpts) {
o.top = x
o.right = x
o.bottom = x
o.left = x
}
type TopOpt struct { top ui.Value }
func Top(x ui.Value) TopOpt { return TopOpt{ x } }
type RightOpt struct { right ui.Value }
func Right(x ui.Value) RightOpt { return RightOpt{ x } }
type BottomOpt struct { bottom ui.Value }
func Bottom(x ui.Value) BottomOpt { return BottomOpt{ x } }
type LeftOpt struct { left ui.Value }
func Left(x ui.Value) LeftOpt { return LeftOpt{ x } }
func (x TopOpt) DoInsetOption(o *InsetOpts) { o.top = x.top }
func (x RightOpt) DoInsetOption(o *InsetOpts) { o.right = x.right }
func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
type SizeOpt struct { size ui.Value }
func Size(x ui.Value) SizeOpt { return SizeOpt{ x } }
func (x SizeOpt) DoInsetOption(o *InsetOpts) {
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.
func NewInset(insos ...InsetOption) WidgetCombinator {
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 }
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
@ -287,17 +317,19 @@ type BackgroundOpts struct {
radius ui.Value
}
type BackgroundOption func(*BackgroundOpts)
func BgColor(c color.RGBA) BackgroundOption {
return func(o *BackgroundOpts) { o.c = c }
}
func BgRadius(x ui.Value) BackgroundOption {
return func(o *BackgroundOpts) { o.radius = x }
}
type BackgroundOption interface { DoBackgroundOption(*BackgroundOpts) }
type ColorOpt struct { c color.RGBA }
func Color(x color.RGBA) ColorOpt { return ColorOpt{ x } }
func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
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 {
opts := &BackgroundOpts{}
for _,o := range bos { o(opts) }
for _,o := range bos { o.DoBackgroundOption(opts) }
bg := &Background{
Color: opts.c,
Radius: opts.radius,
@ -369,6 +401,7 @@ func (w cWidget) Layout(ctx *Context) {
func (w cWidget) Clicked(ctx *Context) bool {
for _,e := range w.click.Events(ctx.q) {
if e.Type == gesture.TypeClick {
ctx.w.Invalidate()
return true
}
}