I have been reading bits and pieces about adopting an MVC style approach to UI in Juce.
A very brief example of this is given in a presentation at approx 27 mins into the video: Modern GUIs with Juce
I have used both MVP and MVC for presentation in .Net and enjoy the separation of view from non-UI related logic and would like to experiment with this when working in Juce.
It seems like the Component
class could be set up to behave as a ‘view’ similarly to the way MainComponent
is used in all the tutorials I have been following. A ‘Controller’ class could then be defined to accept a ‘view’ and listen/react to components from the view.
What I am struggling to understand is how to switch between views. I’m used to web apps where some action like a button click in a view would be sent to a controller, the controller would then redirect to another url and show another view - here I am a little lost.
From what I can gather the setContentOwned
method of the ResizableWindow
is what I would need to pass a view to. So what I have tried is…
- Create another Component just like the
MainComponent
but with a few small changes - lets call thisOtherMainComponent
just for example. - Declare a
DocumentWindow
inMainComponent
andOtherMainComponent
like:
//in MainComponent
DocumentWindow window{"Other Window", Desktop::getInstance().getDefaultLookAndFeel()
.findColour(ResizableWindow::backgroundColourId), DocumentWindow::allButtons };
and:
// in OtherMainComponent
DocumentWindow window{ "Main Window", Desktop::getInstance().getDefaultLookAndFeel()
.findColour(ResizableWindow::backgroundColourId), DocumentWindow::allButtons };
- Inside a button click event in
MainComponent
I then call thesetContentOwned
like:
window.setUsingNativeTitleBar(true);
window.setCentrePosition(600, 400);
window.setVisible(true);
window.setResizable(false, false);
window.setContentOwned(new OtherMainComponent(), true);
What happens when I run the project and click the button is that a new window opens with the correct UI, but now both MainComponent
and OtherMainComponent
are both displayed in their own window and when I close a window I get an exception because I have not overidden the DocumentWIndow::closeButtonPressed
method.
I have a strong suspicion that I am going about this all wrong - but I can’t seem to find any tutorials about switching between windows.
Grateful if anyone can point me in the right direction
PS - I tried the following using ResizableWindow
instead of DocumentWIndow
at stage 3:
ScopedPointer<ResizableWindow> window = new ResizableWindow("Other Window", true);
window->setUsingNativeTitleBar(true);
window->setCentrePosition(600, 400);
window->setVisible(true);
window->setResizable(false, false);
window->setContentOwned(new OtherMainComponent(), true);
But then I see another window quickly flash and dissapear whilst the original window remains displayed.