-
Notifications
You must be signed in to change notification settings - Fork 124
/
windows.go
77 lines (68 loc) · 1.46 KB
/
windows.go
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
package main
import (
"fmt"
"image"
"log"
"os"
"sync"
"gioui.org/app"
"gioui.org/font"
"gioui.org/font/gofont"
"gioui.org/font/opentype"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
)
type Windows struct {
active sync.WaitGroup
}
func (windows *Windows) Open(title string, sizeDp image.Point, run func(*app.Window) error) {
windows.active.Add(1)
go func() {
defer windows.active.Done()
window := new(app.Window)
window.Option(
app.Title(title),
app.Size(unit.Dp(sizeDp.X), unit.Dp(sizeDp.Y)),
)
if err := run(window); err != nil {
log.Println(err)
}
}()
}
func (windows *Windows) Wait() {
windows.active.Wait()
}
func WidgetWindow(widget layout.Widget) func(*app.Window) error {
return func(w *app.Window) error {
var ops op.Ops
for {
e := w.Event()
switch e := e.(type) {
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
widget(gtx)
e.Frame(gtx.Ops)
case app.DestroyEvent:
return e.Err
}
}
}
}
func LoadFonts(userfont string) []font.FontFace {
collection := gofont.Collection()
if userfont == "" {
return collection
}
b, err := os.ReadFile(userfont)
if err != nil {
panic(fmt.Errorf("failed to parse font: %v", err))
}
face, err := opentype.Parse(b)
if err != nil {
panic(fmt.Errorf("failed to parse font: %v", err))
}
fnt := font.Font{Typeface: "override-monospace,monospace", Weight: font.Normal}
fface := font.FontFace{Font: fnt, Face: face}
return append(collection, fface)
}