checkbox.go raw

   1  package gel
   2  
   3  import (
   4  	l "github.com/p9c/gio/layout"
   5  )
   6  
   7  // CheckBox creates a checkbox with a text label
   8  func (w *Window) CheckBox(checkBox *Bool) *Checkbox {
   9  	var (
  10  		color     = "DocText"
  11  		textColor = "Primary"
  12  		label     = "this is a label"
  13  	)
  14  	chk := w.Checkable()
  15  	chk.Font("bariol regular").Color(textColor)
  16  	return &Checkbox{
  17  		color:     color,
  18  		textColor: textColor,
  19  		label:     label,
  20  		checkBox:  checkBox,
  21  		Checkable: chk,
  22  		action:    func(b bool) {},
  23  	}
  24  }
  25  
  26  type Checkbox struct {
  27  	*Checkable
  28  	checkBox                *Bool
  29  	color, textColor, label string
  30  	action                  func(b bool)
  31  }
  32  
  33  // IconColor sets the color of the icon in the checkbox
  34  func (c *Checkbox) IconColor(color string) *Checkbox {
  35  	c.Checkable.iconColor = color
  36  	return c
  37  }
  38  
  39  // TextColor sets the color of the text label
  40  func (c *Checkbox) TextColor(color string) *Checkbox {
  41  	c.Checkable.color = color
  42  	return c
  43  }
  44  
  45  // TextScale sets the scale relative to the base font size for the text label
  46  func (c *Checkbox) TextScale(scale float32) *Checkbox {
  47  	c.textSize = c.TextSize.Scale(scale)
  48  	return c
  49  }
  50  
  51  // Text sets the text to be rendered on the checkbox
  52  func (c *Checkbox) Text(label string) *Checkbox {
  53  	c.Checkable.label = label
  54  	return c
  55  }
  56  
  57  // IconScale sets the scaling of the check icon
  58  func (c *Checkbox) IconScale(scale float32) *Checkbox {
  59  	c.size = c.TextSize.Scale(scale)
  60  	return c
  61  }
  62  
  63  // SetOnChange sets the callback when a state change event occurs
  64  func (c *Checkbox) SetOnChange(fn func(b bool)) *Checkbox {
  65  	c.action = fn
  66  	return c
  67  }
  68  
  69  // Fn renders the checkbox
  70  func (c *Checkbox) Fn(gtx l.Context) l.Dimensions {
  71  	dims := c.Checkable.Fn(gtx, c.checkBox.GetValue())
  72  	gtx.Constraints.Min = dims.Size
  73  	c.checkBox.Fn(gtx)
  74  	return dims
  75  }
  76