github.com/Seikaijyu/gio@v0.0.1/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/Seikaijyu/gio/font"
    10  	"github.com/Seikaijyu/gio/internal/f32color"
    11  	"github.com/Seikaijyu/gio/layout"
    12  	"github.com/Seikaijyu/gio/op"
    13  	"github.com/Seikaijyu/gio/op/paint"
    14  	"github.com/Seikaijyu/gio/text"
    15  	"github.com/Seikaijyu/gio/unit"
    16  	"github.com/Seikaijyu/gio/widget"
    17  )
    18  
    19  type checkable struct {
    20  	Label              string
    21  	Color              color.NRGBA
    22  	Font               font.Font
    23  	TextSize           unit.Sp
    24  	IconColor          color.NRGBA
    25  	Size               unit.Dp
    26  	shaper             *text.Shaper
    27  	checkedStateIcon   *widget.Icon
    28  	uncheckedStateIcon *widget.Icon
    29  }
    30  
    31  func (c *checkable) layout(gtx layout.Context, checked, hovered bool) layout.Dimensions {
    32  	var icon *widget.Icon
    33  	if checked {
    34  		icon = c.checkedStateIcon
    35  	} else {
    36  		icon = c.uncheckedStateIcon
    37  	}
    38  
    39  	dims := layout.Flex{Alignment: layout.Middle}.Layout(gtx,
    40  		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
    41  
    42  			return layout.Stack{Alignment: layout.N}.Layout(gtx,
    43  
    44  				layout.Stacked(func(gtx layout.Context) layout.Dimensions {
    45  					defer op.Offset(image.Pt(0, 2)).Push(gtx.Ops).Pop()
    46  					size := gtx.Dp(c.Size)
    47  					col := c.IconColor
    48  					if gtx.Queue == nil {
    49  						col = f32color.Disabled(col)
    50  					}
    51  					gtx.Constraints.Min = image.Point{X: size}
    52  					icon.Layout(gtx, col)
    53  					return layout.Dimensions{
    54  						Size: image.Point{X: size, Y: size},
    55  					}
    56  				}),
    57  			)
    58  		}),
    59  		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
    60  			defer op.Offset(image.Pt(0, 0)).Push(gtx.Ops).Pop()
    61  			colMacro := op.Record(gtx.Ops)
    62  			paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
    63  
    64  			return widget.Label{}.Layout(gtx, c.shaper, c.Font, c.TextSize, c.Label, colMacro.Stop())
    65  		}),
    66  	)
    67  	return dims
    68  }