mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
fb7337f794
It's one less type (Align) and shorter: Before: layout.Align(layout.Center).Layout(...) After layout.Center.Layout(...) It is also safer: since `layout.Align(...)` was a casting operation, the Go compiler would not complain about an incompatible constant. For example, the widget/material package contained a wrong cast: layout.Align(layout.Start) which should have been layout.Align(layout.W) After this change, attempting `layout.Start.Layout(...)` result in a compile error. Signed-off-by: Elias Naur <mail@eliasnaur.com>
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/io/pointer"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type checkable struct {
|
|
Label string
|
|
Color color.RGBA
|
|
Font text.Font
|
|
IconColor color.RGBA
|
|
Size unit.Value
|
|
shaper text.Shaper
|
|
checkedStateIcon *Icon
|
|
uncheckedStateIcon *Icon
|
|
}
|
|
|
|
func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
|
|
|
var icon *Icon
|
|
if checked {
|
|
icon = c.checkedStateIcon
|
|
} else {
|
|
icon = c.uncheckedStateIcon
|
|
}
|
|
|
|
hmin := gtx.Constraints.Width.Min
|
|
vmin := gtx.Constraints.Height.Min
|
|
layout.Flex{Alignment: layout.Middle}.Layout(gtx,
|
|
layout.Rigid(func() {
|
|
layout.Center.Layout(gtx, func() {
|
|
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
|
size := gtx.Px(c.Size)
|
|
icon.Color = c.IconColor
|
|
icon.Layout(gtx, unit.Px(float32(size)))
|
|
gtx.Dimensions = layout.Dimensions{
|
|
Size: image.Point{X: size, Y: size},
|
|
}
|
|
})
|
|
})
|
|
}),
|
|
|
|
layout.Rigid(func() {
|
|
gtx.Constraints.Width.Min = hmin
|
|
gtx.Constraints.Height.Min = vmin
|
|
layout.W.Layout(gtx, func() {
|
|
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
|
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
|
widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Label)
|
|
})
|
|
})
|
|
}),
|
|
)
|
|
pointer.Rect(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
|
|
}
|