mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
2e991f31be
This is a breaking change as Icon.Layout no longer requests a size. Before: sz := unit.Dp(20) ic.Layout(gtx, sz) After: sz := gtx.Metric.Px(unit.Dp(20)) gtx.Constraints.Min = image.Pt(sz, 0) ic.Layout(gtx) Fixes gio#240 Signed-off-by: pierre <pierre.curto@gmail.com>
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/internal/f32color"
|
|
"gioui.org/io/pointer"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type checkable struct {
|
|
Label string
|
|
Color color.NRGBA
|
|
Font text.Font
|
|
TextSize unit.Value
|
|
IconColor color.NRGBA
|
|
Size unit.Value
|
|
shaper text.Shaper
|
|
checkedStateIcon *widget.Icon
|
|
uncheckedStateIcon *widget.Icon
|
|
}
|
|
|
|
func (c *checkable) layout(gtx layout.Context, checked, hovered bool) layout.Dimensions {
|
|
var icon *widget.Icon
|
|
if checked {
|
|
icon = c.checkedStateIcon
|
|
} else {
|
|
icon = c.uncheckedStateIcon
|
|
}
|
|
|
|
dims := layout.Flex{Alignment: layout.Middle}.Layout(gtx,
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
|
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
|
size := gtx.Px(c.Size) * 4 / 3
|
|
dims := layout.Dimensions{
|
|
Size: image.Point{X: size, Y: size},
|
|
}
|
|
if !hovered {
|
|
return dims
|
|
}
|
|
|
|
background := f32color.MulAlpha(c.IconColor, 70)
|
|
|
|
radius := float32(size) / 2
|
|
paint.FillShape(gtx.Ops, background,
|
|
clip.Circle{
|
|
Center: f32.Point{X: radius, Y: radius},
|
|
Radius: radius,
|
|
}.Op(gtx.Ops))
|
|
|
|
return dims
|
|
}),
|
|
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.UniformInset(unit.Dp(2)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
|
|
size := gtx.Px(c.Size)
|
|
icon.Color = c.IconColor
|
|
if gtx.Queue == nil {
|
|
icon.Color = f32color.Disabled(icon.Color)
|
|
}
|
|
gtx.Constraints.Min = image.Point{X: size}
|
|
icon.Layout(gtx)
|
|
return layout.Dimensions{
|
|
Size: image.Point{X: size, Y: size},
|
|
}
|
|
})
|
|
}),
|
|
)
|
|
}),
|
|
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.UniformInset(unit.Dp(2)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
|
|
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
|
return widget.Label{}.Layout(gtx, c.shaper, c.Font, c.TextSize, c.Label)
|
|
})
|
|
}),
|
|
)
|
|
pointer.Rect(image.Rectangle{Max: dims.Size}).Add(gtx.Ops)
|
|
return dims
|
|
}
|