forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
180 lines (158 loc) · 4.24 KB
/
label.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
package widget
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/theme"
)
// Label widget is a label component with appropriate padding and layout.
type Label struct {
BaseWidget
Text string
Alignment fyne.TextAlign // The alignment of the text
Wrapping fyne.TextWrap // The wrapping of the text
TextStyle fyne.TextStyle // The style of the label text
// The truncation mode of the text
//
// Since: 2.4
Truncation fyne.TextTruncation
// Importance informs how the label should be styled, i.e. warning or disabled
//
// Since: 2.4
Importance Importance
provider *RichText
binder basicBinder
}
// NewLabel creates a new label widget with the set text content
func NewLabel(text string) *Label {
return NewLabelWithStyle(text, fyne.TextAlignLeading, fyne.TextStyle{})
}
// NewLabelWithData returns an Label widget connected to the specified data source.
//
// Since: 2.0
func NewLabelWithData(data binding.String) *Label {
label := NewLabel("")
label.Bind(data)
return label
}
// NewLabelWithStyle creates a new label widget with the set text content
func NewLabelWithStyle(text string, alignment fyne.TextAlign, style fyne.TextStyle) *Label {
l := &Label{
Text: text,
Alignment: alignment,
TextStyle: style,
}
l.ExtendBaseWidget(l)
return l
}
// Bind connects the specified data source to this Label.
// The current value will be displayed and any changes in the data will cause the widget to update.
//
// Since: 2.0
func (l *Label) Bind(data binding.String) {
l.binder.SetCallback(l.updateFromData) // This could only be done once, maybe in ExtendBaseWidget?
l.binder.Bind(data)
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (l *Label) CreateRenderer() fyne.WidgetRenderer {
l.provider = NewRichTextWithText(l.Text)
l.ExtendBaseWidget(l)
l.syncSegments()
return l.provider.CreateRenderer()
}
// ExtendBaseWidget is used by an extending widget to make use of BaseWidget functionality.
func (l *Label) ExtendBaseWidget(w fyne.Widget) {
if w == nil {
w = l
}
l.BaseWidget.ExtendBaseWidget(w)
if l.provider != nil {
l.provider.ExtendBaseWidget(l.super())
}
}
// MinSize returns the size that this label should not shrink below.
//
// Implements: fyne.Widget
func (l *Label) MinSize() fyne.Size {
if l.provider == nil {
l.ExtendBaseWidget(l)
cache.Renderer(l.super())
}
return l.provider.MinSize()
}
// Refresh triggers a redraw of the label.
//
// Implements: fyne.Widget
func (l *Label) Refresh() {
if l.provider == nil { // not created until visible
return
}
l.syncSegments()
l.provider.Refresh()
l.BaseWidget.Refresh()
}
// Resize sets a new size for the label.
// This should only be called if it is not in a container with a layout manager.
//
// Implements: fyne.Widget
func (l *Label) Resize(s fyne.Size) {
l.BaseWidget.Resize(s)
if l.provider != nil {
l.provider.Resize(s)
}
}
// SetText sets the text of the label
func (l *Label) SetText(text string) {
l.Text = text
l.Refresh()
}
// Unbind disconnects any configured data source from this Label.
// The current value will remain at the last value of the data source.
//
// Since: 2.0
func (l *Label) Unbind() {
l.binder.Unbind()
}
func (l *Label) syncSegments() {
var color fyne.ThemeColorName
switch l.Importance {
case LowImportance:
color = theme.ColorNameDisabled
case MediumImportance:
color = theme.ColorNameForeground
case HighImportance:
color = theme.ColorNamePrimary
case DangerImportance:
color = theme.ColorNameError
case WarningImportance:
color = theme.ColorNameWarning
case SuccessImportance:
color = theme.ColorNameSuccess
default:
color = theme.ColorNameForeground
}
l.provider.Wrapping = l.Wrapping
l.provider.Truncation = l.Truncation
l.provider.Segments[0].(*TextSegment).Style = RichTextStyle{
Alignment: l.Alignment,
ColorName: color,
Inline: true,
TextStyle: l.TextStyle,
}
l.provider.Segments[0].(*TextSegment).Text = l.Text
}
func (l *Label) updateFromData(data binding.DataItem) {
if data == nil {
return
}
textSource, ok := data.(binding.String)
if !ok {
return
}
val, err := textSource.Get()
if err != nil {
fyne.LogError("Error getting current data value", err)
return
}
l.SetText(val)
}