-
Notifications
You must be signed in to change notification settings - Fork 1
/
custosworld.go
391 lines (331 loc) · 14.7 KB
/
custosworld.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package custosworld
import (
"log"
"os"
"sort"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"github.com/mrjrieke/nute/mashupsdk"
"github.com/mrjrieke/nute/mashupsdk/client"
"github.com/mrjrieke/nute/mashupsdk/guiboot"
"github.com/mrjrieke/nute/mashupsdk/server"
)
type mashupSdkApiHandler struct {
}
type worldClientInitHandler struct {
}
type ICustosRenderer interface {
OnSelected(tabItem *container.TabItem)
}
type fyneMashupApiHandler struct {
}
type CustosContext struct {
mashupContext *mashupsdk.MashupContext // Needed for callbacks to other mashups
}
type FyneWidgetBundle struct {
mashupsdk.GuiWidgetBundle
}
func (fwb *FyneWidgetBundle) OnStatusChanged() {
if CUWorldApp.HeadsupFyneContext.mashupContext == nil {
return
}
selectedDetailedElement := fwb.MashupDetailedElement
if selectedDetailedElement != nil {
if selectedDetailedElement.IsStateSet(mashupsdk.SourceExternal) {
// Avoid infinite feedback...
CUWorldApp.MashupDetailedElementLibrary[selectedDetailedElement.Id].ApplyState(mashupsdk.SourceExternal, false)
return
}
elementStateBundle := mashupsdk.MashupElementStateBundle{
AuthToken: server.GetServerAuthToken(),
ElementStates: []*mashupsdk.MashupElementState{selectedDetailedElement.State},
}
CUWorldApp.HeadsupFyneContext.mashupContext.Client.ResetStates(CUWorldApp.HeadsupFyneContext.mashupContext, &mashupsdk.MashupEmpty{AuthToken: server.GetServerAuthToken()})
log.Printf("Status Changed: display fields set to: %d", selectedDetailedElement.State.State)
CUWorldApp.HeadsupFyneContext.mashupContext.Client.TweakStates(CUWorldApp.HeadsupFyneContext.mashupContext, &elementStateBundle)
log.Printf("Finished status change.\n")
}
}
type ITabItemRenderer interface {
GetPriority() int64
BuildTabItem(id int64, concreteElement *mashupsdk.MashupDetailedElement)
PreRender() // Called at end of all tab item renders.
RenderTabItem(concreteElement *mashupsdk.MashupDetailedElement)
Refresh() // Called at end of all tab item renders.
}
type CustosWorldApp struct {
Headless bool // Mode for troubleshooting.
Titlebar bool // With a title bar
mashupSdkApiHandler *mashupSdkApiHandler
Title string
Icon *fyne.StaticResource
MainWindowSize fyne.Size
wClientInitHandler *worldClientInitHandler
HeadsupFyneContext *CustosContext
MainWin fyne.Window
mashupDisplayContext *mashupsdk.MashupDisplayContext
DetailedElements []*mashupsdk.MashupDetailedElement
MashupDetailedElementLibrary map[int64]*mashupsdk.MashupDetailedElement
ElementLoaderIndex map[string]int64 // mashup indexes by Name
FyneWidgetElements map[string]*FyneWidgetBundle
TabItemMenu *container.AppTabs
CustomTabItems map[string]func(custosWorlApp *CustosWorldApp, id string) *container.TabItem
CustomTabItemRenderer map[string]ITabItemRenderer
CustosRenderer ICustosRenderer
}
var CUWorldApp *CustosWorldApp
func (w *CustosWorldApp) InitServer(callerCreds string, insecure bool, maxMessageLength int) {
if callerCreds != "" {
server.InitServer(callerCreds, insecure, maxMessageLength, w.mashupSdkApiHandler, w.wClientInitHandler)
} else {
// TODO: These might not make sense in Custos.
// go func() {
// w.displaySetupChan <- &mashupsdk.MashupDisplayHint{Xpos: 0, Ypos: 0, Width: 400, Height: 800}
// }()
}
}
func NewCustosWorldApp(headless bool,
titlebar bool,
detailedElements []*mashupsdk.MashupDetailedElement,
renderer ICustosRenderer) *CustosWorldApp {
CUWorldApp = &CustosWorldApp{
Headless: headless,
Titlebar: titlebar,
mashupSdkApiHandler: &mashupSdkApiHandler{},
HeadsupFyneContext: &CustosContext{},
DetailedElements: detailedElements,
MainWin: nil,
mashupDisplayContext: &mashupsdk.MashupDisplayContext{MainWinDisplay: &mashupsdk.MashupDisplayHint{}},
MashupDetailedElementLibrary: map[int64]*mashupsdk.MashupDetailedElement{}, // mashupDetailedElementLibrary,
ElementLoaderIndex: map[string]int64{}, // elementLoaderIndex
FyneWidgetElements: map[string]*FyneWidgetBundle{},
CustomTabItems: map[string]func(custosWorlApp *CustosWorldApp, id string) *container.TabItem{},
CustomTabItemRenderer: map[string]ITabItemRenderer{},
CustosRenderer: renderer,
}
return CUWorldApp
}
type InitEvent struct {
}
func (w *CustosWorldApp) ResetChangeStates() []*mashupsdk.MashupElementState {
changedElements := []*mashupsdk.MashupElementState{}
for _, g3nDetailedElement := range w.MashupDetailedElementLibrary {
if mashupsdk.DisplayElementState(g3nDetailedElement.GetMashupElementState().State) != mashupsdk.Init {
g3nDetailedElement.ApplyState(mashupsdk.Clicked, false)
changedElements = append(changedElements, g3nDetailedElement.GetMashupElementState())
}
}
return changedElements
}
func (w *CustosWorldApp) InitMainWindow() {
log.Printf("Initializing MainWin.")
initHandler := func(a fyne.App) {
log.Printf("InitHandler.")
drv := a.Driver()
if drv, ok := drv.(desktop.Driver); ok && !fyne.CurrentDevice().IsMobile() && !w.Titlebar {
CUWorldApp.MainWin = drv.CreateSplashWindow(false)
} else {
CUWorldApp.MainWin = a.NewWindow(w.Title)
CUWorldApp.MainWin.SetIcon(w.Icon)
}
CUWorldApp.MainWin.Resize(CUWorldApp.MainWindowSize)
CUWorldApp.MainWin.SetFixedSize(false)
CUWorldApp.TabItemMenu = container.NewAppTabs()
if CUWorldApp.CustosRenderer != nil {
CUWorldApp.TabItemMenu.OnSelected = CUWorldApp.CustosRenderer.OnSelected
}
CUWorldApp.TabItemMenu.SetTabLocation(container.TabLocationTop)
CUWorldApp.MainWin.SetContent(CUWorldApp.TabItemMenu)
CUWorldApp.MainWin.SetCloseIntercept(func() {
if CUWorldApp.HeadsupFyneContext.mashupContext != nil {
CUWorldApp.HeadsupFyneContext.mashupContext.Client.Shutdown(CUWorldApp.HeadsupFyneContext.mashupContext, &mashupsdk.MashupEmpty{AuthToken: client.GetServerAuthToken()})
}
os.Exit(0)
})
}
runtimeHandler := func() {
if w.MainWin != nil {
log.Printf("CustosWorld main win initialized\n")
if CUWorldApp.mashupDisplayContext != nil &&
(CUWorldApp.Headless ||
(CUWorldApp.mashupDisplayContext.GetSettled()&mashupsdk.AppInitted) == mashupsdk.AppInitted) {
log.Printf("CustosWorld app settled... starting up.\n")
w.MainWin.ShowAndRun()
} else {
if !CUWorldApp.Headless {
w.MainWin.ShowAndRun()
}
}
}
}
guiboot.InitMainWindow(guiboot.Fyne, initHandler, runtimeHandler)
}
func (w *worldClientInitHandler) RegisterContext(context *mashupsdk.MashupContext) {
CUWorldApp.HeadsupFyneContext.mashupContext = context
}
// Sets all elements to a "Rest state."
func (w *mashupSdkApiHandler) ResetStates() {
log.Printf("CustosWorld Received ResetStates\n")
for _, wes := range CUWorldApp.MashupDetailedElementLibrary {
wes.SetElementState(mashupsdk.Init)
}
log.Printf("CustosWorld finished ResetStates handle.\n")
}
func (mSdk *mashupSdkApiHandler) OnDisplayChange(displayHint *mashupsdk.MashupDisplayHint) {
log.Printf("CustosWorld OnDisplayChange - not implemented yet..\n")
if CUWorldApp.MainWin != nil && CUWorldApp.mashupDisplayContext != nil && CUWorldApp.mashupDisplayContext.MainWinDisplay != nil {
CUWorldApp.mashupDisplayContext.MainWinDisplay.Focused = displayHint.Focused
// TODO: Resize without infinite looping....
// The moment fyne is resized, it'll want to resize g3n...
// Which then wants to resize fyne ad-infinitum
//CUWorldApp.MainWin.PosResize(int(displayHint.Xpos), int(displayHint.Ypos), int(displayHint.Width), int(displayHint.Height))
log.Printf("CustosWorld Received OnDisplayChange xpos: %d ypos: %d width: %d height: %d ytranslate: %d\n", int(displayHint.Xpos), int(displayHint.Ypos), int(displayHint.Width), int(displayHint.Height), int(displayHint.Ypos+displayHint.Height))
} else {
log.Printf("CustosWorld Could not apply xpos: %d ypos: %d width: %d height: %d ytranslate: %d\n", int(displayHint.Xpos), int(displayHint.Ypos), int(displayHint.Width), int(displayHint.Height), int(displayHint.Ypos+displayHint.Height))
}
}
func (custosWorldApp *CustosWorldApp) DetailMappedFyneComponent(id, description string, genre string, renderer string, tabItemFunc func(custosWorlApp *CustosWorldApp, id string) *container.TabItem) {
de := &mashupsdk.MashupDetailedElement{Name: id, Description: description, Genre: genre, Renderer: renderer}
custosWorldApp.FyneWidgetElements[id] = &FyneWidgetBundle{
GuiWidgetBundle: mashupsdk.GuiWidgetBundle{
GuiComponent: nil,
MashupDetailedElement: de,
},
}
custosWorldApp.CustomTabItems[id] = tabItemFunc
}
func (custosWorldApp *CustosWorldApp) DetailFyneComponent(de *mashupsdk.MashupDetailedElement, tabItemFunc func(custosWorlApp *CustosWorldApp, id string) *container.TabItem) {
log.Printf("CustosWorldApp.DetailFyneComponent building on: %s name: %s\n", de.Alias, de.Name)
custosWorldApp.FyneWidgetElements[de.Name] = &FyneWidgetBundle{
GuiWidgetBundle: mashupsdk.GuiWidgetBundle{
GuiComponent: nil,
MashupDetailedElement: de,
},
}
custosWorldApp.CustomTabItems[de.Name] = tabItemFunc
}
func (mSdk *mashupSdkApiHandler) GetElements() (*mashupsdk.MashupDetailedElementBundle, error) {
log.Printf("CustosWorld Received GetElements\n")
return &mashupsdk.MashupDetailedElementBundle{
AuthToken: client.GetServerAuthToken(),
DetailedElements: CUWorldApp.DetailedElements,
}, nil
}
func (mSdk *mashupSdkApiHandler) UpsertElements(detailedElementBundle *mashupsdk.MashupDetailedElementBundle) (*mashupsdk.MashupDetailedElementBundle, error) {
log.Printf("CustosWorld Received UpsertElements\n")
for _, concreteElement := range detailedElementBundle.DetailedElements {
//helloApp.fyneComponentCache[generatedComponent.Basisid]
CUWorldApp.MashupDetailedElementLibrary[concreteElement.Id] = concreteElement
CUWorldApp.ElementLoaderIndex[concreteElement.Name] = concreteElement.Id
}
log.Printf("CustosWorld parsing tori.\n")
for _, concreteElement := range detailedElementBundle.DetailedElements {
if tabItemRenderer, tabItemRendererOk := CUWorldApp.CustomTabItemRenderer[concreteElement.GetCustosrenderer()]; tabItemRendererOk {
tabItemRenderer.BuildTabItem(concreteElement.Id, concreteElement)
}
}
log.Printf("Mashup elements delivered.\n")
CUWorldApp.mashupDisplayContext.ApplySettled(mashupsdk.AppInitted, false)
log.Printf("CustosWorld UpsertElements updated\n")
return &mashupsdk.MashupDetailedElementBundle{
AuthToken: client.GetServerAuthToken(),
DetailedElements: detailedElementBundle.DetailedElements,
}, nil
}
func (mSdk *mashupSdkApiHandler) setStateHelper(g3nId int64, x mashupsdk.DisplayElementState) {
child := CUWorldApp.MashupDetailedElementLibrary[g3nId]
if child.Genre != "Attitude" {
child.SetElementState(mashupsdk.DisplayElementState(x))
}
if len(child.Childids) > 0 {
for _, cId := range child.Childids {
mSdk.setStateHelper(cId, x)
}
}
}
func (mSdk *mashupSdkApiHandler) TweakStates(elementStateBundle *mashupsdk.MashupElementStateBundle) (*mashupsdk.MashupElementStateBundle, error) {
log.Printf("CustosWorld TweakStates called\n")
hasClick := false
recursiveElements := map[int64]*mashupsdk.MashupDetailedElement{}
// Separate clicked from declicked.
for _, es := range elementStateBundle.ElementStates {
if g3nDetailedElement, ok := CUWorldApp.MashupDetailedElementLibrary[es.GetId()]; ok {
g3nDetailedElement.SetElementState(mashupsdk.DisplayElementState(es.State))
if g3nDetailedElement.IsStateSet(mashupsdk.Recursive) {
recursiveElements[es.GetId()] = g3nDetailedElement
}
log.Printf("Display fields set to: %d", g3nDetailedElement.GetMashupElementState())
if libraryElement, libraryElementOk := CUWorldApp.MashupDetailedElementLibrary[g3nDetailedElement.Id]; libraryElementOk {
if (mashupsdk.DisplayElementState(es.State) & mashupsdk.Clicked) == mashupsdk.Clicked {
libraryElement.ApplyState(mashupsdk.Clicked, true)
if (mashupsdk.DisplayElementState(es.State) & mashupsdk.ControlClicked) == mashupsdk.ControlClicked {
hasClick = true
}
} else {
libraryElement.ApplyState(mashupsdk.Clicked, false)
}
libraryElement.ApplyState(mashupsdk.SourceExternal, true)
}
}
}
if hasClick {
CUWorldApp.MainWin.RequestFocus()
}
if len(recursiveElements) > 0 {
log.Printf("CustosWorld TweakStates apply recursive elements\n")
for _, recursiveElement := range recursiveElements {
stateBits := recursiveElement.State.State
// Unset recursive for child elements
stateBits &= ^int64(mashupsdk.Recursive)
stateBits |= int64(mashupsdk.SourceExternal)
// Apply this state change to all child elements.
mSdk.setStateHelper(recursiveElement.GetId(), mashupsdk.DisplayElementState(stateBits))
}
}
log.Printf("Clearing tab menu contents before reload")
CUWorldApp.TabItemMenu.Hide()
// Wipe anything there out.
CUWorldApp.TabItemMenu.SetItems([]*container.TabItem{})
orderedRenderingMap := map[int64][]*mashupsdk.MashupDetailedElement{}
// Get ready for new render cycle.
for _, tabItemRenderer := range CUWorldApp.CustomTabItemRenderer {
tabItemRenderer.PreRender()
}
// Impossible to determine ordering of clicks from upsert at this time.
for _, concreteElement := range CUWorldApp.MashupDetailedElementLibrary {
// Set all clicked elements...
if tabItemRenderer, tabItemRendererOk := CUWorldApp.CustomTabItemRenderer[concreteElement.Custosrenderer]; tabItemRendererOk {
orderedRenderingMap[tabItemRenderer.GetPriority()] = append(orderedRenderingMap[tabItemRenderer.GetPriority()], concreteElement)
}
}
keys := make([]int64, 0, len(orderedRenderingMap))
for k := range orderedRenderingMap {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, k := range keys {
var tir ITabItemRenderer
for _, concreteElement := range orderedRenderingMap[k] {
if tabItemRenderer, tabItemRendererOk := CUWorldApp.CustomTabItemRenderer[concreteElement.Custosrenderer]; tabItemRendererOk {
tabItemRenderer.RenderTabItem(concreteElement)
tir = tabItemRenderer
}
}
tir.Refresh()
}
CUWorldApp.TabItemMenu.Show()
log.Printf("CustosWorld dispatching focus\n")
// if CUWorldApp.MainWin != nil {
// CUWorldApp.MainWin.Hide()
// }
log.Printf("CustosWorld End TweakStates called\n")
return &mashupsdk.MashupElementStateBundle{}, nil
}
func (mSdk *mashupSdkApiHandler) TweakStatesByMotiv(motivIn mashupsdk.Motiv) {
log.Printf("CustosWorld Received TweakStatesByMotiv\n")
// TODO: Find and TweakStates...
log.Printf("CustosWorld finished TweakStatesByMotiv handle.\n")
}