This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
/
EffectExampleBox.go
99 lines (82 loc) 路 1.75 KB
/
EffectExampleBox.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
package components
import (
"time"
"github.com/gdamore/tcell"
"retort.dev/components/box"
"retort.dev/r"
)
type EffectExampleBoxState struct {
Color tcell.Color
}
func EffectExampleBox(p r.Properties) r.Element {
boxProps := p.GetProperty(
box.Properties{},
"EffectExampleBox requires ContainerProps",
).(box.Properties)
children := p.GetProperty(
r.Children{},
"EffectExampleBox requires r.Children",
).(r.Children)
s, setState := r.UseState(r.State{
EffectExampleBoxState{Color: boxProps.Border.Foreground},
})
state := s.GetState(
EffectExampleBoxState{},
).(EffectExampleBoxState)
r.UseEffect(func() r.EffectCancel {
ticker := time.NewTicker(2 * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
setState(func(s r.State) r.State {
ms := s.GetState(
EffectExampleBoxState{},
).(EffectExampleBoxState)
color := tcell.ColorGreen
if ms.Color == tcell.ColorGreen {
color = tcell.ColorBlue
}
if ms.Color == tcell.ColorBlue {
color = tcell.ColorGreen
}
return r.State{EffectExampleBoxState{
Color: color,
},
}
})
}
}
}()
return func() {
<-done
}
}, r.EffectDependencies{})
mouseEventHandler := func(e *tcell.EventMouse) {
color := tcell.ColorGreen
if state.Color == tcell.ColorGreen {
color = tcell.ColorBlue
}
if state.Color == tcell.ColorBlue {
color = tcell.ColorGreen
}
setState(func(s r.State) r.State {
return r.State{EffectExampleBoxState{
Color: color,
},
}
})
}
boxProps.Border.Foreground = state.Color
return r.CreateElement(
box.Box,
r.Properties{
boxProps,
mouseEventHandler,
},
children,
)
}