-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
269 lines (244 loc) · 6.19 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"gioui.org/app"
"gioui.org/f32"
"gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/x/profiling"
"git.sr.ht/~whereswaldon/sprig/core"
sprigTheme "git.sr.ht/~whereswaldon/sprig/widget/theme"
"github.com/inkeliz/giohyperlink"
"github.com/pkg/profile"
)
type (
C = layout.Context
D = layout.Dimensions
)
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
go func() {
w := app.NewWindow(app.Title("Sprig"))
if err := eventLoop(w); err != nil {
log.Fatalf("exiting due to error: %v", err)
}
os.Exit(0)
}()
app.Main()
}
func eventLoop(w *app.Window) error {
var (
dataDir string
invalidate bool
profileOpt string
)
dataDir, err := getDataDir("sprig")
if err != nil {
log.Printf("finding application data dir: %v", err)
}
flag.StringVar(&profileOpt, "profile", "none", "create the provided kind of profile. Use one of [none, cpu, mem, block, goroutine, mutex, trace, gio]")
flag.BoolVar(&invalidate, "invalidate", false, "invalidate every single frame, only useful for profiling")
flag.StringVar(&dataDir, "data-dir", dataDir, "application state directory")
flag.Parse()
profiler := ProfileOpt(profileOpt).NewProfiler()
profiler.Start()
defer profiler.Stop()
app, err := core.NewApp(w, dataDir)
if err != nil {
log.Fatalf("Failed initializing application: %v", err)
}
go app.Arbor().StartHeartbeat()
// handle ctrl+c to shutdown
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
vm := NewViewManager(w, app)
vm.ApplySettings(app.Settings())
vm.RegisterView(ReplyViewID, NewReplyListView(app))
vm.RegisterView(ConnectFormID, NewConnectFormView(app))
vm.RegisterView(SubscriptionViewID, NewSubscriptionView(app))
vm.RegisterView(SettingsID, NewCommunityMenuView(app))
vm.RegisterView(IdentityFormID, NewIdentityFormView(app))
vm.RegisterView(ConsentViewID, NewConsentView(app))
vm.RegisterView(SubscriptionSetupFormViewID, NewSubSetupFormView(app))
vm.RegisterView(DynamicChatViewID, NewDynamicChatView(app))
if app.Settings().AcknowledgedNoticeVersion() < NoticeVersion {
vm.SetView(ConsentViewID)
} else if app.Settings().Address() == "" {
vm.SetView(ConnectFormID)
} else if app.Settings().ActiveArborIdentityID() == nil {
vm.SetView(IdentityFormID)
} else if len(app.Settings().Subscriptions()) < 1 {
vm.SetView(SubscriptionSetupFormViewID)
} else {
vm.SetView(ReplyViewID)
}
go func() {
<-sigs
w.Perform(system.ActionClose)
}()
var ops op.Ops
for {
ev := w.NextEvent()
giohyperlink.ListenEvents(ev)
switch event := ev.(type) {
case system.DestroyEvent:
app.Shutdown()
return event.Err
case system.FrameEvent:
gtx := layout.NewContext(&ops, event)
if profiler.Recorder != nil {
profiler.Record(gtx)
}
if invalidate {
op.InvalidateOp{}.Add(gtx.Ops)
}
for _, event := range gtx.Events(w) {
if ke, ok := event.(key.Event); ok && ke.Name == key.NameBack {
vm.HandleBackNavigation()
}
}
key.InputOp{
Tag: w,
Keys: key.Set(key.NameBack),
}.Add(gtx.Ops)
th := app.Theme().Current()
layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return sprigTheme.Rect{
Color: th.Background.Dark.Bg,
Size: f32.Point{
X: float32(gtx.Constraints.Max.X),
Y: float32(gtx.Constraints.Max.Y),
},
}.Layout(gtx)
}),
layout.Stacked(func(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return sprigTheme.Rect{
Color: th.Background.Dark.Bg,
Size: f32.Point{
X: float32(gtx.Constraints.Max.X),
Y: float32(gtx.Constraints.Max.Y),
},
}.Layout(gtx)
}),
layout.Stacked(vm.Layout),
)
}),
)
event.Frame(gtx.Ops)
default:
ProcessPlatformEvent(app, event)
}
}
}
type ViewID int
const (
ConnectFormID ViewID = iota
IdentityFormID
SettingsID
ReplyViewID
ConsentViewID
SubscriptionViewID
SubscriptionSetupFormViewID
DynamicChatViewID
)
// getDataDir returns application specific file directory to use for storage.
// Suffix is joined to the path for convenience.
func getDataDir(suffix string) (string, error) {
d, err := app.DataDir()
if err != nil {
return "", err
}
return filepath.Join(d, suffix), nil
}
// Profiler unifies the profiling api between Gio profiler and pkg/profile.
type Profiler struct {
Starter func(p *profile.Profile)
Stopper func()
Recorder func(gtx C)
}
// Start profiling.
func (pfn *Profiler) Start() {
if pfn.Starter != nil {
pfn.Stopper = profile.Start(pfn.Starter).Stop
}
}
// Stop profiling.
func (pfn *Profiler) Stop() {
if pfn.Stopper != nil {
pfn.Stopper()
}
}
// Record GUI stats per frame.
func (pfn Profiler) Record(gtx C) {
if pfn.Recorder != nil {
pfn.Recorder(gtx)
}
}
// ProfileOpt specifies the various profiling options.
type ProfileOpt string
const (
None ProfileOpt = "none"
CPU ProfileOpt = "cpu"
Memory ProfileOpt = "mem"
Block ProfileOpt = "block"
Goroutine ProfileOpt = "goroutine"
Mutex ProfileOpt = "mutex"
Trace ProfileOpt = "trace"
Gio ProfileOpt = "gio"
)
// NewProfiler creates a profiler based on the selected option.
func (p ProfileOpt) NewProfiler() Profiler {
switch p {
case "", None:
return Profiler{}
case CPU:
return Profiler{Starter: profile.CPUProfile}
case Memory:
return Profiler{Starter: profile.MemProfile}
case Block:
return Profiler{Starter: profile.BlockProfile}
case Goroutine:
return Profiler{Starter: profile.GoroutineProfile}
case Mutex:
return Profiler{Starter: profile.MutexProfile}
case Trace:
return Profiler{Starter: profile.TraceProfile}
case Gio:
var (
recorder *profiling.CSVTimingRecorder
err error
)
return Profiler{
Starter: func(*profile.Profile) {
recorder, err = profiling.NewRecorder(nil)
if err != nil {
log.Printf("starting profiler: %v", err)
}
},
Stopper: func() {
if recorder == nil {
return
}
if err := recorder.Stop(); err != nil {
log.Printf("stopping profiler: %v", err)
}
},
Recorder: func(gtx C) {
if recorder == nil {
return
}
recorder.Profile(gtx)
},
}
}
return Profiler{}
}