~eliasnaur/gio-example

610c46d683ab2f0ca5669ff7fec5ed7f8d2740a5 — Elias Naur 3 months ago 469723c
customdeco: demonstrate custom decorations

Signed-off-by: Elias Naur <mail@eliasnaur.com>
3 files changed, 78 insertions(+), 3 deletions(-)

A customdeco/main.go
M go.mod
M go.sum
A customdeco/main.go => customdeco/main.go +75 -0
@@ 0,0 1,75 @@
// SPDX-License-Identifier: Unlicense OR MIT

// The customdeco program demonstrates custom decorations
// in Gio.
package main

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

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

	"gioui.org/font/gofont"
)

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

func loop(w *app.Window) error {
	var (
		b    widget.Clickable
		deco widget.Decorations
	)
	var (
		toggle    bool
		decorated bool
		title     string
	)
	th := material.NewTheme(gofont.Collection())
	var ops op.Ops
	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case app.ConfigEvent:
			decorated = e.Config.Decorated
			title = e.Config.Title
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			for b.Clicked() {
				toggle = !toggle
				w.Option(app.Decorated(toggle))
			}
			cl := clip.Rect{Max: e.Size}.Push(gtx.Ops)
			paint.ColorOp{Color: color.NRGBA{A: 0xff, G: 0xff}}.Add(gtx.Ops)
			paint.PaintOp{}.Add(gtx.Ops)
			layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
				return material.Button(th, &b, "Toggle decorations").Layout(gtx)
			})
			cl.Pop()
			if !decorated {
				w.Perform(deco.Actions())
				material.Decorations(th, &deco, ^system.Action(0), title).Layout(gtx)
			}
			e.Frame(gtx.Ops)
		}
	}
}

M go.mod => go.mod +1 -1
@@ 3,7 3,7 @@ module gioui.org/example
go 1.17

require (
	gioui.org v0.0.0-20220607102828-5cf916c0753c
	gioui.org v0.0.0-20220625174046-3f38e67ce0ca
	gioui.org/cmd v0.0.0-20211103131231-1d0769ae89a5
	gioui.org/x v0.0.0-20220606121947-8732bd398e12
	github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d

M go.sum => go.sum +2 -2
@@ 38,8 38,8 @@ eliasnaur.com/font v0.0.0-20220215125817-de715634c840/go.mod h1:OYVuxibdk9OSLX8v
gioui.org v0.0.0-20210910062418-d5d0a75a9bcb/go.mod h1:BTldRXnY5mrUrYZCdWyDwyMzyUzpfZN1cF4MMRrOt9w=
gioui.org v0.0.0-20211103131231-1d0769ae89a5/go.mod h1:yoWOxPng6WkDpsud+NRmkoftmyWn3rkKsYGEcWHpjTI=
gioui.org v0.0.0-20220429101739-35b70e15c9e5/go.mod h1:lR+IVRz1+uf9mN/gD5oj05sr/vb1aSoctAqDRevt90M=
gioui.org v0.0.0-20220607102828-5cf916c0753c h1:NnSe6ebWNvFkRu13Ouo1ENvZRsZBqY1rETAPrRJGRCU=
gioui.org v0.0.0-20220607102828-5cf916c0753c/go.mod h1:WHoHbUjH91BJS2xkfps2AhKxji+9o3xwfsphGsCBfnM=
gioui.org v0.0.0-20220625174046-3f38e67ce0ca h1:rwL1JJOHEgt6/DDO7kUugMBMA3orwRSfBl0woF6sFG0=
gioui.org v0.0.0-20220625174046-3f38e67ce0ca/go.mod h1:WHoHbUjH91BJS2xkfps2AhKxji+9o3xwfsphGsCBfnM=
gioui.org/cmd v0.0.0-20211103131231-1d0769ae89a5 h1:745EIA9yw5pYHXNPS6hv+gX7UKhoQUIR0X4rrhYXaqo=
gioui.org/cmd v0.0.0-20211103131231-1d0769ae89a5/go.mod h1:qrH3h4nt/PyIqx/XabL/eJ5cXQnQ0ERHqC3VEXx/Rmg=
gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=