@@ -7,10 +7,11 @@ import (
7
7
"strconv"
8
8
"time"
9
9
10
+ "fyne.io/fyne/v2/widget"
11
+
10
12
"fyne.io/fyne/v2"
11
13
"fyne.io/fyne/v2/canvas"
12
14
"fyne.io/fyne/v2/theme"
13
- "fyne.io/fyne/v2/widget"
14
15
)
15
16
16
17
type termGridRenderer struct {
@@ -27,44 +28,86 @@ type termGridRenderer struct {
27
28
}
28
29
29
30
func (t * termGridRenderer ) appendTextCell (str rune ) {
30
- text := canvas .NewText (string (str ), theme .ForegroundColor ())
31
+ th := t .text .Theme ()
32
+ v := fyne .CurrentApp ().Settings ().ThemeVariant ()
33
+
34
+ text := canvas .NewText (string (str ), th .Color (theme .ColorNameForeground , v ))
31
35
text .TextStyle .Monospace = true
32
36
33
37
bg := canvas .NewRectangle (color .Transparent )
34
- t .objects = append (t .objects , bg , text )
38
+
39
+ ul := canvas .NewLine (color .Transparent )
40
+
41
+ t .objects = append (t .objects , bg , text , ul )
42
+ }
43
+
44
+ func (t * termGridRenderer ) refreshCell (row , col int ) {
45
+ pos := row * t .cols + col
46
+ if pos * 3 + 1 >= len (t .objects ) {
47
+ return
48
+ }
49
+
50
+ cell := t .text .Rows [row ].Cells [col ]
51
+ t .setCellRune (cell .Rune , pos , cell .Style )
35
52
}
36
53
37
54
func (t * termGridRenderer ) setCellRune (str rune , pos int , style widget.TextGridStyle ) {
38
55
if str == 0 {
39
56
str = ' '
40
57
}
41
- fg := theme .ForegroundColor ()
58
+ rect := t .objects [pos * 3 ].(* canvas.Rectangle )
59
+ text := t .objects [pos * 3 + 1 ].(* canvas.Text )
60
+ underline := t .objects [pos * 3 + 2 ].(* canvas.Line )
61
+
62
+ th := t .text .Theme ()
63
+ v := fyne .CurrentApp ().Settings ().ThemeVariant ()
64
+ fg := th .Color (theme .ColorNameForeground , v )
65
+ bg := color .Color (color .Transparent )
66
+ text .TextSize = th .Size (theme .SizeNameText )
67
+ textStyle := fyne.TextStyle {}
68
+ var underlineStrokeWidth float32 = 1
69
+ var underlineStrokeColor color.Color = color .Transparent
70
+
42
71
if style != nil && style .TextColor () != nil {
43
72
fg = style .TextColor ()
44
73
}
45
- bg := color .Color (color .Transparent )
46
- if style != nil && style .BackgroundColor () != nil {
47
- bg = style .BackgroundColor ()
74
+
75
+ if style != nil {
76
+ if style .BackgroundColor () != nil {
77
+ bg = style .BackgroundColor ()
78
+ }
79
+ if style .Style ().Bold {
80
+ underlineStrokeWidth = 2
81
+ textStyle = fyne.TextStyle {
82
+ Bold : true ,
83
+ }
84
+ }
85
+ if style .Style ().Underline {
86
+ underlineStrokeColor = fg
87
+ }
48
88
}
49
89
50
90
if s , ok := style .(* TermTextGridStyle ); ok && s != nil && s .BlinkEnabled {
51
91
t .shouldBlink = true
52
92
if t .blink {
53
93
fg = bg
94
+ underlineStrokeColor = bg
54
95
}
55
96
}
56
97
57
- text := t .objects [pos * 2 + 1 ].(* canvas.Text )
58
- text .TextSize = theme .TextSize ()
59
-
60
98
newStr := string (str )
61
- if text .Text != newStr || text .Color != fg {
99
+ if text .Text != newStr || text .Color != fg || textStyle != text . TextStyle {
62
100
text .Text = newStr
63
101
text .Color = fg
102
+ text .TextStyle = textStyle
64
103
t .refresh (text )
65
104
}
66
105
67
- rect := t .objects [pos * 2 ].(* canvas.Rectangle )
106
+ if underlineStrokeWidth != underline .StrokeWidth || underlineStrokeColor != underline .StrokeColor {
107
+ underline .StrokeWidth , underline .StrokeColor = underlineStrokeWidth , underlineStrokeColor
108
+ t .refresh (underline )
109
+ }
110
+
68
111
if rect .FillColor != bg {
69
112
rect .FillColor = bg
70
113
t .refresh (rect )
@@ -73,10 +116,10 @@ func (t *termGridRenderer) setCellRune(str rune, pos int, style widget.TextGridS
73
116
74
117
func (t * termGridRenderer ) addCellsIfRequired () {
75
118
cellCount := t .cols * t .rows
76
- if len (t .objects ) == cellCount * 2 {
119
+ if len (t .objects ) == cellCount * 3 {
77
120
return
78
121
}
79
- for i := len (t .objects ); i < cellCount * 2 ; i += 2 {
122
+ for i := len (t .objects ); i < cellCount * 3 ; i += 3 {
80
123
t .appendTextCell (' ' )
81
124
}
82
125
}
@@ -141,7 +184,7 @@ func (t *termGridRenderer) refreshGrid() {
141
184
142
185
line ++
143
186
}
144
- for ; x < len (t .objects )/ 2 ; x ++ {
187
+ for ; x < len (t .objects )/ 3 ; x ++ {
145
188
t .setCellRune (' ' , x , widget .TextGridStyleDefault ) // trailing cells and blank lines
146
189
}
147
190
@@ -209,10 +252,17 @@ func (t *termGridRenderer) Layout(size fyne.Size) {
209
252
cellPos := fyne .NewPos (0 , 0 )
210
253
for y := 0 ; y < t .rows ; y ++ {
211
254
for x := 0 ; x < t .cols ; x ++ {
212
- t .objects [i * 2 + 1 ].Move (cellPos )
255
+ // rect
256
+ t .objects [i * 3 ].Resize (t .cellSize )
257
+ t .objects [i * 3 ].Move (cellPos )
258
+
259
+ // text
260
+ t .objects [i * 3 + 1 ].Move (cellPos )
261
+
262
+ // underline
263
+ t .objects [i * 3 + 2 ].Move (cellPos .Add (fyne.Position {X : 0 , Y : t .cellSize .Height }))
264
+ t .objects [i * 3 + 2 ].Resize (fyne.Size {Width : t .cellSize .Width })
213
265
214
- t .objects [i * 2 ].Resize (t .cellSize )
215
- t .objects [i * 2 ].Move (cellPos )
216
266
cellPos .X += t .cellSize .Width
217
267
i ++
218
268
}
@@ -240,7 +290,9 @@ func (t *termGridRenderer) Refresh() {
240
290
// theme could change text size
241
291
t .updateCellSize ()
242
292
243
- widget .TextGridStyleWhitespace = & widget.CustomTextGridStyle {FGColor : theme .DisabledColor ()}
293
+ th := t .text .Theme ()
294
+ v := fyne .CurrentApp ().Settings ().ThemeVariant ()
295
+ widget .TextGridStyleWhitespace = & widget.CustomTextGridStyle {FGColor : th .Color (theme .ColorNameDisabled , v )}
244
296
t .updateGridSize (t .text .Size ())
245
297
t .refreshGrid ()
246
298
}
@@ -271,11 +323,12 @@ func (t *termGridRenderer) refresh(obj fyne.CanvasObject) {
271
323
}
272
324
273
325
func (t * termGridRenderer ) updateCellSize () {
274
- size := fyne .MeasureText ("M" , theme .TextSize (), fyne.TextStyle {Monospace : true })
326
+ th := t .text .Theme ()
327
+ size := fyne .MeasureText ("M" , th .Size (theme .SizeNameText ), fyne.TextStyle {Monospace : true })
275
328
276
329
// round it for seamless background
277
- size .Width = float32 (math .Round (float64 (( size .Width ) )))
278
- size .Height = float32 (math .Round (float64 (( size .Height ) )))
330
+ size .Width = float32 (math .Round (float64 (size .Width )))
331
+ size .Height = float32 (math .Round (float64 (size .Height )))
279
332
280
333
t .cellSize = size
281
334
}
0 commit comments