/
text.go
127 lines (99 loc) · 2.05 KB
/
text.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
package app
import (
"context"
"fmt"
"html"
"io"
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
// Text creates a simple text element.
func Text(v any) UI {
return &text{value: toString(v)}
}
// Text creates a simple text element with the given format and values.
func Textf(format string, v ...any) UI {
return &text{value: fmt.Sprintf(format, v...)}
}
type text struct {
disp Dispatcher
jsvalue Value
parentElem UI
value string
}
func (t *text) Kind() Kind {
return SimpleText
}
func (t *text) JSValue() Value {
return t.jsvalue
}
func (t *text) Mounted() bool {
return t.jsvalue != nil && t.getDispatcher() != nil
}
func (t *text) name() string {
return "text"
}
func (t *text) self() UI {
return t
}
func (t *text) setSelf(n UI) {
}
func (t *text) getContext() context.Context {
return context.TODO()
}
func (t *text) getDispatcher() Dispatcher {
return t.disp
}
func (t *text) getAttributes() attributes {
return nil
}
func (t *text) getEventHandlers() eventHandlers {
return nil
}
func (t *text) getParent() UI {
return t.parentElem
}
func (t *text) setParent(p UI) {
t.parentElem = p
}
func (t *text) getChildren() []UI {
return nil
}
func (t *text) mount(d Dispatcher) error {
if t.Mounted() {
return errors.New("mounting ui element failed").
WithTag("reason", "already mounted").
WithTag("kind", t.Kind()).
WithTag("name", t.name()).
WithTag("value", t.value)
}
t.disp = d
t.jsvalue = Window().createTextNode(t.value)
return nil
}
func (t *text) dismount() {
t.jsvalue = nil
}
func (t *text) canUpdateWith(n UI) bool {
_, ok := n.(*text)
return ok
}
func (t *text) updateWith(n UI) error {
if !t.Mounted() {
return nil
}
o, _ := n.(*text)
if t.value != o.value {
t.value = o.value
t.JSValue().setNodeValue(o.value)
}
return nil
}
func (t *text) onComponentEvent(any) {
}
func (t *text) html(w io.Writer) {
w.Write([]byte(html.EscapeString(t.value)))
}
func (t *text) htmlWithIndent(w io.Writer, indent int) {
writeIndent(w, indent)
w.Write([]byte(html.EscapeString(t.value)))
}