textgrid.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package widget
  2. import (
  3. "image/color"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "fyne.io/fyne/v2/internal/cache"
  8. "fyne.io/fyne/v2/internal/painter"
  9. "fyne.io/fyne/v2"
  10. "fyne.io/fyne/v2/canvas"
  11. "fyne.io/fyne/v2/theme"
  12. )
  13. const (
  14. textAreaSpaceSymbol = '·'
  15. textAreaTabSymbol = '→'
  16. textAreaNewLineSymbol = '↵'
  17. )
  18. var (
  19. // TextGridStyleDefault is a default style for test grid cells
  20. TextGridStyleDefault TextGridStyle
  21. // TextGridStyleWhitespace is the style used for whitespace characters, if enabled
  22. TextGridStyleWhitespace TextGridStyle
  23. )
  24. // TextGridCell represents a single cell in a text grid.
  25. // It has a rune for the text content and a style associated with it.
  26. type TextGridCell struct {
  27. Rune rune
  28. Style TextGridStyle
  29. }
  30. // TextGridRow represents a row of cells cell in a text grid.
  31. // It contains the cells for the row and an optional style.
  32. type TextGridRow struct {
  33. Cells []TextGridCell
  34. Style TextGridStyle
  35. }
  36. // TextGridStyle defines a style that can be applied to a TextGrid cell.
  37. type TextGridStyle interface {
  38. TextColor() color.Color
  39. BackgroundColor() color.Color
  40. }
  41. // CustomTextGridStyle is a utility type for those not wanting to define their own style types.
  42. type CustomTextGridStyle struct {
  43. FGColor, BGColor color.Color
  44. }
  45. // TextColor is the color a cell should use for the text.
  46. func (c *CustomTextGridStyle) TextColor() color.Color {
  47. return c.FGColor
  48. }
  49. // BackgroundColor is the color a cell should use for the background.
  50. func (c *CustomTextGridStyle) BackgroundColor() color.Color {
  51. return c.BGColor
  52. }
  53. // TextGrid is a monospaced grid of characters.
  54. // This is designed to be used by a text editor, code preview or terminal emulator.
  55. type TextGrid struct {
  56. BaseWidget
  57. Rows []TextGridRow
  58. ShowLineNumbers bool
  59. ShowWhitespace bool
  60. TabWidth int // If set to 0 the fyne.DefaultTabWidth is used
  61. }
  62. // MinSize returns the smallest size this widget can shrink to
  63. func (t *TextGrid) MinSize() fyne.Size {
  64. t.ExtendBaseWidget(t)
  65. return t.BaseWidget.MinSize()
  66. }
  67. // Resize is called when this widget changes size. We should make sure that we refresh cells.
  68. func (t *TextGrid) Resize(size fyne.Size) {
  69. t.BaseWidget.Resize(size)
  70. t.Refresh()
  71. }
  72. // SetText updates the buffer of this textgrid to contain the specified text.
  73. // New lines and columns will be added as required. Lines are separated by '\n'.
  74. // The grid will use default text style and any previous content and style will be removed.
  75. // Tab characters are padded with spaces to the next tab stop.
  76. func (t *TextGrid) SetText(text string) {
  77. lines := strings.Split(text, "\n")
  78. rows := make([]TextGridRow, len(lines))
  79. for i, line := range lines {
  80. cells := make([]TextGridCell, 0, len(line))
  81. for _, r := range line {
  82. cells = append(cells, TextGridCell{Rune: r})
  83. if r == '\t' {
  84. col := len(cells)
  85. next := nextTab(col-1, t.tabWidth())
  86. for i := col; i < next; i++ {
  87. cells = append(cells, TextGridCell{Rune: ' '})
  88. }
  89. }
  90. }
  91. rows[i] = TextGridRow{Cells: cells}
  92. }
  93. t.Rows = rows
  94. t.Refresh()
  95. }
  96. // Text returns the contents of the buffer as a single string (with no style information).
  97. // It reconstructs the lines by joining with a `\n` character.
  98. // Tab characters have padded spaces removed.
  99. func (t *TextGrid) Text() string {
  100. count := len(t.Rows) - 1 // newlines
  101. for _, row := range t.Rows {
  102. count += len(row.Cells)
  103. }
  104. if count <= 0 {
  105. return ""
  106. }
  107. runes := make([]rune, 0, count)
  108. for i, row := range t.Rows {
  109. next := 0
  110. for col, cell := range row.Cells {
  111. if col < next {
  112. continue
  113. }
  114. runes = append(runes, cell.Rune)
  115. if cell.Rune == '\t' {
  116. next = nextTab(col, t.tabWidth())
  117. }
  118. }
  119. if i < len(t.Rows)-1 {
  120. runes = append(runes, '\n')
  121. }
  122. }
  123. return string(runes)
  124. }
  125. // Row returns a copy of the content in a specified row as a TextGridRow.
  126. // If the index is out of bounds it returns an empty row object.
  127. func (t *TextGrid) Row(row int) TextGridRow {
  128. if row < 0 || row >= len(t.Rows) {
  129. return TextGridRow{}
  130. }
  131. return t.Rows[row]
  132. }
  133. // RowText returns a string representation of the content at the row specified.
  134. // If the index is out of bounds it returns an empty string.
  135. func (t *TextGrid) RowText(row int) string {
  136. rowData := t.Row(row)
  137. count := len(rowData.Cells)
  138. if count <= 0 {
  139. return ""
  140. }
  141. runes := make([]rune, 0, count)
  142. next := 0
  143. for col, cell := range rowData.Cells {
  144. if col < next {
  145. continue
  146. }
  147. runes = append(runes, cell.Rune)
  148. if cell.Rune == '\t' {
  149. next = nextTab(col, t.tabWidth())
  150. }
  151. }
  152. return string(runes)
  153. }
  154. // SetRow updates the specified row of the grid's contents using the specified content and style and then refreshes.
  155. // If the row is beyond the end of the current buffer it will be expanded.
  156. // Tab characters are not padded with spaces.
  157. func (t *TextGrid) SetRow(row int, content TextGridRow) {
  158. if row < 0 {
  159. return
  160. }
  161. for len(t.Rows) <= row {
  162. t.Rows = append(t.Rows, TextGridRow{})
  163. }
  164. t.Rows[row] = content
  165. for col := 0; col > len(content.Cells); col++ {
  166. t.refreshCell(row, col)
  167. }
  168. }
  169. // SetRowStyle sets a grid style to all the cells cell at the specified row.
  170. // Any cells in this row with their own style will override this value when displayed.
  171. func (t *TextGrid) SetRowStyle(row int, style TextGridStyle) {
  172. if row < 0 {
  173. return
  174. }
  175. for len(t.Rows) <= row {
  176. t.Rows = append(t.Rows, TextGridRow{})
  177. }
  178. t.Rows[row].Style = style
  179. }
  180. // SetCell sets a grid data to the cell at named row and column.
  181. func (t *TextGrid) SetCell(row, col int, cell TextGridCell) {
  182. if row < 0 || col < 0 {
  183. return
  184. }
  185. t.ensureCells(row, col)
  186. t.Rows[row].Cells[col] = cell
  187. t.refreshCell(row, col)
  188. }
  189. // SetRune sets a character to the cell at named row and column.
  190. func (t *TextGrid) SetRune(row, col int, r rune) {
  191. if row < 0 || col < 0 {
  192. return
  193. }
  194. t.ensureCells(row, col)
  195. t.Rows[row].Cells[col].Rune = r
  196. t.refreshCell(row, col)
  197. }
  198. // SetStyle sets a grid style to the cell at named row and column.
  199. func (t *TextGrid) SetStyle(row, col int, style TextGridStyle) {
  200. if row < 0 || col < 0 {
  201. return
  202. }
  203. t.ensureCells(row, col)
  204. t.Rows[row].Cells[col].Style = style
  205. t.refreshCell(row, col)
  206. }
  207. // SetStyleRange sets a grid style to all the cells between the start row and column through to the end row and column.
  208. func (t *TextGrid) SetStyleRange(startRow, startCol, endRow, endCol int, style TextGridStyle) {
  209. if startRow >= len(t.Rows) || endRow < 0 {
  210. return
  211. }
  212. if startRow < 0 {
  213. startRow = 0
  214. startCol = 0
  215. }
  216. if endRow >= len(t.Rows) {
  217. endRow = len(t.Rows) - 1
  218. endCol = len(t.Rows[endRow].Cells) - 1
  219. }
  220. if startRow == endRow {
  221. for col := startCol; col <= endCol; col++ {
  222. t.SetStyle(startRow, col, style)
  223. }
  224. return
  225. }
  226. // first row
  227. for col := startCol; col < len(t.Rows[startRow].Cells); col++ {
  228. t.SetStyle(startRow, col, style)
  229. }
  230. // possible middle rows
  231. for rowNum := startRow + 1; rowNum < endRow; rowNum++ {
  232. for col := 0; col < len(t.Rows[rowNum].Cells); col++ {
  233. t.SetStyle(rowNum, col, style)
  234. }
  235. }
  236. // last row
  237. for col := 0; col <= endCol; col++ {
  238. t.SetStyle(endRow, col, style)
  239. }
  240. }
  241. // CreateRenderer is a private method to Fyne which links this widget to it's renderer
  242. func (t *TextGrid) CreateRenderer() fyne.WidgetRenderer {
  243. t.ExtendBaseWidget(t)
  244. render := &textGridRenderer{text: t}
  245. render.updateCellSize()
  246. TextGridStyleDefault = &CustomTextGridStyle{}
  247. TextGridStyleWhitespace = &CustomTextGridStyle{FGColor: theme.DisabledColor()}
  248. return render
  249. }
  250. func (t *TextGrid) ensureCells(row, col int) {
  251. for len(t.Rows) <= row {
  252. t.Rows = append(t.Rows, TextGridRow{})
  253. }
  254. data := t.Rows[row]
  255. for len(data.Cells) <= col {
  256. data.Cells = append(data.Cells, TextGridCell{})
  257. t.Rows[row] = data
  258. }
  259. }
  260. func (t *TextGrid) refreshCell(row, col int) {
  261. r := cache.Renderer(t).(*textGridRenderer)
  262. r.refreshCell(row, col)
  263. }
  264. // NewTextGrid creates a new empty TextGrid widget.
  265. func NewTextGrid() *TextGrid {
  266. grid := &TextGrid{}
  267. grid.ExtendBaseWidget(grid)
  268. return grid
  269. }
  270. // NewTextGridFromString creates a new TextGrid widget with the specified string content.
  271. func NewTextGridFromString(content string) *TextGrid {
  272. grid := NewTextGrid()
  273. grid.SetText(content)
  274. return grid
  275. }
  276. // nextTab finds the column of the next tab stop for the given column
  277. func nextTab(column int, tabWidth int) int {
  278. tabStop, _ := math.Modf(float64(column+tabWidth) / float64(tabWidth))
  279. return tabWidth * int(tabStop)
  280. }
  281. type textGridRenderer struct {
  282. text *TextGrid
  283. cols, rows int
  284. cellSize fyne.Size
  285. objects []fyne.CanvasObject
  286. }
  287. func (t *textGridRenderer) appendTextCell(str rune) {
  288. text := canvas.NewText(string(str), theme.ForegroundColor())
  289. text.TextStyle.Monospace = true
  290. bg := canvas.NewRectangle(color.Transparent)
  291. t.objects = append(t.objects, bg, text)
  292. }
  293. func (t *textGridRenderer) refreshCell(row, col int) {
  294. pos := row*t.cols + col
  295. if pos*2+1 >= len(t.objects) {
  296. return
  297. }
  298. cell := t.text.Rows[row].Cells[col]
  299. t.setCellRune(cell.Rune, pos, cell.Style, t.text.Rows[row].Style)
  300. }
  301. func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGridStyle) {
  302. if str == 0 {
  303. str = ' '
  304. }
  305. text := t.objects[pos*2+1].(*canvas.Text)
  306. fg := theme.ForegroundColor()
  307. if style != nil && style.TextColor() != nil {
  308. fg = style.TextColor()
  309. } else if rowStyle != nil && rowStyle.TextColor() != nil {
  310. fg = rowStyle.TextColor()
  311. }
  312. text.Text = string(str)
  313. text.Color = fg
  314. canvas.Refresh(text)
  315. rect := t.objects[pos*2].(*canvas.Rectangle)
  316. bg := color.Color(color.Transparent)
  317. if style != nil && style.BackgroundColor() != nil {
  318. bg = style.BackgroundColor()
  319. } else if rowStyle != nil && rowStyle.BackgroundColor() != nil {
  320. bg = rowStyle.BackgroundColor()
  321. }
  322. rect.FillColor = bg
  323. canvas.Refresh(rect)
  324. }
  325. func (t *textGridRenderer) addCellsIfRequired() {
  326. cellCount := t.cols * t.rows
  327. if len(t.objects) == cellCount*2 {
  328. return
  329. }
  330. for i := len(t.objects); i < cellCount*2; i += 2 {
  331. t.appendTextCell(' ')
  332. }
  333. }
  334. func (t *textGridRenderer) refreshGrid() {
  335. line := 1
  336. x := 0
  337. for rowIndex, row := range t.text.Rows {
  338. rowStyle := row.Style
  339. i := 0
  340. if t.text.ShowLineNumbers {
  341. lineStr := []rune(strconv.Itoa(line))
  342. pad := t.lineNumberWidth() - len(lineStr)
  343. for ; i < pad; i++ {
  344. t.setCellRune(' ', x, TextGridStyleWhitespace, rowStyle) // padding space
  345. x++
  346. }
  347. for c := 0; c < len(lineStr); c++ {
  348. t.setCellRune(lineStr[c], x, TextGridStyleDefault, rowStyle) // line numbers
  349. i++
  350. x++
  351. }
  352. t.setCellRune('|', x, TextGridStyleWhitespace, rowStyle) // last space
  353. i++
  354. x++
  355. }
  356. for _, r := range row.Cells {
  357. if i >= t.cols { // would be an overflow - bad
  358. continue
  359. }
  360. if t.text.ShowWhitespace && (r.Rune == ' ' || r.Rune == '\t') {
  361. sym := textAreaSpaceSymbol
  362. if r.Rune == '\t' {
  363. sym = textAreaTabSymbol
  364. }
  365. if r.Style != nil && r.Style.BackgroundColor() != nil {
  366. whitespaceBG := &CustomTextGridStyle{FGColor: TextGridStyleWhitespace.TextColor(),
  367. BGColor: r.Style.BackgroundColor()}
  368. t.setCellRune(sym, x, whitespaceBG, rowStyle) // whitespace char
  369. } else {
  370. t.setCellRune(sym, x, TextGridStyleWhitespace, rowStyle) // whitespace char
  371. }
  372. } else {
  373. t.setCellRune(r.Rune, x, r.Style, rowStyle) // regular char
  374. }
  375. i++
  376. x++
  377. }
  378. if t.text.ShowWhitespace && i < t.cols && rowIndex < len(t.text.Rows)-1 {
  379. t.setCellRune(textAreaNewLineSymbol, x, TextGridStyleWhitespace, rowStyle) // newline
  380. i++
  381. x++
  382. }
  383. for ; i < t.cols; i++ {
  384. t.setCellRune(' ', x, TextGridStyleDefault, rowStyle) // blanks
  385. x++
  386. }
  387. line++
  388. }
  389. for ; x < len(t.objects)/2; x++ {
  390. t.setCellRune(' ', x, TextGridStyleDefault, nil) // trailing cells and blank lines
  391. }
  392. }
  393. // tabWidth either returns the set tab width or if not set the returns the DefaultTabWidth
  394. func (t *TextGrid) tabWidth() int {
  395. if t.TabWidth == 0 {
  396. return painter.DefaultTabWidth
  397. }
  398. return t.TabWidth
  399. }
  400. func (t *textGridRenderer) lineNumberWidth() int {
  401. return len(strconv.Itoa(t.rows + 1))
  402. }
  403. func (t *textGridRenderer) updateGridSize(size fyne.Size) {
  404. bufRows := len(t.text.Rows)
  405. bufCols := 0
  406. for _, row := range t.text.Rows {
  407. bufCols = int(math.Max(float64(bufCols), float64(len(row.Cells))))
  408. }
  409. sizeCols := math.Floor(float64(size.Width) / float64(t.cellSize.Width))
  410. sizeRows := math.Floor(float64(size.Height) / float64(t.cellSize.Height))
  411. if t.text.ShowWhitespace {
  412. bufCols++
  413. }
  414. if t.text.ShowLineNumbers {
  415. bufCols += t.lineNumberWidth()
  416. }
  417. t.cols = int(math.Max(sizeCols, float64(bufCols)))
  418. t.rows = int(math.Max(sizeRows, float64(bufRows)))
  419. t.addCellsIfRequired()
  420. }
  421. func (t *textGridRenderer) Layout(size fyne.Size) {
  422. t.updateGridSize(size)
  423. i := 0
  424. cellPos := fyne.NewPos(0, 0)
  425. for y := 0; y < t.rows; y++ {
  426. for x := 0; x < t.cols; x++ {
  427. t.objects[i*2+1].Move(cellPos)
  428. t.objects[i*2].Resize(t.cellSize)
  429. t.objects[i*2].Move(cellPos)
  430. cellPos.X += t.cellSize.Width
  431. i++
  432. }
  433. cellPos.X = 0
  434. cellPos.Y += t.cellSize.Height
  435. }
  436. }
  437. func (t *textGridRenderer) MinSize() fyne.Size {
  438. longestRow := float32(0)
  439. for _, row := range t.text.Rows {
  440. longestRow = fyne.Max(longestRow, float32(len(row.Cells)))
  441. }
  442. return fyne.NewSize(t.cellSize.Width*longestRow,
  443. t.cellSize.Height*float32(len(t.text.Rows)))
  444. }
  445. func (t *textGridRenderer) Refresh() {
  446. // theme could change text size
  447. t.updateCellSize()
  448. TextGridStyleWhitespace = &CustomTextGridStyle{FGColor: theme.DisabledColor()}
  449. t.updateGridSize(t.text.size)
  450. t.refreshGrid()
  451. }
  452. func (t *textGridRenderer) ApplyTheme() {
  453. }
  454. func (t *textGridRenderer) Objects() []fyne.CanvasObject {
  455. return t.objects
  456. }
  457. func (t *textGridRenderer) Destroy() {
  458. }
  459. func (t *textGridRenderer) updateCellSize() {
  460. size := fyne.MeasureText("M", theme.TextSize(), fyne.TextStyle{Monospace: true})
  461. // round it for seamless background
  462. size.Width = float32(math.Round(float64((size.Width))))
  463. size.Height = float32(math.Round(float64((size.Height))))
  464. t.cellSize = size
  465. }