Question about Component.addToDesktop

Hello everyone. I have very little experience, so I’m asking for advice. I need to place a child component on the desktop next to the main component on the right using ComponentPeer. I was trying to find a way to get the parent component’s coordinates relative to the desktop. The only thing I could come up with was placing a timer listener on the DocumentWindow, passing the values ​​via getWindowStateAsString() to global variables, and using them in MainComponent. Everything works perfectly, but I have some doubts: am I doing it correctly? Is it acceptable to use a timer in a DocumentWindow? Maybe there is another way without a timer? The code is below.
Main.h (DocumentWindow)

void timerCallback() override
        {
            String a = getWindowStateAsString();
            StringArray pos;
            pos = StringArray::fromTokens (a, " ", {});
            pX = pos[0].getIntValue();
            pY = pos[1].getIntValue();
        }

MainComponent.cpp

void GUI::MainComponent::displaySyncManager()     //SYNC
{
    showSyncCompInt = 1;   
    syncComponent.syncPlayer.dragComp.syncPlayerFontInt = 1;
    
    syncComponent.addToDesktop (ComponentPeer::windowIsResizable | ComponentPeer::windowIgnoresKeyPresses);
    thuComp_S->addToDesktop (ComponentPeer::windowIsResizable | ComponentPeer::windowIgnoresKeyPresses);

    #if JUCE_WINDOWS
    syncComponent.setBounds (getWidth() + pX + 1, pY - 29, 600, 230);
    thuComp_S->setBounds (getWidth() + pX + 1, 231 + pY - 29, 600, 49);
    #else
    syncComponent.setBounds (getWidth() + pX + 1, pY - 20, 600, 230);
    thuComp_S->setBounds (getWidth() + pX + 1, 231 + pY - 20, 600, 49);
    #endif
    
    syncComponent.loadSync();    
    syncComponent.setVisible (true);
    thuComp_S->setVisible (true);
}

And one more thing. ComponentPeer::windowIsResizable works on Mac OS, but not on Windows. Why?
I would be glad to receive any advice.

Do these two components belong to an app object? I would expect you could use the app object to manage the positions of the two windows/components rather than a global.

If I was trying to do this, I would start by trying a ComponentBoundsConstrainer and updating the position of the ‘following’ component in the resized() method of the ‘leading’ component. Maybe the BoundsConstrainer is an unnecessary complication though.

Also, I’m surprised if getScreenBounds() doesn’t work and you have to access the ComponentPeer.

Thanks. I spent half a day trying to figure out how to get the app’s coordinates. Great!

1 Like