radiobutton.go raw

   1  // SPDX-License-Identifier: Unlicense OR MIT
   2  
   3  package gel
   4  
   5  import (
   6  	"golang.org/x/exp/shiny/materialdesign/icons"
   7  
   8  	l "github.com/p9c/gio/layout"
   9  )
  10  
  11  type RadioButton struct {
  12  	*Checkable
  13  	*Window
  14  	key   string
  15  	group *Enum
  16  }
  17  
  18  // RadioButton returns a RadioButton with a label. The key specifies the value for the Enum.
  19  func (w *Window) RadioButton(checkable *Checkable, group *Enum, key,
  20  	label string) *RadioButton {
  21  	// if checkable == nil {
  22  	// 	debug.PrintStack()
  23  	// 	os.Exit(0)
  24  	// }
  25  	return &RadioButton{
  26  		group:  group,
  27  		Window: w,
  28  		Checkable: checkable.
  29  			CheckedStateIcon(&icons.ToggleRadioButtonChecked). // Color("Primary").
  30  			UncheckedStateIcon(&icons.ToggleRadioButtonUnchecked). // Color("PanelBg").
  31  			Label(label), // .Color("DocText").IconColor("PanelBg"),
  32  		key: key,
  33  	}
  34  }
  35  
  36  // Key sets the key initially active on the radiobutton
  37  func (r *RadioButton) Key(key string) *RadioButton {
  38  	r.key = key
  39  	return r
  40  }
  41  
  42  // Group sets the enum group of the radio button
  43  func (r *RadioButton) Group(group *Enum) *RadioButton {
  44  	r.group = group
  45  	return r
  46  }
  47  
  48  // Fn updates enum and displays the radio button.
  49  func (r RadioButton) Fn(gtx l.Context) l.Dimensions {
  50  	dims := r.Checkable.Fn(gtx, r.group.Value() == r.key)
  51  	gtx.Constraints.Min = dims.Size
  52  	r.group.Fn(gtx, r.key)
  53  	return dims
  54  }
  55