diff --git a/src/main/java/tornadofx/Layouts.kt b/src/main/java/tornadofx/Layouts.kt index eb6883f2b..bac8e1193 100644 --- a/src/main/java/tornadofx/Layouts.kt +++ b/src/main/java/tornadofx/Layouts.kt @@ -329,3 +329,35 @@ fun Region.paddingAll(p: Double) { var Region.paddingAll: Number get() = (padding.top + padding.right + padding.bottom + padding.left) / 4.0; set(value) { padding = Insets(value.toDouble(), value.toDouble(), value.toDouble(), value.toDouble()) } + +fun Region.fitToParentHeight() { + val parent = this.parent + if (parent != null && parent is Region) { + fitToHeight(parent) + } +} + +fun Region.fitToParentWidth() { + val parent = this.parent + if (parent != null && parent is Region) { + fitToWidth(parent) + } +} + +fun Region.fitToParentSize() { + fitToParentHeight() + fitToParentWidth() +} + +fun Region.fitToHeight(region: Region) { + minHeightProperty().bind(region.heightProperty()) +} + +fun Region.fitToWidth(region: Region) { + minWidthProperty().bind(region.widthProperty()) +} + +fun Region.fitToSize(region: Region) { + fitToHeight(region) + fitToWidth(region) +} \ No newline at end of file diff --git a/src/test/kotlin/tornadofx/tests/LayoutsTest.kt b/src/test/kotlin/tornadofx/tests/LayoutsTest.kt new file mode 100644 index 000000000..9d26d1990 --- /dev/null +++ b/src/test/kotlin/tornadofx/tests/LayoutsTest.kt @@ -0,0 +1,58 @@ +package tornadofx.tests + +import javafx.scene.Scene +import javafx.scene.control.ScrollPane +import javafx.scene.layout.Pane +import javafx.scene.layout.VBox +import javafx.stage.Stage +import org.junit.Test +import org.testfx.api.FxToolkit +import tornadofx.* +import kotlin.test.assertEquals + +/** + * + * @author Anindya Chatterjee + */ +class RegionTest { + val primaryStage: Stage = FxToolkit.registerPrimaryStage() + + lateinit var vbox: VBox + + @Test + fun testFitToParentSize() { + FxToolkit.setupFixture { + val root = Pane().apply { + vbox { + this@RegionTest.vbox = this + fitToParentSize() + } + setPrefSize(400.0, 160.0) + } + primaryStage.scene = Scene(root) + primaryStage.show() + + assertEquals(root.width, vbox.width) + assertEquals(root.height, vbox.height) + } + } + + @Test + fun testFitToSize() { + FxToolkit.setupFixture { + val root = ScrollPane().apply { + content = vbox { + this@RegionTest.vbox = this + } + setPrefSize(400.0, 160.0) + } + + vbox.fitToSize(root) + primaryStage.scene = Scene(root) + primaryStage.show() + + assertEquals(root.width, vbox.width) + assertEquals(root.height, vbox.height) + } + } +} \ No newline at end of file