Skip to content

Commit

Permalink
feat: ViewComponent.getChild() allows retrieving a specific child tha…
Browse files Browse the repository at this point in the history
…t is directly cast to given type
  • Loading branch information
AlmasB committed Oct 22, 2022
1 parent 3828786 commit d5c546a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Expand Up @@ -173,6 +173,13 @@ class ViewComponent : Component() {
updatableViews += node
}

/**
* @return a child node at given [index] which is then cast into [type]
*/
fun <T : Node> getChild(index: Int, type: Class<T>): T {
return type.cast(children[index])
}

/**
* Remove a child from this view.
*/
Expand Down
Expand Up @@ -14,6 +14,7 @@ import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.input.MouseButton
import javafx.scene.input.MouseEvent
import javafx.scene.shape.Circle
import javafx.scene.shape.Rectangle
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
Expand Down Expand Up @@ -68,6 +69,21 @@ class ViewComponentTest {
assertThat(rect2.parent.transforms.size, `is`(0))
}

@Test
fun `Get child with cast`() {
val rect = Rectangle()
val circle = Circle()

view.addChild(rect)
view.addChild(circle)

val rect2: Rectangle = view.getChild(0, Rectangle::class.java)
val circle2: Circle = view.getChild(1, Circle::class.java)

assertThat(rect2, `is`(rect))
assertThat(circle2, `is`(circle))
}

@Test
fun `View type children are correctly updated onUpdate and disposed on remove`() {
val child = TestView()
Expand Down

0 comments on commit d5c546a

Please sign in to comment.