commit 6f89fd3d215744762b6cc3d48040780126ce4d58
parent 0aa4fd4854250569c09b956dbb45c3acfd53316a
Author: falkTX <falktx@falktx.com>
Date: Mon, 13 Sep 2021 10:36:36 +0100
Better handling of transient windows, using ScopedGraphicsContext
Diffstat:
4 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/dgl/StandaloneWindow.hpp b/dgl/StandaloneWindow.hpp
@@ -37,12 +37,12 @@ public:
sgc((Window&)*this) {}
/**
- Constructor with parent window, typically used to run as modal.
+ Constructor with a transient parent window, typically used to run as modal.
*/
- StandaloneWindow(Application& app, Window& parentWindow)
- : Window(app, parentWindow),
+ StandaloneWindow(Application& app, Window& transientParentWindow)
+ : Window(app, transientParentWindow),
TopLevelWidget((Window&)*this),
- sgc((Window&)*this) {}
+ sgc((Window&)*this, transientParentWindow) {}
/**
Clear current graphics context.
diff --git a/dgl/Window.hpp b/dgl/Window.hpp
@@ -49,6 +49,8 @@ class TopLevelWidget;
*/
class Window
{
+ struct PrivateData;
+
public:
#ifndef DGL_FILE_BROWSER_DISABLED
/**
@@ -131,6 +133,9 @@ public:
/** Constructor that will make the @a window graphics context the current one */
explicit ScopedGraphicsContext(Window& window);
+ /** Overloaded constructor, gives back context to its transient parent when done */
+ explicit ScopedGraphicsContext(Window& window, Window& transientParentWindow);
+
/** Desstructor for clearing current context, if not done yet */
~ScopedGraphicsContext();
@@ -142,6 +147,7 @@ public:
private:
Window& window;
+ Window::PrivateData* ppData;
bool active;
};
@@ -451,7 +457,6 @@ protected:
#endif
private:
- struct PrivateData;
PrivateData* const pData;
friend class Application;
friend class PluginWindow;
diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp
@@ -25,20 +25,35 @@ START_NAMESPACE_DGL
Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win)
: window(win),
+ ppData(nullptr),
active(puglBackendEnter(window.pData->view)) {}
+Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win, Window& transientWin)
+ : window(win),
+ ppData(transientWin.pData),
+ active(false)
+{
+ puglBackendLeave(ppData->view);
+ active = puglBackendEnter(window.pData->view);
+}
+
Window::ScopedGraphicsContext::~ScopedGraphicsContext()
{
- if (active)
- puglBackendLeave(window.pData->view);
+ done();
}
void Window::ScopedGraphicsContext::done()
{
if (active)
{
- active = false;
puglBackendLeave(window.pData->view);
+ active = false;
+ }
+
+ if (ppData != nullptr)
+ {
+ puglBackendEnter(ppData->view);
+ ppData = nullptr;
}
}
diff --git a/dgl/src/WindowPrivateData.cpp b/dgl/src/WindowPrivateData.cpp
@@ -123,7 +123,6 @@ Window::PrivateData::PrivateData(Application& a, Window* const s, PrivateData* c
#endif
modal(ppData)
{
- puglBackendLeave(transientParentView);
puglSetTransientFor(view, puglGetNativeWindow(transientParentView));
initPre(DEFAULT_WIDTH, DEFAULT_HEIGHT, false);
@@ -266,10 +265,6 @@ bool Window::PrivateData::initPost()
puglShow(view);
}
- // give context back to transient parent window
- if (transientParentView != nullptr)
- puglBackendEnter(transientParentView);
-
return true;
}