mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
b39d1bdac7
Before this change, a radio button with the empty key ("") would be
displayed as hovering if no other button were.
It's still not possible to have no radio buttons selected when one of
them is the empty key. If that's becomes necessary, Enum.Value can be
converted to a *string.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"gioui.org/layout"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type RadioButtonStyle struct {
|
|
checkable
|
|
Key string
|
|
Group *widget.Enum
|
|
}
|
|
|
|
// RadioButton returns a RadioButton with a label. The key specifies
|
|
// the value for the Enum.
|
|
func RadioButton(th *Theme, group *widget.Enum, key, label string) RadioButtonStyle {
|
|
return RadioButtonStyle{
|
|
Group: group,
|
|
checkable: checkable{
|
|
Label: label,
|
|
|
|
Color: th.Palette.Fg,
|
|
IconColor: th.Palette.ContrastBg,
|
|
TextSize: th.TextSize.Scale(14.0 / 16.0),
|
|
Size: unit.Dp(26),
|
|
shaper: th.Shaper,
|
|
checkedStateIcon: th.Icon.RadioChecked,
|
|
uncheckedStateIcon: th.Icon.RadioUnchecked,
|
|
},
|
|
Key: key,
|
|
}
|
|
}
|
|
|
|
// Layout updates enum and displays the radio button.
|
|
func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|
hovered, hovering := r.Group.Hovered()
|
|
dims := r.layout(gtx, r.Group.Value == r.Key, hovering && hovered == r.Key)
|
|
gtx.Constraints.Min = dims.Size
|
|
r.Group.Layout(gtx, r.Key)
|
|
return dims
|
|
}
|