mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +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>
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/internal/f32color"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type checkable struct {
|
|
Label string
|
|
Color color.NRGBA
|
|
Font text.Font
|
|
TextSize unit.Sp
|
|
IconColor color.NRGBA
|
|
Size unit.Dp
|
|
shaper text.Shaper
|
|
checkedStateIcon *widget.Icon
|
|
uncheckedStateIcon *widget.Icon
|
|
}
|
|
|
|
func (c *checkable) layout(gtx layout.Context, checked, hovered bool) layout.Dimensions {
|
|
var icon *widget.Icon
|
|
if checked {
|
|
icon = c.checkedStateIcon
|
|
} else {
|
|
icon = c.uncheckedStateIcon
|
|
}
|
|
|
|
dims := layout.Flex{Alignment: layout.Middle}.Layout(gtx,
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
|
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
|
size := gtx.Dp(c.Size) * 4 / 3
|
|
dims := layout.Dimensions{
|
|
Size: image.Point{X: size, Y: size},
|
|
}
|
|
if !hovered {
|
|
return dims
|
|
}
|
|
|
|
background := f32color.MulAlpha(c.IconColor, 70)
|
|
|
|
b := image.Rectangle{Max: image.Pt(size, size)}
|
|
paint.FillShape(gtx.Ops, background, clip.Ellipse(b).Op(gtx.Ops))
|
|
|
|
return dims
|
|
}),
|
|
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.UniformInset(2).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
|
|
size := gtx.Dp(c.Size)
|
|
col := c.IconColor
|
|
if gtx.Queue == nil {
|
|
col = f32color.Disabled(col)
|
|
}
|
|
gtx.Constraints.Min = image.Point{X: size}
|
|
icon.Layout(gtx, col)
|
|
return layout.Dimensions{
|
|
Size: image.Point{X: size, Y: size},
|
|
}
|
|
})
|
|
}),
|
|
)
|
|
}),
|
|
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.UniformInset(2).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
|
|
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
|
return widget.Label{}.Layout(gtx, c.shaper, c.Font, c.TextSize, c.Label)
|
|
})
|
|
}),
|
|
)
|
|
return dims
|
|
}
|