-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtextgrid.go
630 lines (531 loc) · 15.8 KB
/
textgrid.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package widget
import (
"image/color"
"math"
"strconv"
"strings"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/painter"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
)
const (
textAreaSpaceSymbol = '·'
textAreaTabSymbol = '→'
textAreaNewLineSymbol = '↵'
)
var (
// TextGridStyleDefault is a default style for test grid cells
TextGridStyleDefault TextGridStyle
// TextGridStyleWhitespace is the style used for whitespace characters, if enabled
TextGridStyleWhitespace TextGridStyle
)
// TextGridCell represents a single cell in a text grid.
// It has a rune for the text content and a style associated with it.
type TextGridCell struct {
Rune rune
Style TextGridStyle
}
// TextGridRow represents a row of cells cell in a text grid.
// It contains the cells for the row and an optional style.
type TextGridRow struct {
Cells []TextGridCell
Style TextGridStyle
}
// TextGridStyle defines a style that can be applied to a TextGrid cell.
type TextGridStyle interface {
Style() fyne.TextStyle
TextColor() color.Color
BackgroundColor() color.Color
}
// CustomTextGridStyle is a utility type for those not wanting to define their own style types.
type CustomTextGridStyle struct {
// Since: 2.5
TextStyle fyne.TextStyle
FGColor, BGColor color.Color
}
// TextColor is the color a cell should use for the text.
func (c *CustomTextGridStyle) TextColor() color.Color {
return c.FGColor
}
// BackgroundColor is the color a cell should use for the background.
func (c *CustomTextGridStyle) BackgroundColor() color.Color {
return c.BGColor
}
// Style is the text style a cell should use.
func (c *CustomTextGridStyle) Style() fyne.TextStyle {
return c.TextStyle
}
// TextGrid is a monospaced grid of characters.
// This is designed to be used by a text editor, code preview or terminal emulator.
type TextGrid struct {
BaseWidget
Rows []TextGridRow
ShowLineNumbers bool
ShowWhitespace bool
TabWidth int // If set to 0 the fyne.DefaultTabWidth is used
}
// MinSize returns the smallest size this widget can shrink to
func (t *TextGrid) MinSize() fyne.Size {
t.ExtendBaseWidget(t)
return t.BaseWidget.MinSize()
}
// Resize is called when this widget changes size. We should make sure that we refresh cells.
func (t *TextGrid) Resize(size fyne.Size) {
t.BaseWidget.Resize(size)
t.Refresh()
}
// SetText updates the buffer of this textgrid to contain the specified text.
// New lines and columns will be added as required. Lines are separated by '\n'.
// The grid will use default text style and any previous content and style will be removed.
// Tab characters are padded with spaces to the next tab stop.
func (t *TextGrid) SetText(text string) {
lines := strings.Split(text, "\n")
rows := make([]TextGridRow, len(lines))
for i, line := range lines {
cells := make([]TextGridCell, 0, len(line))
for _, r := range line {
cells = append(cells, TextGridCell{Rune: r})
if r == '\t' {
col := len(cells)
next := nextTab(col-1, t.tabWidth())
for i := col; i < next; i++ {
cells = append(cells, TextGridCell{Rune: ' '})
}
}
}
rows[i] = TextGridRow{Cells: cells}
}
t.Rows = rows
t.Refresh()
}
// Text returns the contents of the buffer as a single string (with no style information).
// It reconstructs the lines by joining with a `\n` character.
// Tab characters have padded spaces removed.
func (t *TextGrid) Text() string {
count := len(t.Rows) - 1 // newlines
for _, row := range t.Rows {
count += len(row.Cells)
}
if count <= 0 {
return ""
}
runes := make([]rune, 0, count)
for i, row := range t.Rows {
next := 0
for col, cell := range row.Cells {
if col < next {
continue
}
runes = append(runes, cell.Rune)
if cell.Rune == '\t' {
next = nextTab(col, t.tabWidth())
}
}
if i < len(t.Rows)-1 {
runes = append(runes, '\n')
}
}
return string(runes)
}
// Row returns a copy of the content in a specified row as a TextGridRow.
// If the index is out of bounds it returns an empty row object.
func (t *TextGrid) Row(row int) TextGridRow {
if row < 0 || row >= len(t.Rows) {
return TextGridRow{}
}
return t.Rows[row]
}
// RowText returns a string representation of the content at the row specified.
// If the index is out of bounds it returns an empty string.
func (t *TextGrid) RowText(row int) string {
rowData := t.Row(row)
count := len(rowData.Cells)
if count <= 0 {
return ""
}
runes := make([]rune, 0, count)
next := 0
for col, cell := range rowData.Cells {
if col < next {
continue
}
runes = append(runes, cell.Rune)
if cell.Rune == '\t' {
next = nextTab(col, t.tabWidth())
}
}
return string(runes)
}
// SetRow updates the specified row of the grid's contents using the specified content and style and then refreshes.
// If the row is beyond the end of the current buffer it will be expanded.
// Tab characters are not padded with spaces.
func (t *TextGrid) SetRow(row int, content TextGridRow) {
if row < 0 {
return
}
for len(t.Rows) <= row {
t.Rows = append(t.Rows, TextGridRow{})
}
t.Rows[row] = content
for col := 0; col > len(content.Cells); col++ {
t.refreshCell(row, col)
}
}
// SetRowStyle sets a grid style to all the cells cell at the specified row.
// Any cells in this row with their own style will override this value when displayed.
func (t *TextGrid) SetRowStyle(row int, style TextGridStyle) {
if row < 0 {
return
}
for len(t.Rows) <= row {
t.Rows = append(t.Rows, TextGridRow{})
}
t.Rows[row].Style = style
}
// SetCell sets a grid data to the cell at named row and column.
func (t *TextGrid) SetCell(row, col int, cell TextGridCell) {
if row < 0 || col < 0 {
return
}
t.ensureCells(row, col)
t.Rows[row].Cells[col] = cell
t.refreshCell(row, col)
}
// SetRune sets a character to the cell at named row and column.
func (t *TextGrid) SetRune(row, col int, r rune) {
if row < 0 || col < 0 {
return
}
t.ensureCells(row, col)
t.Rows[row].Cells[col].Rune = r
t.refreshCell(row, col)
}
// SetStyle sets a grid style to the cell at named row and column.
func (t *TextGrid) SetStyle(row, col int, style TextGridStyle) {
if row < 0 || col < 0 {
return
}
t.ensureCells(row, col)
t.Rows[row].Cells[col].Style = style
t.refreshCell(row, col)
}
// SetStyleRange sets a grid style to all the cells between the start row and column through to the end row and column.
func (t *TextGrid) SetStyleRange(startRow, startCol, endRow, endCol int, style TextGridStyle) {
if startRow >= len(t.Rows) || endRow < 0 {
return
}
if startRow < 0 {
startRow = 0
startCol = 0
}
if endRow >= len(t.Rows) {
endRow = len(t.Rows) - 1
endCol = len(t.Rows[endRow].Cells) - 1
}
if startRow == endRow {
for col := startCol; col <= endCol; col++ {
t.SetStyle(startRow, col, style)
}
return
}
// first row
for col := startCol; col < len(t.Rows[startRow].Cells); col++ {
t.SetStyle(startRow, col, style)
}
// possible middle rows
for rowNum := startRow + 1; rowNum < endRow; rowNum++ {
for col := 0; col < len(t.Rows[rowNum].Cells); col++ {
t.SetStyle(rowNum, col, style)
}
}
// last row
for col := 0; col <= endCol; col++ {
t.SetStyle(endRow, col, style)
}
}
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (t *TextGrid) CreateRenderer() fyne.WidgetRenderer {
t.ExtendBaseWidget(t)
render := &textGridRenderer{text: t}
render.updateCellSize()
th := t.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
TextGridStyleDefault = &CustomTextGridStyle{}
TextGridStyleWhitespace = &CustomTextGridStyle{FGColor: th.Color(theme.ColorNameDisabled, v)}
return render
}
func (t *TextGrid) ensureCells(row, col int) {
for len(t.Rows) <= row {
t.Rows = append(t.Rows, TextGridRow{})
}
data := t.Rows[row]
for len(data.Cells) <= col {
data.Cells = append(data.Cells, TextGridCell{})
t.Rows[row] = data
}
}
func (t *TextGrid) refreshCell(row, col int) {
r := cache.Renderer(t).(*textGridRenderer)
r.refreshCell(row, col)
}
// NewTextGrid creates a new empty TextGrid widget.
func NewTextGrid() *TextGrid {
grid := &TextGrid{}
grid.ExtendBaseWidget(grid)
return grid
}
// NewTextGridFromString creates a new TextGrid widget with the specified string content.
func NewTextGridFromString(content string) *TextGrid {
grid := NewTextGrid()
grid.SetText(content)
return grid
}
// nextTab finds the column of the next tab stop for the given column
func nextTab(column int, tabWidth int) int {
tabStop, _ := math.Modf(float64(column+tabWidth) / float64(tabWidth))
return tabWidth * int(tabStop)
}
type textGridRenderer struct {
text *TextGrid
cols, rows int
cellSize fyne.Size
objects []fyne.CanvasObject
current fyne.Canvas
}
func (t *textGridRenderer) appendTextCell(str rune) {
th := t.text.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
text := canvas.NewText(string(str), th.Color(theme.ColorNameForeground, v))
text.TextStyle.Monospace = true
bg := canvas.NewRectangle(color.Transparent)
ul := canvas.NewLine(color.Transparent)
t.objects = append(t.objects, bg, text, ul)
}
func (t *textGridRenderer) refreshCell(row, col int) {
pos := row*t.cols + col
if pos*3+1 >= len(t.objects) {
return
}
cell := t.text.Rows[row].Cells[col]
t.setCellRune(cell.Rune, pos, cell.Style, t.text.Rows[row].Style)
}
func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGridStyle) {
if str == 0 {
str = ' '
}
rect := t.objects[pos*3].(*canvas.Rectangle)
text := t.objects[pos*3+1].(*canvas.Text)
underline := t.objects[pos*3+2].(*canvas.Line)
th := t.text.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
fg := th.Color(theme.ColorNameForeground, v)
text.TextSize = th.Size(theme.SizeNameText)
var underlineStrokeWidth float32 = 1
var underlineStrokeColor color.Color = color.Transparent
textStyle := fyne.TextStyle{}
if style != nil {
textStyle = style.Style()
} else if rowStyle != nil {
textStyle = rowStyle.Style()
}
if textStyle.Bold {
underlineStrokeWidth = 2
}
if textStyle.Underline {
underlineStrokeColor = fg
}
textStyle.Monospace = true
if style != nil && style.TextColor() != nil {
fg = style.TextColor()
} else if rowStyle != nil && rowStyle.TextColor() != nil {
fg = rowStyle.TextColor()
}
newStr := string(str)
if text.Text != newStr || text.Color != fg || textStyle != text.TextStyle {
text.Text = newStr
text.Color = fg
text.TextStyle = textStyle
t.refresh(text)
}
if underlineStrokeWidth != underline.StrokeWidth || underlineStrokeColor != underline.StrokeColor {
underline.StrokeWidth, underline.StrokeColor = underlineStrokeWidth, underlineStrokeColor
t.refresh(underline)
}
bg := color.Color(color.Transparent)
if style != nil && style.BackgroundColor() != nil {
bg = style.BackgroundColor()
} else if rowStyle != nil && rowStyle.BackgroundColor() != nil {
bg = rowStyle.BackgroundColor()
}
if rect.FillColor != bg {
rect.FillColor = bg
t.refresh(rect)
}
}
func (t *textGridRenderer) addCellsIfRequired() {
cellCount := t.cols * t.rows
if len(t.objects) == cellCount*3 {
return
}
for i := len(t.objects); i < cellCount*3; i += 3 {
t.appendTextCell(' ')
}
}
func (t *textGridRenderer) refreshGrid() {
line := 1
x := 0
for rowIndex, row := range t.text.Rows {
rowStyle := row.Style
i := 0
if t.text.ShowLineNumbers {
lineStr := []rune(strconv.Itoa(line))
pad := t.lineNumberWidth() - len(lineStr)
for ; i < pad; i++ {
t.setCellRune(' ', x, TextGridStyleWhitespace, rowStyle) // padding space
x++
}
for c := 0; c < len(lineStr); c++ {
t.setCellRune(lineStr[c], x, TextGridStyleDefault, rowStyle) // line numbers
i++
x++
}
t.setCellRune('|', x, TextGridStyleWhitespace, rowStyle) // last space
i++
x++
}
for _, r := range row.Cells {
if i >= t.cols { // would be an overflow - bad
continue
}
if t.text.ShowWhitespace && (r.Rune == ' ' || r.Rune == '\t') {
sym := textAreaSpaceSymbol
if r.Rune == '\t' {
sym = textAreaTabSymbol
}
if r.Style != nil && r.Style.BackgroundColor() != nil {
whitespaceBG := &CustomTextGridStyle{FGColor: TextGridStyleWhitespace.TextColor(),
BGColor: r.Style.BackgroundColor()}
t.setCellRune(sym, x, whitespaceBG, rowStyle) // whitespace char
} else {
t.setCellRune(sym, x, TextGridStyleWhitespace, rowStyle) // whitespace char
}
} else {
t.setCellRune(r.Rune, x, r.Style, rowStyle) // regular char
}
i++
x++
}
if t.text.ShowWhitespace && i < t.cols && rowIndex < len(t.text.Rows)-1 {
t.setCellRune(textAreaNewLineSymbol, x, TextGridStyleWhitespace, rowStyle) // newline
i++
x++
}
for ; i < t.cols; i++ {
t.setCellRune(' ', x, TextGridStyleDefault, rowStyle) // blanks
x++
}
line++
}
for ; x < len(t.objects)/3; x++ {
t.setCellRune(' ', x, TextGridStyleDefault, nil) // trailing cells and blank lines
}
}
// tabWidth either returns the set tab width or if not set the returns the DefaultTabWidth
func (t *TextGrid) tabWidth() int {
if t.TabWidth == 0 {
return painter.DefaultTabWidth
}
return t.TabWidth
}
func (t *textGridRenderer) lineNumberWidth() int {
return len(strconv.Itoa(t.rows + 1))
}
func (t *textGridRenderer) updateGridSize(size fyne.Size) {
bufRows := len(t.text.Rows)
bufCols := 0
for _, row := range t.text.Rows {
bufCols = int(math.Max(float64(bufCols), float64(len(row.Cells))))
}
sizeCols := math.Floor(float64(size.Width) / float64(t.cellSize.Width))
sizeRows := math.Floor(float64(size.Height) / float64(t.cellSize.Height))
if t.text.ShowWhitespace {
bufCols++
}
if t.text.ShowLineNumbers {
bufCols += t.lineNumberWidth()
}
t.cols = int(math.Max(sizeCols, float64(bufCols)))
t.rows = int(math.Max(sizeRows, float64(bufRows)))
t.addCellsIfRequired()
}
func (t *textGridRenderer) Layout(size fyne.Size) {
t.updateGridSize(size)
i := 0
cellPos := fyne.NewPos(0, 0)
for y := 0; y < t.rows; y++ {
for x := 0; x < t.cols; x++ {
// rect
t.objects[i*3].Resize(t.cellSize)
t.objects[i*3].Move(cellPos)
// text
t.objects[i*3+1].Move(cellPos)
// underline
t.objects[i*3+2].Move(cellPos.Add(fyne.Position{X: 0, Y: t.cellSize.Height}))
t.objects[i*3+2].Resize(fyne.Size{Width: t.cellSize.Width})
cellPos.X += t.cellSize.Width
i++
}
cellPos.X = 0
cellPos.Y += t.cellSize.Height
}
}
func (t *textGridRenderer) MinSize() fyne.Size {
longestRow := float32(0)
for _, row := range t.text.Rows {
longestRow = fyne.Max(longestRow, float32(len(row.Cells)))
}
return fyne.NewSize(t.cellSize.Width*longestRow,
t.cellSize.Height*float32(len(t.text.Rows)))
}
func (t *textGridRenderer) Refresh() {
// we may be on a new canvas, so just update it to be sure
if fyne.CurrentApp() != nil && fyne.CurrentApp().Driver() != nil {
t.current = fyne.CurrentApp().Driver().CanvasForObject(t.text)
}
// theme could change text size
t.updateCellSize()
th := t.text.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
TextGridStyleWhitespace = &CustomTextGridStyle{FGColor: th.Color(theme.ColorNameDisabled, v)}
t.updateGridSize(t.text.Size())
t.refreshGrid()
}
func (t *textGridRenderer) ApplyTheme() {
}
func (t *textGridRenderer) Objects() []fyne.CanvasObject {
return t.objects
}
func (t *textGridRenderer) Destroy() {
}
func (t *textGridRenderer) refresh(obj fyne.CanvasObject) {
if t.current == nil {
if fyne.CurrentApp() != nil && fyne.CurrentApp().Driver() != nil {
// cache canvas for this widget, so we don't look it up many times for every cell/row refresh!
t.current = fyne.CurrentApp().Driver().CanvasForObject(t.text)
}
if t.current == nil {
return // not yet set up perhaps?
}
}
t.current.Refresh(obj)
}
func (t *textGridRenderer) updateCellSize() {
th := t.text.Theme()
size := fyne.MeasureText("M", th.Size(theme.SizeNameText), fyne.TextStyle{Monospace: true})
// round it for seamless background
size.Width = float32(math.Round(float64(size.Width)))
size.Height = float32(math.Round(float64(size.Height)))
t.cellSize = size
}