Skip to content

Commit

Permalink
Support changing text in TextGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed May 19, 2019
1 parent 9661a80 commit 22bc250
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
48 changes: 39 additions & 9 deletions widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func (t *TextGrid) Hide() {
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (t *TextGrid) CreateRenderer() fyne.WidgetRenderer {
render := &textGridRender{text: t}
t.updated = render.update
t.updateRowBounds()
render.ensureGrid()
render.update()

cell := canvas.NewText("M", color.White)
cell.TextStyle.Monospace = true
Expand All @@ -81,15 +82,25 @@ type textGridRender struct {
objects []fyne.CanvasObject
}

func newTextCell(str rune) *canvas.Text {
func (t *textGridRender) appendTextCell(str rune) {
text := canvas.NewText(string(str), theme.TextColor())
text.TextStyle.Monospace = true

t.objects = append(t.objects, text)
}

func (t *textGridRender) setCellRune(str rune, pos int) {
text := t.objects[pos].(*canvas.Text)
text.Text = string(str)

if str == textAreaSpaceSymbol || str == textAreaTabSymbol || str == textAreaNewLineSymbol {
text.Color = theme.PlaceHolderColor()
}
}

return text
func (t *textGridRender) update() {
t.ensureGrid()
t.refreshGrid()
}

func (t *textGridRender) ensureGrid() {
Expand All @@ -101,37 +112,55 @@ func (t *textGridRender) ensureGrid() {
t.cols += t.lineCountWidth() + 1
}
t.rows = t.text.rows()

cellCount := t.cols * t.rows
if len(t.objects) == cellCount {
return
}
for i := len(t.objects); i < cellCount; i++ {
t.appendTextCell(' ')
}
}

func (t *textGridRender) refreshGrid() {
line := 1
x := 0

for _, bound := range t.text.rowBounds {
i := 0
if t.text.LineNumbers {
lineStr := []rune(fmt.Sprintf("%d", line))
for c := 0; c < len(lineStr); c++ {
t.objects = append(t.objects, newTextCell(lineStr[c]))
t.setCellRune(lineStr[c], x)
i++
x++
}
for ; i < t.lineCountWidth(); i++ {
t.objects = append(t.objects, newTextCell(' '))
t.setCellRune(' ', x)
x++
}

t.objects = append(t.objects, newTextCell(' '))
t.setCellRune(' ', x)
i++
x++
}
for j := bound[0]; j < bound[1]; j++ {
r := t.text.buffer[j]
if t.text.Whitespace && r == ' ' {
r = textAreaSpaceSymbol
}
t.objects = append(t.objects, newTextCell(r))
t.setCellRune(r, x)
i++
x++
}
if t.text.Whitespace {
t.objects = append(t.objects, newTextCell(textAreaNewLineSymbol))
t.setCellRune(textAreaNewLineSymbol, x)
i++
x++
}
for ; i < t.cols; i++ {
t.objects = append(t.objects, newTextCell(' '))
t.setCellRune(' ', x)
x++
}

line++
Expand Down Expand Up @@ -163,6 +192,7 @@ func (t *textGridRender) MinSize() fyne.Size {
}

func (t *textGridRender) Refresh() {
t.refreshGrid()
}

func (t *textGridRender) ApplyTheme() {
Expand Down
18 changes: 18 additions & 0 deletions widget/textgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func TestNewTextGrid(t *testing.T) {
assert.Equal(t, 1, grid.maxCols)
}

func TestTextGrid_SetText(t *testing.T) {
grid := NewTextGrid("")
grid.SetText("Hello\nthere")

assert.Equal(t, 2, grid.rows())
assert.Equal(t, 5, grid.maxCols)
}

func TestTextGrid_Rows(t *testing.T) {
grid := NewTextGrid("Ab\nC")
Renderer(grid).Refresh()
Expand Down Expand Up @@ -60,3 +68,13 @@ func TestTextGridRender_Whitespace(t *testing.T) {
assert.Equal(t, rend.objects[3].(*canvas.Text).Text, string(textAreaNewLineSymbol)) // col 4 is newline
assert.Equal(t, rend.objects[5].(*canvas.Text).Text, string(textAreaNewLineSymbol)) // col 2 on line 2
}

func TestTextGridRender_Refresh(t *testing.T) {
grid := NewTextGrid("Ab")
rend := Renderer(grid).(*textGridRender)
rend.Refresh()
rend.Refresh()
rend.Refresh() // check we are not appending more content

assert.Equal(t, 2, len(rend.objects))
}

0 comments on commit 22bc250

Please sign in to comment.