Search code examples
tornadofx

Dialog window positioning


I'm creating a small non-modal dialog in TornadoFX like this:

find<Grib>(scope).apply { openModal(block = true,
                          owner = FX.primaryStage,
                          stageStyle = StageStyle.UTILITY,
                          modality = Modality.NONE) }

How do I go about setting (and retrieving) it's window position for later? That is, I have a preferences object for the window location and I want to update it so that the next time the user opens the window, it opens in the same place they last closed it.


Solution

  • I was able to mostly solve my problem by digging through some of the TornadoFX source code.

    I added this to my init{} function:

    Platform.runLater {
        root.scene.window.x = Main.preferences.getDouble(GRIB_WINDOW_X, 400.0)
        root.scene.window.y = Main.preferences.getDouble(GRIB_WINDOW_Y, 400.0)
    }
    

    And then adding this to my close() function:

    Main.preferences.putDouble(GRIB_WINDOW_X, root.scene.window.x)
    Main.preferences.putDouble(GRIB_WINDOW_Y, root.scene.window.y)
    

    This "mostly" solves the problem in that it does save/restore the window position, however the window flickers when it is created as it moves from some default position to the newly set position.