table.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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() (rows int, cols 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() (rows int, cols 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() (rows int, cols 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 == noCellMatch {
  475. return // out of col range
  476. }
  477. row := t.rowAt(e.Position)
  478. if row == noCellMatch {
  479. return // out of row range
  480. }
  481. t.Select(TableCellID{row, col})
  482. if !fyne.CurrentDevice().IsMobile() {
  483. t.RefreshItem(t.currentFocus)
  484. canvas := fyne.CurrentApp().Driver().CanvasForObject(t)
  485. if canvas != nil {
  486. canvas.Focus(t)
  487. }
  488. t.currentFocus = TableCellID{row, col}
  489. t.RefreshItem(t.currentFocus)
  490. }
  491. }
  492. // columnAt returns a positive integer (or 0) for the column that is found at the `pos` X position.
  493. // If the position is between cells the method will return a negative integer representing the next column,
  494. // i.e. -1 means the gap between 0 and 1.
  495. func (t *Table) columnAt(pos fyne.Position) int {
  496. dataCols := 0
  497. if f := t.Length; f != nil {
  498. _, dataCols = t.Length()
  499. }
  500. visibleColWidths, offX, minCol, maxCol := t.visibleColumnWidths(t.cellSize.Width, dataCols)
  501. i := minCol
  502. end := maxCol
  503. if pos.X < t.stuckXOff+t.stuckWidth {
  504. offX = t.stuckXOff
  505. end = t.StickyColumnCount
  506. i = 0
  507. } else {
  508. pos.X += t.content.Offset.X
  509. offX += t.stuckXOff
  510. }
  511. padding := theme.Padding()
  512. for x := offX; i < end; x += visibleColWidths[i-1] + padding {
  513. if pos.X < x {
  514. return -i // the space between i-1 and i
  515. } else if pos.X < x+visibleColWidths[i] {
  516. return i
  517. }
  518. i++
  519. }
  520. return noCellMatch
  521. }
  522. func (t *Table) createHeader() fyne.CanvasObject {
  523. if f := t.CreateHeader; f != nil {
  524. return f()
  525. }
  526. l := NewLabel("00")
  527. l.TextStyle.Bold = true
  528. l.Alignment = fyne.TextAlignCenter
  529. return l
  530. }
  531. func (t *Table) findX(col int) (cellX float32, cellWidth float32) {
  532. cellSize := t.templateSize()
  533. padding := theme.Padding()
  534. for i := 0; i <= col; i++ {
  535. if cellWidth > 0 {
  536. cellX += cellWidth + padding
  537. }
  538. width := cellSize.Width
  539. if w, ok := t.columnWidths[i]; ok {
  540. width = w
  541. }
  542. cellWidth = width
  543. }
  544. return
  545. }
  546. func (t *Table) findY(row int) (cellY float32, cellHeight float32) {
  547. cellSize := t.templateSize()
  548. padding := theme.Padding()
  549. for i := 0; i <= row; i++ {
  550. if cellHeight > 0 {
  551. cellY += cellHeight + padding
  552. }
  553. height := cellSize.Height
  554. if h, ok := t.rowHeights[i]; ok {
  555. height = h
  556. }
  557. cellHeight = height
  558. }
  559. return
  560. }
  561. func (t *Table) finishScroll() {
  562. if t.moveCallback != nil {
  563. t.moveCallback()
  564. }
  565. t.cells.Refresh()
  566. }
  567. func (t *Table) hoverAt(pos fyne.Position) {
  568. col := t.columnAt(pos)
  569. row := t.rowAt(pos)
  570. t.hoveredCell = &TableCellID{row, col}
  571. overHeaderRow := t.ShowHeaderRow && pos.Y < t.headerSize.Height
  572. overHeaderCol := t.ShowHeaderColumn && pos.X < t.headerSize.Width
  573. if overHeaderRow && !overHeaderCol {
  574. if col >= 0 {
  575. t.hoverHeaderCol = noCellMatch
  576. } else {
  577. t.hoverHeaderCol = -col - 1
  578. }
  579. } else {
  580. t.hoverHeaderCol = noCellMatch
  581. }
  582. if overHeaderCol && !overHeaderRow {
  583. if row >= 0 {
  584. t.hoverHeaderRow = noCellMatch
  585. } else {
  586. t.hoverHeaderRow = -row - 1
  587. }
  588. } else {
  589. t.hoverHeaderRow = noCellMatch
  590. }
  591. rows, cols := 0, 0
  592. if f := t.Length; f != nil {
  593. rows, cols = t.Length()
  594. }
  595. if t.hoveredCell.Col >= cols || t.hoveredCell.Row >= rows || t.hoveredCell.Col < 0 || t.hoveredCell.Row < 0 {
  596. t.hoverOut()
  597. return
  598. }
  599. if t.moveCallback != nil {
  600. t.moveCallback()
  601. }
  602. }
  603. func (t *Table) hoverOut() {
  604. t.hoveredCell = nil
  605. if t.moveCallback != nil {
  606. t.moveCallback()
  607. }
  608. }
  609. // rowAt returns a positive integer (or 0) for the row that is found at the `pos` Y position.
  610. // If the position is between cells the method will return a negative integer representing the next row,
  611. // i.e. -1 means the gap between rows 0 and 1.
  612. func (t *Table) rowAt(pos fyne.Position) int {
  613. dataRows := 0
  614. if f := t.Length; f != nil {
  615. dataRows, _ = t.Length()
  616. }
  617. visibleRowHeights, offY, minRow, maxRow := t.visibleRowHeights(t.cellSize.Height, dataRows)
  618. i := minRow
  619. end := maxRow
  620. if pos.Y < t.stuckYOff+t.stuckHeight {
  621. offY = t.stuckYOff
  622. end = t.StickyRowCount
  623. i = 0
  624. } else {
  625. pos.Y += t.content.Offset.Y
  626. offY += t.stuckYOff
  627. }
  628. padding := theme.Padding()
  629. for y := offY; i < end; y += visibleRowHeights[i-1] + padding {
  630. if pos.Y < y {
  631. return -i // the space between i-1 and i
  632. } else if pos.Y >= y && pos.Y < y+visibleRowHeights[i] {
  633. return i
  634. }
  635. i++
  636. }
  637. return noCellMatch
  638. }
  639. func (t *Table) tapped(pos fyne.Position) {
  640. if t.dragCol == noCellMatch && t.dragRow == noCellMatch {
  641. t.dragStartPos = pos
  642. if t.hoverHeaderRow != noCellMatch {
  643. t.dragCol = noCellMatch
  644. t.dragRow = t.hoverHeaderRow
  645. size, ok := t.rowHeights[t.hoverHeaderRow]
  646. if !ok {
  647. size = t.cellSize.Height
  648. }
  649. t.dragStartSize = size
  650. } else if t.hoverHeaderCol != noCellMatch {
  651. t.dragCol = t.hoverHeaderCol
  652. t.dragRow = noCellMatch
  653. size, ok := t.columnWidths[t.hoverHeaderCol]
  654. if !ok {
  655. size = t.cellSize.Width
  656. }
  657. t.dragStartSize = size
  658. }
  659. }
  660. }
  661. func (t *Table) templateSize() fyne.Size {
  662. if f := t.CreateCell; f != nil {
  663. template := f() // don't use cache, we need new template
  664. if !t.ShowHeaderRow && !t.ShowHeaderColumn {
  665. return template.MinSize()
  666. }
  667. return template.MinSize().Max(t.createHeader().MinSize())
  668. }
  669. fyne.LogError("Missing CreateCell callback required for Table", nil)
  670. return fyne.Size{}
  671. }
  672. func (t *Table) updateHeader(id TableCellID, o fyne.CanvasObject) {
  673. if f := t.UpdateHeader; f != nil {
  674. f(id, o)
  675. return
  676. }
  677. l := o.(*Label)
  678. if id.Row < 0 {
  679. ids := []rune{'A' + rune(id.Col%26)}
  680. pre := (id.Col - id.Col%26) / 26
  681. for pre > 0 {
  682. ids = append([]rune{'A' - 1 + rune(pre%26)}, ids...)
  683. pre = (pre - pre%26) / 26
  684. }
  685. l.SetText(string(ids))
  686. } else if id.Col < 0 {
  687. l.SetText(strconv.Itoa(id.Row + 1))
  688. } else {
  689. l.SetText("")
  690. }
  691. }
  692. func (t *Table) stickyColumnWidths(colWidth float32, cols int) (visible []float32) {
  693. if cols == 0 {
  694. return []float32{}
  695. }
  696. max := t.StickyColumnCount
  697. if max > cols {
  698. max = cols
  699. }
  700. visible = make([]float32, max)
  701. if len(t.columnWidths) == 0 {
  702. for i := 0; i < max; i++ {
  703. visible[i] = colWidth
  704. }
  705. return
  706. }
  707. for i := 0; i < max; i++ {
  708. height := colWidth
  709. if h, ok := t.columnWidths[i]; ok {
  710. height = h
  711. }
  712. visible[i] = height
  713. }
  714. return
  715. }
  716. func (t *Table) visibleColumnWidths(colWidth float32, cols int) (visible map[int]float32, offX float32, minCol, maxCol int) {
  717. maxCol = cols
  718. colOffset, headWidth := float32(0), float32(0)
  719. isVisible := false
  720. visible = make(map[int]float32)
  721. if t.content.Size().Width <= 0 {
  722. return
  723. }
  724. // theme.Padding is a slow call, so we cache it
  725. padding := theme.Padding()
  726. stick := t.StickyColumnCount
  727. if len(t.columnWidths) == 0 {
  728. paddedWidth := colWidth + padding
  729. offX = float32(math.Floor(float64(t.offset.X/paddedWidth))) * paddedWidth
  730. minCol = int(math.Floor(float64(offX / paddedWidth)))
  731. maxCol = int(math.Ceil(float64((t.offset.X + t.size.Width) / paddedWidth)))
  732. if minCol > cols-1 {
  733. minCol = cols - 1
  734. }
  735. if minCol < 0 {
  736. minCol = 0
  737. }
  738. if maxCol > cols {
  739. maxCol = cols
  740. }
  741. visible = make(map[int]float32, maxCol-minCol+stick)
  742. for i := minCol; i < maxCol; i++ {
  743. visible[i] = colWidth
  744. }
  745. for i := 0; i < stick; i++ {
  746. visible[i] = colWidth
  747. }
  748. return
  749. }
  750. for i := 0; i < cols; i++ {
  751. width := colWidth
  752. if w, ok := t.columnWidths[i]; ok {
  753. width = w
  754. }
  755. if colOffset <= t.offset.X-width-padding {
  756. // before visible content
  757. } else if colOffset <= headWidth || colOffset <= t.offset.X {
  758. minCol = i
  759. offX = colOffset
  760. isVisible = true
  761. }
  762. if colOffset < t.offset.X+t.size.Width {
  763. maxCol = i + 1
  764. } else {
  765. break
  766. }
  767. colOffset += width + padding
  768. if isVisible || i < stick {
  769. visible[i] = width
  770. }
  771. }
  772. return
  773. }
  774. func (t *Table) stickyRowHeights(rowHeight float32, rows int) (visible []float32) {
  775. if rows == 0 {
  776. return []float32{}
  777. }
  778. max := t.StickyRowCount
  779. if max > rows {
  780. max = rows
  781. }
  782. visible = make([]float32, max)
  783. if len(t.rowHeights) == 0 {
  784. for i := 0; i < max; i++ {
  785. visible[i] = rowHeight
  786. }
  787. return
  788. }
  789. for i := 0; i < max; i++ {
  790. height := rowHeight
  791. if h, ok := t.rowHeights[i]; ok {
  792. height = h
  793. }
  794. visible[i] = height
  795. }
  796. return
  797. }
  798. func (t *Table) visibleRowHeights(rowHeight float32, rows int) (visible map[int]float32, offY float32, minRow, maxRow int) {
  799. maxRow = rows
  800. rowOffset, headHeight := float32(0), float32(0)
  801. isVisible := false
  802. visible = make(map[int]float32)
  803. if t.content.Size().Height <= 0 {
  804. return
  805. }
  806. // theme.Padding is a slow call, so we cache it
  807. padding := theme.Padding()
  808. stick := t.StickyRowCount
  809. if len(t.rowHeights) == 0 {
  810. paddedHeight := rowHeight + padding
  811. offY = float32(math.Floor(float64(t.offset.Y/paddedHeight))) * paddedHeight
  812. minRow = int(math.Floor(float64(offY / paddedHeight)))
  813. maxRow = int(math.Ceil(float64((t.offset.Y + t.size.Height) / paddedHeight)))
  814. if minRow > rows-1 {
  815. minRow = rows - 1
  816. }
  817. if minRow < 0 {
  818. minRow = 0
  819. }
  820. if maxRow > rows {
  821. maxRow = rows
  822. }
  823. visible = make(map[int]float32, maxRow-minRow+stick)
  824. for i := minRow; i < maxRow; i++ {
  825. visible[i] = rowHeight
  826. }
  827. for i := 0; i < stick; i++ {
  828. visible[i] = rowHeight
  829. }
  830. return
  831. }
  832. for i := 0; i < rows; i++ {
  833. height := rowHeight
  834. if h, ok := t.rowHeights[i]; ok {
  835. height = h
  836. }
  837. if rowOffset <= t.offset.Y-height-padding {
  838. // before visible content
  839. } else if rowOffset <= headHeight || rowOffset <= t.offset.Y {
  840. minRow = i
  841. offY = rowOffset
  842. isVisible = true
  843. }
  844. if rowOffset < t.offset.Y+t.size.Height {
  845. maxRow = i + 1
  846. } else {
  847. break
  848. }
  849. rowOffset += height + padding
  850. if isVisible || i < stick {
  851. visible[i] = height
  852. }
  853. }
  854. return
  855. }
  856. // Declare conformity with WidgetRenderer interface.
  857. var _ fyne.WidgetRenderer = (*tableRenderer)(nil)
  858. type tableRenderer struct {
  859. widget.BaseRenderer
  860. t *Table
  861. }
  862. func (t *tableRenderer) Layout(s fyne.Size) {
  863. t.t.propertyLock.RLock()
  864. t.calculateHeaderSizes()
  865. off := fyne.NewPos(t.t.stuckWidth, t.t.stuckHeight)
  866. if t.t.ShowHeaderRow {
  867. off.Y += t.t.headerSize.Height
  868. }
  869. if t.t.ShowHeaderColumn {
  870. off.X += t.t.headerSize.Width
  871. }
  872. t.t.propertyLock.RUnlock()
  873. t.t.content.Move(off)
  874. t.t.content.Resize(s.SubtractWidthHeight(off.X, off.Y))
  875. t.t.top.Move(fyne.NewPos(off.X, 0))
  876. t.t.top.Resize(fyne.NewSize(s.Width-off.X, off.Y))
  877. t.t.left.Move(fyne.NewPos(0, off.Y))
  878. t.t.left.Resize(fyne.NewSize(off.X, s.Height-off.Y))
  879. t.t.corner.Resize(fyne.NewSize(off.X, off.Y))
  880. t.t.dividerLayer.Resize(s)
  881. }
  882. func (t *tableRenderer) MinSize() fyne.Size {
  883. sep := theme.Padding()
  884. t.t.propertyLock.RLock()
  885. defer t.t.propertyLock.RUnlock()
  886. min := t.t.content.MinSize().Max(t.t.cellSize)
  887. if t.t.ShowHeaderRow {
  888. min.Height += t.t.headerSize.Height + sep
  889. }
  890. if t.t.ShowHeaderColumn {
  891. min.Width += t.t.headerSize.Width + sep
  892. }
  893. if t.t.StickyRowCount > 0 {
  894. for i := 0; i < t.t.StickyRowCount; i++ {
  895. height := t.t.cellSize.Height
  896. if h, ok := t.t.rowHeights[i]; ok {
  897. height = h
  898. }
  899. min.Height += height + sep
  900. }
  901. }
  902. if t.t.StickyColumnCount > 0 {
  903. for i := 0; i < t.t.StickyColumnCount; i++ {
  904. width := t.t.cellSize.Width
  905. if w, ok := t.t.columnWidths[i]; ok {
  906. width = w
  907. }
  908. min.Width += width + sep
  909. }
  910. }
  911. return min
  912. }
  913. func (t *tableRenderer) Refresh() {
  914. t.t.propertyLock.Lock()
  915. t.t.headerSize = t.t.createHeader().MinSize()
  916. t.t.cellSize = t.t.templateSize()
  917. t.calculateHeaderSizes()
  918. t.t.propertyLock.Unlock()
  919. t.Layout(t.t.Size())
  920. t.t.cells.Refresh()
  921. }
  922. func (t *tableRenderer) calculateHeaderSizes() {
  923. t.t.stuckXOff = 0
  924. t.t.stuckYOff = 0
  925. if t.t.ShowHeaderRow {
  926. t.t.stuckYOff = t.t.headerSize.Height
  927. }
  928. if t.t.ShowHeaderColumn {
  929. t.t.stuckXOff = t.t.headerSize.Width
  930. }
  931. separatorThickness := theme.Padding()
  932. stickyColWidths := t.t.stickyColumnWidths(t.t.cellSize.Width, t.t.StickyColumnCount)
  933. stickyRowHeights := t.t.stickyRowHeights(t.t.cellSize.Height, t.t.StickyRowCount)
  934. var stuckHeight float32
  935. for _, rowHeight := range stickyRowHeights {
  936. stuckHeight += rowHeight + separatorThickness
  937. }
  938. t.t.stuckHeight = stuckHeight
  939. var stuckWidth float32
  940. for _, colWidth := range stickyColWidths {
  941. stuckWidth += colWidth + separatorThickness
  942. }
  943. t.t.stuckWidth = stuckWidth
  944. }
  945. // Declare conformity with Widget interface.
  946. var _ fyne.Widget = (*tableCells)(nil)
  947. type tableCells struct {
  948. BaseWidget
  949. t *Table
  950. }
  951. func newTableCells(t *Table) *tableCells {
  952. c := &tableCells{t: t}
  953. c.ExtendBaseWidget(c)
  954. return c
  955. }
  956. func (c *tableCells) CreateRenderer() fyne.WidgetRenderer {
  957. marker := canvas.NewRectangle(theme.SelectionColor())
  958. marker.CornerRadius = theme.SelectionRadiusSize()
  959. hover := canvas.NewRectangle(theme.HoverColor())
  960. hover.CornerRadius = theme.SelectionRadiusSize()
  961. r := &tableCellsRenderer{cells: c, pool: &syncPool{}, headerPool: &syncPool{},
  962. visible: make(map[TableCellID]fyne.CanvasObject), headers: make(map[TableCellID]fyne.CanvasObject),
  963. headRowBG: canvas.NewRectangle(theme.HeaderBackgroundColor()), headColBG: canvas.NewRectangle(theme.HeaderBackgroundColor()),
  964. headRowStickyBG: canvas.NewRectangle(theme.HeaderBackgroundColor()), headColStickyBG: canvas.NewRectangle(theme.HeaderBackgroundColor()),
  965. marker: marker, hover: hover}
  966. c.t.moveCallback = r.moveIndicators
  967. return r
  968. }
  969. func (c *tableCells) Resize(s fyne.Size) {
  970. c.BaseWidget.Resize(s)
  971. c.Refresh() // trigger a redraw
  972. }
  973. // Declare conformity with WidgetRenderer interface.
  974. var _ fyne.WidgetRenderer = (*tableCellsRenderer)(nil)
  975. type tableCellsRenderer struct {
  976. widget.BaseRenderer
  977. cells *tableCells
  978. pool, headerPool pool
  979. visible, headers map[TableCellID]fyne.CanvasObject
  980. hover, marker *canvas.Rectangle
  981. dividers []fyne.CanvasObject
  982. headColBG, headRowBG, headRowStickyBG, headColStickyBG *canvas.Rectangle
  983. }
  984. func (r *tableCellsRenderer) Layout(fyne.Size) {
  985. r.cells.propertyLock.Lock()
  986. r.moveIndicators()
  987. r.cells.propertyLock.Unlock()
  988. }
  989. func (r *tableCellsRenderer) MinSize() fyne.Size {
  990. r.cells.propertyLock.RLock()
  991. defer r.cells.propertyLock.RUnlock()
  992. rows, cols := 0, 0
  993. if f := r.cells.t.Length; f != nil {
  994. rows, cols = r.cells.t.Length()
  995. } else {
  996. fyne.LogError("Missing Length callback required for Table", nil)
  997. }
  998. stickRows := r.cells.t.StickyRowCount
  999. stickCols := r.cells.t.StickyColumnCount
  1000. width := float32(0)
  1001. if len(r.cells.t.columnWidths) == 0 {
  1002. width = r.cells.t.cellSize.Width * float32(cols-stickCols)
  1003. } else {
  1004. cellWidth := r.cells.t.cellSize.Width
  1005. for col := stickCols; col < cols; col++ {
  1006. colWidth, ok := r.cells.t.columnWidths[col]
  1007. if ok {
  1008. width += colWidth
  1009. } else {
  1010. width += cellWidth
  1011. }
  1012. }
  1013. }
  1014. height := float32(0)
  1015. if len(r.cells.t.rowHeights) == 0 {
  1016. height = r.cells.t.cellSize.Height * float32(rows-stickRows)
  1017. } else {
  1018. cellHeight := r.cells.t.cellSize.Height
  1019. for row := stickRows; row < rows; row++ {
  1020. rowHeight, ok := r.cells.t.rowHeights[row]
  1021. if ok {
  1022. height += rowHeight
  1023. } else {
  1024. height += cellHeight
  1025. }
  1026. }
  1027. }
  1028. separatorSize := theme.Padding()
  1029. return fyne.NewSize(width+float32(cols-stickCols-1)*separatorSize, height+float32(rows-stickRows-1)*separatorSize)
  1030. }
  1031. func (r *tableCellsRenderer) Refresh() {
  1032. r.refreshForID(allTableCellsID)
  1033. }
  1034. func (r *tableCellsRenderer) refreshForID(toDraw TableCellID) {
  1035. r.cells.propertyLock.Lock()
  1036. separatorThickness := theme.Padding()
  1037. dataRows, dataCols := 0, 0
  1038. if f := r.cells.t.Length; f != nil {
  1039. dataRows, dataCols = r.cells.t.Length()
  1040. }
  1041. visibleColWidths, offX, minCol, maxCol := r.cells.t.visibleColumnWidths(r.cells.t.cellSize.Width, dataCols)
  1042. if len(visibleColWidths) == 0 && dataCols > 0 { // we can't show anything until we have some dimensions
  1043. r.cells.propertyLock.Unlock()
  1044. return
  1045. }
  1046. visibleRowHeights, offY, minRow, maxRow := r.cells.t.visibleRowHeights(r.cells.t.cellSize.Height, dataRows)
  1047. if len(visibleRowHeights) == 0 && dataRows > 0 { // we can't show anything until we have some dimensions
  1048. r.cells.propertyLock.Unlock()
  1049. return
  1050. }
  1051. updateCell := r.cells.t.UpdateCell
  1052. if updateCell == nil {
  1053. fyne.LogError("Missing UpdateCell callback required for Table", nil)
  1054. }
  1055. var cellXOffset, cellYOffset float32
  1056. stickRows := r.cells.t.StickyRowCount
  1057. if r.cells.t.ShowHeaderRow {
  1058. cellYOffset += r.cells.t.headerSize.Height
  1059. }
  1060. stickCols := r.cells.t.StickyColumnCount
  1061. if r.cells.t.ShowHeaderColumn {
  1062. cellXOffset += r.cells.t.headerSize.Width
  1063. }
  1064. startRow := minRow + stickRows
  1065. if startRow < stickRows {
  1066. startRow = stickRows
  1067. }
  1068. startCol := minCol + stickCols
  1069. if startCol < stickCols {
  1070. startCol = stickCols
  1071. }
  1072. wasVisible := r.visible
  1073. r.visible = make(map[TableCellID]fyne.CanvasObject)
  1074. var cells []fyne.CanvasObject
  1075. displayCol := func(row, col int, rowHeight float32, cells *[]fyne.CanvasObject) {
  1076. id := TableCellID{row, col}
  1077. colWidth := visibleColWidths[col]
  1078. c, ok := wasVisible[id]
  1079. if !ok {
  1080. c = r.pool.Obtain()
  1081. if f := r.cells.t.CreateCell; f != nil && c == nil {
  1082. c = f()
  1083. }
  1084. if c == nil {
  1085. return
  1086. }
  1087. }
  1088. c.Move(fyne.NewPos(cellXOffset, cellYOffset))
  1089. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1090. r.visible[id] = c
  1091. *cells = append(*cells, c)
  1092. cellXOffset += colWidth + separatorThickness
  1093. }
  1094. displayRow := func(row int, cells *[]fyne.CanvasObject) {
  1095. rowHeight := visibleRowHeights[row]
  1096. cellXOffset = offX
  1097. for col := startCol; col < maxCol; col++ {
  1098. displayCol(row, col, rowHeight, cells)
  1099. }
  1100. cellXOffset = r.cells.t.content.Offset.X
  1101. stick := r.cells.t.StickyColumnCount
  1102. if r.cells.t.ShowHeaderColumn {
  1103. cellXOffset += r.cells.t.headerSize.Width
  1104. stick--
  1105. }
  1106. cellYOffset += rowHeight + separatorThickness
  1107. }
  1108. cellYOffset = offY
  1109. for row := startRow; row < maxRow; row++ {
  1110. displayRow(row, &cells)
  1111. }
  1112. inline := r.refreshHeaders(visibleRowHeights, visibleColWidths, offX, offY, startRow, maxRow, startCol, maxCol, separatorThickness)
  1113. cells = append(cells, inline...)
  1114. offX -= r.cells.t.content.Offset.X
  1115. cellYOffset = r.cells.t.stuckYOff
  1116. for row := 0; row < stickRows; row++ {
  1117. displayRow(row, &r.cells.t.top.Content.(*fyne.Container).Objects)
  1118. }
  1119. cellYOffset = offY - r.cells.t.content.Offset.Y
  1120. for row := startRow; row < maxRow; row++ {
  1121. cellXOffset = r.cells.t.stuckXOff
  1122. rowHeight := visibleRowHeights[row]
  1123. for col := 0; col < stickCols; col++ {
  1124. displayCol(row, col, rowHeight, &r.cells.t.left.Content.(*fyne.Container).Objects)
  1125. }
  1126. cellYOffset += rowHeight + separatorThickness
  1127. }
  1128. cellYOffset = r.cells.t.stuckYOff
  1129. for row := 0; row < stickRows; row++ {
  1130. cellXOffset = r.cells.t.stuckXOff
  1131. rowHeight := visibleRowHeights[row]
  1132. for col := 0; col < stickCols; col++ {
  1133. displayCol(row, col, rowHeight, &r.cells.t.corner.Content.(*fyne.Container).Objects)
  1134. }
  1135. cellYOffset += rowHeight + separatorThickness
  1136. }
  1137. for id, old := range wasVisible {
  1138. if _, ok := r.visible[id]; !ok {
  1139. r.pool.Release(old)
  1140. }
  1141. }
  1142. visible := r.visible
  1143. headers := r.headers
  1144. r.cells.propertyLock.Unlock()
  1145. r.SetObjects(cells)
  1146. if updateCell != nil {
  1147. for id, cell := range visible {
  1148. if toDraw != allTableCellsID && toDraw != id {
  1149. continue
  1150. }
  1151. updateCell(id, cell)
  1152. }
  1153. }
  1154. for id, head := range headers {
  1155. r.cells.t.updateHeader(id, head)
  1156. }
  1157. r.moveIndicators()
  1158. r.marker.FillColor = theme.SelectionColor()
  1159. r.marker.CornerRadius = theme.SelectionRadiusSize()
  1160. r.marker.Refresh()
  1161. r.hover.FillColor = theme.HoverColor()
  1162. r.hover.CornerRadius = theme.SelectionRadiusSize()
  1163. r.hover.Refresh()
  1164. }
  1165. func (r *tableCellsRenderer) moveIndicators() {
  1166. rows, cols := 0, 0
  1167. if f := r.cells.t.Length; f != nil {
  1168. rows, cols = r.cells.t.Length()
  1169. }
  1170. visibleColWidths, offX, minCol, maxCol := r.cells.t.visibleColumnWidths(r.cells.t.cellSize.Width, cols)
  1171. visibleRowHeights, offY, minRow, maxRow := r.cells.t.visibleRowHeights(r.cells.t.cellSize.Height, rows)
  1172. separatorThickness := theme.SeparatorThicknessSize()
  1173. padding := theme.Padding()
  1174. dividerOff := (padding - separatorThickness) / 2
  1175. stickRows := r.cells.t.StickyRowCount
  1176. stickCols := r.cells.t.StickyColumnCount
  1177. if r.cells.t.ShowHeaderColumn {
  1178. offX += r.cells.t.headerSize.Width
  1179. }
  1180. if r.cells.t.ShowHeaderRow {
  1181. offY += r.cells.t.headerSize.Height
  1182. }
  1183. if r.cells.t.selectedCell == nil {
  1184. r.moveMarker(r.marker, -1, -1, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1185. } else {
  1186. r.moveMarker(r.marker, r.cells.t.selectedCell.Row, r.cells.t.selectedCell.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1187. }
  1188. if r.cells.t.hoveredCell == nil && !r.cells.t.focused {
  1189. r.moveMarker(r.hover, -1, -1, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1190. } else if r.cells.t.focused {
  1191. r.moveMarker(r.hover, r.cells.t.currentFocus.Row, r.cells.t.currentFocus.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1192. } else {
  1193. r.moveMarker(r.hover, r.cells.t.hoveredCell.Row, r.cells.t.hoveredCell.Col, offX, offY, minCol, minRow, visibleColWidths, visibleRowHeights)
  1194. }
  1195. colDivs := stickCols + maxCol - minCol - 1
  1196. if colDivs < 0 {
  1197. colDivs = 0
  1198. }
  1199. rowDivs := stickRows + maxRow - minRow - 1
  1200. if rowDivs < 0 {
  1201. rowDivs = 0
  1202. }
  1203. if colDivs < 0 {
  1204. colDivs = 0
  1205. }
  1206. if rowDivs < 0 {
  1207. rowDivs = 0
  1208. }
  1209. if len(r.dividers) < colDivs+rowDivs {
  1210. for i := len(r.dividers); i < colDivs+rowDivs; i++ {
  1211. r.dividers = append(r.dividers, NewSeparator())
  1212. }
  1213. objs := []fyne.CanvasObject{r.marker, r.hover}
  1214. r.cells.t.dividerLayer.Content.(*fyne.Container).Objects = append(objs, r.dividers...)
  1215. r.cells.t.dividerLayer.Content.Refresh()
  1216. }
  1217. divs := 0
  1218. i := 0
  1219. if stickCols > 0 {
  1220. for x := r.cells.t.stuckXOff + visibleColWidths[i]; i < stickCols && divs < colDivs; x += visibleColWidths[i] + padding {
  1221. i++
  1222. xPos := x + dividerOff
  1223. r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
  1224. r.dividers[divs].Move(fyne.NewPos(xPos, 0))
  1225. r.dividers[divs].Show()
  1226. divs++
  1227. }
  1228. }
  1229. i = minCol + stickCols
  1230. for x := offX + r.cells.t.stuckWidth + visibleColWidths[i]; i < maxCol-1 && divs < colDivs; x += visibleColWidths[i] + padding {
  1231. i++
  1232. xPos := x - r.cells.t.content.Offset.X + dividerOff
  1233. r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
  1234. r.dividers[divs].Move(fyne.NewPos(xPos, 0))
  1235. r.dividers[divs].Show()
  1236. divs++
  1237. }
  1238. i = 0
  1239. if stickRows > 0 {
  1240. for y := r.cells.t.stuckYOff + visibleRowHeights[i]; i < stickRows && divs-colDivs < rowDivs; y += visibleRowHeights[i] + padding {
  1241. i++
  1242. yPos := y + dividerOff
  1243. r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
  1244. r.dividers[divs].Move(fyne.NewPos(0, yPos))
  1245. r.dividers[divs].Show()
  1246. divs++
  1247. }
  1248. }
  1249. i = minRow + stickRows
  1250. for y := offY + r.cells.t.stuckHeight + visibleRowHeights[i]; i < maxRow-1 && divs-colDivs < rowDivs; y += visibleRowHeights[i] + padding {
  1251. i++
  1252. yPos := y - r.cells.t.content.Offset.Y + dividerOff
  1253. r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
  1254. r.dividers[divs].Move(fyne.NewPos(0, yPos))
  1255. r.dividers[divs].Show()
  1256. divs++
  1257. }
  1258. for i := divs; i < len(r.dividers); i++ {
  1259. r.dividers[i].Hide()
  1260. }
  1261. }
  1262. func (r *tableCellsRenderer) moveMarker(marker fyne.CanvasObject, row, col int, offX, offY float32, minCol, minRow int, widths, heights map[int]float32) {
  1263. if col == -1 || row == -1 {
  1264. marker.Hide()
  1265. marker.Refresh()
  1266. return
  1267. }
  1268. xPos := offX
  1269. stickCols := r.cells.t.StickyColumnCount
  1270. if col < stickCols {
  1271. if r.cells.t.ShowHeaderColumn {
  1272. xPos = r.cells.t.stuckXOff
  1273. } else {
  1274. xPos = 0
  1275. }
  1276. minCol = 0
  1277. }
  1278. padding := theme.Padding()
  1279. for i := minCol; i < col; i++ {
  1280. xPos += widths[i]
  1281. xPos += padding
  1282. }
  1283. x1 := xPos
  1284. if col >= stickCols {
  1285. x1 -= r.cells.t.content.Offset.X
  1286. }
  1287. x2 := x1 + widths[col]
  1288. yPos := offY
  1289. stickRows := r.cells.t.StickyRowCount
  1290. if row < stickRows {
  1291. if r.cells.t.ShowHeaderRow {
  1292. yPos = r.cells.t.stuckYOff
  1293. } else {
  1294. yPos = 0
  1295. }
  1296. minRow = 0
  1297. }
  1298. for i := minRow; i < row; i++ {
  1299. yPos += heights[i]
  1300. yPos += padding
  1301. }
  1302. y1 := yPos
  1303. if row >= stickRows {
  1304. y1 -= r.cells.t.content.Offset.Y
  1305. }
  1306. y2 := y1 + heights[row]
  1307. if x2 < 0 || x1 > r.cells.t.size.Width || y2 < 0 || y1 > r.cells.t.size.Height {
  1308. marker.Hide()
  1309. } else {
  1310. left := x1
  1311. if col >= stickCols { // clip X
  1312. left = fyne.Max(r.cells.t.stuckXOff+r.cells.t.stuckWidth, x1)
  1313. }
  1314. top := y1
  1315. if row >= stickRows { // clip Y
  1316. top = fyne.Max(r.cells.t.stuckYOff+r.cells.t.stuckHeight, y1)
  1317. }
  1318. marker.Move(fyne.NewPos(left, top))
  1319. marker.Resize(fyne.NewSize(x2-left, y2-top))
  1320. marker.Show()
  1321. }
  1322. marker.Refresh()
  1323. }
  1324. func (r *tableCellsRenderer) refreshHeaders(visibleRowHeights, visibleColWidths map[int]float32, offX, offY float32,
  1325. startRow, maxRow, startCol, maxCol int, separatorThickness float32) []fyne.CanvasObject {
  1326. wasVisible := r.headers
  1327. r.headers = make(map[TableCellID]fyne.CanvasObject)
  1328. headerMin := r.cells.t.createHeader().MinSize()
  1329. rowHeight := headerMin.Height
  1330. colWidth := headerMin.Width
  1331. var cells, over []fyne.CanvasObject
  1332. corner := []fyne.CanvasObject{r.headColStickyBG, r.headRowStickyBG}
  1333. over = []fyne.CanvasObject{r.headRowBG}
  1334. if r.cells.t.ShowHeaderRow {
  1335. cellXOffset := offX - r.cells.t.content.Offset.X
  1336. displayColHeader := func(col int, list *[]fyne.CanvasObject) {
  1337. id := TableCellID{-1, col}
  1338. colWidth := visibleColWidths[col]
  1339. c, ok := wasVisible[id]
  1340. if !ok {
  1341. c = r.headerPool.Obtain()
  1342. if c == nil {
  1343. c = r.cells.t.createHeader()
  1344. }
  1345. if c == nil {
  1346. return
  1347. }
  1348. }
  1349. c.Move(fyne.NewPos(cellXOffset, 0))
  1350. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1351. r.headers[id] = c
  1352. *list = append(*list, c)
  1353. cellXOffset += colWidth + separatorThickness
  1354. }
  1355. for col := startCol; col < maxCol; col++ {
  1356. displayColHeader(col, &over)
  1357. }
  1358. if r.cells.t.StickyColumnCount > 0 {
  1359. cellXOffset = 0
  1360. if r.cells.t.ShowHeaderColumn {
  1361. cellXOffset += r.cells.t.headerSize.Width
  1362. }
  1363. for col := 0; col < r.cells.t.StickyColumnCount; col++ {
  1364. displayColHeader(col, &corner)
  1365. }
  1366. }
  1367. }
  1368. r.cells.t.top.Content.(*fyne.Container).Objects = over
  1369. r.cells.t.top.Content.Refresh()
  1370. over = []fyne.CanvasObject{r.headColBG}
  1371. if r.cells.t.ShowHeaderColumn {
  1372. cellYOffset := offY - r.cells.t.content.Offset.Y
  1373. displayRowHeader := func(row int, list *[]fyne.CanvasObject) {
  1374. id := TableCellID{row, -1}
  1375. rowHeight := visibleRowHeights[row]
  1376. c, ok := wasVisible[id]
  1377. if !ok {
  1378. c = r.headerPool.Obtain()
  1379. if c == nil {
  1380. c = r.cells.t.createHeader()
  1381. }
  1382. if c == nil {
  1383. return
  1384. }
  1385. }
  1386. c.Move(fyne.NewPos(0, cellYOffset))
  1387. c.Resize(fyne.NewSize(colWidth, rowHeight))
  1388. r.headers[id] = c
  1389. *list = append(*list, c)
  1390. cellYOffset += rowHeight + separatorThickness
  1391. }
  1392. for row := startRow; row < maxRow; row++ {
  1393. displayRowHeader(row, &over)
  1394. }
  1395. if r.cells.t.StickyRowCount > 0 {
  1396. cellYOffset = 0
  1397. if r.cells.t.ShowHeaderRow {
  1398. cellYOffset += r.cells.t.headerSize.Height
  1399. }
  1400. for row := 0; row < r.cells.t.StickyRowCount; row++ {
  1401. displayRowHeader(row, &corner)
  1402. }
  1403. }
  1404. }
  1405. r.cells.t.left.Content.(*fyne.Container).Objects = over
  1406. r.cells.t.left.Content.Refresh()
  1407. r.headColBG.Hidden = !r.cells.t.ShowHeaderColumn
  1408. r.headColBG.FillColor = theme.HeaderBackgroundColor()
  1409. r.headColBG.Resize(fyne.NewSize(colWidth, r.cells.t.Size().Height))
  1410. r.headColStickyBG.Hidden = !r.cells.t.ShowHeaderColumn
  1411. r.headColStickyBG.FillColor = theme.HeaderBackgroundColor()
  1412. r.headColStickyBG.Resize(fyne.NewSize(colWidth, r.cells.t.stuckHeight+rowHeight))
  1413. r.headRowBG.Hidden = !r.cells.t.ShowHeaderRow
  1414. r.headRowBG.FillColor = theme.HeaderBackgroundColor()
  1415. r.headRowBG.Resize(fyne.NewSize(r.cells.t.Size().Width, rowHeight))
  1416. r.headRowStickyBG.Hidden = !r.cells.t.ShowHeaderRow
  1417. r.headRowStickyBG.FillColor = theme.HeaderBackgroundColor()
  1418. r.headRowStickyBG.Resize(fyne.NewSize(r.cells.t.stuckWidth+colWidth, rowHeight))
  1419. r.cells.t.corner.Content.(*fyne.Container).Objects = corner
  1420. r.cells.t.corner.Content.Refresh()
  1421. for id, old := range wasVisible {
  1422. if _, ok := r.headers[id]; !ok {
  1423. r.headerPool.Release(old)
  1424. }
  1425. }
  1426. return cells
  1427. }
  1428. type clip struct {
  1429. widget.Scroll
  1430. t *Table
  1431. }
  1432. func newClip(t *Table, o fyne.CanvasObject) *clip {
  1433. c := &clip{t: t}
  1434. c.Content = o
  1435. c.Direction = widget.ScrollNone
  1436. return c
  1437. }
  1438. func (c *clip) DragEnd() {
  1439. c.t.DragEnd()
  1440. c.t.dragCol = noCellMatch
  1441. c.t.dragRow = noCellMatch
  1442. }
  1443. func (c *clip) Dragged(e *fyne.DragEvent) {
  1444. c.t.Dragged(e)
  1445. }