github.com/utopiagio/gio@v0.0.8/widget/material/theme.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package material 4 5 import ( 6 "image/color" 7 8 "golang.org/x/exp/shiny/materialdesign/icons" 9 10 "github.com/utopiagio/gio/font" 11 "github.com/utopiagio/gio/text" 12 "github.com/utopiagio/gio/unit" 13 "github.com/utopiagio/gio/widget" 14 ) 15 16 // Palette contains the minimal set of colors that a widget may need to 17 // draw itself. 18 type Palette struct { 19 // Bg is the background color atop which content is currently being 20 // drawn. 21 Bg color.NRGBA 22 23 // Fg is a color suitable for drawing on top of Bg. 24 Fg color.NRGBA 25 26 // ContrastBg is a color used to draw attention to active, 27 // important, interactive widgets such as buttons. 28 ContrastBg color.NRGBA 29 30 // ContrastFg is a color suitable for content drawn on top of 31 // ContrastBg. 32 ContrastFg color.NRGBA 33 } 34 35 type Theme struct { 36 Shaper *text.Shaper 37 Palette 38 TextSize unit.Sp 39 Icon struct { 40 CheckBoxChecked *widget.Icon 41 CheckBoxUnchecked *widget.Icon 42 RadioChecked *widget.Icon 43 RadioUnchecked *widget.Icon 44 } 45 // Face selects the default typeface for text. 46 Face font.Typeface 47 48 // FingerSize is the minimum touch target size. 49 FingerSize unit.Dp 50 } 51 52 // NewTheme constructs a theme (and underlying text shaper). 53 func NewTheme() *Theme { 54 t := &Theme{Shaper: &text.Shaper{}} 55 t.Palette = Palette{ 56 Fg: rgb(0x000000), 57 Bg: rgb(0xffffff), 58 ContrastBg: rgb(0x3f51b5), 59 ContrastFg: rgb(0xffffff), 60 } 61 t.TextSize = 16 62 63 t.Icon.CheckBoxChecked = mustIcon(widget.NewIcon(icons.ToggleCheckBox)) 64 t.Icon.CheckBoxUnchecked = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank)) 65 t.Icon.RadioChecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked)) 66 t.Icon.RadioUnchecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked)) 67 68 // 38dp is on the lower end of possible finger size. 69 t.FingerSize = 38 70 71 return t 72 } 73 74 func (t Theme) WithPalette(p Palette) Theme { 75 t.Palette = p 76 return t 77 } 78 79 func mustIcon(ic *widget.Icon, err error) *widget.Icon { 80 if err != nil { 81 panic(err) 82 } 83 return ic 84 } 85 86 func rgb(c uint32) color.NRGBA { 87 return argb(0xff000000 | c) 88 } 89 90 func argb(c uint32) color.NRGBA { 91 return color.NRGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)} 92 }