mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
apps/hello: add hello world example
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
module gioui.org/apps
|
module gioui.org/apps
|
||||||
|
|
||||||
go 1.13
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3
|
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"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() {
|
||||||
|
err := app.CreateWindow(app.WindowOptions{
|
||||||
|
Width: ui.Dp(400),
|
||||||
|
Height: ui.Dp(800),
|
||||||
|
Title: "Hello World",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
app.Main()
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
go func() {
|
||||||
|
for w := range app.Windows() {
|
||||||
|
go loop(w)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func loop(w *app.Window) {
|
||||||
|
regular, err := sfnt.Parse(goregular.TTF)
|
||||||
|
if err != nil {
|
||||||
|
panic("failed to load font")
|
||||||
|
}
|
||||||
|
var faces measure.Faces
|
||||||
|
black := &image.Uniform{color.Black}
|
||||||
|
face := faces.For(regular, ui.Dp(50))
|
||||||
|
for w.IsAlive() {
|
||||||
|
e := <-w.Events()
|
||||||
|
switch e := e.(type) {
|
||||||
|
case app.Draw:
|
||||||
|
faces.Cfg = e.Config
|
||||||
|
cs := layout.ExactConstraints(w.Size())
|
||||||
|
root, _ := (text.Label{Src: black, Face: face, Text: "Hello, World!"}).Layout(cs)
|
||||||
|
w.Draw(root)
|
||||||
|
faces.Frame()
|
||||||
|
}
|
||||||
|
w.Ack()
|
||||||
|
}
|
||||||
|
if w.Err() != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user