2023-04-03 18:01:44 -06:00
|
|
|
package containers
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-04-03 20:22:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-04-03 18:01:44 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
|
|
|
|
2023-04-04 13:05:26 -06:00
|
|
|
// TODO: using the event propagator core might not be the best idea here. we
|
|
|
|
// should have slightly different behavior to sync the focused element with the
|
2023-04-04 14:39:12 -06:00
|
|
|
// selected cell. alternatively we could pass a callback to the propagator that
|
|
|
|
// fires when the focused child changes. this would also allow things like
|
|
|
|
// scrolling to the focused child (for this element and others).
|
2023-04-04 13:05:26 -06:00
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
type tableCell struct {
|
|
|
|
tomo.Element
|
2023-04-03 20:22:29 -06:00
|
|
|
tomo.Pattern
|
2023-04-03 18:01:44 -06:00
|
|
|
image.Rectangle
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableContainer is a container that lays its contents out in a table. It can
|
|
|
|
// be scrolled.
|
|
|
|
type TableContainer struct {
|
|
|
|
*core.Core
|
|
|
|
*core.Propagator
|
|
|
|
core core.CoreControl
|
|
|
|
|
|
|
|
topHeading bool
|
|
|
|
leftHeading bool
|
|
|
|
|
|
|
|
columns int
|
|
|
|
rows int
|
|
|
|
scroll image.Point
|
|
|
|
warping bool
|
|
|
|
grid [][]tableCell
|
|
|
|
children []tomo.Element
|
|
|
|
|
|
|
|
contentBounds image.Rectangle
|
|
|
|
forcedMinimumWidth int
|
|
|
|
forcedMinimumHeight int
|
2023-04-03 20:22:29 -06:00
|
|
|
|
|
|
|
selectedColumn int
|
|
|
|
selectedRow int
|
2023-04-03 18:01:44 -06:00
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
onSelect func ()
|
2023-04-03 18:01:44 -06:00
|
|
|
onScrollBoundsChange func ()
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:10:39 -06:00
|
|
|
// NewTable creates a new table element with the specified amount of columns and
|
|
|
|
// rows. If top or left heading is set to true, the first row or column
|
|
|
|
// respectively will display as a table header.
|
2023-04-03 18:01:44 -06:00
|
|
|
func NewTableContainer (
|
|
|
|
columns, rows int,
|
|
|
|
topHeading, leftHeading bool,
|
|
|
|
) (
|
|
|
|
element *TableContainer,
|
|
|
|
) {
|
|
|
|
element = &TableContainer {
|
2023-04-03 20:22:29 -06:00
|
|
|
topHeading: topHeading,
|
|
|
|
leftHeading: leftHeading,
|
|
|
|
selectedColumn: -1,
|
|
|
|
selectedRow: -1,
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
element.theme.Case = tomo.C("tomo", "tableContainer")
|
|
|
|
element.Core, element.core = core.NewCore(element, element.redoAll)
|
|
|
|
element.Propagator = core.NewPropagator(element, element.core)
|
|
|
|
element.Resize(columns, rows)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set places an element at the specified column and row. If the element passed
|
|
|
|
// is nil, whatever element occupies the cell currently is removed.
|
|
|
|
func (element *TableContainer) Set (column, row int, child tomo.Element) {
|
|
|
|
if row < 0 || row >= element.rows { return }
|
|
|
|
if column < 0 || column >= element.columns { return }
|
|
|
|
|
|
|
|
childList := element.children
|
|
|
|
if child == nil {
|
|
|
|
if element.grid[row][column].Element == nil {
|
|
|
|
// no-op
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// removing the child that is currently in a slow
|
|
|
|
element.unhook(element.grid[row][column].Element)
|
|
|
|
childList = childList[:len(childList) - 1]
|
|
|
|
element.grid[row][column].Element = child
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.hook(child)
|
|
|
|
if element.grid[row][column].Element == nil {
|
|
|
|
// putting the child in an empty slot
|
|
|
|
childList = append(childList, nil)
|
|
|
|
element.grid[row][column].Element = child
|
|
|
|
} else {
|
|
|
|
// replacing the child that is currently in a slow
|
|
|
|
element.unhook(element.grid[row][column].Element)
|
|
|
|
element.grid[row][column].Element = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.rebuildChildList(childList)
|
|
|
|
element.children = childList
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize changes the amount of columns and rows in the table. If the table is
|
|
|
|
// resized to be smaller, children in cells that do not exist anymore will be
|
|
|
|
// removed. The minimum size for a TableContainer is 1x1.
|
|
|
|
func (element *TableContainer) Resize (columns, rows int) {
|
|
|
|
if columns < 1 { columns = 1 }
|
|
|
|
if rows < 1 { rows = 1 }
|
|
|
|
if element.columns == columns && element.rows == rows { return }
|
|
|
|
amountRemoved := 0
|
|
|
|
|
|
|
|
// handle rows as a whole
|
|
|
|
if rows < element.rows {
|
|
|
|
// disown children in bottom rows
|
|
|
|
for _, row := range element.grid[rows:] {
|
|
|
|
for index, child := range row {
|
|
|
|
if child.Element != nil {
|
|
|
|
element.unhook(child.Element)
|
|
|
|
amountRemoved ++
|
|
|
|
row[index].Element = nil
|
|
|
|
}}}
|
|
|
|
// cut grid to size
|
|
|
|
element.grid = element.grid[:rows]
|
|
|
|
} else {
|
|
|
|
// expand grid
|
|
|
|
newGrid := make([][]tableCell, rows)
|
|
|
|
copy(newGrid, element.grid)
|
|
|
|
element.grid = newGrid
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle each row individually
|
|
|
|
for rowIndex, row := range element.grid {
|
|
|
|
if columns < element.columns {
|
|
|
|
// disown children in the far right of the row
|
|
|
|
for index, child := range row[columns:] {
|
|
|
|
if child.Element != nil {
|
|
|
|
element.unhook(child.Element)
|
|
|
|
amountRemoved ++
|
|
|
|
row[index].Element = nil
|
|
|
|
}}
|
|
|
|
// cut row to size
|
|
|
|
element.grid[rowIndex] = row[:columns]
|
|
|
|
} else {
|
|
|
|
// expand row
|
|
|
|
newRow := make([]tableCell, columns)
|
|
|
|
copy(newRow, row)
|
|
|
|
element.grid[rowIndex] = newRow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.columns = columns
|
|
|
|
element.rows = rows
|
|
|
|
|
|
|
|
if amountRemoved > 0 {
|
|
|
|
childList := element.children[:len(element.children) - amountRemoved]
|
|
|
|
element.rebuildChildList(childList)
|
|
|
|
element.children = childList
|
|
|
|
}
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
// Selected returns the column and row of the cell that is currently selected.
|
|
|
|
// If no cell is selected, this method will return (-1, -1).
|
|
|
|
func (element *TableContainer) Selected () (column, row int) {
|
|
|
|
return element.selectedColumn, element.selectedRow
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnSelect sets a function to be called when the user selects a table cell.
|
|
|
|
func (element *TableContainer) OnSelect (callback func ()) {
|
|
|
|
element.onSelect = callback
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
// Warp runs the specified callback, deferring all layout and rendering updates
|
|
|
|
// until the callback has finished executing. This allows for aplications to
|
|
|
|
// perform batch gui updates without flickering and stuff.
|
|
|
|
func (element *TableContainer) Warp (callback func ()) {
|
|
|
|
if element.warping {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
element.warping = true
|
|
|
|
callback()
|
|
|
|
element.warping = false
|
|
|
|
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collapse collapses the element's minimum width and height. A value of zero
|
|
|
|
// for either means that the element's normal value is used.
|
|
|
|
func (element *TableContainer) Collapse (width, height int) {
|
|
|
|
if
|
|
|
|
element.forcedMinimumWidth == width &&
|
|
|
|
element.forcedMinimumHeight == height {
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
element.forcedMinimumWidth = width
|
|
|
|
element.forcedMinimumHeight = height
|
|
|
|
element.updateMinimumSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountChildren returns the amount of children contained within this element.
|
|
|
|
func (element *TableContainer) CountChildren () (count int) {
|
|
|
|
return len(element.children)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Child returns the child at the specified index. If the index is out of
|
|
|
|
// bounds, this method will return nil.
|
|
|
|
func (element *TableContainer) Child (index int) (child tomo.Element) {
|
|
|
|
if index < 0 || index > len(element.children) { return }
|
|
|
|
return element.children[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) Window () tomo.Window {
|
|
|
|
return element.core.Window()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyMinimumSizeChange notifies the container that the minimum size of a
|
|
|
|
// child element has changed.
|
|
|
|
func (element *TableContainer) NotifyMinimumSizeChange (child tomo.Element) {
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawBackground draws a portion of the container's background pattern within
|
|
|
|
// the specified bounds. The container will not push these changes.
|
|
|
|
func (element *TableContainer) DrawBackground (bounds image.Rectangle) {
|
|
|
|
if !bounds.Overlaps(element.core.Bounds()) { return }
|
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
for rowIndex, row := range element.grid {
|
|
|
|
for columnIndex, child := range row {
|
2023-04-03 18:01:44 -06:00
|
|
|
if bounds.Overlaps(child.Rectangle) {
|
2023-04-03 20:22:29 -06:00
|
|
|
element.theme.Pattern (
|
|
|
|
child.Pattern,
|
|
|
|
element.state(columnIndex, rowIndex)).
|
|
|
|
Draw(canvas.Cut(element.core, bounds), child.Rectangle)
|
2023-04-03 19:41:39 -06:00
|
|
|
return
|
2023-04-03 18:01:44 -06:00
|
|
|
}}}
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
func (element *TableContainer) HandleMouseDown (x, y int, button input.Button) {
|
2023-04-04 13:05:26 -06:00
|
|
|
element.Focus()
|
2023-04-03 20:22:29 -06:00
|
|
|
element.Propagator.HandleMouseDown(x, y, button)
|
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
|
|
|
|
for rowIndex, row := range element.grid {
|
|
|
|
for columnIndex, child := range row {
|
|
|
|
if image.Pt(x, y).In(child.Rectangle) {
|
2023-04-04 13:05:26 -06:00
|
|
|
element.selectCell(columnIndex, rowIndex)
|
2023-04-03 20:22:29 -06:00
|
|
|
return
|
|
|
|
}}}
|
2023-04-03 21:09:02 -06:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:05:26 -06:00
|
|
|
func (element *TableContainer) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
switch key {
|
|
|
|
case input.KeyLeft: element.changeSelectionBy(-1, 0)
|
|
|
|
case input.KeyRight: element.changeSelectionBy(1, 0)
|
|
|
|
case input.KeyUp: element.changeSelectionBy(0, -1)
|
|
|
|
case input.KeyDown: element.changeSelectionBy(0, 1)
|
|
|
|
case input.KeyEscape: element.selectCell(-1, -1)
|
|
|
|
default: element.Propagator.HandleKeyDown(key, modifiers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:09:02 -06:00
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
func (element *TableContainer) ScrollContentBounds () image.Rectangle {
|
|
|
|
return element.contentBounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's
|
|
|
|
// viewport relative to ScrollBounds.
|
|
|
|
func (element *TableContainer) ScrollViewportBounds () image.Rectangle {
|
|
|
|
bounds := element.Bounds()
|
|
|
|
bounds = bounds.Sub(bounds.Min).Add(element.scroll)
|
|
|
|
return bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
func (element *TableContainer) ScrollTo (position image.Point) {
|
|
|
|
if position.Y < 0 {
|
|
|
|
position.Y = 0
|
|
|
|
}
|
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if position.Y > maxScrollHeight {
|
|
|
|
position.Y = maxScrollHeight
|
|
|
|
}
|
|
|
|
if position.X < 0 {
|
|
|
|
position.X = 0
|
|
|
|
}
|
|
|
|
maxScrollWidth := element.maxScrollWidth()
|
|
|
|
if position.X > maxScrollWidth {
|
|
|
|
position.X = maxScrollWidth
|
|
|
|
}
|
|
|
|
element.scroll = position
|
|
|
|
if element.core.HasImage() && !element.warping {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnScrollBoundsChange sets a function to be called when the element's viewport
|
|
|
|
// bounds, content bounds, or scroll axes change.
|
|
|
|
func (element *TableContainer) OnScrollBoundsChange (callback func ()) {
|
|
|
|
element.onScrollBoundsChange = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
func (element *TableContainer) ScrollAxes () (horizontal, vertical bool) {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:05:26 -06:00
|
|
|
func (element *TableContainer) changeSelectionBy (column, row int) {
|
|
|
|
column += element.selectedColumn
|
|
|
|
row += element.selectedRow
|
|
|
|
if column < 0 { column = 0 }
|
|
|
|
if row < 0 { row = 0 }
|
|
|
|
element.selectCell(column, row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) selectCell (column, row int) {
|
|
|
|
if column < -1 { column = -1 }
|
|
|
|
if row < -1 { row = -1 }
|
|
|
|
if column >= element.columns { column = element.columns - 1 }
|
|
|
|
if row >= element.rows { row = element.rows - 1 }
|
|
|
|
|
|
|
|
if column == element.selectedColumn && row == element.selectedRow {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldColumn, oldRow := element.selectedColumn, element.selectedRow
|
|
|
|
element.selectedColumn = column
|
|
|
|
element.selectedRow = row
|
|
|
|
if oldColumn >= 0 && oldRow >= 0 {
|
|
|
|
element.core.DamageRegion(element.redoCell(oldColumn, oldRow))
|
|
|
|
}
|
|
|
|
if column >= 0 && row >= 0 {
|
|
|
|
element.core.DamageRegion(element.redoCell(column, row))
|
|
|
|
}
|
|
|
|
if element.onSelect != nil {
|
|
|
|
element.onSelect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:09:02 -06:00
|
|
|
func (element *TableContainer) maxScrollHeight () (height int) {
|
|
|
|
viewportHeight := element.Bounds().Dy()
|
|
|
|
height = element.contentBounds.Dy() - viewportHeight
|
|
|
|
if height < 0 { height = 0 }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) maxScrollWidth () (width int) {
|
|
|
|
viewportWidth := element.Bounds().Dx()
|
|
|
|
width = element.contentBounds.Dx() - viewportWidth
|
|
|
|
if width < 0 { width = 0 }
|
|
|
|
return
|
2023-04-03 20:22:29 -06:00
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
func (element *TableContainer) hook (child tomo.Element) {
|
|
|
|
if child0, ok := child.(tomo.Themeable); ok {
|
|
|
|
child0.SetTheme(element.theme.Theme)
|
|
|
|
}
|
|
|
|
if child0, ok := child.(tomo.Configurable); ok {
|
|
|
|
child0.SetConfig(element.config.Config)
|
|
|
|
}
|
|
|
|
child.SetParent(element)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) unhook (child tomo.Element) {
|
|
|
|
child.SetParent(nil)
|
|
|
|
child.DrawTo(nil, image.Rectangle { }, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) rebuildChildList (list []tomo.Element) {
|
|
|
|
index := 0
|
|
|
|
for _, row := range element.grid {
|
|
|
|
for _, child := range row {
|
2023-04-03 18:06:17 -06:00
|
|
|
if child.Element == nil { continue }
|
2023-04-03 18:01:44 -06:00
|
|
|
list[index] = child.Element
|
2023-04-03 18:06:17 -06:00
|
|
|
index ++
|
2023-04-03 18:01:44 -06:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
func (element *TableContainer) state (column, row int) (state tomo.State) {
|
|
|
|
if column == element.selectedColumn && row == element.selectedRow {
|
|
|
|
state.On = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) redoCell (column, row int) image.Rectangle {
|
|
|
|
padding := element.theme.Padding(tomo.PatternTableCell)
|
|
|
|
cell := element.grid[row][column]
|
|
|
|
pattern := element.theme.Pattern (
|
|
|
|
cell.Pattern, element.state(column, row))
|
|
|
|
|
|
|
|
if cell.Element != nil {
|
|
|
|
// give child canvas portion
|
|
|
|
innerCellBounds := padding.Apply(cell.Rectangle)
|
|
|
|
artist.DrawShatter (
|
|
|
|
element.core, pattern,
|
|
|
|
cell.Rectangle, innerCellBounds)
|
|
|
|
cell.DrawTo (
|
|
|
|
canvas.Cut(element.core, innerCellBounds),
|
|
|
|
innerCellBounds,
|
|
|
|
element.childDrawCallback)
|
|
|
|
} else {
|
|
|
|
// draw cell pattern in empty cells
|
|
|
|
pattern.Draw(element.core, cell.Rectangle)
|
|
|
|
}
|
|
|
|
return cell.Rectangle
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
func (element *TableContainer) redoAll () {
|
|
|
|
if element.warping || !element.core.HasImage() {
|
|
|
|
element.updateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
2023-04-03 21:09:02 -06:00
|
|
|
|
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if element.scroll.Y > maxScrollHeight {
|
|
|
|
element.scroll.Y = maxScrollHeight
|
|
|
|
}
|
|
|
|
maxScrollWidth := element.maxScrollWidth()
|
|
|
|
if element.scroll.X > maxScrollWidth {
|
|
|
|
element.scroll.X = maxScrollWidth
|
|
|
|
}
|
2023-04-03 18:01:44 -06:00
|
|
|
|
|
|
|
// calculate the minimum size of each column and row
|
2023-04-03 19:48:57 -06:00
|
|
|
var minWidth, minHeight float64
|
|
|
|
columnWidths := make([]float64, element.columns)
|
|
|
|
rowHeights := make([]float64, element.rows)
|
2023-04-03 18:01:44 -06:00
|
|
|
padding := element.theme.Padding(tomo.PatternTableCell)
|
|
|
|
|
|
|
|
for rowIndex, row := range element.grid {
|
|
|
|
for columnIndex, child := range row {
|
|
|
|
width, height := padding.Horizontal(), padding.Vertical()
|
|
|
|
|
|
|
|
if child.Element != nil {
|
|
|
|
minWidth, minHeight := child.MinimumSize()
|
|
|
|
width += minWidth
|
|
|
|
height += minHeight
|
2023-04-03 19:48:57 -06:00
|
|
|
fwidth := float64(width)
|
|
|
|
fheight := float64(height)
|
2023-04-03 18:01:44 -06:00
|
|
|
if fwidth > columnWidths[columnIndex] {
|
|
|
|
columnWidths[columnIndex] = fwidth
|
|
|
|
}
|
|
|
|
if fheight > rowHeights[rowIndex] {
|
|
|
|
rowHeights[rowIndex] = fheight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2023-04-03 21:09:02 -06:00
|
|
|
for _, width := range columnWidths { minWidth += width }
|
|
|
|
for _, height := range rowHeights { minHeight += height }
|
2023-04-03 18:01:44 -06:00
|
|
|
|
2023-04-03 21:09:02 -06:00
|
|
|
// ignore given bounds for layout if they are below minimum size. we do
|
|
|
|
// this because we are scrollable in both directions and we might be
|
|
|
|
// collapsed.
|
|
|
|
bounds := element.Bounds().Sub(element.scroll)
|
|
|
|
if bounds.Dx() < int(minWidth) {
|
|
|
|
bounds.Max.X = bounds.Min.X + int(minWidth)
|
|
|
|
}
|
|
|
|
if bounds.Dy() < int(minHeight) {
|
|
|
|
bounds.Max.Y = bounds.Min.Y + int(minHeight)
|
|
|
|
}
|
|
|
|
element.contentBounds = bounds
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
// scale up those minimum sizes to an actual size.
|
|
|
|
// FIXME: replace this with a more accurate algorithm
|
2023-04-03 19:48:57 -06:00
|
|
|
widthRatio := float64(bounds.Dx()) / minWidth
|
|
|
|
heightRatio := float64(bounds.Dy()) / minHeight
|
|
|
|
for index := range columnWidths {
|
|
|
|
columnWidths[index] *= widthRatio
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
2023-04-03 19:48:57 -06:00
|
|
|
for index := range rowHeights {
|
|
|
|
rowHeights[index] *= heightRatio
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// cut up canvas
|
2023-04-03 19:48:57 -06:00
|
|
|
x := float64(bounds.Min.X)
|
|
|
|
y := float64(bounds.Min.Y)
|
2023-04-03 18:01:44 -06:00
|
|
|
for rowIndex, row := range element.grid {
|
2023-04-03 20:22:29 -06:00
|
|
|
for columnIndex, _ := range row {
|
2023-04-03 19:48:57 -06:00
|
|
|
width := columnWidths[columnIndex]
|
|
|
|
height := rowHeights[rowIndex]
|
|
|
|
cellBounds := image.Rect (
|
|
|
|
int(x), int(y),
|
|
|
|
int(x + width), int(y + height))
|
2023-04-03 18:01:44 -06:00
|
|
|
|
|
|
|
var id tomo.Pattern
|
|
|
|
isHeading :=
|
|
|
|
rowIndex == 0 && element.topHeading ||
|
|
|
|
columnIndex == 0 && element.leftHeading
|
|
|
|
if isHeading {
|
|
|
|
id = tomo.PatternTableHead
|
|
|
|
} else {
|
|
|
|
id = tomo.PatternTableCell
|
|
|
|
}
|
|
|
|
element.grid[rowIndex][columnIndex].Rectangle = cellBounds
|
2023-04-03 20:22:29 -06:00
|
|
|
element.grid[rowIndex][columnIndex].Pattern = id
|
2023-04-03 18:01:44 -06:00
|
|
|
|
2023-04-03 20:22:29 -06:00
|
|
|
element.redoCell(columnIndex, rowIndex)
|
2023-04-03 19:48:57 -06:00
|
|
|
x += float64(width)
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:48:57 -06:00
|
|
|
x = float64(bounds.Min.X)
|
|
|
|
y += rowHeights[rowIndex]
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
element.core.DamageAll()
|
|
|
|
|
|
|
|
// update the minimum size of the element
|
2023-04-03 21:09:02 -06:00
|
|
|
if element.forcedMinimumHeight > 0 {
|
|
|
|
minHeight = float64(element.forcedMinimumHeight)
|
|
|
|
}
|
|
|
|
if element.forcedMinimumWidth > 0 {
|
|
|
|
minWidth = float64(element.forcedMinimumWidth)
|
|
|
|
}
|
2023-04-03 19:48:57 -06:00
|
|
|
element.core.SetMinimumSize(int(minWidth), int(minHeight))
|
2023-04-03 21:09:02 -06:00
|
|
|
|
|
|
|
// notify parent of scroll bounds change
|
|
|
|
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
|
|
|
|
parent.NotifyScrollBoundsChange(element)
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
2023-04-03 18:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TableContainer) updateMinimumSize () {
|
2023-04-03 21:09:02 -06:00
|
|
|
if element.forcedMinimumHeight > 0 && element.forcedMinimumWidth > 0 {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
element.forcedMinimumWidth,
|
|
|
|
element.forcedMinimumHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
columnWidths := make([]int, element.columns)
|
|
|
|
rowHeights := make([]int, element.rows)
|
|
|
|
padding := element.theme.Padding(tomo.PatternTableCell)
|
|
|
|
|
|
|
|
for rowIndex, row := range element.grid {
|
|
|
|
for columnIndex, child := range row {
|
|
|
|
width, height := padding.Horizontal(), padding.Vertical()
|
|
|
|
|
|
|
|
if child.Element != nil {
|
|
|
|
minWidth, minHeight := child.MinimumSize()
|
|
|
|
width += minWidth
|
|
|
|
height += minHeight
|
|
|
|
if width > columnWidths[columnIndex] {
|
|
|
|
columnWidths[columnIndex] = width
|
|
|
|
}
|
|
|
|
if height > rowHeights[rowIndex] {
|
|
|
|
rowHeights[rowIndex] = height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
|
|
|
var minWidth, minHeight int
|
|
|
|
for _, width := range columnWidths { minWidth += width }
|
|
|
|
for _, height := range rowHeights { minHeight += height }
|
|
|
|
|
2023-04-03 21:09:02 -06:00
|
|
|
if element.forcedMinimumHeight > 0 {
|
|
|
|
minHeight = element.forcedMinimumHeight
|
|
|
|
}
|
|
|
|
if element.forcedMinimumWidth > 0 {
|
|
|
|
minWidth = element.forcedMinimumWidth
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:44 -06:00
|
|
|
element.core.SetMinimumSize(minWidth, minHeight)
|
|
|
|
}
|
2023-04-03 20:22:29 -06:00
|
|
|
|
|
|
|
func (element *TableContainer) childDrawCallback (region image.Rectangle) {
|
|
|
|
element.core.DamageRegion(region)
|
|
|
|
}
|