Files
prevara/buttonlayout.go

107 lines
2.5 KiB
Go

package gel
import (
"image"
"image/color"
l "gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/unit"
"git.mleku.dev/mleku/prevara/f32color"
)
type ButtonLayout struct {
*Window
background color.NRGBA
cornerRadius unit.Dp
button *Clickable
w l.Widget
corners int
}
// ButtonLayout creates a button with a background and another widget over top
func (w *Window) ButtonLayout(button *Clickable) *ButtonLayout {
return &ButtonLayout{
Window: w,
button: button,
background: w.Colors.GetNRGBAFromName("ButtonBg"),
cornerRadius: unit.Dp(float32(w.TextSize) * 0.25),
}
}
// Corners sets which corners have the radius of rounding
func (b *ButtonLayout) Corners(corners int) *ButtonLayout {
b.corners = corners
return b
}
// Background sets the background color of the button
func (b *ButtonLayout) Background(color string) *ButtonLayout {
b.background = b.Theme.Colors.GetNRGBAFromName(color)
return b
}
// CornerRadius sets the radius of the corners of the button
func (b *ButtonLayout) CornerRadius(radius float32) *ButtonLayout {
b.cornerRadius = unit.Dp(float32(b.Theme.TextSize) * radius)
return b
}
// Embed a widget in the button
func (b *ButtonLayout) Embed(w l.Widget) *ButtonLayout {
b.w = w
return b
}
func (b *ButtonLayout) SetClick(fn func()) *ButtonLayout {
b.button.SetClick(fn)
return b
}
func (b *ButtonLayout) SetCancel(fn func()) *ButtonLayout {
b.button.SetCancel(fn)
return b
}
func (b *ButtonLayout) SetPress(fn func()) *ButtonLayout {
b.button.SetPress(fn)
return b
}
// Fn is the function that draws the button and its child widget
func (b *ButtonLayout) Fn(gtx l.Context) l.Dimensions {
min := gtx.Constraints.Min
return b.Stack().Alignment(l.Center).
Expanded(
func(gtx l.Context) l.Dimensions {
rr := gtx.Dp(b.cornerRadius)
defer clip.RRect{
Rect: image.Rectangle{Max: image.Point{
X: gtx.Constraints.Min.X,
Y: gtx.Constraints.Min.Y,
}},
NW: ifDirInt(rr, b.corners&NW),
NE: ifDirInt(rr, b.corners&NE),
SW: ifDirInt(rr, b.corners&SW),
SE: ifDirInt(rr, b.corners&SE),
}.Push(gtx.Ops).Pop()
background := b.background
if !gtx.Source.Enabled() {
background = f32color.MulAlpha(b.background, 150)
}
dims := Fill(gtx, background)
for _, c := range b.button.History() {
drawInk(gtx, c)
}
return dims
}).
Stacked(
func(gtx l.Context) l.Dimensions {
gtx.Constraints.Min = min
return l.Center.Layout(gtx, b.w)
}).
Expanded(b.button.Fn).
Fn(gtx)
}