forked from loov/lensm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.go
79 lines (70 loc) · 1.48 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
78
79
package main
import (
"fmt"
"image"
"log"
"os"
"sync"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/font/opentype"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"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 := app.NewWindow(
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 {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
widget(gtx)
e.Frame(gtx.Ops)
case system.DestroyEvent:
return e.Err
}
}
}
}
}
func LoadFonts(userfont string) []text.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 := text.Font{Variant: "Mono", Weight: text.Normal}
fface := text.FontFace{Font: fnt, Face: face}
return append(collection, fface)
}