[BR] Wrong position for top window when opened

On Linux (Ubuntu 20.04) opening a window at a given position is sometimes wrong.

Note that i get the same error if restoreWindowStateFromString is used.

Slightly changed the code in the repository…

Note that my example doesn’t seems do reproduce easily the bug anymore, whereas that bug remains in my main application (and in other dummy examples). I need to investigate why. :thinking:

Hmm, that is strange. One thing that comes to mind, is that on Linux restoreWindowStateFromString() will try to adjust window positions to account for the fact that border sizes are unknown for a while after creating a window.

But this adjustment can only be correct if everything about the window’s styling is know at the time of calling restoreWindowStateFromString() and that includes knowing whether a native or JUCE titlebar is used.

So this is just a guess, but make sure you call setUsingNativeTitleBar (true); before restoreWindowStateFromString(). I wonder if it will fix this for you.

In the small example provided i didn’t use restoreWindowStateFromString at all!
An arbitrary bound of juce::Rectangle<int> (300, 300, 300, 200) is used.
The error was rare. It seems that it is even more hard (almost impossible) to reproduce it nowadays.
Something might have changed since in JUCE code.

But the window position is always wrong in my application. :roll_eyes:
Hence I’ll try to make a new example.

I refactored my dummy example. But i can’t reproduce the bug easily anymore. :sob:

In my application (in Release mode) window positions are always wrong.

In my application (in Debug mode) window positions are (at first view) good.

On macOS it is ok in both versions. Weird. :face_with_spiral_eyes:

Notice that in my code below if…

void BaseWindow::makeVisible (juce::Rectangle<int> window)
{
    if (!window.isEmpty()) { setBounds (window); }
    else if (keyName_.isNotEmpty()) {
    //
    juce::PropertiesFile& p = Spaghettis()->getProperties();
    
    const std::unique_ptr<juce::XmlElement> e (p.getXmlValue (keyName_ + "Position"));
    
    if (e && e->hasTagName (Ids::POSITION) && e->hasAttribute (Ids::value)) {
        const juce::String s = e->getStringAttribute (Ids::value);
        if (s.isNotEmpty()) {
            std::cout << s.toStdString() << std::endl;    /* Good values!!! */
            restoreWindowStateFromString (s);
        }
    }
    //
    }

    setVisible (true); addToDesktop(); toFront (true);
}

…i move the following line…

setVisible (true); addToDesktop(); toFront (true);

…at start of the function the window blink, but is properly arranged at the end. :exploding_head:

(Switched back from lowlatency to generic kernel doesn’t change anything.)

If i add an outrageous pause after restoreWindowStateFromString it works!

void BaseWindow::makeVisible (juce::Rectangle<int> window)
{
    ...
    
    juce::Thread::sleep (50);

    setVisible (true); addToDesktop(); toFront (true);
}

But that’s clearly not an usable workaround.

Frankly am i the only one to struggle that much with that?

By the way (at the end of the week) that code seems to be a reasonable workaround for me.

auto f = [p = juce::Component::SafePointer<BaseWindow> (this)]()
{
    if (p.getComponent()) { p->setVisible (true); p->addToDesktop(); p->toFront (true); }
};
    
#if JUCE_LINUX
    Timer::callAfterDelay (100, f);
#else
    f();
#endif

:yawning_face: