checkbox.go raw

   1  // SPDX-License-Identifier: Unlicense OR MIT
   2  
   3  package material
   4  
   5  import (
   6  	"github.com/p9c/p9/pkg/gel/gio/layout"
   7  	"github.com/p9c/p9/pkg/gel/gio/unit"
   8  	"github.com/p9c/p9/pkg/gel/gio/widget"
   9  )
  10  
  11  type CheckBoxStyle struct {
  12  	checkable
  13  	CheckBox *widget.Bool
  14  }
  15  
  16  func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle {
  17  	return CheckBoxStyle{
  18  		CheckBox: checkBox,
  19  		checkable: checkable{
  20  			Label:              label,
  21  			Color:              th.Palette.Fg,
  22  			IconColor:          th.Palette.ContrastBg,
  23  			TextSize:           th.TextSize.Scale(14.0 / 16.0),
  24  			Size:               unit.Dp(26),
  25  			shaper:             th.Shaper,
  26  			checkedStateIcon:   th.Icon.CheckBoxChecked,
  27  			uncheckedStateIcon: th.Icon.CheckBoxUnchecked,
  28  		},
  29  	}
  30  }
  31  
  32  // Layout updates the checkBox and displays it.
  33  func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions {
  34  	dims := c.layout(gtx, c.CheckBox.Value, c.CheckBox.Hovered())
  35  	gtx.Constraints.Min = dims.Size
  36  	c.CheckBox.Layout(gtx)
  37  	return dims
  38  }
  39