Files
prevara/checkable.go
2025-11-30 16:45:22 +00:00

134 lines
3.3 KiB
Go

package gel
import (
"image"
"github.com/p9c/p9/pkg/gel/gio/font"
l "github.com/p9c/p9/pkg/gel/gio/layout"
"github.com/p9c/p9/pkg/gel/gio/op/clip"
"github.com/p9c/p9/pkg/gel/gio/op/paint"
"github.com/p9c/p9/pkg/gel/gio/text"
"github.com/p9c/p9/pkg/gel/gio/unit"
"golang.org/x/exp/shiny/materialdesign/icons"
)
type Checkable struct {
*Window
label string
color string
font font.Font
textSize unit.Sp
iconColor string
size unit.Sp
checkedStateIcon *[]byte
uncheckedStateIcon *[]byte
shaper *text.Shaper
checked bool
}
// Checkable creates a checkbox type widget
func (w *Window) Checkable() *Checkable {
fontName := "bariol regular"
var f font.Font
if fon, e := w.collection.Font(fontName); !E.Chk(e) {
f = fon
}
return &Checkable{
Window: w,
label: "checkable",
color: "Primary",
font: f,
textSize: unit.Sp(float32(w.TextSize) * 14.0 / 16.0),
iconColor: "Primary",
size: unit.Sp(float32(w.TextSize) * 1.5),
checkedStateIcon: &icons.ToggleCheckBox,
uncheckedStateIcon: &icons.ToggleCheckBoxOutlineBlank,
shaper: w.shaper,
}
}
// Label sets the label on the checkbox
func (c *Checkable) Label(txt string) *Checkable {
c.label = txt
return c
}
// Color sets the color of the checkbox label
func (c *Checkable) Color(color string) *Checkable {
c.color = color
return c
}
// Font sets the font used on the label
func (c *Checkable) Font(font string) *Checkable {
if fon, e := c.Theme.collection.Font(font); !E.Chk(e) {
c.font = fon
}
return c
}
// TextScale sets the size of the font relative to the base text size
func (c *Checkable) TextScale(scale float32) *Checkable {
c.textSize = unit.Sp(float32(c.Theme.TextSize) * scale)
return c
}
// IconColor sets the color of the icon
func (c *Checkable) IconColor(color string) *Checkable {
c.iconColor = color
return c
}
// Scale sets the size of the checkbox icon relative to the base font size
func (c *Checkable) Scale(size float32) *Checkable {
c.size = unit.Sp(float32(c.Theme.TextSize) * size)
return c
}
// CheckedStateIcon loads the icon for the checked state
func (c *Checkable) CheckedStateIcon(ic *[]byte) *Checkable {
c.checkedStateIcon = ic
return c
}
// UncheckedStateIcon loads the icon for the unchecked state
func (c *Checkable) UncheckedStateIcon(ic *[]byte) *Checkable {
c.uncheckedStateIcon = ic
return c
}
// Fn renders the checkbox widget
func (c *Checkable) Fn(gtx l.Context, checked bool) l.Dimensions {
var icon *Icon
if checked {
icon = c.Icon().
Color(c.iconColor).
Src(c.checkedStateIcon)
} else {
icon = c.Icon().
Color(c.iconColor).
Src(c.uncheckedStateIcon)
}
icon.size = c.size
dims :=
c.Theme.Flex().
Rigid(
func(gtx l.Context) l.Dimensions {
size := gtx.Sp(c.size)
icon.Fn(gtx)
return l.Dimensions{
Size: image.Point{X: size, Y: size},
}
},
).
Rigid(
func(gtx l.Context) l.Dimensions {
paint.ColorOp{Color: c.Theme.Colors.GetNRGBAFromName(c.color)}.Add(gtx.Ops)
return c.Caption(c.label).Color(c.color).Fn(gtx)
},
).
Fn(gtx)
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
return dims
}