gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/widget/material/checkbox.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package material 4 5 import ( 6 "gioui.org/io/semantic" 7 "gioui.org/layout" 8 "gioui.org/widget" 9 ) 10 11 type CheckBoxStyle struct { 12 checkable 13 CheckBox *widget.Bool 14 } 15 16 func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle { 17 c := CheckBoxStyle{ 18 CheckBox: checkBox, 19 checkable: checkable{ 20 Label: label, 21 Color: th.Palette.Fg, 22 IconColor: th.Palette.ContrastBg, 23 TextSize: th.TextSize * 14.0 / 16.0, 24 Size: 26, 25 shaper: th.Shaper, 26 checkedStateIcon: th.Icon.CheckBoxChecked, 27 uncheckedStateIcon: th.Icon.CheckBoxUnchecked, 28 }, 29 } 30 c.checkable.Font.Typeface = th.Face 31 return c 32 } 33 34 // Layout updates the checkBox and displays it. 35 func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions { 36 return c.CheckBox.Layout(gtx, func(gtx layout.Context) layout.Dimensions { 37 semantic.CheckBox.Add(gtx.Ops) 38 return c.layout(gtx, c.CheckBox.Value, c.CheckBox.Hovered() || gtx.Focused(c.CheckBox)) 39 }) 40 }