~eliasnaur/gio-example

ref: ci gio-example/multiwindow/letters.go -rw-r--r-- 1.5 KiB
bb9a341eElias Naur opengl: don't switch rendering threads 2 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
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"gioui.org/app"
	"gioui.org/layout"
	"gioui.org/widget"
	"gioui.org/widget/material"
)

// Letters displays a clickable list of text items that open a new window.
type Letters struct {
	win *Window
	log *Log

	items []*LetterListItem
	list  widget.List
}

// NewLetters creates a new letters view with the provided log.
func NewLetters(log *Log) *Letters {
	view := &Letters{
		log:  log,
		list: widget.List{List: layout.List{Axis: layout.Vertical}},
	}
	for text := 'a'; text <= 'z'; text++ {
		view.items = append(view.items, &LetterListItem{Text: string(text)})
	}
	return view
}

// Run implements Window.Run method.
func (v *Letters) Run(w *Window) error {
	v.win = w
	return WidgetView(v.Layout).Run(w)
}

// Layout handles drawing the letters view.
func (v *Letters) Layout(gtx layout.Context) layout.Dimensions {
	th := v.win.App.Theme
	return material.List(th, &v.list).Layout(gtx, len(v.items), func(gtx layout.Context, index int) layout.Dimensions {
		item := v.items[index]
		for item.Click.Clicked() {
			v.log.Printf("opening %s view", item.Text)

			bigText := material.H1(th, item.Text)
			size := bigText.TextSize
			size.V *= 2
			v.win.App.NewWindow(item.Text,
				WidgetView(func(gtx layout.Context) layout.Dimensions {
					return layout.Center.Layout(gtx, bigText.Layout)
				}),
				app.Size(size, size),
			)
		}
		return material.Button(th, &item.Click, item.Text).Layout(gtx)
	})
}

type LetterListItem struct {
	Text  string
	Click widget.Clickable
}