Skip to content

Commit 707591b

Browse files
committed
Update renderer with latest from fyne widget
1 parent 7d504a2 commit 707591b

File tree

1 file changed

+75
-22
lines changed

1 file changed

+75
-22
lines changed

internal/widget/renderer.go

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"strconv"
88
"time"
99

10+
"fyne.io/fyne/v2/widget"
11+
1012
"fyne.io/fyne/v2"
1113
"fyne.io/fyne/v2/canvas"
1214
"fyne.io/fyne/v2/theme"
13-
"fyne.io/fyne/v2/widget"
1415
)
1516

1617
type termGridRenderer struct {
@@ -27,44 +28,86 @@ type termGridRenderer struct {
2728
}
2829

2930
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))
3135
text.TextStyle.Monospace = true
3236

3337
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)
3552
}
3653

3754
func (t *termGridRenderer) setCellRune(str rune, pos int, style widget.TextGridStyle) {
3855
if str == 0 {
3956
str = ' '
4057
}
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+
4271
if style != nil && style.TextColor() != nil {
4372
fg = style.TextColor()
4473
}
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+
}
4888
}
4989

5090
if s, ok := style.(*TermTextGridStyle); ok && s != nil && s.BlinkEnabled {
5191
t.shouldBlink = true
5292
if t.blink {
5393
fg = bg
94+
underlineStrokeColor = bg
5495
}
5596
}
5697

57-
text := t.objects[pos*2+1].(*canvas.Text)
58-
text.TextSize = theme.TextSize()
59-
6098
newStr := string(str)
61-
if text.Text != newStr || text.Color != fg {
99+
if text.Text != newStr || text.Color != fg || textStyle != text.TextStyle {
62100
text.Text = newStr
63101
text.Color = fg
102+
text.TextStyle = textStyle
64103
t.refresh(text)
65104
}
66105

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+
68111
if rect.FillColor != bg {
69112
rect.FillColor = bg
70113
t.refresh(rect)
@@ -73,10 +116,10 @@ func (t *termGridRenderer) setCellRune(str rune, pos int, style widget.TextGridS
73116

74117
func (t *termGridRenderer) addCellsIfRequired() {
75118
cellCount := t.cols * t.rows
76-
if len(t.objects) == cellCount*2 {
119+
if len(t.objects) == cellCount*3 {
77120
return
78121
}
79-
for i := len(t.objects); i < cellCount*2; i += 2 {
122+
for i := len(t.objects); i < cellCount*3; i += 3 {
80123
t.appendTextCell(' ')
81124
}
82125
}
@@ -141,7 +184,7 @@ func (t *termGridRenderer) refreshGrid() {
141184

142185
line++
143186
}
144-
for ; x < len(t.objects)/2; x++ {
187+
for ; x < len(t.objects)/3; x++ {
145188
t.setCellRune(' ', x, widget.TextGridStyleDefault) // trailing cells and blank lines
146189
}
147190

@@ -209,10 +252,17 @@ func (t *termGridRenderer) Layout(size fyne.Size) {
209252
cellPos := fyne.NewPos(0, 0)
210253
for y := 0; y < t.rows; y++ {
211254
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})
213265

214-
t.objects[i*2].Resize(t.cellSize)
215-
t.objects[i*2].Move(cellPos)
216266
cellPos.X += t.cellSize.Width
217267
i++
218268
}
@@ -240,7 +290,9 @@ func (t *termGridRenderer) Refresh() {
240290
// theme could change text size
241291
t.updateCellSize()
242292

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)}
244296
t.updateGridSize(t.text.Size())
245297
t.refreshGrid()
246298
}
@@ -271,11 +323,12 @@ func (t *termGridRenderer) refresh(obj fyne.CanvasObject) {
271323
}
272324

273325
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})
275328

276329
// 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)))
279332

280333
t.cellSize = size
281334
}

0 commit comments

Comments
 (0)