<commit_before>package tornadofx

import javafx.beans.property.SimpleStringProperty
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.canvas.Canvas
import javafx.scene.effect.DropShadow
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.layout.BorderPane
import javafx.scene.layout.StackPane
import javafx.scene.paint.Color
import java.net.URL

internal class InternalWindow(icon: Node?, modal: Boolean, escapeClosesWindow: Boolean, closeButton: Boolean) : StackPane() {
    private lateinit var window: BorderPane
    private lateinit var coverNode: Node
    private var titleProperty = SimpleStringProperty()
    var overlay: Canvas? = null
    private var indexInCoverParent: Int? = null
    private var coverParent: Parent? = null
    private var offsetX = 0.0
    private var offsetY = 0.0

    init {
        if (escapeClosesWindow) {
            addEventFilter(KeyEvent.KEY_PRESSED) {
                if (it.code == KeyCode.ESCAPE)
                    close()
            }
        }

        addClass(Styles.floatingWindowWrapper)

        if (modal) {
            canvas {
                overlay = this
                graphicsContext2D.fill = c("#000", 0.4)
                widthProperty().bind(this@InternalWindow.widthProperty())
                heightProperty().bind(this@InternalWindow.heightProperty())
                widthProperty().onChange { fillOverlay() }
                heightProperty().onChange { fillOverlay() }
            }
        }

        borderpane {
            addClass(Styles.window)
            window = this
            top {
                hbox(5.0) {
                    addClass(Styles.top)
                    label(titleProperty) {
                        graphic = icon
                        isMouseTransparent = true
                    }
                    spacer {
                        isMouseTransparent = true
                    }
                    if (closeButton) {
                        button {
                            setOnMouseClicked {
                                close()
                            }
                            graphic = svgpath("M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z") {
                                addClass(Styles.closebutton)
                            }
                        }
                    }
                }
            }
        }
        moveWindowOnDrag()

        window.center = stackpane {
            addClass(Styles.floatingWindowContent)
        }
    }

    class Styles : Stylesheet() {
        companion object {
            val floatingWindowWrapper by cssclass()
            val floatingWindowContent by cssclass()
            val window by cssclass()
            val top by cssclass()
            val closebutton by cssclass()
        }

        init {
            floatingWindowWrapper {
                window {
                    effect = DropShadow()
                }

                top {
                    backgroundColor += Color.WHITE
                    padding = box(1.px, 1.px, 2.px, 5.px)
                    borderColor += box(Color.TRANSPARENT, Color.TRANSPARENT, Color.GRAY, Color.TRANSPARENT)
                    borderWidth += box(0.2.px)

                    alignment = Pos.CENTER

                    button {
                        padding = box(4.px, 12.px)
                        backgroundRadius += box(0.px)
                        backgroundColor += Color.WHITE
                        and(hover) {
                            backgroundColor += Color.RED
                            star {
                                fill = Color.WHITE
                            }
                        }
                        star {
                            shape = "M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"
                        }
                    }
                }

                floatingWindowContent {
                    backgroundColor += c(244, 244, 244)
                    padding = box(5.px)
                }
            }
        }
    }

    override fun getUserAgentStylesheet() = URL("css://${Styles::class.java.name}").toExternalForm()!!

    fun fillOverlay() {
        overlay?.graphicsContext2D.apply {
            val lb = coverNode.layoutBounds
            this?.clearRect(0.0, 0.0, lb.width, lb.height)
            this?.fillRect(0.0, 0.0, lb.width, lb.height)
        }
    }

    fun open(view: UIComponent, owner: Node) {
        if (owner.parent is InternalWindow) return
        this.coverNode = owner
        this.coverParent = owner.parent
        this.titleProperty.bind(view.titleProperty)

        if (coverParent != null) {
            indexInCoverParent = coverParent!!.getChildList()!!.indexOf(owner)
            owner.removeFromParent()
            coverParent!!.getChildList()!!.add(indexInCoverParent!!, this)
        } else {
            val scene = owner.scene
            scene.root = this
        }

        (window.center as Parent) += view

        children.add(0, owner)
        fillOverlay()
    }

    fun close() {
        coverNode.removeFromParent()
        removeFromParent()
        if (indexInCoverParent != null) {
            coverParent!!.getChildList()!!.add(indexInCoverParent!!, coverNode)
        } else {
            scene.root = coverNode as Parent
        }
    }

