From 1c3cc956e82a13bfb17cfae3eb81f45dfd0cd480 Mon Sep 17 00:00:00 2001 From: Anindya Chatterjee Date: Mon, 30 Oct 2017 10:26:38 +0530 Subject: [PATCH 1/2] Changes for #518. --- src/main/java/tornadofx/Layouts.kt | 19 ++++++++ .../kotlin/tornadofx/tests/LayoutsTest.kt | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/kotlin/tornadofx/tests/LayoutsTest.kt diff --git a/src/main/java/tornadofx/Layouts.kt b/src/main/java/tornadofx/Layouts.kt index eb6883f2b..01bd4bb2d 100644 --- a/src/main/java/tornadofx/Layouts.kt +++ b/src/main/java/tornadofx/Layouts.kt @@ -329,3 +329,22 @@ 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) { + minHeightProperty().bind(parent.heightProperty()) + } +} + +fun Region.fitToParentWidth() { + val parent = this.parent + if (parent != null && parent is Region) { + minWidthProperty().bind(parent.widthProperty()) + } +} + +fun Region.fitToParentSize() { + fitToParentHeight() + fitToParentWidth() +} \ 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..50702b6c5 --- /dev/null +++ b/src/test/kotlin/tornadofx/tests/LayoutsTest.kt @@ -0,0 +1,45 @@ +package tornadofx.tests + +import javafx.scene.Scene +import javafx.scene.layout.Pane +import javafx.scene.layout.VBox +import javafx.stage.Stage +import org.junit.Before +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 pane: Pane + lateinit var vbox: VBox + + @Before + fun setup() { + pane = Pane() + } + + @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) + } + } +} \ No newline at end of file From a69b7af968e27c0037a0fe6e48281ec5519668b1 Mon Sep 17 00:00:00 2001 From: Anindya Chatterjee Date: Mon, 30 Oct 2017 10:45:13 +0530 Subject: [PATCH 2/2] Changes for #518. Fit size to another container --- src/main/java/tornadofx/Layouts.kt | 17 ++++++++++-- .../kotlin/tornadofx/tests/LayoutsTest.kt | 27 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/tornadofx/Layouts.kt b/src/main/java/tornadofx/Layouts.kt index 01bd4bb2d..bac8e1193 100644 --- a/src/main/java/tornadofx/Layouts.kt +++ b/src/main/java/tornadofx/Layouts.kt @@ -333,18 +333,31 @@ var Region.paddingAll: Number get() = (padding.top + padding.right + padding.bot fun Region.fitToParentHeight() { val parent = this.parent if (parent != null && parent is Region) { - minHeightProperty().bind(parent.heightProperty()) + fitToHeight(parent) } } fun Region.fitToParentWidth() { val parent = this.parent if (parent != null && parent is Region) { - minWidthProperty().bind(parent.widthProperty()) + 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 index 50702b6c5..9d26d1990 100644 --- a/src/test/kotlin/tornadofx/tests/LayoutsTest.kt +++ b/src/test/kotlin/tornadofx/tests/LayoutsTest.kt @@ -1,10 +1,10 @@ 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.Before import org.junit.Test import org.testfx.api.FxToolkit import tornadofx.* @@ -17,14 +17,8 @@ import kotlin.test.assertEquals class RegionTest { val primaryStage: Stage = FxToolkit.registerPrimaryStage() - lateinit var pane: Pane lateinit var vbox: VBox - @Before - fun setup() { - pane = Pane() - } - @Test fun testFitToParentSize() { FxToolkit.setupFixture { @@ -42,4 +36,23 @@ class RegionTest { 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