mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
7bf3265ccd
Instead of
type Contraints struct {
Width, Height Constraint
}
use
type Constraints struct {
Min, Max image.Point
}
which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.
API Change. Rewrites:
gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'
Signed-off-by: Elias Naur <mail@eliasnaur.com>
64 lines
1.4 KiB
Go
64 lines
1.4 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
|
|
TextSize unit.Value
|
|
IconColor color.RGBA
|
|
Size unit.Value
|
|
shaper text.Shaper
|
|
checkedStateIcon *widget.Icon
|
|
uncheckedStateIcon *widget.Icon
|
|
}
|
|
|
|
func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
|
var icon *widget.Icon
|
|
if checked {
|
|
icon = c.checkedStateIcon
|
|
} else {
|
|
icon = c.uncheckedStateIcon
|
|
}
|
|
|
|
min := gtx.Constraints.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.Min = min
|
|
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.TextSize, c.Label)
|
|
})
|
|
})
|
|
}),
|
|
)
|
|
pointer.Rect(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
|
|
}
|