~eliasnaur/gio

ref: 82e51019e1a2dd164927f42066c5746d5bc4d1a0 gio/widget/material/checkbox.go -rw-r--r-- 2.0 KiB
82e51019Elias Naur widget/material: document package 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: Unlicense OR MIT

// Package material implements the Material design.
package material

import (
	"gioui.org/io/pointer"
	"gioui.org/layout"
	"gioui.org/op/paint"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget"
	"image"
	"image/color"
)

type CheckBox struct {
	Text string
	// Color is the text color.
	Color              color.RGBA
	Font               text.Font
	IconColor          color.RGBA
	Size               unit.Value
	shaper             *text.Shaper
	checkedStateIcon   *Icon
	uncheckedStateIcon *Icon
}

func (t *Theme) CheckBox(txt string) CheckBox {
	return CheckBox{
		Text:      txt,
		Color:     t.Color.Text,
		IconColor: t.Color.Primary,
		Font: text.Font{
			Size: t.TextSize.Scale(14.0 / 16.0),
		},
		Size:               unit.Dp(26),
		shaper:             t.Shaper,
		checkedStateIcon:   t.checkedStateIcon,
		uncheckedStateIcon: t.uncheckedStateIcon,
	}
}

func (c CheckBox) Layout(gtx *layout.Context, checkBox *widget.CheckBox) {

	textColor := c.Color
	iconColor := c.IconColor

	var icon *Icon
	if checkBox.Checked(gtx) {
		icon = c.checkedStateIcon
	} else {
		icon = c.uncheckedStateIcon
	}

	hmin := gtx.Constraints.Width.Min
	vmin := gtx.Constraints.Height.Min

	flex := layout.Flex{Alignment: layout.Middle}

	ico := flex.Rigid(gtx, func() {
		layout.Align(layout.Center).Layout(gtx, func() {
			layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
				size := gtx.Px(c.Size)
				icon.Color = iconColor
				icon.Layout(gtx, unit.Px(float32(size)))
				gtx.Dimensions = layout.Dimensions{
					Size: image.Point{X: size, Y: size},
				}
			})
		})
	})

	lbl := flex.Rigid(gtx, func() {
		gtx.Constraints.Width.Min = hmin
		gtx.Constraints.Height.Min = vmin
		layout.Align(layout.Start).Layout(gtx, func() {
			layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
				paint.ColorOp{Color: textColor}.Add(gtx.Ops)
				widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Text)
			})
		})
	})

	flex.Layout(gtx, ico, lbl)
	pointer.RectAreaOp{Rect: image.Rectangle{Max: gtx.Dimensions.Size}}.Add(gtx.Ops)
	checkBox.Layout(gtx)
}