mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
7256bdae38
Avoids flickering between GPU content and the CPU drawn background color. gofmt -w -s . now we're here. Signed-off-by: Elias Naur <mail@eliasnaur.com>
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/font"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"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 *Icon
|
|
checkBoxUncheckedIcon *Icon
|
|
radioCheckedIcon *Icon
|
|
radioUncheckedIcon *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(NewIcon(icons.ToggleCheckBox))
|
|
t.checkBoxUncheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
|
t.radioCheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonChecked))
|
|
t.radioUncheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonUnchecked))
|
|
|
|
return t
|
|
}
|
|
|
|
func mustIcon(ic *Icon, err error) *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) {
|
|
cs := gtx.Constraints
|
|
d := image.Point{X: cs.Width.Min, Y: cs.Height.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)
|
|
gtx.Dimensions = layout.Dimensions{Size: d}
|
|
}
|
|
|
|
// https://pomax.github.io/bezierinfo/#circles_cubic.
|
|
func rrect(ops *op.Ops, width, height, se, sw, nw, ne float32) {
|
|
w, h := float32(width), float32(height)
|
|
const c = 0.55228475 // 4*(sqrt(2)-1)/3
|
|
var b paint.Path
|
|
b.Begin(ops)
|
|
b.Move(f32.Point{X: w, Y: h - se})
|
|
b.Cube(f32.Point{X: 0, Y: se * c}, f32.Point{X: -se + se*c, Y: se}, f32.Point{X: -se, Y: se}) // SE
|
|
b.Line(f32.Point{X: sw - w + se, Y: 0})
|
|
b.Cube(f32.Point{X: -sw * c, Y: 0}, f32.Point{X: -sw, Y: -sw + sw*c}, f32.Point{X: -sw, Y: -sw}) // SW
|
|
b.Line(f32.Point{X: 0, Y: nw - h + sw})
|
|
b.Cube(f32.Point{X: 0, Y: -nw * c}, f32.Point{X: nw - nw*c, Y: -nw}, f32.Point{X: nw, Y: -nw}) // NW
|
|
b.Line(f32.Point{X: w - ne - nw, Y: 0})
|
|
b.Cube(f32.Point{X: ne * c, Y: 0}, f32.Point{X: ne, Y: ne - ne*c}, f32.Point{X: ne, Y: ne}) // NE
|
|
b.End().Add(ops)
|
|
}
|