/
window.go
110 lines (95 loc) · 2.05 KB
/
window.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package fui
import (
"gioui.org/app"
"gioui.org/io/event"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/unit"
"math"
"time"
)
type scaledConfig struct {
Scale float32
}
func (s *scaledConfig) Now() time.Time {
return time.Now()
}
func (s *scaledConfig) Px(v unit.Value) int {
scale := s.Scale
if v.U == unit.UnitPx {
scale = 1
}
return int(math.Round(float64(scale * v.V)))
}
type window struct {
w *app.Window
opts []app.Option
scale *scaledConfig
}
// Window creates a new window
func Window() (out *window) {
out = &window{}
return
}
// Title sets the title of the window
func (w *window) Title(title string) (out *window) {
w.opts = append(w.opts, app.Title(title))
return w
}
// Size sets the dimensions of the window
func (w *window) Size(width, height int) (out *window) {
w.opts = append(w.opts,
app.Size(unit.Dp(float32(width)), unit.Dp(float32(height))))
return w
}
// Scale sets the scale factor for rendering
func (w *window) Scale(s float32) *window {
w.scale = &scaledConfig{s}
return w
}
// Set the window options and initialise the app.window
func (w *window) set() (out *window) {
if w.opts != nil {
w.w = app.NewWindow(w.opts...)
w.opts = nil
}
if w.scale == nil {
w.Scale(1)
}
return w
}
// These override app.window methods to ensure the options are set first
// Queue returns the window's event queue
func (w *window) Queue() (out *app.Queue) {
w.set()
return w.w.Queue()
}
// Events returns the channel for events registered with the window
func (w *window) Events() (out <-chan event.Event) {
w.set()
return w.w.Events()
}
// Context for the window
func (w *window) Context() (out *layout.Context) {
w.set()
return layout.NewContext(w.w.Queue())
}
func (w *window) Run(frame func(ctx *layout.Context), destroy func()) {
w.set()
ctx := w.Context()
go func() {
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.DestroyEvent:
destroy()
case system.FrameEvent:
ctx.Reset(w.scale, e.Size)
frame(ctx)
e.Frame(ctx.Ops)
}
}
}
}()
}