forked from joejulian/gio
3af01a3f43
Change the definition of Widget from the implicit
type Widget func()
to the explicit functional
type Widget func(gtx layout.Context) layout.Dimensions
The advantages are numerous:
- Clearer connection between the incoming context and the output dimensions.
- Returning the Dimensions are impossible to omit.
- Contexts passed by value, so its fields can be exported
and freely mutated by the program.
The only disadvantage is the longer function literals and the many "returns".
What tipped the scales in favour of the explicit Widget variant is that type
aliases can dramatically shorten the literals:
type (
C = layout.Context
D = layout.Dimensions
)
widget := func(gtx C) D {
...
}
Note that the aliases are not part of the Gio API and it is up to each user
whether they want to use them.
Finally the Go proposal for lightweight function literals,
https://github.com/golang/go/issues/21498, may remove the disadvantage
completely in future.
Context becomes a plain struct with only public fields, and its Reset is
replaced by a NewContext convenience constructor.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/font"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
"golang.org/x/exp/shiny/materialdesign/icons"
|
|
)
|
|
|
|
type Theme struct {
|
|
Shaper text.Shaper
|
|
Color struct {
|
|
Primary color.RGBA
|
|
Text color.RGBA
|
|
Hint color.RGBA
|
|
InvText color.RGBA
|
|
}
|
|
TextSize unit.Value
|
|
checkBoxCheckedIcon *widget.Icon
|
|
checkBoxUncheckedIcon *widget.Icon
|
|
radioCheckedIcon *widget.Icon
|
|
radioUncheckedIcon *widget.Icon
|
|
}
|
|
|
|
func NewTheme() *Theme {
|
|
t := &Theme{
|
|
Shaper: font.Default(),
|
|
}
|
|
t.Color.Primary = rgb(0x3f51b5)
|
|
t.Color.Text = rgb(0x000000)
|
|
t.Color.Hint = rgb(0xbbbbbb)
|
|
t.Color.InvText = rgb(0xffffff)
|
|
t.TextSize = unit.Sp(16)
|
|
|
|
t.checkBoxCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBox))
|
|
t.checkBoxUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
|
t.radioCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked))
|
|
t.radioUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked))
|
|
|
|
return t
|
|
}
|
|
|
|
func mustIcon(ic *widget.Icon, err error) *widget.Icon {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ic
|
|
}
|
|
|
|
func rgb(c uint32) color.RGBA {
|
|
return argb(0xff000000 | c)
|
|
}
|
|
|
|
func argb(c uint32) color.RGBA {
|
|
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
|
|
}
|
|
|
|
func fill(gtx layout.Context, col color.RGBA) layout.Dimensions {
|
|
cs := gtx.Constraints
|
|
d := cs.Min
|
|
dr := f32.Rectangle{
|
|
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
|
}
|
|
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
|
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
|
|
return layout.Dimensions{Size: d}
|
|
}
|