radiobutton.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 RadioButtonStyle struct {
  12  	checkable
  13  	Key   string
  14  	Group *widget.Enum
  15  }
  16  
  17  // RadioButton returns a RadioButton with a label. The key specifies
  18  // the value for the Enum.
  19  func RadioButton(th *Theme, group *widget.Enum, key, label string) RadioButtonStyle {
  20  	return RadioButtonStyle{
  21  		Group: group,
  22  		checkable: checkable{
  23  			Label: label,
  24  
  25  			Color:              th.Palette.Fg,
  26  			IconColor:          th.Palette.ContrastBg,
  27  			TextSize:           th.TextSize.Scale(14.0 / 16.0),
  28  			Size:               unit.Dp(26),
  29  			shaper:             th.Shaper,
  30  			checkedStateIcon:   th.Icon.RadioChecked,
  31  			uncheckedStateIcon: th.Icon.RadioUnchecked,
  32  		},
  33  		Key: key,
  34  	}
  35  }
  36  
  37  // Layout updates enum and displays the radio button.
  38  func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
  39  	hovered, hovering := r.Group.Hovered()
  40  	dims := r.layout(gtx, r.Group.Value == r.Key, hovering && hovered == r.Key)
  41  	gtx.Constraints.Min = dims.Size
  42  	r.Group.Layout(gtx, r.Key)
  43  	return dims
  44  }
  45