From 6f89fd3d215744762b6cc3d48040780126ce4d58 Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 13 Sep 2021 10:37:03 +0100 Subject: [PATCH] Better handling of transient windows, using ScopedGraphicsContext --- dgl/StandaloneWindow.hpp | 8 ++++---- dgl/Window.hpp | 7 ++++++- dgl/src/Window.cpp | 21 ++++++++++++++++++--- dgl/src/WindowPrivateData.cpp | 5 ----- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/dgl/StandaloneWindow.hpp b/dgl/StandaloneWindow.hpp index 6502f3ae..f86e4c39 100644 --- 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 index 8c646777..744f7f2d 100644 --- 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 index 154c4974..b418fdad 100644 --- 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 index e2dddeb4..eed5c04d 100644 --- 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; }