~eliasnaur/gio-example

ref: 914fd8dc60a3 gio-example/x/outlay/table/main.go -rw-r--r-- 3.8 KiB
914fd8dcChris Waldon deps: update to latest gioui.org/x a month 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main

import (
	"fmt"
	"image"
	"image/color"
	"log"
	"os"
	"strconv"

	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"gioui.org/x/outlay"
)

func main() {
	go func() {
		w := app.NewWindow(
			app.Size(unit.Dp(800), unit.Dp(400)),
			app.Title("Gio layouts"),
		)
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func loop(w *app.Window) error {
	var table = uiTable{
		theme: material.NewTheme(gofont.Collection()),
		Table: outlay.Table{
			CellSize: func(m unit.Metric, x, y int) image.Point {
				return image.Pt(m.Px(unit.Dp(50)), m.Px(unit.Dp(30)))
			},
		},
	}

	var ops op.Ops
	for e := range w.Events() {
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err

		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			table.Layout(gtx)
			e.Frame(gtx.Ops)
		}
	}
	return nil
}

type uiTable struct {
	theme    *material.Theme
	xed, yed widget.Editor
	cells    []cell
	outlay.Table
}

type cell struct {
	click   widget.Clickable
	clicked bool
}

func (t *uiTable) Layout(gtx layout.Context) layout.Dimensions {
	th := t.theme
	return layout.Flex{
		Axis: layout.Vertical,
	}.Layout(gtx,
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			return layout.Flex{
				Axis:    layout.Horizontal,
				Spacing: layout.SpaceSides,
			}.Layout(gtx,
				layout.Rigid(material.Body1(th, "Number of columns: ").Layout),
				layout.Rigid(material.Editor(th, &t.xed, "columns").Layout),
			)
		}),
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			return layout.Flex{
				Axis:    layout.Horizontal,
				Spacing: layout.SpaceSides,
			}.Layout(gtx,
				layout.Rigid(material.Body1(th, "Number of rows: ").Layout),
				layout.Rigid(material.Editor(th, &t.yed, "rows").Layout),
			)
		}),
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			return layout.Flex{
				Axis:    layout.Horizontal,
				Spacing: layout.SpaceSides,
			}.Layout(gtx,
				layout.Rigid(func(gtx layout.Context) layout.Dimensions {
					var selected int
					for i := range t.cells {
						if t.cells[i].clicked {
							selected++
						}
					}
					var txt string
					switch selected {
					case 0:
						txt = "Click cells to select them."
					case 1:
						txt = fmt.Sprint("1 cell selected. Click again to unselect.")
					default:
						txt = fmt.Sprintf("%d cells selected. Click again to unselect.", selected)
					}
					return material.Body1(th, txt).Layout(gtx)
				}),
			)
		}),
		layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
			xn, _ := strconv.Atoi(t.xed.Text())
			yn, _ := strconv.Atoi(t.yed.Text())
			t.cells = growCells(t.cells, xn*yn)
			return t.Table.Layout(gtx, xn, yn, func(gtx layout.Context, x, y int) layout.Dimensions {
				c := &t.cells[x+y*xn]
				return c.Layout(gtx, th, x, y)
			})
		}),
	)
}

func (c *cell) Layout(gtx layout.Context, th *material.Theme, x, y int) layout.Dimensions {
	defer op.Save(gtx.Ops).Load()
	var txt string
	if y < 0 {
		txt = fmt.Sprintf("item %d", x)
	} else {
		txt = fmt.Sprintf("%dx%d", x, y)
	}
	macro := op.Record(gtx.Ops)
	dims := material.Clickable(gtx, &c.click, func(gtx layout.Context) layout.Dimensions {
		return layout.Center.Layout(gtx, material.Body1(th, txt).Layout)
	})
	call := macro.Stop()

	if c.click.Clicked() {
		c.clicked = !c.clicked
	}
	clip.Rect{Max: dims.Size}.Add(gtx.Ops)
	col := color.NRGBA{R: 255, G: 255, B: 255, A: 255}
	if c.clicked {
		col = color.NRGBA{R: 128, G: 128, B: 128, A: 255}
	}
	paint.Fill(gtx.Ops, col)
	call.Add(gtx.Ops)
	return dims
}

func growCells(cells []cell, n int) []cell {
	if len(cells) < n {
		cells = append(cells, make([]cell, n-len(cells))...)
	}
	return cells[:n]
}