github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/widget/material/checkable.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package material
     4  
     5  import (
     6  	"image"
     7  	"image/color"
     8  
     9  	"github.com/gop9/olt/gio/io/pointer"
    10  	"github.com/gop9/olt/gio/layout"
    11  	"github.com/gop9/olt/gio/op/paint"
    12  	"github.com/gop9/olt/gio/text"
    13  	"github.com/gop9/olt/gio/unit"
    14  	"github.com/gop9/olt/gio/widget"
    15  )
    16  
    17  type checkable struct {
    18  	Label              string
    19  	Color              color.RGBA
    20  	Font               text.Font
    21  	IconColor          color.RGBA
    22  	Size               unit.Value
    23  	shaper             *text.Shaper
    24  	checkedStateIcon   *Icon
    25  	uncheckedStateIcon *Icon
    26  }
    27  
    28  func (c *checkable) layout(gtx *layout.Context, checked bool) {
    29  
    30  	var icon *Icon
    31  	if checked {
    32  		icon = c.checkedStateIcon
    33  	} else {
    34  		icon = c.uncheckedStateIcon
    35  	}
    36  
    37  	hmin := gtx.Constraints.Width.Min
    38  	vmin := gtx.Constraints.Height.Min
    39  	layout.Flex{Alignment: layout.Middle}.Layout(gtx,
    40  		layout.Rigid(func() {
    41  			layout.Align(layout.Center).Layout(gtx, func() {
    42  				layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
    43  					size := gtx.Px(c.Size)
    44  					icon.Color = c.IconColor
    45  					icon.Layout(gtx, unit.Px(float32(size)))
    46  					gtx.Dimensions = layout.Dimensions{
    47  						Size: image.Point{X: size, Y: size},
    48  					}
    49  				})
    50  			})
    51  		}),
    52  
    53  		layout.Rigid(func() {
    54  			gtx.Constraints.Width.Min = hmin
    55  			gtx.Constraints.Height.Min = vmin
    56  			layout.Align(layout.Start).Layout(gtx, func() {
    57  				layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
    58  					paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
    59  					widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Label)
    60  				})
    61  			})
    62  		}),
    63  	)
    64  	pointer.Rect(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
    65  }