-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.go
More file actions
7745 lines (7007 loc) · 201 KB
/
template.go
File metadata and controls
7745 lines (7007 loc) · 201 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package glyph
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode"
"unsafe"
)
// Component is the extension interface for custom components.
// External packages can implement this to create custom components
// that expand to built-in primitives at compile time.
type Component interface {
Build() any
}
// Renderer is the extension interface for components that render directly.
// Unlike Component (which expands to primitives), Renderer draws to the
// buffer itself. This is useful for custom widgets like charts, sparklines, etc.
type Renderer interface {
// MinSize returns the minimum dimensions needed by this component.
// Called during layout phase.
MinSize() (width, height int)
// Render draws the component to the buffer at the given position.
// w and h are the allocated dimensions (may be larger than MinSize).
Render(buf *Buffer, x, y, w, h int)
}
// forEachCompiler is implemented by generic ForEach types to compile themselves
type forEachCompiler interface {
compileTo(t *Template, parent int16, depth int) int16
}
// listCompiler is implemented by generic List types to compile themselves
type listCompiler interface {
toSelectionList() *SelectionList
}
// bindable is implemented by components that declare key bindings as data.
type bindable interface {
bindings() []binding
}
// textInputBindable is implemented by InputC for text input routing.
type textInputBindable interface {
textBinding() *textInputBinding
}
// templateTree is implemented by compound components that compose existing
// building blocks into a template subtree.
type templateTree interface {
toTemplate() any
}
// LayoutFunc positions children given their sizes and available space.
type LayoutFunc func(children []ChildSize, availW, availH int) []Rect
// ChildSize represents a child's computed minimum dimensions.
type ChildSize struct {
MinW, MinH int
}
// Rect represents a positioned rectangle.
type Rect struct {
X, Y, W, H int
}
// NodeRef holds a node's rendered screen bounds, populated each frame after layout.
// Declare one, attach it to a node with .NodeRef(), then read it in effects or
// anywhere that needs to know where something actually rendered.
type NodeRef = Rect
// Box is a container with a custom layout function.
// Use this when HBox/VBox don't fit your needs.
type Box struct {
Layout LayoutFunc
Children []any
}
// Template is a compiled UI template.
// Compile does all reflection. Execute is pure pointer arithmetic.
type Template struct {
ops []Op
geom []Geom // parallel to ops, filled at runtime
// For bottom-up layout traversal
maxDepth int
byDepth [][]int16 // ops grouped by tree depth
// Current element base for ForEach context (set during layout/render)
elemBase unsafe.Pointer
// App reference for jump mode coordination
app *App
// per-item index for ForEach/SelectionList (reset per iteration, used by per-item tweens)
itemIndex int
// row styling for SelectionList selected rows (merged with cell styles)
rowBG Color
rowFG Color
rowAttr Attribute
// Style inheritance - current inherited style during render
inheritedStyle *Style
inheritedFill Color // cascades through nested containers
// vertical clip: maximum Y coordinate for rendering (exclusive, 0 = no clip)
clipMaxY int16
// compile-time: tracks the outermost property pointer and collects
// nested tween items maps so the outermost condition can record
// per-item displayed values for transition detection
compilePropertyPtr *Color
compileTweenItemsMaps []map[unsafe.Pointer]*perItemColorState
// Pending overlays to render after main content (cleared each frame)
pendingOverlays []pendingOverlay
// Pending screen effects collected from tree (cleared each frame)
pendingScreenEffects []Effect
// scratch buffers for per-frame reuse (avoid nil-slice allocs in hot paths)
flexScratchIdx []int16 // flex child indices (shared by VBox + HBox phases)
flexScratchGrow []float32 // flex grow values (shared by VBox + HBox phases)
flexScratchImpl []int16 // implicit flex children (HBox only)
treeScratchPfx []bool // tree node line prefix
// ext pools — contiguous allocations for cache-friendly render access.
// Declarative bindings collected during compile, wired during setup
pendingBindings []binding
pendingTIB *textInputBinding
pendingLogs []*LogC // Logs that need app.RequestRender wiring
pendingFocusManager *FocusManager // Focus manager for multi-input routing
// per-frame evaluators — conditions, animations, etc. run at start of Execute
evals []func()
// per-item evaluators — run once per ForEach item with elemBase set
itemEvals []func()
// frame timing — single timestamp per frame, shared by all animations
frameTime time.Time
animating bool
// animation ticker — runs at ~60fps only while animations are active
animTicker *time.Ticker
requestRender func()
// root points to the outermost template so sub-templates (If branches,
// Overlays, ForEach) register evaluators where Execute actually runs them.
root *Template
}
// evalRoot returns the root template where evaluators should be registered.
// for top-level templates root is nil and we return self.
func (t *Template) evalRoot() *Template {
if t.root != nil {
return t.root
}
return t
}
// pendingOverlay stores info needed to render an overlay after main content
type pendingOverlay struct {
op *Op // pointer to the overlay op
}
// SetApp links this template to an App for jump mode support.
func (t *Template) SetApp(a *App) {
t.app = a
}
func (t *Template) collectBindings(node any) {
if b, ok := node.(bindable); ok {
t.pendingBindings = append(t.pendingBindings, b.bindings()...)
}
}
func (t *Template) collectTextInputBinding(node any) {
if tib, ok := node.(textInputBindable); ok {
t.pendingTIB = tib.textBinding()
}
}
func (t *Template) collectFocusManager(node any) {
// check if InputC or FilterLogC has a manager
switch v := node.(type) {
case *InputC:
if v.manager != nil && t.pendingFocusManager == nil {
t.pendingFocusManager = v.manager
}
case *FilterLogC:
if v.manager != nil && t.pendingFocusManager == nil {
t.pendingFocusManager = v.manager
}
}
}
// Geom holds runtime geometry for an op.
// Filled during execute, parallel array to ops.
type Geom struct {
W, H int16 // dimensions
LocalX, LocalY int16 // position relative to parent
ContentH int16 // natural content height (before flex distribution)
}
// Op represents a single compiled template instruction.
// The template compiler produces a flat array of these; Execute walks them to render.
type Op struct {
Kind OpKind
Depth int8 // tree depth (root children = 0)
Parent int16 // parent op index, -1 for root children
// Layout hints
Width int16 // explicit width
Height int16 // explicit height
PercentWidth float32 // 0.0-1.0
FlexGrow float32 // share of remaining space
Gap int8 // gap between children
ContentSized bool // has fixed-width children (don't implicit flex)
FitContent bool // size to content instead of filling available space
// Container
IsRow bool // true=HBox, false=VBox
Border BorderStyle // border style
BorderFG *Color // border foreground color
BorderBG *Color // border background color
Title string // border title
ChildStart int16 // first child op index
ChildEnd int16 // last child op index (exclusive)
CascadeStyle *Style // style inherited by children (pointer for dynamic themes)
LocalStyle *Style // style for this container only (not inherited)
Fill Color // container fill color (fills entire area)
Margin [4]int16 // outer margin: top, right, bottom, left
Padding [4]int16 // inner padding: top, right, bottom, left
NodeRef *NodeRef // if set, populated with rendered screen bounds each frame
// kind-specific data — type-assert based on Kind.
// we use a Kind switch + type assertion instead of interface dispatch because
// concrete method calls after assertion are inlinable. interface calls are not,
// and cause parameters to escape to heap. verified via go build -gcflags='-m -m'.
Ext any
// dynamic layout property overrides — nil for static ops
Dyn *OpDyn
}
// OpDyn holds pointer overrides for shared layout properties.
// only allocated for ops that use dynamic values (e.g. Height(&h)).
type OpDyn struct {
Height *int16
Width *int16
FlexGrow *float32
PercentWidth *float32
Gap *int8
Fill *Color
Opacity *float64
OpacityArmed *bool // set true by render to signal From tween activation
}
// resolver methods — inlinable nil-check + deref, zero cost when Dyn is nil
func (op *Op) height() int16 {
if op.Dyn != nil {
if p := op.Dyn.Height; p != nil {
return *p
}
}
return op.Height
}
func (op *Op) width() int16 {
if op.Dyn != nil {
if p := op.Dyn.Width; p != nil {
return *p
}
}
return op.Width
}
func (op *Op) flexGrow() float32 {
if op.Dyn != nil {
if p := op.Dyn.FlexGrow; p != nil {
return *p
}
}
return op.FlexGrow
}
func (op *Op) percentWidth() float32 {
if op.Dyn != nil {
if p := op.Dyn.PercentWidth; p != nil {
return *p
}
}
return op.PercentWidth
}
func (op *Op) gap() int8 {
if op.Dyn != nil {
if p := op.Dyn.Gap; p != nil {
return *p
}
}
return op.Gap
}
func (op *Op) fill() Color {
if op.Dyn != nil {
if p := op.Dyn.Fill; p != nil {
return *p
}
}
return op.Fill
}
// compileCond registers a conditional evaluator and returns a pointer to its storage.
// the evaluator runs each frame, resolving the condition and writing the active value.
func (t *Template) compileCondInt16(cond conditionNode) *int16 {
root := t.evalRoot()
storage := new(int16)
thenVal := cond.getThen()
elseVal := cond.getElse()
eval := func() {
if cond.evaluate() {
*storage = anyToInt16(thenVal)
} else {
*storage = anyToInt16(elseVal)
}
}
eval() // set initial value
root.evals = append(root.evals, eval)
return storage
}
func (t *Template) compileCondFloat32(cond conditionNode) *float32 {
root := t.evalRoot()
storage := new(float32)
thenVal := cond.getThen()
elseVal := cond.getElse()
eval := func() {
if cond.evaluate() {
*storage = anyToFloat32(thenVal)
} else {
*storage = anyToFloat32(elseVal)
}
}
eval()
root.evals = append(root.evals, eval)
return storage
}
func (t *Template) compileCondFloat64(cond conditionNode) *float64 {
root := t.evalRoot()
storage := new(float64)
thenVal := cond.getThen()
elseVal := cond.getElse()
eval := func() {
if cond.evaluate() {
*storage = anyToFloat64(thenVal)
} else {
*storage = anyToFloat64(elseVal)
}
}
eval()
root.evals = append(root.evals, eval)
return storage
}
func (t *Template) compileCondInt8(cond conditionNode) *int8 {
root := t.evalRoot()
storage := new(int8)
thenVal := cond.getThen()
elseVal := cond.getElse()
eval := func() {
if cond.evaluate() {
*storage = anyToInt8(thenVal)
} else {
*storage = anyToInt8(elseVal)
}
}
eval()
root.evals = append(root.evals, eval)
return storage
}
func anyToInt16(v any) int16 {
switch val := v.(type) {
case int16:
return val
case int:
return int16(val)
case *int16:
return *val
}
return 0
}
func anyToFloat64(v any) float64 {
switch val := v.(type) {
case float64:
return val
case float32:
return float64(val)
case int:
return float64(val)
case *float64:
return *val
}
return 0
}
func anyToFloat32(v any) float32 {
switch val := v.(type) {
case float32:
return val
case float64:
return float32(val)
case int:
return float32(val)
case *float32:
return *val
}
return 0
}
func anyToInt8(v any) int8 {
switch val := v.(type) {
case int8:
return val
case int:
return int8(val)
case *int8:
return *val
}
return 0
}
// compileDyn resolves a dynamic property value (conditionNode or tweenNode) to a pointer.
// used by compile sites where Cond fields can hold either type.
func (t *Template) compileDynInt16(v any) *int16 {
switch c := v.(type) {
case conditionNode:
return t.compileCondInt16(c)
case tweenNode:
return t.compileTweenInt16(c)
}
return nil
}
func (t *Template) compileDynFloat32(v any) *float32 {
switch c := v.(type) {
case conditionNode:
return t.compileCondFloat32(c)
case tweenNode:
return t.compileTweenFloat32(c)
}
return nil
}
func (t *Template) compileDynFloat64(v any) *float64 {
switch c := v.(type) {
case *float64:
return c
case conditionNode:
return t.compileCondFloat64(c)
case tweenNode:
return t.compileTweenFloat64(c, nil)
}
return nil
}
func (t *Template) compileDynInt8(v any) *int8 {
switch c := v.(type) {
case conditionNode:
return t.compileCondInt8(c)
case tweenNode:
return t.compileTweenInt8(c)
}
return nil
}
func (t *Template) compileDynColor(v any, elemBase unsafe.Pointer, elemSize uintptr) *Color {
switch c := v.(type) {
case *Color:
return c
case conditionNode:
return t.compileCondColor(c, elemBase, elemSize)
case switchNodeInterface:
return t.compileSwitchColor(c)
case tweenNode:
return t.compileTweenColor(c, elemBase, elemSize)
}
return nil
}
func (t *Template) compileDynStyle(v any, elemBase unsafe.Pointer, elemSize uintptr) *Style {
switch c := v.(type) {
case *Style:
return c
case conditionNode:
return t.compileCondStyle(c, elemBase, elemSize)
case tweenNode:
return t.compileTweenStyle(c, elemBase, elemSize)
}
return nil
}
// compileStyleDyn wires styleDyn/fgDyn/bgDyn into a *Style for any leaf component.
// returns nil if no dynamic styling is needed.
func (t *Template) compileStyleDyn(baseStyle Style, styleDyn, fgDyn, bgDyn any, elemBase unsafe.Pointer, elemSize uintptr) *Style {
if styleDyn != nil {
return t.compileDynStyle(styleDyn, elemBase, elemSize)
}
if fgDyn == nil && bgDyn == nil {
return nil
}
storage := new(Style)
*storage = baseStyle
var fgPtr *Color
var bgPtr *Color
if fgDyn != nil {
fgPtr = t.compileDynColor(fgDyn, elemBase, elemSize)
}
if bgDyn != nil {
bgPtr = t.compileDynColor(bgDyn, elemBase, elemSize)
}
root := t.evalRoot()
base := baseStyle
root.evals = append(root.evals, func() {
s := base
if fgPtr != nil {
s.FG = *fgPtr
}
if bgPtr != nil {
s.BG = *bgPtr
}
*storage = s
})
return storage
}
func (t *Template) compileCondColor(cond conditionNode, elemBase unsafe.Pointer, elemSize uintptr) *Color {
storage := new(Color)
isOutermost := t.compilePropertyPtr == nil
if isOutermost {
t.compilePropertyPtr = storage
t.compileTweenItemsMaps = nil
defer func() {
t.compilePropertyPtr = nil
t.compileTweenItemsMaps = nil
}()
}
thenVal := cond.getThen()
elseVal := cond.getElse()
// recursively compile nested conditions, tweens, and reactive pointers
resolveColor := func(v any) func() Color {
switch nested := v.(type) {
case conditionNode:
ptr := t.compileCondColor(nested, elemBase, elemSize)
return func() Color { return *ptr }
case tweenNode:
ptr, items := t.compileTweenColorItems(nested, elemBase, elemSize)
if items != nil {
t.compileTweenItemsMaps = append(t.compileTweenItemsMaps, items)
}
return func() Color { return *ptr }
case *Color:
return func() Color { return *nested }
default:
c := anyToColor(v)
return func() Color { return c }
}
}
thenFn := resolveColor(thenVal)
elseFn := resolveColor(elseVal)
inForEach := false
if elemBase != nil && elemSize > 0 {
ptrAddr := cond.getPtrAddr()
baseAddr := uintptr(elemBase)
if ptrAddr >= baseAddr && ptrAddr < baseAddr+elemSize {
cond.setOffset(ptrAddr - baseAddr)
inForEach = true
}
}
if inForEach {
if cond.evaluate() {
*storage = thenFn()
} else {
*storage = elseFn()
}
eval := func() {
if cond.evaluateWithBase(t.elemBase) {
*storage = thenFn()
} else {
*storage = elseFn()
}
}
t.itemEvals = append(t.itemEvals, eval)
// outermost condition records per-item displayed value into nested tweens
if isOutermost && len(t.compileTweenItemsMaps) > 0 {
propPtr := storage
tweenMaps := t.compileTweenItemsMaps
t.itemEvals = append(t.itemEvals, func() {
displayed := *propPtr
key := t.elemBase
for _, items := range tweenMaps {
if state, ok := items[key]; ok {
state.lastDisplayed = displayed
}
}
})
}
} else {
root := t.evalRoot()
eval := func() {
if cond.evaluate() {
*storage = thenFn()
} else {
*storage = elseFn()
}
}
eval()
root.evals = append(root.evals, eval)
}
return storage
}
func (t *Template) compileCondStyle(cond conditionNode, elemBase unsafe.Pointer, elemSize uintptr) *Style {
storage := new(Style)
thenVal := cond.getThen()
elseVal := cond.getElse()
// recursively compile nested conditions, tweens, and reactive pointers
resolveStyle := func(v any) func() Style {
switch nested := v.(type) {
case conditionNode:
ptr := t.compileCondStyle(nested, elemBase, elemSize)
return func() Style { return *ptr }
case tweenNode:
ptr := t.compileTweenStyle(nested, elemBase, elemSize)
return func() Style { return *ptr }
case *Style:
return func() Style { return *nested }
default:
s := anyToStyle(v)
return func() Style { return s }
}
}
thenFn := resolveStyle(thenVal)
elseFn := resolveStyle(elseVal)
// check if the condition pointer is within a ForEach element
inForEach := false
if elemBase != nil && elemSize > 0 {
ptrAddr := cond.getPtrAddr()
baseAddr := uintptr(elemBase)
if ptrAddr >= baseAddr && ptrAddr < baseAddr+elemSize {
cond.setOffset(ptrAddr - baseAddr)
inForEach = true
}
}
if inForEach {
if cond.evaluate() {
*storage = thenFn()
} else {
*storage = elseFn()
}
eval := func() {
if cond.evaluateWithBase(t.elemBase) {
*storage = thenFn()
} else {
*storage = elseFn()
}
}
t.itemEvals = append(t.itemEvals, eval)
} else {
// global eval — runs once per frame
root := t.evalRoot()
eval := func() {
if cond.evaluate() {
*storage = thenFn()
} else {
*storage = elseFn()
}
}
eval()
root.evals = append(root.evals, eval)
}
return storage
}
func (t *Template) compileSwitchColor(sw switchNodeInterface) *Color {
root := t.evalRoot()
storage := new(Color)
cases := sw.getCaseNodes()
def := sw.getDefaultNode()
eval := func() {
idx := sw.getMatchIndex()
if idx >= 0 && idx < len(cases) {
*storage = anyToColor(cases[idx])
} else {
*storage = anyToColor(def)
}
}
eval()
root.evals = append(root.evals, eval)
return storage
}
func anyToColor(v any) Color {
switch val := v.(type) {
case Color:
return val
case *Color:
return *val
}
return Color{}
}
func anyToStyle(v any) Style {
switch val := v.(type) {
case Style:
return val
case *Style:
return *val
}
return Style{}
}
// Animating returns true if any tween is currently in progress.
// check this after Execute to determine if another frame is needed.
func (t *Template) Animating() bool { return t.animating }
// compileTween resolves a tweenNode's target to a typed pointer, allocates
// interpolation storage, and registers a per-frame evaluator that watches the
// target and lerps toward it. all tweens in a frame share t.frameTime.
func (t *Template) compileTweenInt16(tw tweenNode) *int16 {
root := t.evalRoot()
watchPtr := t.resolveTweenTargetInt16(tw.getTarget())
storage := new(int16)
*storage = *watchPtr
durVal := tw.getTweenDuration()
durPtr := tw.(*tween).durationPtr
onComplete := tw.getTweenOnComplete()
ease := tw.getTweenEasing()
lastTarget := *watchPtr
startVal := float64(*watchPtr)
var startTime time.Time
needsFirstFrame := false
if from := tw.getTweenFrom(); from != nil {
*storage = anyToInt16(from)
startVal = float64(*storage)
needsFirstFrame = true
}
root.evals = append(root.evals, func() {
dur := durVal
if durPtr != nil {
dur = *durPtr
}
target := *watchPtr
now := root.frameTime
if needsFirstFrame {
startVal = float64(*storage)
lastTarget = target
startTime = now
needsFirstFrame = false
} else if target != lastTarget {
startVal = float64(*storage)
lastTarget = target
startTime = now
}
if startTime.IsZero() {
return
}
elapsed := now.Sub(startTime)
if elapsed >= dur {
*storage = target
startTime = time.Time{}
if onComplete != nil {
onComplete()
}
return
}
progress := float64(elapsed) / float64(dur)
if ease != nil {
progress = ease(progress)
}
*storage = int16(startVal + progress*(float64(target)-startVal))
root.animating = true
})
return storage
}
func (t *Template) compileTweenFloat32(tw tweenNode) *float32 {
root := t.evalRoot()
watchPtr := t.resolveTweenTargetFloat32(tw.getTarget())
storage := new(float32)
*storage = *watchPtr
durVal := tw.getTweenDuration()
durPtr := tw.(*tween).durationPtr
onComplete := tw.getTweenOnComplete()
ease := tw.getTweenEasing()
lastTarget := *watchPtr
startVal := float64(*watchPtr)
var startTime time.Time
needsFirstFrame := false
if from := tw.getTweenFrom(); from != nil {
*storage = anyToFloat32(from)
startVal = float64(*storage)
needsFirstFrame = true
}
root.evals = append(root.evals, func() {
dur := durVal
if durPtr != nil {
dur = *durPtr
}
target := *watchPtr
now := root.frameTime
if needsFirstFrame {
startVal = float64(*storage)
lastTarget = target
startTime = now
needsFirstFrame = false
} else if target != lastTarget {
startVal = float64(*storage)
lastTarget = target
startTime = now
}
if startTime.IsZero() {
return
}
elapsed := now.Sub(startTime)
if elapsed >= dur {
*storage = target
startTime = time.Time{}
if onComplete != nil {
onComplete()
}
return
}
progress := float64(elapsed) / float64(dur)
if ease != nil {
progress = ease(progress)
}
*storage = float32(startVal + progress*(float64(target)-startVal))
root.animating = true
})
return storage
}
func (t *Template) compileTweenFloat64(tw tweenNode, armed *bool) *float64 {
root := t.evalRoot()
watchPtr := t.resolveTweenTargetFloat64(tw.getTarget())
storage := new(float64)
*storage = *watchPtr
durVal := tw.getTweenDuration()
durPtr := tw.(*tween).durationPtr
onComplete := tw.getTweenOnComplete()
ease := tw.getTweenEasing()
lastTarget := *watchPtr
startVal := *watchPtr
var startTime time.Time
needsFirstFrame := false
var fromVal float64
if from := tw.getTweenFrom(); from != nil {
fromVal = anyToFloat64(from)
*storage = fromVal
startVal = fromVal
needsFirstFrame = true
}
// tracks whether resolve() was called last frame (effect was active)
wasActive := armed == nil // nil armed = always active (non-effect tweens)
root.evals = append(root.evals, func() {
dur := durVal
if durPtr != nil {
dur = *durPtr
}
target := *watchPtr
now := root.frameTime
// activation gating: From tweens in screen effects wait for resolve()
if armed != nil {
active := *armed
*armed = false // reset each frame; resolve() re-sets if still active
if !active {
wasActive = false
*storage = fromVal // reset so stale target doesn't flash on re-open
return
}
if !wasActive {
// inactive → active transition: (re)start From animation
wasActive = true
*storage = fromVal
startVal = fromVal
lastTarget = target
startTime = now
needsFirstFrame = false
goto interpolate
}
}
if needsFirstFrame {
startVal = *storage
lastTarget = target
startTime = now
needsFirstFrame = false
} else if target != lastTarget {
startVal = *storage
lastTarget = target
startTime = now
}
interpolate:
if startTime.IsZero() {
return
}
elapsed := now.Sub(startTime)
if elapsed >= dur {
*storage = target
startTime = time.Time{}
if onComplete != nil {
onComplete()
}
return
}
progress := float64(elapsed) / float64(dur)
if ease != nil {
progress = ease(progress)
}
*storage = startVal + progress*(target-startVal)
root.animating = true
})
return storage
}
func (t *Template) compileTweenInt8(tw tweenNode) *int8 {
root := t.evalRoot()
watchPtr := t.resolveTweenTargetInt8(tw.getTarget())
storage := new(int8)
*storage = *watchPtr
durVal := tw.getTweenDuration()
durPtr := tw.(*tween).durationPtr
onComplete := tw.getTweenOnComplete()
ease := tw.getTweenEasing()
lastTarget := *watchPtr
startVal := float64(*watchPtr)
var startTime time.Time
needsFirstFrame := false
if from := tw.getTweenFrom(); from != nil {
*storage = anyToInt8(from)
startVal = float64(*storage)
needsFirstFrame = true
}
root.evals = append(root.evals, func() {
dur := durVal
if durPtr != nil {
dur = *durPtr
}
target := *watchPtr
now := root.frameTime
if needsFirstFrame {
startVal = float64(*storage)
lastTarget = target
startTime = now
needsFirstFrame = false
} else if target != lastTarget {