Is there any way to bind a property to appConfig in tornadofx?

182 Views Asked by At

Suppose I want to save a view's height and width value using appConfig in tornadofx. Is there anyway I can bind these properties to appConfig, so that when I save the config, the latest value of height and width will always be saved?

1

There are 1 best solutions below

0
On BEST ANSWER

If what you want to do is to save the current width/height of the Window and restore that when the View is docked again, you can override onDock to do both operations there:

override fun onDock() {
    if (config["w"] != null && config["h"] != null) {
        currentWindow?.apply {
            width = config.double("w")!!
            height = config.double("h")!!
        }
    }

    currentWindow?.apply {
        Bindings.add(widthProperty(), heightProperty()).onChange {
            with (config) {
                put("w", width.toString())
                put("h", height.toString())
                save()
            }
        }
    }
}