<commit_before>package tornadofx

import javafx.beans.property.ObjectProperty
import javafx.event.EventTarget
import javafx.geometry.Insets
import javafx.geometry.Orientation
import javafx.scene.Group
import javafx.scene.Node
import javafx.scene.canvas.Canvas
import javafx.scene.control.*
import javafx.scene.layout.*
import javafx.scene.Parent
import kotlin.reflect.KClass
import kotlin.reflect.KFunction1

private val GridPaneRowIdKey = "TornadoFX.GridPaneRowId"
private val GridPaneParentObjectKey = "TornadoFX.GridPaneParentObject"

fun GridPane.row(title: String? = null, op: (Pane.() -> Unit)? = null) {
    properties[GridPaneRowIdKey] = if (properties.containsKey(GridPaneRowIdKey)) properties[GridPaneRowIdKey] as Int + 1 else 0

    // Allow the caller to add children to a fake pane
    val fake = Pane()
    fake.properties[GridPaneParentObjectKey] = this
    if (title != null)
        fake.children.add(Label(title))

    op?.invoke(fake)

    // Create a new row in the GridPane and add the children added to the fake pane
    addRow(properties[GridPaneRowIdKey] as Int, *fake.children.toTypedArray())
}

fun GridPane.constraintsForColumn(columnIndex: Int): ColumnConstraints {
    val constraints = columnConstraints
    while (constraints.size <= columnIndex)
        constraints.add(ColumnConstraints())
    return constraints[columnIndex]
}

val Parent.columnConstraints: ColumnConstraints? get() {
    var cursor = this
    var next = parent
    while (next != null) {
        val gridReference = if (next is GridPane)
            next to GridPane.getColumnIndex(cursor)?.let { it }
        else if (next.parent == null) // perhaps we're still in the row builder
            (next.properties[GridPaneParentObjectKey] as? GridPane)?.let {
                it to next.getChildList()?.indexOf(cursor)
            }
        else null

        if (gridReference != null) {
            val (grid, columnIndex) = gridReference
            if (columnIndex != null && columnIndex >= 0)
                return grid.constraintsForColumn(columnIndex)
        }
        cursor = next
        next = next.parent
    }
    return null
}

fun Parent.columnConstraints(op: ColumnConstraints.() -> Unit) = columnConstraints?.apply { op() }

fun ToolBar.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null): Pane {
    val pane = Pane().apply {
        addClass("spacer")
        hgrow = prio
    }
    op?.invoke(pane)
    add(pane)
    return pane
}

fun HBox.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null) = opcr(this, Pane().apply { HBox.setHgrow(this, prio) }, op)
fun VBox.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null) = opcr(this, Pane().apply { VBox.setVgrow(this, prio) }, op)

fun EventTarget.toolbar(vararg nodes: Node, op: (ToolBar.() -> Unit)? = null): ToolBar {
    val toolbar = ToolBar()
    if (nodes.isNotEmpty())
        toolbar.items.addAll(nodes)
    opcr(this, toolbar, op)
    return toolbar
}


@Deprecated("No need to wrap ToolBar children in children{} anymore. Remove the wrapper and all builder items will still be added as before.", ReplaceWith("no children{} wrapper"), DeprecationLevel.WARNING)
fun ToolBar.children(op: ToolBar.() -> Unit): ToolBar {
    op(this)<commit_msg>Rename GridPane column constraints shortcuts to remove ambiguity.<commit_after>package tornadofx

import javafx.beans.property.ObjectProperty
import javafx.event.EventTarget
import javafx.geometry.Insets
import javafx.geometry.Orientation
import javafx.scene.Group
import javafx.scene.Node
import javafx.scene.canvas.Canvas
import javafx.scene.control.*
import javafx.scene.layout.*
import javafx.scene.Parent
import kotlin.reflect.KClass
import kotlin.reflect.KFunction1

private val GridPaneRowIdKey = "TornadoFX.GridPaneRowId"
private val GridPaneParentObjectKey = "TornadoFX.GridPaneParentObject"

fun GridPane.row(title: String? = null, op: (Pane.() -> Unit)? = null) {
    properties[GridPaneRowIdKey] = if (properties.containsKey(GridPaneRowIdKey)) properties[GridPaneRowIdKey] as Int + 1 else 0

    // Allow the caller to add children to a fake pane
    val fake = Pane()
    fake.properties[GridPaneParentObjectKey] = this
    if (title != null)
        fake.children.add(Label(title))

    op?.invoke(fake)

    // Create a new row in the GridPane and add the children added to the fake pane
    addRow(properties[GridPaneRowIdKey] as Int, *fake.children.toTypedArray())
}

fun GridPane.constraintsForColumn(columnIndex: Int): ColumnConstraints {
    val constraints = columnConstraints
    while (constraints.size <= columnIndex)
        constraints.add(ColumnConstraints())
    return constraints[columnIndex]
}

val Parent.gridpaneColumnConstraints: ColumnConstraints? get() {
    var cursor = this
    var next = parent
    while (next != null) {
        val gridReference = if (next is GridPane)
            next to GridPane.getColumnIndex(cursor)?.let { it }
        else if (next.parent == null) // perhaps we're still in the row builder
            (next.properties[GridPaneParentObjectKey] as? GridPane)?.let {
                it to next.getChildList()?.indexOf(cursor)
            }
        else null

        if (gridReference != null) {
            val (grid, columnIndex) = gridReference
            if (columnIndex != null && columnIndex >= 0)
                return grid.constraintsForColumn(columnIndex)
        }
        cursor = next
        next = next.parent
    }
    return null
}

fun Parent.gridpaneColumnConstraints(op: ColumnConstraints.() -> Unit) = gridpaneColumnConstraints?.apply { op() }

fun ToolBar.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null): Pane {
    val pane = Pane().apply {
        addClass("spacer")
        hgrow = prio
    }
    op?.invoke(pane)
    add(pane)
    return pane
}

fun HBox.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null) = opcr(this, Pane().apply { HBox.setHgrow(this, prio) }, op)
fun VBox.spacer(prio: Priority = Priority.ALWAYS, op: (Pane.() -> Unit)? = null) = opcr(this, Pane().apply { VBox.setVgrow(this, prio) }, op)

fun EventTarget.toolbar(vararg nodes: Node, op: (ToolBar.() -> Unit)? = null): ToolBar {
    val toolbar = ToolBar()
    if (nodes.isNotEmpty())
        toolbar.items.addAll(nodes)
    opcr(this, toolbar, op)
    return toolbar
}


@Deprecated("No need to wrap ToolBar children in children{} anymore. Remove the wrapper and all builder items will still be added as before.", ReplaceWith("no children{} wrapper"), DeprecationLevel.WARNING)
fun ToolBar.children(op: ToolBar.() -> Unit): ToolBar {
    op(this)
{'fraction_non_alphanumeric': 0.1147812971342383, 'fraction_numerical': 0.0012066365007541479, 'mean_word_length': 3.4473507712944333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345202', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1960, 'n_words': 544}