Skip to content

Instantly share code, notes, and snippets.

@bodiam
Created October 10, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodiam/053e81f93b36e5b69e7e6200c7988503 to your computer and use it in GitHub Desktop.
Save bodiam/053e81f93b36e5b69e7e6200c7988503 to your computer and use it in GitHub Desktop.
import io.nacular.doodle.application.Application
import io.nacular.doodle.application.application
import io.nacular.doodle.controls.panels.GridPanel
import io.nacular.doodle.controls.text.Label
import io.nacular.doodle.core.*
import io.nacular.doodle.geometry.Size
import io.nacular.doodle.layout.constrain
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.kodein.di.instance
class HelloWorld(display: Display) : Application {
init {
display += FormView()
}
override fun shutdown() {}
}
class FormView() : View() {
init {
GlobalScope.launch {
val outputHeight = 100.0
val buttonSpacing = 10.0
val gridPanel = GridPanel().apply {
add(Label("x"), 0, 0); add(Label("y"), 0, 1); add(Label("z"), 0, 2); add(Label("1"), 0, 3)
add(Label("z"), 1, 0); add(Label("3"), 1, 1); add(Label("0"), 1, 2); add(Label("4"), 1, 3)
add(Label("b"), 2, 0); add(Label("q"), 2, 1); add(Label("t"), 2, 2); add(Label("o"), 2, 3)
verticalSpacing = { buttonSpacing }
horizontalSpacing = { buttonSpacing }
}
children += listOf(gridPanel)
// Place output outside grid so the height can be more easily controlled
val constraints = constrain(gridPanel) { grid ->
grid.top = parent.top
grid.left = parent.left
grid.right = parent.right
grid.bottom = parent.bottom
}
layout = object: Layout by constraints {
// Set total height to grid panel's ideal width and height, plus output and spacing
override fun idealSize(container: PositionableContainer, default: Size?) = gridPanel.idealSize?.let {
Size(it.width, it.height + outputHeight + buttonSpacing)
}
}
// Force idealSize when gridPanel is laid out
gridPanel.sizePreferencesChanged += { _,_,new ->
idealSize = new.idealSize?.let { Size(it.width, it.height + outputHeight + buttonSpacing) }
}
}
}
}
fun main() {
application {
HelloWorld(display = instance())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment