table.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. package widget
  2. import (
  3. "math"
  4. "strconv"
  5. "fyne.io/fyne/v2"
  6. "fyne.io/fyne/v2/canvas"
  7. "fyne.io/fyne/v2/driver/desktop"
  8. "fyne.io/fyne/v2/driver/mobile"
  9. "fyne.io/fyne/v2/internal/cache"
  10. "fyne.io/fyne/v2/internal/widget"
  11. "fyne.io/fyne/v2/theme"
  12. )
  13. const noCellMatch = math.MaxInt32 // TODO make this MaxInt once we move to newer Go version
  14. // allTableCellsID represents all table cells when refreshing requested cells
  15. var allTableCellsID = TableCellID{-1, -1}
  16. // Declare conformity with interfaces
  17. var _ desktop.Cursorable = (*Table)(nil)
  18. var _ fyne.Draggable = (*Table)(nil)
  19. var _ fyne.Focusable = (*Table)(nil)
  20. var _ desktop.Hoverable = (*Table)(nil)
  21. var _ fyne.Tappable = (*Table)(nil)
  22. var _ fyne.Widget = (*Table)(nil)
  23. // TableCellID is a type that represents a cell's position in a table based on its row and column location.
  24. type TableCellID struct {
  25. Row int
  26. Col int
  27. }
  28. // Table widget is a grid of items that can be scrolled and a cell selected.
  29. // Its performance is provided by caching cell templates created with CreateCell and re-using them with UpdateCell.
  30. // The size of the content rows/columns is returned by the Length callback.
  31. //
  32. // Since: 1.4
  33. type Table struct {
  34. BaseWidget
  35. Length func() (int, int) `json:"-"`
  36. CreateCell func() fyne.CanvasObject `json:"-"`
  37. UpdateCell func(id TableCellID, template fyne.CanvasObject) `json:"-"`
  38. OnSelected func(id TableCellID) `json:"-"`
  39. OnUnselected func(id TableCellID) `json:"-"`
  40. // ShowHeaderRow specifies that a row should be added to the table with header content.
  41. // This will default to an A-Z style content, unless overridden with `CreateHeader` and `UpdateHeader` calls.
  42. //
  43. // Since: 2.4
  44. ShowHeaderRow bool
  45. // ShowHeaderColumn specifies that a column should be added to the table with header content.
  46. // This will default to an 1-10 style numeric content, unless overridden with `CreateHeader` and `UpdateHeader` calls.
  47. //
  48. // Since: 2.4
  49. ShowHeaderColumn bool
  50. // CreateHeader is an optional function that allows overriding of the default header widget.
  51. // Developers must also override `UpdateHeader`.
  52. //
  53. // Since: 2.4
  54. CreateHeader func() fyne.CanvasObject `json:"-"`
  55. // UpdateHeader is used with `CreateHeader` to support custom header content.
  56. // The `id` parameter will have `-1` value to indicate a header, and `> 0` where the column or row refer to data.
  57. //
  58. // Since: 2.4
  59. UpdateHeader func(id TableCellID, template fyne.CanvasObject) `json:"-"`
  60. // StickyRowCount specifies how many data rows should not scroll when the content moves.
  61. // If `ShowHeaderRow` us `true` then the stuck row will appear immediately underneath.
  62. //
  63. // Since: 2.4
  64. StickyRowCount int
  65. // StickyColumnCount specifies how many data columns should not scroll when the content moves.
  66. // If `ShowHeaderColumn` us `true` then the stuck column will appear immediately next to the header.
  67. //
  68. // Since: 2.4
  69. StickyColumnCount int
  70. currentFocus TableCellID
  71. focused bool
  72. selectedCell, hoveredCell *TableCellID
  73. cells *tableCells
  74. columnWidths, rowHeights map[int]float32
  75. moveCallback func()
  76. offset fyne.Position
  77. content *widget.Scroll
  78. cellSize, headerSize fyne.Size
  79. stuckXOff, stuckYOff, stuckWidth, stuckHeight, dragStartSize float32
  80. top, left, corner, dividerLayer *clip
  81. hoverHeaderRow, hoverHeaderCol, dragCol, dragRow int
  82. dragStartPos fyne.Position
  83. }
  84. // NewTable returns a new performant table widget defined by the passed functions.
  85. // The first returns the data size in rows and columns, second parameter is a function that returns cell
  86. // template objects that can be cached and the third is used to apply data at specified data location to the
  87. // passed template CanvasObject.
  88. //
  89. // Since: 1.4
  90. func NewTable(length func() (int, int), create func() fyne.CanvasObject, update func(TableCellID, fyne.CanvasObject)) *Table {
  91. t := &Table{Length: length, CreateCell: create, UpdateCell: update}
  92. t.ExtendBaseWidget(t)
  93. return t
  94. }
  95. // NewTableWithHeaders returns a new performant table widget defined by the passed functions including sticky headers.
  96. // The first returns the data size in rows and columns, second parameter is a function that returns cell
  97. // template objects that can be cached and the third is used to apply data at specified data location to the
  98. // passed template CanvasObject.
  99. // The row and column headers will stick to the leading and top edges of the table and contain "1-10" and "A-Z" formatted labels.
  100. //
  101. // Since: 2.4
  102. func NewTableWithHeaders(length func() (int, int), create func() fyne.CanvasObject, update func(TableCellID, fyne.CanvasObject)) *Table {
  103. t := NewTable(length, create, update)
  104. t.ShowHeaderRow = true
  105. t.ShowHeaderColumn = true
  106. return t
  107. }
  108. // CreateRenderer returns a new renderer for the table.
  109. //
  110. // Implements: fyne.Widget
  111. func (t *Table) CreateRenderer() fyne.WidgetRenderer {
  112. t.ExtendBaseWidget(t)
  113. t.propertyLock.Lock()
  114. t.headerSize = t.createHeader().MinSize()
  115. t.cellSize = t.templateSize()
  116. t.cells = newTableCells(t)
  117. t.content = widget.NewScroll(t.cells)
  118. t.top = newClip(t, &fyne.Container{})
  119. t.left = newClip(t, &fyne.Container{})
  120. t.corner = newClip(t, &fyne.Container{})
  121. t.dividerLayer = newClip(t, &fyne.Container{})
  122. t.propertyLock.Unlock()
  123. t.dragCol = noCellMatch
  124. t.dragRow = noCellMatch
  125. r := &tableRenderer{t: t}
  126. r.SetObjects([]fyne.CanvasObject{t.top, t.left, t.corner, t.dividerLayer, t.content})
  127. t.content.OnScrolled = func(pos fyne.Position) {
  128. t.offset = pos
  129. t.cells.Refresh()
  130. }
  131. r.Layout(t.Size())
  132. return r
  133. }
  134. func (t *Table) Cursor() desktop.Cursor {
  135. if t.hoverHeaderRow != noCellMatch {
  136. return desktop.VResizeCursor
  137. } else if t.hoverHeaderCol != noCellMatch {
  138. return desktop.HResizeCursor
  139. }
  140. return desktop.DefaultCursor
  141. }
  142. func (t *Table) Dragged(e *fyne.DragEvent) {
  143. t.propertyLock.Lock()
  144. min := t.cellSize
  145. col := t.dragCol
  146. row := t.dragRow
  147. startPos := t.dragStartPos
  148. startSize := t.dragStartSize
  149. t.propertyLock.Unlock()
  150. if col != noCellMatch {
  151. newSize := startSize + (e.Position.X - startPos.X)
  152. if newSize < min.Width {
  153. newSize = min.Width
  154. }
  155. t.SetColumnWidth(t.dragCol, newSize)
  156. }
  157. if row != noCellMatch {
  158. newSize := startSize + (e.Position.Y - startPos.Y)
  159. if newSize < min.Height {
  160. newSize = min.Height
  161. }
  162. t.SetRowHeight(t.dragRow, newSize)
  163. }
  164. }
  165. func (t *Table) DragEnd() {
  166. t.dragCol = noCellMatch
  167. t.dragRow = noCellMatch
  168. }
  169. // FocusGained is called after this table has gained focus.
  170. //
  171. // Implements: fyne.Focusable
  172. func (t *Table) FocusGained() {
  173. t.focused = true
  174. t.ScrollTo(t.currentFocus)
  175. t.RefreshItem(t.currentFocus)
  176. }
  177. // FocusLost is called after this Table has lost focus.
  178. //
  179. // Implements: fyne.Focusable
  180. func (t *Table) FocusLost() {
  181. t.focused = false
  182. t.Refresh() //Item(t.currentFocus)
  183. }
  184. func (t *Table) MouseIn(ev *desktop.MouseEvent) {
  185. t.hoverAt(ev.Position)
  186. }
  187. // MouseDown response to desktop mouse event
  188. func (t *Table) MouseDown(e *desktop.MouseEvent) {
  189. t.tapped(e.Position)
  190. }
  191. func (t *Table) MouseMoved(ev *desktop.MouseEvent) {
  192. t.hoverAt(ev.Position)
  193. }
  194. func (t *Table) MouseOut() {
  195. t.hoverOut()
  196. }
  197. // MouseUp response to desktop mouse event
  198. func (t *Table) MouseUp(*desktop.MouseEvent) {
  199. }
  200. // RefreshItem refreshes a single item, specified by the item ID passed in.
  201. //
  202. // Since: 2.4
  203. func (t *Table) RefreshItem(id TableCellID) {
  204. if t.cells == nil {
  205. return
  206. }
  207. r := cache.Renderer(t.cells)
  208. if r == nil {
  209. return
  210. }
  211. r.(*tableCellsRenderer).refreshForID(id)
  212. }
  213. // Select will mark the specified cell as selected.
  214. func (t *Table) Select(id TableCellID) {
  215. if t.Length == nil {
  216. return
  217. }
  218. rows, cols := t.Length()
  219. if id.Row >= rows || id.Col >= cols {
  220. return
  221. }
  222. if t.selectedCell != nil && *t.selectedCell == id {
  223. return
  224. }
  225. if f := t.OnUnselected; f != nil && t.selectedCell != nil {
  226. f(*t.selectedCell)
  227. }
  228. t.selectedCell = &id
  229. t.ScrollTo(id)
  230. if f := t.OnSelected; f != nil {
  231. f(id)
  232. }
  233. }
  234. // SetColumnWidth supports changing the width of the specified column. Columns normally take the width of the template
  235. // cell returned from the CreateCell callback. The width parameter uses the same units as a fyne.Size type and refers
  236. // to the internal content width not including the divider size.
  237. //
  238. // Since: 1.4.1
  239. func (t *Table) SetColumnWidth(id int, width float32) {
  240. t.propertyLock.Lock()
  241. if t.columnWidths == nil {
  242. t.columnWidths = make(map[int]float32)
  243. }
  244. t.columnWidths[id] = width
  245. t.propertyLock.Unlock()
  246. t.Refresh()
  247. }
  248. // SetRowHeight supports changing the height of the specified row. Rows normally take the height of the template
  249. // cell returned from the CreateCell callback. The height parameter uses the same units as a fyne.Size type and refers
  250. // to the internal content height not including the divider size.
  251. //
  252. // Since: 2.3
  253. func (t *Table) SetRowHeight(id int, height float32) {
  254. t.propertyLock.Lock()
  255. if t.rowHeights == nil {
  256. t.rowHeights = make(map[int]float32)
  257. }
  258. t.rowHeights[id] = height
  259. t.propertyLock.Unlock()
  260. t.Refresh()
  261. }
  262. // TouchDown response to mobile touch event
  263. func (t *Table) TouchDown(e *mobile.TouchEvent) {
  264. t.tapped(e.Position)
  265. }
  266. // TouchUp response to mobile touch event
  267. func (t *Table) TouchUp(*mobile.TouchEvent) {
  268. }
  269. // TouchCancel response to mobile touch event
  270. func (t *Table) TouchCancel(*mobile.TouchEvent) {
  271. }
  272. // TypedKey is called if a key event happens while this Table is focused.
  273. //
  274. // Implements: fyne.Focusable
  275. func (t *Table) TypedKey(event *fyne.KeyEvent) {
  276. switch event.Name {
  277. case fyne.KeySpace:
  278. t.Select(t.currentFocus)
  279. case fyne.KeyDown:
  280. if f := t.Length; f != nil {
  281. rows, _ := f()
  282. if t.currentFocus.Row >= rows-1 {
  283. return
  284. }
  285. }
  286. t.RefreshItem(t.currentFocus)
  287. t.currentFocus.Row++
  288. t.ScrollTo(t.currentFocus)
  289. t.RefreshItem(t.currentFocus)
  290. case fyne.KeyLeft:
  291. if t.currentFocus.Col <= 0 {
  292. return
  293. }
  294. t.RefreshItem(t.currentFocus)
  295. t.currentFocus.Col--
  296. t.ScrollTo(t.currentFocus)
  297. t.RefreshItem(t.currentFocus)
  298. case fyne.KeyRight:
  299. if f := t.Length; f != nil {
  300. _, cols := f()
  301. if t.currentFocus.Col >= cols-1 {
  302. return
  303. }
  304. }
  305. t.RefreshItem(t.currentFocus)
  306. t.currentFocus.Col++
  307. t.ScrollTo(t.currentFocus)
  308. t.RefreshItem(t.currentFocus)
  309. case fyne.KeyUp:
  310. if t.currentFocus.Row <= 0 {
  311. return
  312. }
  313. t.RefreshItem(t.currentFocus)
  314. t.currentFocus.Row--
  315. t.ScrollTo(t.currentFocus)
  316. t.RefreshItem(t.currentFocus)
  317. }
  318. }
  319. // TypedRune is called if a text event happens while this Table is focused.
  320. //
  321. // Implements: fyne.Focusable
  322. func (t *Table) TypedRune(_ rune) {
  323. // intentionally left blank
  324. }
  325. // Unselect will mark the cell provided by id as unselected.
  326. func (t *Table) Unselect(id TableCellID) {
  327. if t.selectedCell == nil || id != *t.selectedCell {
  328. return
  329. }
  330. t.selectedCell = nil
  331. if t.moveCallback != nil {
  332. t.moveCallback()
  333. }
  334. if f := t.OnUnselected; f != nil {
  335. f(id)
  336. }
  337. }
  338. // UnselectAll will mark all cells as unselected.
  339. //
  340. // Since: 2.1
  341. func (t *Table) UnselectAll() {
  342. if t.selectedCell == nil {
  343. return
  344. }
  345. selected := *t.selectedCell
  346. t.selectedCell = nil
  347. if t.moveCallback != nil {
  348. t.moveCallback()
  349. }
  350. if f := t.OnUnselected; f != nil {
  351. f(selected)
  352. }
  353. }
  354. // ScrollTo will scroll to the given cell without changing the selection.
  355. // Attempting to scroll beyond the limits of the table will scroll to
  356. // the edge of the table instead.
  357. //
  358. // Since: 2.1
  359. func (t *Table) ScrollTo(id TableCellID) {
  360. if t.Length == nil {
  361. return
  362. }
  363. if t.content == nil {
  364. return
  365. }
  366. rows, cols := t.Length()
  367. if id.Row >= rows {
  368. id.Row = rows - 1
  369. }
  370. if id.Col >= cols {
  371. id.Col = cols - 1
  372. }
  373. scrollPos := t.offset
  374. cellX, cellWidth := t.findX(id.Col)
  375. stickCols := t.StickyColumnCount
  376. if stickCols > 0 {
  377. cellX -= t.stuckXOff + t.stuckWidth
  378. }
  379. if t.ShowHeaderColumn {
  380. cellX += t.headerSize.Width
  381. stickCols--
  382. }
  383. if stickCols == 0 || id.Col > stickCols {
  384. if cellX < scrollPos.X {
  385. scrollPos.X = cellX
  386. } else if cellX+cellWidth > scrollPos.X+t.content.Size().Width {
  387. scrollPos.X = cellX + cellWidth - t.content.Size().Width
  388. }
  389. }
  390. cellY, cellHeight := t.findY(id.Row)
  391. stickRows := t.StickyRowCount
  392. if stickRows > 0 {
  393. cellY -= t.stuckYOff + t.stuckHeight
  394. }
  395. if t.ShowHeaderRow {
  396. cellY += t.headerSize.Height
  397. stickRows--
  398. }
  399. if stickRows == 0 || id.Row >= stickRows {
  400. if cellY < scrollPos.Y {
  401. scrollPos.Y = cellY
  402. } else if cellY+cellHeight > scrollPos.Y+t.content.Size().Height {
  403. scrollPos.Y = cellY + cellHeight - t.content.Size().Height
  404. }
  405. }
  406. t.offset = scrollPos
  407. t.content.Offset = scrollPos
  408. t.content.Refresh()
  409. t.finishScroll()
  410. }
  411. // ScrollToBottom scrolls to the last row in the table
  412. //
  413. // Since: 2.1
  414. func (t *Table) ScrollToBottom() {
  415. if t.Length == nil || t.content == nil {
  416. return
  417. }
  418. rows, _ := t.Length()
  419. cellY, cellHeight := t.findY(rows - 1)
  420. y := cellY + cellHeight - t.content.Size().Height
  421. if y <= 0 {
  422. return
  423. }
  424. t.content.Offset.Y = y
  425. t.offset.Y = y
  426. t.finishScroll()
  427. }
  428. // ScrollToLeading scrolls horizontally to the leading edge of the table
  429. //
  430. // Since: 2.1
  431. func (t *Table) ScrollToLeading() {
  432. if t.content == nil {
  433. return
  434. }
  435. t.content.Offset.X = 0
  436. t.offset.X = 0
  437. t.finishScroll()
  438. }
  439. // ScrollToTop scrolls to the first row in the table
  440. //
  441. // Since: 2.1
  442. func (t *Table) ScrollToTop() {
  443. if t.content == nil {
  444. return
  445. }
  446. t.content.Offset.Y = 0
  447. t.offset.Y = 0
  448. t.finishScroll()
  449. }
  450. // ScrollToTrailing scrolls horizontally to the trailing edge of the table
  451. //
  452. // Since: 2.1
  453. func (t *Table) ScrollToTrailing() {
  454. if t.content == nil || t.Length == nil {
  455. return
  456. }
  457. _, cols := t.Length()
  458. cellX, cellWidth := t.findX(cols - 1)
  459. scrollX := cellX + cellWidth - t.content.Size().Width
  460. if scrollX <= 0 {
  461. return
  462. }
  463. t.content.Offset.X = scrollX
  464. t.offset.X = scrollX
  465. t.finishScroll()
  466. }
  467. func (t *Table) Tapped(e *fyne.PointEvent) {
  468. if e.Position.X < 0 || e.Position.X >= t.Size().Width || e.Position.Y < 0 || e.Position.Y >= t.Size().Height {
  469. t.selectedCell = nil
  470. t.Refresh()
  471. return
  472. }
  473. col := t.columnAt(e.Position)
  474. if col == -1 {
  475. return // out of col range
  476. }
  477. row := t.rowAt(e.Position)
  478. if row == -1 {
  479. return // out of row range
  480. }
  481. t.Select(TableCellID{row, col})
  482. t.RefreshItem(t.currentFocus)
  483. canvas := fyne.CurrentApp().Driver().CanvasForObject(t)
  484. if canvas != nil {
  485. canvas.Focus(t)
  486. }
  487. t.currentFocus = TableCellID{row, col}
  488. t.RefreshItem(t.currentFocus)
  489. }
  490. // columnAt returns a positive integer (or 0) for the column that is found at the `pos` X position.
  491. // If the position is between cells the method will return a negative integer representing the next column,
  492. // i.e. -1 means the gap between 0 and 1.
  493. func (t *Table) columnAt(pos fyne.Position) int {
  494. dataCols := 0
  495. if f := t.Length; f != nil {
  496. _, dataCols = t.Length()
  497. }
  498. visibleColWidths, offX, minCol, maxCol := t.visibleColumnWidths(t.cellSize.Width, dataCols)
  499. i := minCol
  500. end := maxCol
  501. if pos.X < t.stuckXOff+t.stuckWidth {
  502. offX = t.stuckXOff
  503. end = t.StickyColumnCount
  504. i = 0
  505. } else {
  506. pos.X += t.content.Offset.X
  507. offX += t.stuckXOff
  508. }
  509. padding := theme.Padding()
  510. for x := offX; i < end; x += visibleColWidths[i-1] + padding {
  511. if pos.X < x {
  512. return -i // the space between i-1 and i
  513. } else if pos.X < x+visibleColWidths[i] {
  514. return i
  515. }
  516. i++
  517. }
  518. return noCellMatch
  519. }
  520. func (t *Table) createHeader() fyne.CanvasObject {
  521. if f := t.CreateHeader; f != nil {
  522. return f()
  523. }
  524. l := NewLabel("00")
  525. l.TextStyle.Bold = true
  526. l.Alignment = fyne.TextAlignCenter
  527. return l
  528. }
  529. func (t *Table) findX(col int) (cellX float32, cellWidth float32) {
  530. cellSize := t.templateSize()
  531. padding := theme.Padding()
  532. for i := 0; i <= col; i++ {
  533. if cellWidth > 0 {
  534. cellX += cellWidth + padding
  535. }
  536. width := cellSize.Width
  537. if w, ok := t.columnWidths[i]; ok {
  538. width = w
  539. }
  540. cellWidth = width
  541. }
  542. return
  543. }
  544. func (t *Table) findY(row int) (cellY float32, cellHeight float32) {
  545. cellSize := t.templateSize()
  546. padding := theme.Padding()
  547. for i := 0; i <= row; i++ {
  548. if cellHeight > 0 {
  549. cellY += cellHeight + padding
  550. }
  551. height := cellSize.Height
  552. if h, ok := t.rowHeights[i]; ok {
  553. height = h
  554. }
  555. cellHeight = height
  556. }
  557. return
  558. }
  559. func (t *Table) finishScroll() {
  560. if t.moveCallback != nil {
  561. t.moveCallback()
  562. }
  563. t.cells.Refresh()
  564. }
  565. func (t *Table) hoverAt(pos fyne.Position) {
  566. col := t.columnAt(pos)
  567. row := t.rowAt(pos)
  568. t.hoveredCell = &TableCellID{row, col}
  569. overHeaderRow := t.ShowHeaderRow && pos.Y < t.headerSize.Height
  570. overHeaderCol := t.ShowHeaderColumn && pos.X < t.headerSize.Width
  571. if overHeaderRow && !overHeaderCol {
  572. if col >= 0 {
  573. t.hoverHeaderCol = noCellMatch
  574. } else {
  575. t.hoverHeaderCol = -col - 1
  576. }
  577. } else {
  578. t.hoverHeaderCol = noCellMatch
  579. }
  580. if overHeaderCol && !overHeaderRow {
  581. if row >= 0 {
  582. t.hoverHeaderRow = noCellMatch
  583. } else {
  584. t.hoverHeaderRow = -row - 1
  585. }
  586. } else {
  587. t.hoverHeaderRow = noCellMatch
  588. }
  589. rows, cols := 0, 0
  590. if f := t.Length; f != nil {
  591. rows, cols = t.Length()
  592. }
  593. if t.hoveredCell.Col >= cols || t.hoveredCell.Row >= rows || t.hoveredCell.Col < 0 || t.hoveredCell.Row < 0 {
  594. t.hoverOut()
  595. return
  596. }
  597. if t.moveCallback != nil {
  598. t.moveCallback()
  599. }
  600. }
  601. func (t *Table) hoverOut() {
  602. t.hoveredCell = nil
  603. if t.moveCallback != nil {
  604. t.moveCallback()
  605. }
  606. }
  607. // rowAt returns a positive integer (or 0) for the row that is found at the `pos` Y position.
  608. // If the position is between cells the method will return a negative integer representing the next row,
  609. // i.e. -1 means the gap between rows 0 and 1.
  610. func (t *Table) rowAt(pos fyne.Position) int {
  611. dataRows := 0
  612. if f := t.Length; f != nil {
  613. dataRows, _ = t.Length()
  614. }
  615. visibleRowHeights, offY, minRow, maxRow := t.visibleRowHeights(t.cellSize.Height, dataRows)
  616. i := minRow
  617. end := maxRow
  618. if pos.Y < t.stuckYOff+t.stuckHeight {
  619. offY = t.stuckYOff
  620. end = t.StickyRowCount
  621. i = 0
  622. } else {
  623. pos.Y += t.content.Offset.Y
  624. offY += t.stuckYOff
  625. }
  626. padding := theme.Padding()
  627. for y := offY; i < end; y += visibleRowHeights[i-1] + padding {
  628. if pos.Y < y {
  629. return -i // the space between i-1 and i
  630. } else if pos.Y >= y && pos.Y < y+visibleRowHeights[i] {
  631. return i
  632. }
  633. i++
  634. }
  635. return noCellMatch
  636. }
  637. func (t *Table) tapped(pos fyne.Position) {
  638. if t.dragCol == noCellMatch && t.dragRow == noCellMatch {
  639. t.dragStartPos = pos
  640. if t.hoverHeaderRow != noCellMatch {
  641. t.dragCol = noCellMatch
  642. t.dragRow = t.hoverHeaderRow
  643. size, ok := t.rowHeights[t.hoverHeaderRow]
  644. if !ok {
  645. size = t.cellSize.Height
  646. }
  647. t.dragStartSize = size
  648. } else if t.hoverHeaderCol != noCellMatch {
  649. t.dragCol = t.hoverHeaderCol
  650. t.dragRow = noCellMatch
  651. size, ok := t.columnWidths[t.hoverHeaderCol]
  652. if !ok {
  653. size = t.cellSize.Width
  654. }
  655. t.dragStartSize = size
  656. }
  657. }
  658. }
  659. func (t *Table) templateSize() fyne.Size {
  660. if f := t.CreateCell; f != nil {
  661. template := f() // don't use cache, we need new template
  662. if !t.ShowHeaderRow && !t.ShowHeaderColumn {
  663. return template.MinSize()
  664. }
  665. return template.MinSize().Max(t.createHeader().MinSize())
  666. }
  667. fyne.LogError("Missing CreateCell callback required for Table", nil)
  668. return fyne.Size{}
  669. }
  670. func (t *Table) updateHeader(id TableCellID, o fyne.CanvasObject) {
  671. if f := t.UpdateHeader; f != nil {
  672. f(id, o)
  673. return
  674. }
  675. l := o.(*Label)
  676. if id.Row < 0 {
  677. ids := []rune{'A' + rune(id.Col%26)}
  678. pre := (id.Col - id.Col%26) / 26
  679. for pre > 0 {
  680. ids = append([]rune{'A' - 1 + rune(pre%26)}, ids...)
  681. pre = (pre - pre%26) / 26
  682. }
  683. l.SetText(string(ids))
  684. } else if id.Col < 0 {
  685. l.SetText(strconv.Itoa(id.Row + 1))
  686. } else {
  687. l.SetText("")
  688. }
  689. }
  690. func (t *Table) stickyColumnWidths(colWidth float32, cols int) (visible []float32) {
  691. if cols == 0 {
  692. return []float32{}
  693. }
  694. max := t.StickyColumnCount
  695. if max > cols {
  696. max = cols
  697. }
  698. visible = make([]float32, max)
  699. if len(t.columnWidths) == 0 {
  700. for i := 0; i < max; i++ {
  701. visible[i] = colWidth
  702. }
  703. return
  704. }
  705. for i := 0; i < max; i++ {
  706. height := colWidth
  707. if h, ok := t.columnWidths[i]; ok {
  708. height = h
  709. }
  710. visible[i] = height
  711. }
  712. return
  713. }
  714. func (t *Table) visibleColumnWidths(colWidth float32, cols int) (visible map[int]float32, offX float32, minCol, maxCol int) {
  715. maxCol = cols
  716. colOffset, headWidth := float32(0), float32(0)
  717. isVisible := false
  718. visible = make(map[int]float32)
  719. if t.content.Size().Width <= 0 {
  720. return
  721. }
  722. // theme.Padding is a slow call, so we cache it
  723. padding := theme.Padding()
  724. stick := t.StickyColumnCount
  725. if len(t.columnWidths) == 0 {
  726. paddedWidth := colWidth + padding
  727. offX = float32(math.Floor(float64(t.offset.X/paddedWidth))) * paddedWidth
  728. minCol = int(math.Floor(float64(offX / paddedWidth)))
  729. maxCol = int(math.Ceil(float64((t.offset.X + t.size.Width) / paddedWidth)))
  730. if minCol > cols-1 {
  731. minCol = cols - 1
  732. }
  733. if minCol < 0 {
  734. minCol = 0
  735. }
  736. if maxCol > cols {
  737. maxCol = cols
  738. }
  739. visible = make(map[int]float32, maxCol-minCol+stick)
  740. for i := minCol; i < maxCol; i++ {
  741. visible[i] = colWidth
  742. }
  743. for i := 0; i < stick; i++ {
  744. visible[i] = colWidth
  745. }
  746. return
  747. }
  748. for i := 0; i < cols; i++ {
  749. width := colWidth
  750. if w, ok := t.columnWidths[i]; ok {
  751. width = w
  752. }
  753. if colOffset <= t.offset.X-width-padding {
  754. // before visible content
  755. } else if colOffset <= headWidth || colOffset <= t.offset.X {
  756. minCol = i
  757. offX = colOffset
  758. isVisible = true
  759. }
  760. if colOffset < t.offset.X+t.size.Width {
  761. maxCol = i + 1
  762. } else {
  763. break
  764. }
  765. colOffset += width + padding
  766. if isVisible || i < stick {
  767. visible[i] = width
  768. }
  769. }
  770. return
  771. }
  772. func (t *Table) stickyRowHeights(rowHeight float32, rows int) (visible []float32) {
  773. if rows == 0 {
  774. return []float32{}
  775. }
  776. max := t.StickyRowCount
  777. if max > rows {
  778. max = rows
  779. }
  780. visible = make([]float32, max)
  781. if len(t.rowHeights) == 0 {
  782. for i := 0; i < max; i++ {
  783. visible[i] = rowHeight
  784. }
  785. return
  786. }
  787. for i := 0; i < max; i++ {
  788. height := rowHeight
  789. if h, ok := t.rowHeights[i]; ok {
  790. height = h
  791. }
  792. visible[i] = height
  793. }
  794. return
  795. }
  796. func (t *Table) visibleRowHeights(rowHeight float32, rows int) (visible map[int]float32, offY float32, minRow, maxRow int) {
  797. maxRow = rows
  798. rowOffset, headHeight := float32(0), float32(0)
  799. isVisible := false
  800. visible = make(map[int]float32)
  801. if t.content.Size().Height <= 0 {
  802. return
  803. }
  804. // theme.Padding is a slow call, so we cache it
  805. padding := theme.Padding()
  806. stick := t.StickyRowCount
  807. if len(t.rowHeights) == 0 {
  808. paddedHeight := rowHeight + padding
  809. offY = float32(math.Floor(float64(t.offset.Y/paddedHeight))) * paddedHeight
  810. minRow = int(math.Floor(float64(offY / paddedHeight)))
  811. maxRow = int(math.Ceil(float64((t.offset.Y + t.size.Height) / paddedHeight)))
  812. if minRow > rows-1 {
  813. minRow = rows - 1
  814. }
  815. if minRow < 0 {
  816. minRow = 0
  817. }
  818. if maxRow > rows {
  819. maxRow = rows
  820. }
  821. visible = make(map[int]float32, maxRow-minRow+stick)
  822. for i := minRow; i < maxRow; i++ {
  823. visible[i] = rowHeight
  824. }
  825. for i := 0; i < stick; i++ {
  826. visible[i] = rowHeight
  827. }
  828. return
  829. }
  830. for i := 0; i < rows; i++ {
  831. height := rowHeight
  832. if h, ok := t.rowHeights[i]; ok {
  833. height = h
  834. }
  835. if rowOffset <= t.offset.Y-height-padding {
  836. // before visible content
  837. } else if rowOffset <= headHeight || rowOffset <= t.offset.Y {
  838. minRow = i
  839. offY = rowOffset
  840. isVisible = true
  841. }
  842. if rowOffset < t.offset.Y+t.size.Height {
  843. maxRow = i + 1
  844. } else {
  845. break
  846. }
  847. rowOffset += height + padding
  848. if isVisible || i < stick {
  849. visible[i] = height
  850. }
  851. }
  852. return
  853. }
  854. // Declare conformity with WidgetRenderer interface.
  855. var _ fyne.WidgetRenderer = (*tableRenderer)(nil)
  856. type tableRenderer struct {
  857. widget.BaseRenderer
  858. t *Table
  859. }
  860. func (t *tableRenderer) Layout(s fyne.Size) {
  861. t.t.propertyLock.RLock()
  862. t.calculateHeaderSizes()
  863. off := fyne.NewPos(t.t.stuckWidth, t.t.stuckHeight)
  864. if t.t.ShowHeaderRow {
  865. off.Y += t.t.headerSize.Height
  866. }
  867. if t.t.ShowHeaderColumn {
  868. off.X += t.t.headerSize.Width
  869. }
  870. t.t.propertyLock.RUnlock()
  871. t.t.content.Move(off)
  872. t.t.content.Resize(s.SubtractWidthHeight(off.X, off.Y))
  873. t.t.top.Move(fyne.NewPos(off.X, 0))
  874. t.t.top.Resize(fyne.NewSize(s.Width-off.X, off.Y))
  875. t.t.left.Move(fyne.NewPos(0, off.Y))
  876. t.t.left.Resize(fyne.NewSize(off.X, s.Height-off.Y))
  877. t.t.corner.Resize(fyne.NewSize(off.X, off.Y))
  878. t.t.dividerLayer.Resize(s)
  879. }
  880. func (t *tableRenderer) MinSize() fyne.Size {
  881. sep := theme.Padding()
  882. t.t.propertyLock.RLock()
  883. defer t.t.propertyLock.RUnlock()
  884. min := t.t.content.MinSize().Max(t.t.cellSize)
  885. if t.t.ShowHeaderRow {
  886. min.Height += t.t.headerSize.Height + sep
  887. }
  888. if t.t.ShowHeaderColumn {
  889. min.Width += t.t.headerSize.Width + sep
  890. }
  891. if t.t.StickyRowCount > 0 {
  892. for i := 0; i < t.t.StickyRowCount; i++ {
  893. height := t.t.cellSize.Height
  894. if h, ok := t.t.rowHeights[i]; ok {
  895. height = h
  896. }
  897. min.Height += height + sep
  898. }
  899. }
  900. if t.t.StickyColumnCount > 0 {
  901. for i := 0; i < t.t.StickyColumnCount; i++ {
  902. width := t.t.cellSize.Width
  903. if w, ok := t.t.columnWidths[i]; ok {
  904. width = w
  905. }
  906. min.Width += width + sep
  907. }
  908. }
  909. return min
  910. }
  911. func (t *tableRenderer) Refresh() {
  912. t.t.propertyLock.Lock()
  913. t.t.headerSize = t.t.createHeader().MinSize()
  914. t.t.cellSize = t.t.templateSize()
  915. t.calculateHeaderSizes()
  916. t.t.propertyLock.Unlock()
  917. t.Layout(t.t.Size())
  918. t.t.cells.Refresh()
  919. }
  920. func (t *tableRenderer) calculateHeaderSizes() {
  921. t.t.stuckXOff = 0
  922. t.t.stuckYOff = 0
  923. if t.t.ShowHeaderRow {
  924. t.t.stuckYOff = t.t.headerSize.Height
  925. }
  926. if t.t.ShowHeaderColumn {
  927. t.t.stuckXOff = t.t.headerSize.Width
  928. }
  929. separatorThickness := theme.Padding()
  930. stickyColWidths := t.t.stickyColumnWidths(t.t.cellSize.Width, t.t.StickyColumnCount)
  931. stickyRowHeights := t.t.stickyRowHeights(t.t.cellSize.Height, t.t.StickyRowCount)
  932. var stuckHeight float32
  933. for _, rowHeight := range stickyRowHeights {
  934. stuckHeight += rowHeight + separatorThickness
  935. }
  936. t.t.stuckHeight = stuckHeight
  937. var stuckWidth float32
  938. for _, colWidth := range stickyColWidths {
  939. stuckWidth += colWidth + separatorThickness
  940. }
  941. t.t.stuckWidth = stuckWidth
  942. }
  943. // Declare conformity with Widget interface.
  944. var _ fyne.Widget = (*tableCells)(nil)
  945. type tableCells struct {
  946. BaseWidget
  947. t *Table
  948. }
  949. func newTableCells(t *Table) *tableCells {
  950. c := &tableCells{t: t}
  951. c.ExtendBaseWidget(c)
  952. return c
  953. }
  954. func (c *tableCells) CreateRenderer() fyne.WidgetRenderer {
  955. marker := canvas.NewRectangle(theme.SelectionColor())
  956. marker.CornerRadius = theme.SelectionRadiusSize()
  957. hover := canvas.NewRectangle(theme.HoverColor())
  958. hover.CornerRadius = theme.SelectionRadiusSize()
  959. r := &tableCellsRenderer{cells: c, pool: &syncPool{}, headerPool: &syncPool{},
  960. visible: make(map[TableCellID]fyne.CanvasObject), headers: make(map[TableCellID]fyne.CanvasObject),
  961. headRowBG: canvas.NewRectangle(theme.HeaderBackgroundColor()), headColBG: canvas.NewRectangle(theme.HeaderBackgroundColor()),
  962. headRowStickyBG: canvas.NewRectangle(theme.HeaderBackgroundColor()), headColStickyBG: canvas.NewRectangle(theme.HeaderBackgroundColor()),
  963. marker: marker, hover: hover}
  964. c.t.moveCallback = r.moveIndicators
  965. return r
  966. }
  967. func (c *tableCells) Resize(s fyne.Size) {
  968. c.BaseWidget.Resize(s)
  969. c.Refresh() // trigger a redraw
  970. }
  971. // Declare conformity with WidgetRenderer interface.
  972. var _ fyne.WidgetRenderer = (*tableCellsRenderer)(nil)
  973. type tableCellsRenderer struct {
  974. widget.BaseRenderer
  975. cells *tableCells
  976. pool, headerPool pool
  977. visible, headers map[TableCellID]fyne.CanvasObject
  978. hover, marker *canvas.Rectangle
  979. dividers []fyne.CanvasObject
  980. headColBG, headRowBG, headRowStickyBG, headColStickyBG *canvas.Rectangle
  981. }
  982. func (r *tableCellsRenderer) Layout(fyne.Size) {
  983. r.cells.propertyLock.Lock()
  984. r.moveIndicators()
  985. r.cells.propertyLock.Unlock()
  986. }
  987. func (r *tableCellsRenderer) MinSize() fyne.Size {
  988. r.cells.propertyLock.RLock()
  989. defer r.cells.propertyLock.RUnlock()
  990. rows, cols := 0, 0
  991. if f := r.cells.t.Length; f != nil {
  992. rows, cols = r.cells.t.Length()
  993. } else {
  994. fyne.LogError("Missing Length callback required for Table", nil)
  995. }
  996. stickRows := r.cells.t.StickyRowCount
  997. stickCols := r.cells.t.StickyColumnCount
  998. width := float32(0)
  999. if len(r.cells.t.columnWidths) == 0 {
  1000. width = r.cells.t.cellSize.Width * float32(cols-stickCols)
  1001. } else {
  1002. cellWidth := r.cells.t.cellSize.Width
  1003. for col := stickCols; col < cols; col++ {
  1004. colWidth, ok := r.cells.t.columnWidths[col]
  1005. if ok {
  1006. width += colWidth
  1007. } else {
  1008. width += cellWidth
  1009. }
  1010. }
  1011. }
  1012. height := float32(0)
  1013. if len(r.cells.t.rowHeights) == 0 {
  1014. height = r.cells.t.cellSize.Height * float32(rows-stickRows)
  1015. } else {
  1016. cellHeight := r.cells.t.cellSize.Height
  1017. for row := stickRows; row < rows; row++ {
  1018. rowHeight, ok := r.cells.t.rowHeights[row]
  1019. if ok {
  1020. height += rowHeight
  1021. } else {
  1022. height += cellHeight
  1023. }
  1024. }
  1025. }
  1026. separatorSize := theme.Padding()
  1027. return fyne.NewSize(width+float32(cols-stickCols-1)*separatorSize, height+float32(rows-stickRows-1)*separatorSize)
  1028. }
  1029. func (r *tableCellsRenderer) Refresh() {
  1030. r.refreshForID(allTableCellsID)
  1031. }
  1032. func (r *tableCellsRenderer) refreshForID(toDraw TableCellID) {
  1033. r.cells.propertyLock.Lock()
  1034. separatorThickness := theme.Padding()
  1035. dataRows, dataCols := 0, 0
  1036. if f := r.cells.t.Length; f != nil {
  1037. dataRows, dataCols = r.cells.t.Length()
  1038. }
  1039. visibleColWidths, offX, minCol, maxCol := r.cells.t.visibleColumnWidths(r.cells.t.cellSize.Width, dataCols)
  1040. if len(visibleColWidths) == 0 && dataCols > 0 { // we can't show anything until we have some dimensions
  1041. r.cells.propertyLock.Unlock()
  1042. return
  1043. }
  1044. visibleRowHeights, offY, minRow, maxRow := r.cells.t.visibleRowHeights(r.cells.t.cellSize.Height, dataRows)
  1045. if len(visibleRowHeights) == 0 && dataRows > 0 { // we can't show anything until we have some dimensions
  1046. r.cells.propertyLock.Unlock()
  1047. return
  1048. }
  1049. updateCell := r.cells.t.UpdateCell
  1050. if updateCell == nil {
  1051. fyne.LogError("Missing UpdateCell callback required for Table", nil)
  1052. }
  1053. var cellXOffset, cellYOffset float32
  1054. stickRows := r.cells.t.StickyRowCount
  1055. if r.cells.t.ShowHeaderRow {
  1056. cellYOffset += r.cells.t.headerSize.Height
  1057. }
  1058. stickCols := r.cells.t.StickyColumnCount
  1059. if r.cells.t.ShowHeaderColumn {
  1060. cellXOffset += r.cells.t.headerSize.Width
  1061. }
  1062. startRow := minRow + stickRows
  1063. if startRow < stickRows {
  1064. startRow = stickRows
  1065. }
  1066. startCol := minCol + stickCols
  1067. if startCol < stickCols {
  1068. startCol = stickCols
  1069. }
  1070. wasVisible := r.visible
  1071. r.visible = make(map[TableCellID]fyne.CanvasObject)
  1072. var cells []fyne.CanvasObject
  1073. displayCol := func(row, col int, rowHeight float32, cells *[]fyne.CanvasObject) {
  1074. id := TableCellID{row, col}
  1075. colWidth := visibleColWidths[col]
  1076. c, ok := wasVisible[id]
  1077. if !ok {
  1078. c = r.pool.Obtain()
  1079. if f := r.cells.t.CreateCell; f != nil && c == nil {
  1080. c = f()
  1081. }
  1082. if c == nil {
  1083. return
  1084. }
  1085. }
  1086. c.Move(fyne.NewPos(cellXOffset, cellYOffset))
  1087. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1088. r.visible[id] = c
  1089. *cells = append(*cells, c)
  1090. cellXOffset += colWidth + separatorThickness
  1091. }
  1092. displayRow := func(row int, cells *[]fyne.CanvasObject) {
  1093. rowHeight := visibleRowHeights[row]
  1094. cellXOffset = offX
  1095. for col := startCol; col < maxCol; col++ {
  1096. displayCol(row, col, rowHeight, cells)
  1097. }
  1098. cellXOffset = r.cells.t.content.Offset.X
  1099. stick := r.cells.t.StickyColumnCount
  1100. if r.cells.t.ShowHeaderColumn {
  1101. cellXOffset += r.cells.t.headerSize.Width
  1102. stick--
  1103. }
  1104. cellYOffset += rowHeight + separatorThickness
  1105. }
  1106. cellYOffset = offY
  1107. for row := startRow; row < maxRow; row++ {
  1108. displayRow(row, &cells)
  1109. }
  1110. inline := r.refreshHeaders(visibleRowHeights, visibleColWidths, offX, offY, startRow, maxRow, startCol, maxCol, separatorThickness)
  1111. cells = append(cells, inline...)
  1112. offX -= r.cells.t.content.Offset.X
  1113. cellYOffset = r.cells.t.stuckYOff
  1114. for row := 0; row < stickRows; row++ {
  1115. displayRow(row, &r.cells.t.top.Content.(*fyne.Container).Objects)
  1116. }
  1117. cellYOffset = offY - r.cells.t.content.Offset.Y
  1118. for row := startRow; row < maxRow; row++ {
  1119. cellXOffset = r.cells.t.stuckXOff
  1120. rowHeight := visibleRowHeights[row]
  1121. for col := 0; col < stickCols; col++ {
  1122. displayCol(row, col, rowHeight, &r.cells.t.left.Content.(*fyne.Container).Objects)
  1123. }
  1124. cellYOffset += rowHeight + separatorThickness
  1125. }
  1126. cellYOffset = r.cells.t.stuckYOff
  1127. for row := 0; row < stickRows; row++ {
  1128. cellXOffset = r.cells.t.stuckXOff
  1129. rowHeight := visibleRowHeights[row]
  1130. for col := 0; col < stickCols; col++ {
  1131. displayCol(row, col, rowHeight, &r.cells.t.corner.Content.(*fyne.Container).Objects)
  1132. }
  1133. cellYOffset += rowHeight + separatorThickness
  1134. }
  1135. for id, old := range wasVisible {
  1136. if _, ok := r.visible[id]; !ok {
  1137. r.pool.Release(old)
  1138. }
  1139. }
  1140. visible := r.visible
  1141. headers := r.headers
  1142. r.cells.propertyLock.Unlock()
  1143. r.SetObjects(cells)
  1144. if updateCell != nil {
  1145. for id, cell := range visible {
  1146. if toDraw != allTableCellsID && toDraw != id {
  1147. continue
  1148. }
  1149. updateCell(id, cell)
  1150. }
  1151. }
  1152. for id, head := range headers {
  1153. r.cells.t.updateHeader(id, head)
  1154. }
  1155. r.moveIndicators()
  1156. r.marker.FillColor = theme.SelectionColor()
  1157. r.marker.CornerRadius = theme.SelectionRadiusSize()
  1158. r.marker.Refresh()
  1159. r.hover.FillColor = theme.HoverColor()
  1160. r.hover.CornerRadius = theme.SelectionRadiusSize()
  1161. r.hover.Refresh()
  1162. }
  1163. func (r *tableCellsRenderer) moveIndicators() {
  1164. rows, cols := 0, 0
  1165. if f := r.cells.t.Length; f != nil {
  1166. rows, cols = r.cells.t.Length()
  1167. }
  1168. visibleColWidths, offX, minCol, maxCol := r.cells.t.visibleColumnWidths(r.cells.t.cellSize.Width, cols)
  1169. visibleRowHeights, offY, minRow, maxRow := r.cells.t.visibleRowHeights(r.cells.t.cellSize.Height, rows)
  1170. separatorThickness := theme.SeparatorThicknessSize()
  1171. padding := theme.Padding()
  1172. dividerOff := (padding - separatorThickness) / 2
  1173. stickRows := r.cells.t.StickyRowCount
  1174. stickCols := r.cells.t.StickyColumnCount
  1175. if r.cells.t.ShowHeaderColumn {
  1176. offX += r.cells.t.headerSize.Width
  1177. }
  1178. if r.cells.t.ShowHeaderRow {
  1179. offY += r.cells.t.headerSize.Height
  1180. }
  1181. if r.cells.t.selectedCell == nil {
  1182. r.moveMarker(r.marker, -1, -1, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1183. } else {
  1184. r.moveMarker(r.marker, r.cells.t.selectedCell.Row, r.cells.t.selectedCell.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1185. }
  1186. if r.cells.t.hoveredCell == nil && !r.cells.t.focused {
  1187. r.moveMarker(r.hover, -1, -1, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1188. } else if r.cells.t.focused {
  1189. r.moveMarker(r.hover, r.cells.t.currentFocus.Row, r.cells.t.currentFocus.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1190. } else {
  1191. r.moveMarker(r.hover, r.cells.t.hoveredCell.Row, r.cells.t.hoveredCell.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1192. }
  1193. colDivs := stickCols + maxCol - minCol - 1
  1194. if colDivs < 0 {
  1195. colDivs = 0
  1196. }
  1197. rowDivs := stickRows + maxRow - minRow - 1
  1198. if rowDivs < 0 {
  1199. rowDivs = 0
  1200. }
  1201. if colDivs < 0 {
  1202. colDivs = 0
  1203. }
  1204. if rowDivs < 0 {
  1205. rowDivs = 0
  1206. }
  1207. if len(r.dividers) < colDivs+rowDivs {
  1208. for i := len(r.dividers); i < colDivs+rowDivs; i++ {
  1209. r.dividers = append(r.dividers, NewSeparator())
  1210. }
  1211. objs := []fyne.CanvasObject{r.marker, r.hover}
  1212. r.cells.t.dividerLayer.Content.(*fyne.Container).Objects = append(objs, r.dividers...)
  1213. r.cells.t.dividerLayer.Content.Refresh()
  1214. }
  1215. divs := 0
  1216. i := 0
  1217. if stickCols > 0 {
  1218. for x := r.cells.t.stuckXOff + visibleColWidths[i]; i < stickCols && divs < colDivs; x += visibleColWidths[i] + padding {
  1219. i++
  1220. xPos := x + dividerOff
  1221. r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
  1222. r.dividers[divs].Move(fyne.NewPos(xPos, 0))
  1223. r.dividers[divs].Show()
  1224. divs++
  1225. }
  1226. }
  1227. i = minCol + stickCols
  1228. for x := offX + r.cells.t.stuckWidth + visibleColWidths[i]; i < maxCol-1 && divs < colDivs; x += visibleColWidths[i] + padding {
  1229. i++
  1230. xPos := x - r.cells.t.content.Offset.X + dividerOff
  1231. r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
  1232. r.dividers[divs].Move(fyne.NewPos(xPos, 0))
  1233. r.dividers[divs].Show()
  1234. divs++
  1235. }
  1236. i = 0
  1237. if stickRows > 0 {
  1238. for y := r.cells.t.stuckYOff + visibleRowHeights[i]; i < stickRows && divs-colDivs < rowDivs; y += visibleRowHeights[i] + padding {
  1239. i++
  1240. yPos := y + dividerOff
  1241. r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
  1242. r.dividers[divs].Move(fyne.NewPos(0, yPos))
  1243. r.dividers[divs].Show()
  1244. divs++
  1245. }
  1246. }
  1247. i = minRow + stickRows
  1248. for y := offY + r.cells.t.stuckHeight + visibleRowHeights[i]; i < maxRow-1 && divs-colDivs < rowDivs; y += visibleRowHeights[i] + padding {
  1249. i++
  1250. yPos := y - r.cells.t.content.Offset.Y + dividerOff
  1251. r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
  1252. r.dividers[divs].Move(fyne.NewPos(0, yPos))
  1253. r.dividers[divs].Show()
  1254. divs++
  1255. }
  1256. for i := divs; i < len(r.dividers); i++ {
  1257. r.dividers[i].Hide()
  1258. }
  1259. }
  1260. func (r *tableCellsRenderer) moveMarker(marker fyne.CanvasObject, row, col int, offX, offY float32, minCol, minRow int, widths, heights map[int]float32) {
  1261. if col == -1 || row == -1 {
  1262. marker.Hide()
  1263. marker.Refresh()
  1264. return
  1265. }
  1266. xPos := offX
  1267. stickCols := r.cells.t.StickyColumnCount
  1268. if col < stickCols {
  1269. if r.cells.t.ShowHeaderColumn {
  1270. xPos = r.cells.t.stuckXOff
  1271. } else {
  1272. xPos = 0
  1273. }
  1274. minCol = 0
  1275. }
  1276. padding := theme.Padding()
  1277. for i := minCol; i < col; i++ {
  1278. xPos += widths[i]
  1279. xPos += padding
  1280. }
  1281. x1 := xPos
  1282. if col >= stickCols {
  1283. x1 -= r.cells.t.content.Offset.X
  1284. }
  1285. x2 := x1 + widths[col]
  1286. yPos := offY
  1287. stickRows := r.cells.t.StickyRowCount
  1288. if row < stickRows {
  1289. if r.cells.t.ShowHeaderRow {
  1290. yPos = r.cells.t.stuckYOff
  1291. } else {
  1292. yPos = 0
  1293. }
  1294. minRow = 0
  1295. }
  1296. for i := minRow; i < row; i++ {
  1297. yPos += heights[i]
  1298. yPos += padding
  1299. }
  1300. y1 := yPos
  1301. if row >= stickRows {
  1302. y1 -= r.cells.t.content.Offset.Y
  1303. }
  1304. y2 := y1 + heights[row]
  1305. if x2 < 0 || x1 > r.cells.t.size.Width || y2 < 0 || y1 > r.cells.t.size.Height {
  1306. marker.Hide()
  1307. } else {
  1308. left := x1
  1309. if col >= stickCols { // clip X
  1310. left = fyne.Max(r.cells.t.stuckXOff+r.cells.t.stuckWidth, x1)
  1311. }
  1312. top := y1
  1313. if row >= stickRows { // clip Y
  1314. top = fyne.Max(r.cells.t.stuckYOff+r.cells.t.stuckHeight, y1)
  1315. }
  1316. marker.Move(fyne.NewPos(left, top))
  1317. marker.Resize(fyne.NewSize(x2-left, y2-top))
  1318. marker.Show()
  1319. }
  1320. marker.Refresh()
  1321. }
  1322. func (r *tableCellsRenderer) refreshHeaders(visibleRowHeights, visibleColWidths map[int]float32, offX, offY float32,
  1323. startRow, maxRow, startCol, maxCol int, separatorThickness float32) []fyne.CanvasObject {
  1324. wasVisible := r.headers
  1325. r.headers = make(map[TableCellID]fyne.CanvasObject)
  1326. headerMin := r.cells.t.createHeader().MinSize()
  1327. rowHeight := headerMin.Height
  1328. colWidth := headerMin.Width
  1329. var cells, over []fyne.CanvasObject
  1330. corner := []fyne.CanvasObject{r.headColStickyBG, r.headRowStickyBG}
  1331. over = []fyne.CanvasObject{r.headRowBG}
  1332. if r.cells.t.ShowHeaderRow {
  1333. cellXOffset := offX - r.cells.t.content.Offset.X
  1334. displayColHeader := func(col int, list *[]fyne.CanvasObject) {
  1335. id := TableCellID{-1, col}
  1336. colWidth := visibleColWidths[col]
  1337. c, ok := wasVisible[id]
  1338. if !ok {
  1339. c = r.headerPool.Obtain()
  1340. if c == nil {
  1341. c = r.cells.t.createHeader()
  1342. }
  1343. if c == nil {
  1344. return
  1345. }
  1346. }
  1347. c.Move(fyne.NewPos(cellXOffset, 0))
  1348. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1349. r.headers[id] = c
  1350. *list = append(*list, c)
  1351. cellXOffset += colWidth + separatorThickness
  1352. }
  1353. for col := startCol; col < maxCol; col++ {
  1354. displayColHeader(col, &over)
  1355. }
  1356. if r.cells.t.StickyColumnCount > 0 {
  1357. cellXOffset = 0
  1358. if r.cells.t.ShowHeaderColumn {
  1359. cellXOffset += r.cells.t.headerSize.Width
  1360. }
  1361. for col := 0; col < r.cells.t.StickyColumnCount; col++ {
  1362. displayColHeader(col, &corner)
  1363. }
  1364. }
  1365. }
  1366. r.cells.t.top.Content.(*fyne.Container).Objects = over
  1367. r.cells.t.top.Content.Refresh()
  1368. over = []fyne.CanvasObject{r.headColBG}
  1369. if r.cells.t.ShowHeaderColumn {
  1370. cellYOffset := offY - r.cells.t.content.Offset.Y
  1371. displayRowHeader := func(row int, list *[]fyne.CanvasObject) {
  1372. id := TableCellID{row, -1}
  1373. rowHeight := visibleRowHeights[row]
  1374. c, ok := wasVisible[id]
  1375. if !ok {
  1376. c = r.headerPool.Obtain()
  1377. if c == nil {
  1378. c = r.cells.t.createHeader()
  1379. }
  1380. if c == nil {
  1381. return
  1382. }
  1383. }
  1384. c.Move(fyne.NewPos(0, cellYOffset))
  1385. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1386. r.headers[id] = c
  1387. *list = append(*list, c)
  1388. cellYOffset += rowHeight + separatorThickness
  1389. }
  1390. for row := startRow; row < maxRow; row++ {
  1391. displayRowHeader(row, &over)
  1392. }
  1393. if r.cells.t.StickyRowCount > 0 {
  1394. cellYOffset = 0
  1395. if r.cells.t.ShowHeaderRow {
  1396. cellYOffset += r.cells.t.headerSize.Height
  1397. }
  1398. for row := 0; row < r.cells.t.StickyRowCount; row++ {
  1399. displayRowHeader(row, &corner)
  1400. }
  1401. }
  1402. }
  1403. r.cells.t.left.Content.(*fyne.Container).Objects = over
  1404. r.cells.t.left.Content.Refresh()
  1405. r.headColBG.Hidden = !r.cells.t.ShowHeaderColumn
  1406. r.headColBG.FillColor = theme.HeaderBackgroundColor()
  1407. r.headColBG.Resize(fyne.NewSize(colWidth, r.cells.t.Size().Height))
  1408. r.headColStickyBG.Hidden = !r.cells.t.ShowHeaderColumn
  1409. r.headColStickyBG.FillColor = theme.HeaderBackgroundColor()
  1410. r.headColStickyBG.Resize(fyne.NewSize(colWidth, r.cells.t.stuckHeight+rowHeight))
  1411. r.headRowBG.Hidden = !r.cells.t.ShowHeaderRow
  1412. r.headRowBG.FillColor = theme.HeaderBackgroundColor()
  1413. r.headRowBG.Resize(fyne.NewSize(r.cells.t.Size().Width, rowHeight))
  1414. r.headRowStickyBG.Hidden = !r.cells.t.ShowHeaderRow
  1415. r.headRowStickyBG.FillColor = theme.HeaderBackgroundColor()
  1416. r.headRowStickyBG.Resize(fyne.NewSize(r.cells.t.stuckWidth+colWidth, rowHeight))
  1417. r.cells.t.corner.Content.(*fyne.Container).Objects = corner
  1418. r.cells.t.corner.Content.Refresh()
  1419. for id, old := range wasVisible {
  1420. if _, ok := r.headers[id]; !ok {
  1421. r.headerPool.Release(old)
  1422. }
  1423. }
  1424. return cells
  1425. }
  1426. type clip struct {
  1427. widget.Scroll
  1428. t *Table
  1429. }
  1430. func newClip(t *Table, o fyne.CanvasObject) *clip {
  1431. c := &clip{t: t}
  1432. c.Content = o
  1433. c.Direction = widget.ScrollNone
  1434. return c
  1435. }
  1436. func (c *clip) DragEnd() {
  1437. c.t.DragEnd()
  1438. c.t.dragCol = noCellMatch
  1439. c.t.dragRow = noCellMatch
  1440. }
  1441. func (c *clip) Dragged(e *fyne.DragEvent) {
  1442. c.t.Dragged(e)
  1443. }