Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Changes for #518. Fit size to another container
Browse files Browse the repository at this point in the history
  • Loading branch information
anidotnet committed Oct 30, 2017
1 parent 1c3cc95 commit a69b7af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/main/java/tornadofx/Layouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 20 additions & 7 deletions src/test/kotlin/tornadofx/tests/LayoutsTest.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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 {
Expand All @@ -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)
}
}
}

0 comments on commit a69b7af

Please sign in to comment.