commonMain.io.nacular.doodle.theme.basic.table.BasicTableBehavior.kt Maven / Gradle / Ivy
package io.nacular.doodle.theme.basic.table
import io.nacular.doodle.controls.ColorPicker
import io.nacular.doodle.controls.EditOperation
import io.nacular.doodle.controls.IndexedItem
import io.nacular.doodle.controls.ItemVisualizer
import io.nacular.doodle.controls.buttons.CheckBox
import io.nacular.doodle.controls.list.ListLike
import io.nacular.doodle.controls.table.AbstractTableBehavior.FooterCellGenerator
import io.nacular.doodle.controls.table.AbstractTableBehavior.HeaderCellGenerator
import io.nacular.doodle.controls.table.AbstractTableBehavior.MetaRowPositioner
import io.nacular.doodle.controls.table.AbstractTableBehavior.OverflowColumnConfig
import io.nacular.doodle.controls.table.Column
import io.nacular.doodle.controls.table.MetaRowGeometry
import io.nacular.doodle.controls.table.MutableColumn
import io.nacular.doodle.controls.table.MutableTable
import io.nacular.doodle.controls.table.Table
import io.nacular.doodle.controls.table.TableBehavior
import io.nacular.doodle.controls.table.TableBehavior.CellGenerator
import io.nacular.doodle.controls.text.TextField
import io.nacular.doodle.core.Container
import io.nacular.doodle.core.Display
import io.nacular.doodle.core.View
import io.nacular.doodle.drawing.Canvas
import io.nacular.doodle.drawing.Color
import io.nacular.doodle.drawing.Color.Companion.Blue
import io.nacular.doodle.drawing.Color.Companion.Lightgray
import io.nacular.doodle.drawing.Color.Companion.White
import io.nacular.doodle.drawing.ColorPaint
import io.nacular.doodle.drawing.horizontalStripedPaint
import io.nacular.doodle.drawing.lighter
import io.nacular.doodle.drawing.opacity
import io.nacular.doodle.event.KeyEvent
import io.nacular.doodle.event.KeyListener
import io.nacular.doodle.event.KeyListener.Companion.released
import io.nacular.doodle.event.KeyText.Companion.Enter
import io.nacular.doodle.event.KeyText.Companion.Escape
import io.nacular.doodle.event.PointerEvent
import io.nacular.doodle.event.PointerListener
import io.nacular.doodle.focus.FocusManager
import io.nacular.doodle.geometry.Point
import io.nacular.doodle.geometry.Rectangle
import io.nacular.doodle.layout.Insets
import io.nacular.doodle.layout.constraints.constrain
import io.nacular.doodle.system.SystemInputEvent.Modifier.Ctrl
import io.nacular.doodle.system.SystemInputEvent.Modifier.Meta
import io.nacular.doodle.system.SystemInputEvent.Modifier.Shift
import io.nacular.doodle.theme.basic.ListItem
import io.nacular.doodle.theme.basic.SelectableListKeyHandler
import io.nacular.doodle.theme.basic.VerticalListPositioner
import io.nacular.doodle.utils.Dimension
import io.nacular.doodle.utils.Encoder
import io.nacular.doodle.utils.HorizontalAlignment
import io.nacular.doodle.utils.PassThroughEncoder
import io.nacular.doodle.utils.PropertyObserver
import io.nacular.doodle.utils.SetObserver
import kotlin.Result.Companion.success
/**
* Created by Nicholas Eddy on 4/8/19.
*/
private class TableListRow(
private val column : Column,
list : ListLike,
row : T,
index : Int,
itemVisualizer : ItemVisualizer,
selectionColor : Color? = Blue,
selectionBlurredColor: Color? = selectionColor): ListItem(list, row, index, itemVisualizer, backgroundSelectionColor = selectionColor, backgroundSelectionBlurredColor = selectionBlurredColor) {
private val alignmentChanged: (Column<*>) -> Unit = {
positioner = when (val alignment = it.cellAlignment) {
null -> defaultPositioner
else -> alignment
}
}
init {
column.cellAlignment?.let { positioner = it }
}
override fun addedToDisplay() {
super.addedToDisplay()
column.alignmentChanged += alignmentChanged
}
override fun removedFromDisplay() {
super.removedFromDisplay()
column.alignmentChanged -= alignmentChanged
}
}
public open class BasicCellGenerator: CellGenerator {
@Suppress("UNCHECKED_CAST")
override fun invoke(table: Table, column: Column, cell: A, row: Int, itemGenerator: ItemVisualizer, current: View?): View = when (current) {
is TableListRow<*> -> (current as TableListRow).apply { update(table, cell, row) }
else -> TableListRow(column, table, cell, row, itemGenerator, selectionColor = null, selectionBlurredColor = null)
}
}
public open class BasicTableBehavior(
private val focusManager : FocusManager?,
protected val rowHeight : Double = 20.0,
protected val headerColor : Color? = Lightgray,
protected val footerColor : Color? = headerColor,
evenRowColor : Color? = White,
oddRowColor : Color? = Lightgray.lighter().lighter(),
protected val selectionColor : Color? = Blue,
protected val selectionBlurredColor: Color? = Lightgray): TableBehavior(), PointerListener, KeyListener, SelectableListKeyHandler {
private val selectionChanged: SetObserver