-
Notifications
You must be signed in to change notification settings - Fork 0
/
customlabel.go
293 lines (225 loc) · 9.84 KB
/
customlabel.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
/* CustomLabel is a widget type to display text with a background and using a customizable style */
/* ================================================================================ Imports */
import (
"strings"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/theme"
)
/* ================================================================================ Public types */
type PaintStyle struct {
Foreground, Background, Stroke color.RGBA
StrokeWidth float32
}
type Paddings struct {
Top, Bottom, Left, Right float32
}
type CustomLabel struct {
widget.BaseWidget
Alignment fyne.TextAlign
Style PaintStyle
LineWrapping bool
Text string
TextSize float32
TextStyle fyne.TextStyle
BackgroundPaddings, TextPaddings Paddings
}
/* ================================================================================ Private types */
type customLabelRenderer struct {
background *canvas.Rectangle
textCanvases *[]*canvas.Text
w *CustomLabel
}
/* ================================================================================ Public functions */
func CalculatePaddings(paddingMultipliers, textPaddingOffsets Paddings) (backgroundPaddings, textPaddings Paddings) {
backgroundPaddings = Paddings{
theme.Padding() * paddingMultipliers.Top, theme.Padding() * paddingMultipliers.Bottom,
theme.Padding() * paddingMultipliers.Left, theme.Padding() * paddingMultipliers.Right,
}
textPaddings = Paddings{
backgroundPaddings.Top + textPaddingOffsets.Top, backgroundPaddings.Bottom + textPaddingOffsets.Bottom,
backgroundPaddings.Left + textPaddingOffsets.Left, backgroundPaddings.Right + textPaddingOffsets.Right,
}
return backgroundPaddings, textPaddings
}
func NewCustomLabel(alignment fyne.TextAlign, style PaintStyle, lineWrapping bool, text string, textSize float32, textStyle fyne.TextStyle, paddingMultipliers, textPaddingOffsets Paddings) *CustomLabel {
backgroundPaddings, textPaddings := CalculatePaddings(paddingMultipliers, textPaddingOffsets)
customLabel := &CustomLabel{ Alignment: alignment, Style: style, LineWrapping: lineWrapping, Text: text, TextSize: textSize, TextStyle: textStyle, BackgroundPaddings: backgroundPaddings, TextPaddings: textPaddings }
customLabel.ExtendBaseWidget(customLabel)
return customLabel
}
/* ================================================================================ Private methods */
func wrapLine(line string, textSize float32, textStyle fyne.TextStyle, maxWidth float32) []string {
/* Ensure there are at least two characters for splitting */
if len(line) < 2 {
return []string{ line }
}
/* Return if the whole line already fits into the available width */
if fyne.MeasureText(line, textSize, textStyle).Width <= maxWidth {
return []string{ line }
}
/* Ensure that at least the first character fits into the available width */
if fyne.MeasureText(line[:1], textSize, textStyle).Width > maxWidth {
return []string{ line }
}
/* Split the line, first by spaces (into words) */
sep := " "
for {
parts := strings.Split(line, sep)
partCount := len(parts)
/* Ensure that there are at least two parts */
if partCount < 2 && sep != "" {
/* Otherwise retry with splitting the line into characters instead of words */
sep = ""
continue
}
/* Find the maximum fitting sequence of parts using binary search */
wrapIndex := (partCount + 1) / 2
wrapIndexStep := 1
rangeUpperIndex := partCount - 1
rangeLowerIndex := 0
innerLoop:
for {
candidate := strings.Join(parts[:wrapIndex], sep)
width := fyne.MeasureText(candidate, textSize, textStyle).Width
switch {
case wrapIndexStep > 0 && width < maxWidth:
/* The candidate sequence fits but is still converging, so use the current
wrap index as lower bound and continue in the center of the new range */
rangeLowerIndex = wrapIndex
/* Floor the increment step to avoid oscillating between the maximum fitting and the minimum non-fitting
candidate sequence if the index difference is just one - stay at the smaller/fitting one instead */
wrapIndexStep = (rangeUpperIndex - rangeLowerIndex) / 2
wrapIndex += wrapIndexStep
case wrapIndexStep > 0 && width > maxWidth:
/* The candidate sequence does not fit but is still converging, so use the current
wrap index as upper bound and continue in the center of the new range */
rangeUpperIndex = wrapIndex
/* Ceil the decrement step to always be able to go back to a smaller/fitting
candidate sequence on overshoot, even if the index difference is just one */
wrapIndexStep = (rangeUpperIndex - rangeLowerIndex + 1) / 2
wrapIndex -= wrapIndexStep
case wrapIndex > 0 && width <= maxWidth:
/* The candidate sequence fits and got stable at a length greater than zero, so
return it appended by the result of this function for the rest of the line */
result := []string{ candidate }
rest := strings.Join(parts[wrapIndex:], sep)
restResult := wrapLine(rest, textSize, textStyle, maxWidth)
return append(result, restResult...)
case wrapIndex < 2 && sep != "":
/* The candidate sequence got stable at a length of zero or one words (and the remaining
word does not fit), so retry with splitting the line into characters instead of words */
sep = ""
break innerLoop
case wrapIndex < 2:
/* The candidate sequence got stable at a length of zero or one characters (and the remaining character
does not fit), so just return it appended by the result of this function for the rest of the line */
result := []string{ candidate }
rest := strings.Join(parts[wrapIndex:], sep)
restResult := wrapLine(rest, textSize, textStyle, maxWidth)
return append(result, restResult...)
}
}
}
}
func (w *CustomLabel) wrapLines(lines []string) []string {
if w.LineWrapping {
linesWrapped := make([]string, 0, len(lines))
maxWidth := w.Size().Width - w.TextPaddings.Left - w.TextPaddings.Right
for _, line := range lines {
subLines := wrapLine(line, w.TextSize, w.TextStyle, maxWidth)
linesWrapped = append(linesWrapped, subLines...)
}
return linesWrapped
} else {
return lines
}
}
/* ================================================================================ Public rendering methods */
func (w *CustomLabel) CreateRenderer() fyne.WidgetRenderer {
w.ExtendBaseWidget(w)
background := canvas.NewRectangle(w.Style.Background)
background.StrokeColor = w.Style.Stroke
background.StrokeWidth = w.Style.StrokeWidth
lines := strings.Split(w.Text, "\n")
linesWrapped := w.wrapLines(lines)
textCanvases := make([]*canvas.Text, len(linesWrapped))
for i, line := range linesWrapped {
// textCanvas := &canvas.Text{ Alignment: w.Alignment, Color: w.Color, Text: line, TextSize: w.TextSize, TextStyle: w.TextStyle }
textCanvas := canvas.NewText(line, w.Style.Foreground)
textCanvas.Alignment = w.Alignment
textCanvas.TextSize = w.TextSize
textCanvas.TextStyle = w.TextStyle
textCanvases[i] = textCanvas
}
return &customLabelRenderer{ background, &textCanvases, w }
}
func (r customLabelRenderer) Layout(size fyne.Size) {
r.Refresh()
backgroundSize := fyne.NewSize(size.Width - r.w.BackgroundPaddings.Left - r.w.BackgroundPaddings.Right, size.Height - r.w.BackgroundPaddings.Top - r.w.BackgroundPaddings.Bottom)
r.background.Resize(backgroundSize)
r.background.Move(fyne.NewPos(r.w.BackgroundPaddings.Left, r.w.BackgroundPaddings.Top))
textSize := fyne.NewSize(size.Width - r.w.TextPaddings.Left - r.w.TextPaddings.Right, size.Height - r.w.TextPaddings.Top - r.w.TextPaddings.Bottom)
lineCount := len(*r.textCanvases)
lineHeight := textSize.Height / float32(lineCount)
heightOffset := r.w.TextPaddings.Top
for _, textCanvas := range *r.textCanvases {
textCanvas.Resize(fyne.NewSize(textSize.Width, lineHeight))
textCanvas.Move(fyne.NewPos(r.w.TextPaddings.Left, heightOffset))
heightOffset += lineHeight
}
}
func (r customLabelRenderer) MinSize() fyne.Size {
lines := strings.Split(r.w.Text, "\n")
linesWrapped := r.w.wrapLines(lines)
maxLineWidth := float32(0)
blockHeight := float32(0)
for _, line := range linesWrapped {
lineSize := fyne.MeasureText(line, r.w.TextSize, r.w.TextStyle)
maxLineWidth = fyne.Max(maxLineWidth, lineSize.Width)
blockHeight += lineSize.Height
}
return fyne.NewSize(maxLineWidth + r.w.TextPaddings.Left + r.w.TextPaddings.Right, blockHeight + r.w.TextPaddings.Top + r.w.TextPaddings.Bottom)
}
func (r customLabelRenderer) Refresh() {
r.background.FillColor = r.w.Style.Background
r.background.StrokeColor = r.w.Style.Stroke
r.background.Refresh()
lines := strings.Split(r.w.Text, "\n")
linesWrapped := r.w.wrapLines(lines)
canvasesCount := len(*r.textCanvases)
for i, line := range linesWrapped {
var textCanvas *canvas.Text
if i < canvasesCount {
textCanvas = (*r.textCanvases)[i]
textCanvas.Text = line
textCanvas.Color = r.w.Style.Foreground
} else {
textCanvas = canvas.NewText(line, r.w.Style.Foreground)
}
textCanvas.Alignment = r.w.Alignment
textCanvas.TextSize = r.w.TextSize
textCanvas.TextStyle = r.w.TextStyle
if i < canvasesCount {
(*r.textCanvases)[i] = textCanvas
textCanvas.Refresh()
} else {
*r.textCanvases = append(*r.textCanvases, textCanvas)
}
}
*r.textCanvases = (*r.textCanvases)[:len(linesWrapped)]
}
func (r customLabelRenderer) Objects() []fyne.CanvasObject {
objectCount := len(*r.textCanvases) + 1
objects := make([]fyne.CanvasObject, objectCount)
objects[0] = r.background
for i, textCanvas := range *r.textCanvases {
objects[i + 1] = textCanvas
}
return objects
}
func (r customLabelRenderer) Destroy() {
}