-
Notifications
You must be signed in to change notification settings - Fork 0
/
richtext.go
237 lines (209 loc) · 5.73 KB
/
richtext.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
// Package richtext provides rendering of text containing multiple fonts, styles, and levels of interactivity.
package richtext
import (
"image"
"image/color"
"time"
"gioui.org/gesture"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/text"
"gioui.org/unit"
"github.com/vlanse/gio-x/styledtext"
)
// LongPressDuration is the default duration of a long press gesture.
// Override this variable to change the detection threshold.
var LongPressDuration time.Duration = 250 * time.Millisecond
// EventType describes a kind of interaction with rich text.
type EventType uint8
const (
Hover EventType = iota
LongPress
Click
)
// Event describes an interaction with rich text.
type Event struct {
Type EventType
// ClickData is only populated if Type == Clicked
ClickData gesture.ClickEvent
}
// InteractiveSpan holds the persistent state of rich text that can
// be interacted with by the user. It can report clicks, hovers, and
// long-presses on the text.
type InteractiveSpan struct {
click gesture.Click
pressing bool
longPressed bool
pressStarted time.Time
contents string
metadata map[string]interface{}
events []Event
}
// Layout adds the pointer input op for this interactive span and updates its
// state. It uses the most recent pointer.AreaOp as its input area.
func (i *InteractiveSpan) Layout(gtx layout.Context) layout.Dimensions {
defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop()
pointer.CursorPointer.Add(gtx.Ops)
i.click.Add(gtx.Ops)
for _, e := range i.click.Events(gtx) {
switch e.Type {
case gesture.TypeClick:
if i.longPressed {
i.longPressed = false
} else {
i.events = append(i.events, Event{Type: Click, ClickData: e})
}
i.pressing = false
case gesture.TypePress:
i.pressStarted = gtx.Now
i.pressing = true
case gesture.TypeCancel:
i.pressing = false
i.longPressed = false
}
}
if i.click.Hovered() {
i.events = append(i.events, Event{Type: Hover})
}
if !i.longPressed && i.pressing && gtx.Now.Sub(i.pressStarted) > LongPressDuration {
i.events = append(i.events, Event{Type: LongPress})
i.longPressed = true
}
if i.pressing && !i.longPressed {
op.InvalidateOp{}.Add(gtx.Ops)
}
return layout.Dimensions{}
}
// Events returns click event information for this span.
func (i *InteractiveSpan) Events() []Event {
out := i.events
i.events = i.events[:0]
return out
}
// Content returns the text content of the interactive span as well as the
// metadata associated with it.
func (i *InteractiveSpan) Content() (string, map[string]interface{}) {
return i.contents, i.metadata
}
// Get looks up a metadata property on the interactive span.
func (i *InteractiveSpan) Get(key string) interface{} {
return i.metadata[key]
}
// InteractiveText holds persistent state for a block of text containing
// spans that may be interactive.
type InteractiveText struct {
Spans []InteractiveSpan
}
// resize makes sure that there are exactly n interactive spans.
func (i *InteractiveText) resize(n int) {
if n == 0 && i == nil {
return
}
if cap(i.Spans) >= n {
i.Spans = i.Spans[:n]
} else {
i.Spans = make([]InteractiveSpan, n)
}
}
// Events returns the first span with unprocessed events and the events that
// need processing for it.
func (i *InteractiveText) Events() (*InteractiveSpan, []Event) {
for k := range i.Spans {
span := &i.Spans[k]
if events := span.Events(); len(events) > 0 {
return span, events
}
}
return nil, nil
}
// SpanStyle describes the appearance of a span of styled text.
type SpanStyle struct {
Font text.Font
Size unit.Sp
Color color.NRGBA
Content string
Interactive bool
metadata map[string]interface{}
interactiveIdx int
Img image.Image
}
// Set configures a metadata key-value pair on the span that can be
// retrieved if the span is interacted with. If the provided value
// is empty, the key will be deleted from the metadata.
func (ss *SpanStyle) Set(key string, value interface{}) {
if value == "" {
if ss.metadata != nil {
delete(ss.metadata, key)
if len(ss.metadata) == 0 {
ss.metadata = nil
}
}
return
}
if ss.metadata == nil {
ss.metadata = make(map[string]interface{})
}
ss.metadata[key] = value
}
// DeepCopy returns an identical SpanStyle with its own copy of its metadata.
func (ss *SpanStyle) DeepCopy() SpanStyle {
out := *ss
if len(ss.metadata) > 0 {
md := make(map[string]interface{})
for k, v := range ss.metadata {
md[k] = v
}
out.metadata = md
}
return out
}
// TextStyle presents rich text.
type TextStyle struct {
State *InteractiveText
Styles []SpanStyle
Alignment text.Alignment
*text.Shaper
}
// Text constructs a TextStyle.
func Text(state *InteractiveText, shaper *text.Shaper, styles ...SpanStyle) TextStyle {
return TextStyle{
State: state,
Styles: styles,
Shaper: shaper,
}
}
// Layout renders the TextStyle.
func (t TextStyle) Layout(gtx layout.Context) layout.Dimensions {
// OPT(dh): it'd be nice to avoid this allocation
styles := make([]styledtext.SpanStyle, len(t.Styles))
numInteractive := 0
for i := range t.Styles {
st := &t.Styles[i]
if st.Interactive {
st.interactiveIdx = numInteractive
numInteractive++
}
styles[i] = styledtext.SpanStyle{
Font: st.Font,
Size: st.Size,
Color: st.Color,
Content: st.Content,
Img: st.Img,
}
}
t.State.resize(numInteractive)
text := styledtext.Text(t.Shaper, styles...)
text.Alignment = t.Alignment
return text.Layout(gtx, func(gtx layout.Context, i int, _ layout.Dimensions) {
span := &t.Styles[i]
if !span.Interactive {
return
}
state := &t.State.Spans[span.interactiveIdx]
state.contents = span.Content
state.metadata = span.metadata
state.Layout(gtx)
})
}