gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/widget/material/radiobutton.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package material
     4  
     5  import (
     6  	"gioui.org/io/semantic"
     7  	"gioui.org/layout"
     8  	"gioui.org/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  	r := 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 * 14.0 / 16.0,
    28  			Size:               26,
    29  			shaper:             th.Shaper,
    30  			checkedStateIcon:   th.Icon.RadioChecked,
    31  			uncheckedStateIcon: th.Icon.RadioUnchecked,
    32  		},
    33  		Key: key,
    34  	}
    35  	r.checkable.Font.Typeface = th.Face
    36  	return r
    37  }
    38  
    39  // Layout updates enum and displays the radio button.
    40  func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
    41  	r.Group.Update(gtx)
    42  	hovered, hovering := r.Group.Hovered()
    43  	focus, focused := r.Group.Focused()
    44  	return r.Group.Layout(gtx, r.Key, func(gtx layout.Context) layout.Dimensions {
    45  		semantic.RadioButton.Add(gtx.Ops)
    46  		highlight := hovering && hovered == r.Key || focused && focus == r.Key
    47  		return r.layout(gtx, r.Group.Value == r.Key, highlight)
    48  	})
    49  }