mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
3d37491342
The unit.Value is a struct and thus more inconvenient to use than its
underlying float32 type. In addition, most uses don't need a general
value, but rather a specific unit given by the context. This change
replaces unit.Value with two float32 units, Dp and Sp. It also changes
variables and parameters of unit.Value to a specific unit type matching
the context. That is, unit.Dp everywhere except for text sizes which are
in Sp.
Switching to typed float32s has multiple advantages
- They can be constants:
const touchSlop = unit.Dp(16)
- Casting untyped constants is no longer necessary:
insets := layout.UniformInset(16)
- Calculation with values is natural:
func (s ScrollbarStyle) Width() unit.Dp {
return s.Indicator.MinorWidth + s.Track.MinorPadding + s.Track.MinorPadding
}
The main API change is that calls to gtx.Px must be replaced with either
gtx.Dp or gtx.Sp depending on the unit.
Idea by Christophe Meessen.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"gioui.org/io/semantic"
|
|
"gioui.org/layout"
|
|
"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 * 14.0 / 16.0,
|
|
Size: 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()
|
|
focus, focused := r.Group.Focused()
|
|
return r.Group.Layout(gtx, r.Key, func(gtx layout.Context) layout.Dimensions {
|
|
semantic.RadioButton.Add(gtx.Ops)
|
|
highlight := hovering && hovered == r.Key || focused && focus == r.Key
|
|
return r.layout(gtx, r.Group.Value == r.Key, highlight)
|
|
})
|
|
}
|