/
richtext.go
59 lines (48 loc) · 1.23 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
package fyner
import (
"sync"
"deedles.dev/state"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
// RichText wraps widget.RichText to provide a component for
// displaying complex text layouts.
type RichText struct {
once sync.Once
w *widget.RichText
// Markdown provides markdown source as a string to generate the
// rich text from. It is not recommended to combine this with manual
// text segments, as any changes to either one will override the
// latest value of the other.
Markdown state.State[string]
markdownCancel state.CancelFunc
// Segments is the list of RichTextSegments to display with the
// component.
Segments state.State[[]widget.RichTextSegment]
segmentsCancel state.CancelFunc
}
func (rt *RichText) init() {
rt.once.Do(func() {
rt.w = widget.NewRichText()
})
}
func (rt *RichText) CanvasObject() fyne.CanvasObject {
rt.init()
return rt.w
}
func (rt *RichText) Bind() {
rt.init()
rt.Unbind()
if rt.Markdown != nil {
rt.markdownCancel = rt.Markdown.Listen(rt.w.ParseMarkdown)
}
if rt.Segments != nil {
rt.segmentsCancel = rt.Segments.Listen(func(s []widget.RichTextSegment) {
rt.w.Segments = s
rt.w.Refresh()
})
}
}
func (rt *RichText) Unbind() {
cancel(&rt.markdownCancel)
}