example/kitchen: add -screenshot flag

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-12-02 13:51:08 +01:00
parent 6fbdefa21f
commit 9023b1b865
3 changed files with 43 additions and 3 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ module gioui.org/example
go 1.13
require (
gioui.org v0.0.0-20191202121315-11506a974e90
gioui.org v0.0.0-20191202125934-6fbdefa21f7c
github.com/google/go-github/v24 v24.0.1
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
+2 -2
View File
@@ -1,7 +1,7 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20191202121315-11506a974e90 h1:+S/bkhXOSFVrqiCmepTFQ7h2ooD8unj+iX1yZfEOGX4=
gioui.org v0.0.0-20191202121315-11506a974e90/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
gioui.org v0.0.0-20191202125934-6fbdefa21f7c h1:iOmAu0il5DOd3IrrIPGk/h1ZEaU2ngZEfBnYaUrd7i8=
gioui.org v0.0.0-20191202125934-6fbdefa21f7c/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
+40
View File
@@ -5,10 +5,18 @@ package main
// A Gio program that demonstrates Gio widgets. See https://gioui.org for more information.
import (
"bytes"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
"gioui.org/app"
"gioui.org/app/headless"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/text"
@@ -21,7 +29,10 @@ import (
"gioui.org/font/gofont"
)
var screenshot = flag.String("screenshot", "", "save a screenshot to a file and exit")
func main() {
flag.Parse()
editor.SetText(longText)
ic, err := material.NewIcon(icons.ContentAdd)
if err != nil {
@@ -29,6 +40,13 @@ func main() {
}
icon = ic
gofont.Register()
if *screenshot != "" {
if err := saveScreenshot(*screenshot); err != nil {
fmt.Fprintf(os.Stderr, "failed to save screenshot: %v", err)
os.Exit(1)
}
os.Exit(0)
}
go func() {
w := app.NewWindow()
if err := loop(w); err != nil {
@@ -38,6 +56,28 @@ func main() {
app.Main()
}
func saveScreenshot(f string) error {
sz := image.Point{X: 800, Y: 600}
w, err := headless.NewWindow(sz.X, sz.Y)
if err != nil {
return err
}
gtx := new(layout.Context)
gtx.Reset(nil, sz)
th := material.NewTheme()
kitchen(gtx, th)
w.Frame(gtx.Ops)
img, err := w.Screenshot()
if err != nil {
return err
}
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
return err
}
return ioutil.WriteFile(f, buf.Bytes(), 0666)
}
func loop(w *app.Window) error {
th := material.NewTheme()
gtx := layout.NewContext(w.Queue())