    override fun layoutChildren() {<commit_msg>InternalWindow defensive close<commit_after>package tornadofx

import javafx.beans.property.SimpleStringProperty
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.canvas.Canvas
import javafx.scene.effect.DropShadow
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.layout.BorderPane
import javafx.scene.layout.StackPane
import javafx.scene.paint.Color
import java.net.URL

internal class InternalWindow(icon: Node?, modal: Boolean, escapeClosesWindow: Boolean, closeButton: Boolean) : StackPane() {
    private lateinit var window: BorderPane
    private lateinit var coverNode: Node
    private var titleProperty = SimpleStringProperty()
    var overlay: Canvas? = null
    private var indexInCoverParent: Int? = null
    private var coverParent: Parent? = null
    private var offsetX = 0.0
    private var offsetY = 0.0

    init {
        if (escapeClosesWindow) {
            addEventFilter(KeyEvent.KEY_PRESSED) {
                if (it.code == KeyCode.ESCAPE)
                    close()
            }
        }

        addClass(Styles.floatingWindowWrapper)

        if (modal) {
            canvas {
                overlay = this
                graphicsContext2D.fill = c("#000", 0.4)
                widthProperty().bind(this@InternalWindow.widthProperty())
                heightProperty().bind(this@InternalWindow.heightProperty())
                widthProperty().onChange { fillOverlay() }
                heightProperty().onChange { fillOverlay() }
            }
        }

        borderpane {
            addClass(Styles.window)
            window = this
            top {
                hbox(5.0) {
                    addClass(Styles.top)
                    label(titleProperty) {
                        graphic = icon
                        isMouseTransparent = true
                    }
                    spacer {
                        isMouseTransparent = true
                    }
                    if (closeButton) {
                        button {
                            setOnMouseClicked {
                                close()
                            }
                            graphic = svgpath("M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z") {
                                addClass(Styles.closebutton)
                            }
                        }
                    }
                }
            }
        }
        moveWindowOnDrag()

        window.center = stackpane {
            addClass(Styles.floatingWindowContent)
        }
    }

    class Styles : Stylesheet() {
        companion object {
            val floatingWindowWrapper by cssclass()
            val floatingWindowContent by cssclass()
            val window by cssclass()
            val top by cssclass()
            val closebutton by cssclass()
        }

        init {
            floatingWindowWrapper {
                window {
                    effect = DropShadow()
                }

                top {
                    backgroundColor += Color.WHITE
                    padding = box(1.px, 1.px, 2.px, 5.px)
                    borderColor += box(Color.TRANSPARENT, Color.TRANSPARENT, Color.GRAY, Color.TRANSPARENT)
                    borderWidth += box(0.2.px)

                    alignment = Pos.CENTER

                    button {
                        padding = box(4.px, 12.px)
                        backgroundRadius += box(0.px)
                        backgroundColor += Color.WHITE
                        and(hover) {
                            backgroundColor += Color.RED
                            star {
                                fill = Color.WHITE
                            }
                        }
                        star {
                            shape = "M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"
                        }
                    }
                }

                floatingWindowContent {
                    backgroundColor += c(244, 244, 244)
                    padding = box(5.px)
                }
            }
        }
    }

    override fun getUserAgentStylesheet() = URL("css://${Styles::class.java.name}").toExternalForm()!!

    fun fillOverlay() {
        overlay?.graphicsContext2D.apply {
            val lb = coverNode.layoutBounds
            this?.clearRect(0.0, 0.0, lb.width, lb.height)
            this?.fillRect(0.0, 0.0, lb.width, lb.height)
        }
    }

    fun open(view: UIComponent, owner: Node) {
        if (owner.parent is InternalWindow) return
        this.coverNode = owner
        this.coverParent = owner.parent
        this.titleProperty.bind(view.titleProperty)

        if (coverParent != null) {
            indexInCoverParent = coverParent!!.getChildList()!!.indexOf(owner)
            owner.removeFromParent()
            coverParent!!.getChildList()!!.add(indexInCoverParent!!, this)
        } else {
            val scene = owner.scene
            scene.root = this
        }

        (window.center as Parent) += view

        children.add(0, owner)
        fillOverlay()
    }

    fun close() {
        coverNode.removeFromParent()
        removeFromParent()
        if (indexInCoverParent != null) {
            coverParent!!.getChildList()!!.add(indexInCoverParent!!, coverNode)
        } else {
            scene?.root = coverNode as Parent?
        }
    }

    override fun layoutChildren() {
{'fraction_non_alphanumeric': 0.09094978068212335, 'fraction_numerical': 0.0300778802255841, 'mean_word_length': 1.295459215122252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5616971', 'n_tokens_mistral': 3191, 'n_tokens_neox': 2896, 'n_words': 576}