edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.68k stars 272 forks source link

Subsequent openModal() calls on View shift the window position downwards #928

Open LittleLightCz opened 5 years ago

LittleLightCz commented 5 years ago

I think the title is already self-explanatory. When you openModal() a new window and close it. And openModal() it again, it will appear a bit shifted to the down-right direction. Shouldn't it keep the last position?

userquin commented 5 years ago

@LittleLightCz have you a workaround to solve this ???

LittleLightCz commented 5 years ago

Hi there, if it wasn't fixed in recent versions then I am afraid I don't 😁

On Wed, 31 Jul 2019, 21:29 userquin, notifications@github.com wrote:

@LittleLightCz https://github.com/LittleLightCz have you a workaround to solve this ???

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/edvin/tornadofx/issues/928?email_source=notifications&email_token=ACSJKEFMJARZVIORI73MIQTQCHR3PA5CNFSM4G25RELKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3IJ3RQ#issuecomment-516988358, or mute the thread https://github.com/notifications/unsubscribe-auth/ACSJKEADNN5IIH6QTQKEZ6TQCHR3PANCNFSM4G25RELA .

userquin commented 5 years ago

I have fixed it using prefSize on the editor's root view and adding whenDockedOnce listener on it, then using parent window calculate the position respect it (findMainView() will return the parent View):

abstract class BaseEditorView: View() {

    override fun onBeforeShow() {
        configurePrefSize(true)
        currentWindow?.sizeToScene()
    }

    abstract fun configurePrefSize(apply: Boolean): Pair<Double, Double>

    fun handleEditorPosition() {
        currentWindow?.let { thisWindow ->
            findMainView().currentWindow?.let { parentWindow ->
                val (width, height) = configurePrefSize(false)
                thisWindow.x = parentWindow.x + ((parentWindow.width / 2.0) - (width / 2.0))
                thisWindow.y = parentWindow.y + (parentWindow.height / 2.0) - (height / 2.0)
            }
        }
    }

}

then in your editor view,


class View1: BaseEditorView() {

    private val preferredSize = 530.0 to 440.0 // customize your pref size

    init {
        whenDockedOnce {
            handleEditorPosition()
       }
    }

    override fun configurePrefSize(apply: Boolean): Pair<Double, Double> {
        if(apply) {
            root.setPrefSize(preferredSize.first, preferredSize.second)
        }
        return preferredSize
    }

    override val root = form {
        usePrefSize = true
        setPrefSize(preferredSize.first, preferredSize.second)
        style {
            hgrow = Priority.ALWAYS
            vgrow = Priority.ALWAYS
        }
   }
edvin commented 5 years ago

This has to do with the code in openModal that tries to place the window directly over the owner. A workaround is to pass owner = null as parameter to openModal(). I'm not entirely sure why we'd need this code and I'm considering removing it:

                    if (owner != null) {
                        x = owner.x + (owner.width / 2) - (scene.width / 2)
                        y = owner.y + (owner.height / 2) - (scene.height / 2)
                    }
userquin commented 5 years ago

@edvin If you see https://github.com/edvin/tornadofx/issues/1005 the space shifted is just this 9 extra pixels...

edvin commented 5 years ago

Yeah, the question is just why this code is in the framework and why it's needed. Removing it seems to place the child window perfectly without it. Anyone have any idea?