75 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // SPDX-License-Identifier: Unlicense OR MIT
 | |
| 
 | |
| package main
 | |
| 
 | |
| // A simple Gio program. See https://gioui.org for more information.
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"log"
 | |
| 	"time"
 | |
| 
 | |
| 	"gioui.org/ui"
 | |
| 	"gioui.org/ui/app"
 | |
| 	"gioui.org/ui/layout"
 | |
| 	"gioui.org/ui/measure"
 | |
| 	"gioui.org/ui/text"
 | |
| 
 | |
| 	"golang.org/x/image/font/gofont/goregular"
 | |
| 	"golang.org/x/image/font/sfnt"
 | |
| )
 | |
| 
 | |
| func main() {
 | |
| 	go func() {
 | |
| 		w := app.NewWindow()
 | |
| 		if err := loop(w); err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 	}()
 | |
| 	app.Main()
 | |
| }
 | |
| 
 | |
| func loop(w *app.Window) error {
 | |
| 	regular, err := sfnt.Parse(goregular.TTF)
 | |
| 	if err != nil {
 | |
| 		panic("failed to load font")
 | |
| 	}
 | |
| 	var faces measure.Faces
 | |
| 	ops := new(ui.Ops)
 | |
| 	q := w.Queue()
 | |
| 	face := faces.For(regular, ui.Sp(16))
 | |
| 	lst := layout.List{Axis: layout.Vertical}
 | |
| 	times := make([]int64, 100)
 | |
| 	timesIndex := 0
 | |
| 	for {
 | |
| 		e := <-w.Events()
 | |
| 		switch e := e.(type) {
 | |
| 		case app.DestroyEvent:
 | |
| 			return e.Err
 | |
| 		case app.UpdateEvent:
 | |
| 			stime := time.Now()
 | |
| 			c := e.Config
 | |
| 			ops.Reset()
 | |
| 			faces.Reset(&c)
 | |
| 			for lst.Init(&c, q, ops, layout.RigidConstraints(e.Size), 200); lst.More(); lst.Next() {
 | |
| 				lst.End(text.Label{Face: face, Text: fmt.Sprintf("label %d",lst.Index())}.Layout(ops, lst.Constraints()))
 | |
| 			}
 | |
| 			lst.Layout()
 | |
| 			w.Update(ops)
 | |
| 			dur := time.Since(stime)
 | |
| 			//fmt.Printf("Frame time: %s\n", dur)
 | |
| 			times[timesIndex] = dur.Nanoseconds()
 | |
| 			timesIndex++
 | |
| 			if timesIndex == 100 {
 | |
| 				timesIndex = 0
 | |
| 				var avg int64
 | |
| 				for i := 0; i < 100; i++ {
 | |
| 					avg += times[i]
 | |
| 				}
 | |
| 				avg = avg / 100000
 | |
| 				fmt.Printf("Average frame time: %d microseconds\n", avg)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 |