package main import ( "image" "image/color" "gioui.org/f32" "gioui.org/gesture" "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op/clip" "gioui.org/op/paint" "gioui.org/text" "gioui.org/unit" "gioui.org/widget/material" ) var ( black = color.RGBA{A: 0xff, R: 0, G: 0, B: 0} white = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff} gray = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xf0} darkgray = color.RGBA{A: 0xff, R: 0xa0, G: 0xa0, B: 0xa0} ) type Overlay struct { Size unit.Value Text string Click gesture.Click Color color.RGBA Background color.RGBA Alignment text.Alignment } func (b *Overlay) Layout(gtx C) D { ins := layout.UniformInset(unit.Dp(1)) return ins.Layout(gtx, func(gtx C) D { st := layout.Stack{} c2 := layout.Stacked(func(gtx C) D { l := material.Label(th, b.Size, b.Text) ins := layout.UniformInset(unit.Dp(4)) l.Color = b.Color ret := ins.Layout(gtx, func(gtx C) D { return l.Layout(gtx) }) pointer.Rect(image.Rect(0, 0, ret.Size.X, ret.Size.Y)).Add(gtx.Ops) return ret }) c1 := layout.Expanded(func(gtx C) D { return layoutRRect(b.Background, gtx) }) return st.Layout(gtx, c1, c2) }) } type SelButton struct { Button SelColor color.RGBA Selected bool } type Button struct { Size unit.Value Label string Click gesture.Click Color color.RGBA Background color.RGBA Alignment text.Alignment clicked bool } func layoutRRect(col color.RGBA, gtx C) D { r := float32(gtx.Px(unit.Dp(4))) sz := image.Point{X: gtx.Constraints.Min.X, Y: gtx.Constraints.Min.Y} w, h := float32(sz.X), float32(sz.Y) rect := f32.Rectangle{ f32.Point{0, 0}, f32.Point{w, h}, } //clip.RoundRect(gtx.Ops, rect, r, r, r, r) clip.RRect{Rect: rect, NE: r, NW: r, SE: r, SW: r}.Add(gtx.Ops) paint.ColorOp{Color: col}.Add(gtx.Ops) paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(gtx.Ops) return layout.Dimensions{Size: sz} } func (b *Button) Layout(gtx C) D { mwidth := gtx.Constraints.Min.X b.clicked = false for _, ev := range b.Click.Events(gtx) { if ev.Type == gesture.TypeClick { b.clicked = true } } ins := layout.UniformInset(unit.Dp(1)) return ins.Layout(gtx, func(gtx C) D { st := layout.Stack{} c2 := layout.Stacked(func(gtx C) D { l := material.Label(th, b.Size, b.Label) ins := layout.UniformInset(unit.Dp(4)) //paint.ColorOp{Color: b.Color}.Add(ops) ret := ins.Layout(gtx, func(gtx C) D { return l.Layout(gtx) }) pointer.Rect(image.Rect(0, 0, ret.Size.X, ret.Size.Y)).Add(gtx.Ops) b.Click.Add(gtx.Ops) return ret }) c1 := layout.Expanded(func(gtx C) D { gtx.Constraints.Min.X = mwidth return layoutRRect(b.Background, gtx) }) return st.Layout(gtx, c1, c2) }) } func (b *Button) Clicked() bool { return b.clicked } func (b *SelButton) Toggle() { b.Selected = !b.Selected b.SelColor, b.Background = b.Background, b.SelColor } func (b *SelButton) Select() { if !b.Selected { b.Toggle() } } func (b *SelButton) Deselect() { if b.Selected { b.Toggle() } } func (b *SelButton) Clicked() bool { if b.clicked { b.Toggle() return true } else { return false } }