github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/widget/material/theme.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package material 4 5 import ( 6 "image" 7 "image/color" 8 9 "github.com/gop9/olt/gio/f32" 10 "github.com/gop9/olt/gio/font" 11 "github.com/gop9/olt/gio/layout" 12 "github.com/gop9/olt/gio/op/paint" 13 "github.com/gop9/olt/gio/text" 14 "github.com/gop9/olt/gio/unit" 15 "golang.org/x/exp/shiny/materialdesign/icons" 16 ) 17 18 type Theme struct { 19 Shaper *text.Shaper 20 Color struct { 21 Primary color.RGBA 22 Text color.RGBA 23 Hint color.RGBA 24 InvText color.RGBA 25 } 26 TextSize unit.Value 27 checkBoxCheckedIcon *Icon 28 checkBoxUncheckedIcon *Icon 29 radioCheckedIcon *Icon 30 radioUncheckedIcon *Icon 31 } 32 33 func NewTheme() *Theme { 34 t := &Theme{ 35 Shaper: font.Default(), 36 } 37 t.Color.Primary = rgb(0x3f51b5) 38 t.Color.Text = rgb(0x000000) 39 t.Color.Hint = rgb(0xbbbbbb) 40 t.Color.InvText = rgb(0xffffff) 41 t.TextSize = unit.Sp(16) 42 43 t.checkBoxCheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBox)) 44 t.checkBoxUncheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBoxOutlineBlank)) 45 t.radioCheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonChecked)) 46 t.radioUncheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonUnchecked)) 47 48 return t 49 } 50 51 func mustIcon(ic *Icon, err error) *Icon { 52 if err != nil { 53 panic(err) 54 } 55 return ic 56 } 57 58 func rgb(c uint32) color.RGBA { 59 return argb(0xff000000 | c) 60 } 61 62 func argb(c uint32) color.RGBA { 63 return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)} 64 } 65 66 func fill(gtx *layout.Context, col color.RGBA) { 67 cs := gtx.Constraints 68 d := image.Point{X: cs.Width.Min, Y: cs.Height.Min} 69 dr := f32.Rectangle{ 70 Max: f32.Point{X: float32(d.X), Y: float32(d.Y)}, 71 } 72 paint.ColorOp{Color: col}.Add(gtx.Ops) 73 paint.PaintOp{Rect: dr}.Add(gtx.Ops) 74 gtx.Dimensions = layout.Dimensions{Size: d} 75 }