~eliasnaur/gio-example

ref: 112655e32697e0e4efdd57e1006be4f6bc4c8176 gio-example/livedit/livedit.go -rw-r--r-- 6.4 KiB
112655e3Chris Waldon livedit: improve visual quality of demo 4 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-License-Identifier: Unlicense OR MIT

package main

// A simple Gio program. See https://gioui.org for more information.

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

	"gioui.org/app"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/paint"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"

	"gioui.org/font/gofont"

	"gioui.org/example/livedit/giopkgs"
	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"

	"golang.org/x/image/draw"
	_ "image/png"
)

func main() {
	go func() {
		w := app.NewWindow(app.Title("Gio Playground"))
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

type (
	C = layout.Context
	D = layout.Dimensions
)

func loadImage(name string) image.Image {
	file, err := os.Open(name)
	if err != nil {
		log.Println(err)
		return nil
	}
	defer file.Close()
	img, _, err := image.Decode(file)
	if err != nil {
		log.Println(err)
		return nil
	}
	return img
}

func imageOpOrEmpty(src image.Image) paint.ImageOp {
	if src == nil {
		return paint.ImageOp{}
	}
	return paint.NewImageOp(src)
}

var (
	yaegiLogoImg = loadImage("yaegi.png")
	yaegiLogoOp  = imageOpOrEmpty(yaegiLogoImg)
	gioLogoImg   = loadImage("gio.png")
	gioLogoOp    = imageOpOrEmpty(gioLogoImg)
)

const startingText = `package live

import (
    "gioui.org/layout"
    "gioui.org/widget/material"
    "gioui.org/op"
    "gioui.org/f32"
    "math"
    "image"
)

// store the current rotation offset between frames
var rotation float32

func Layout(gtx layout.Context, theme *material.Theme) layout.Dimensions {
    // Try changing the denomenator of this fraction!
    rotation += math.Pi/50

    // Compute the center of the available area.
    origin := layout.FPt(image.Pt(gtx.Constraints.Max.X/2,gtx.Constraints.Max.Y/2))

    // Spin our drawing around the center.
    op.Affine(f32.Affine2D{}.Rotate(origin, float32(math.Sin(float64(rotation))))).Add(gtx.Ops)

    // Ensure we draw another frame after this one so that animation is smooth.
    op.InvalidateOp{}.Add(gtx.Ops)

    // Draw a word to have something visibly animated.
    return layout.Center.Layout(gtx, func (gtx layout.Context) layout.Dimensions {
        return material.H1(theme, "Hello!").Layout(gtx)
    })
}

`

func squareLogo(gtx C, src image.Image, imgOp *paint.ImageOp) D {
	if src == nil {
		return D{}
	}
	px := gtx.Constraints.Max.Y
	if gtx.Constraints.Max.X < gtx.Constraints.Max.Y {
		px = gtx.Constraints.Max.X
	}
	dps := float32(px) / gtx.Metric.PxPerDp
	scale := dps / float32(imgOp.Size().Y)
	if px != imgOp.Size().X {
		img := image.NewRGBA(image.Rectangle{Max: image.Point{X: px, Y: px}})
		draw.ApproxBiLinear.Scale(img, img.Bounds(), src, src.Bounds(), draw.Src, nil)
		*imgOp = paint.NewImageOp(img)
	}
	return widget.Image{
		Src:   *imgOp,
		Scale: scale,
	}.Layout(gtx)
}

func layoutLogos(gtx C, th *material.Theme) D {
	return layout.Flex{
		Axis:      layout.Vertical,
		Alignment: layout.Middle,
	}.Layout(gtx,
		layout.Rigid(func(gtx C) D {
			return layout.Center.Layout(gtx, func(gtx C) D {
				return material.Body1(th, "Powered by").Layout(gtx)
			})
		}),
		layout.Flexed(1, func(gtx C) D {
			return layout.Flex{
				Spacing:   layout.SpaceAround,
				Alignment: layout.Middle,
			}.Layout(gtx,
				layout.Flexed(.5, func(gtx C) D {
					return layout.Center.Layout(gtx, func(gtx C) D {
						return squareLogo(gtx, gioLogoImg, &gioLogoOp)
					})
				}),
				layout.Rigid(func(gtx C) D {
					return layout.Center.Layout(gtx, func(gtx C) D {
						return material.H3(th, "+").Layout(gtx)
					})
				}),
				layout.Flexed(.5, func(gtx C) D {
					return layout.Center.Layout(gtx, func(gtx C) D {
						return squareLogo(gtx, yaegiLogoImg, &yaegiLogoOp)
					})
				}),
			)
		}),
	)
}

func containsChange(events []widget.EditorEvent) bool {
	for _, e := range events {
		switch e.(type) {
		case widget.ChangeEvent:
			return true
		}
	}
	return false
}

func loop(w *app.Window) error {
	th := material.NewTheme(gofont.Collection())
	var editor widget.Editor
	editor.SetText(startingText)
	var ops op.Ops
	var yaegi *interp.Interpreter

	first := true
	maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
	var (
		custom func(C, *material.Theme) D
		err    error
	)

	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			if containsChange(editor.Events()) || first {
				yaegi = interp.New(interp.Options{})
				yaegi.Use(stdlib.Symbols)
				yaegi.Use(interp.Symbols)
				yaegi.Use(giopkgs.Symbols)
				func() {
					_, err = yaegi.Eval(editor.Text())
					if err != nil {
						log.Println(err)
						return
					}
					var result reflect.Value
					result, err = yaegi.Eval("live.Layout")
					if err != nil {
						log.Println(err)
						return
					}
					var (
						ok        bool
						newCustom func(C, *material.Theme) D
					)
					newCustom, ok = result.Interface().(func(layout.Context, *material.Theme) layout.Dimensions)
					if !ok {
						err = fmt.Errorf("returned data is not a widget, is %s", result.Type())
						log.Println(err)
						return
					}
					custom = newCustom
				}()
			}

			layout.Flex{}.Layout(gtx,
				layout.Flexed(.5, func(gtx C) D {
					inset := layout.UniformInset(unit.Dp(4))
					return inset.Layout(gtx, func(gtx C) D {
						return widget.Border{
							Width: unit.Dp(2),
							Color: th.Fg,
						}.Layout(gtx, func(gtx C) D {
							return inset.Layout(gtx, func(gtx C) D {
								ed := material.Editor(th, &editor, "write layout code here")
								ed.Font.Variant = "Mono"
								return ed.Layout(gtx)
							})
						})
					})

				}),
				layout.Flexed(.5, func(gtx C) D {
					return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
						layout.Rigid(func(gtx C) D {
							if err != nil {
								msg := material.Body1(th, err.Error())
								msg.Color = maroon
								return msg.Layout(gtx)
							}
							return D{}
						}),
						layout.Flexed(.7, func(gtx C) (dims D) {
							defer func() {
								if err := recover(); err != nil {
									msg := material.Body1(th, "panic: "+err.(error).Error())
									msg.Color = maroon
									dims = msg.Layout(gtx)
								}
							}()
							if custom == nil {
								msg := material.Body1(th, "nil")
								msg.Color = maroon
								return msg.Layout(gtx)
							}
							return custom(gtx, th)
						}),
						layout.Flexed(.3, func(gtx C) D {
							return layoutLogos(gtx, th)
						}),
					)
				}),
			)

			e.Frame(gtx.Ops)
			first = false
		}
	}
}