Browse Source

Do not crash if pugl world/view creation fails

Signed-off-by: falkTX <falktx@falktx.com>
pull/401/merge
falkTX 1 year ago
parent
commit
227a7015a8
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
1 changed files with 31 additions and 19 deletions
  1. +31
    -19
      dgl/src/Window.cpp

+ 31
- 19
dgl/src/Window.cpp View File

@@ -27,17 +27,20 @@ START_NAMESPACE_DGL
Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win) Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win)
: window(win), : window(win),
ppData(nullptr), ppData(nullptr),
active(puglBackendEnter(window.pData->view)),
active(window.pData->view != nullptr && puglBackendEnter(window.pData->view)),
reenter(false) {} reenter(false) {}


Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win, Window& transientWin) Window::ScopedGraphicsContext::ScopedGraphicsContext(Window& win, Window& transientWin)
: window(win), : window(win),
ppData(transientWin.pData), ppData(transientWin.pData),
active(false), active(false),
reenter(true)
reenter(window.pData->view != nullptr)
{ {
puglBackendLeave(ppData->view);
active = puglBackendEnter(window.pData->view);
if (reenter)
{
puglBackendLeave(ppData->view);
active = puglBackendEnter(window.pData->view);
}
} }


Window::ScopedGraphicsContext::~ScopedGraphicsContext() Window::ScopedGraphicsContext::~ScopedGraphicsContext()
@@ -162,7 +165,8 @@ void Window::close()


bool Window::isResizable() const noexcept bool Window::isResizable() const noexcept
{ {
return puglGetViewHint(pData->view, PUGL_RESIZABLE) == PUGL_TRUE;
return pData->view != nullptr
&& puglGetViewHint(pData->view, PUGL_RESIZABLE) == PUGL_TRUE;
} }


void Window::setResizable(const bool resizable) void Window::setResizable(const bool resizable)
@@ -204,7 +208,8 @@ void Window::setOffsetY(const int y)


void Window::setOffset(const int x, const int y) void Window::setOffset(const int x, const int y)
{ {
puglSetPosition(pData->view, x, y);
if (pData->view != nullptr)
puglSetPosition(pData->view, x, y);
} }


void Window::setOffset(const Point<int>& offset) void Window::setOffset(const Point<int>& offset)
@@ -302,7 +307,7 @@ void Window::setSize(uint width, uint height)


topLevelWidget->requestSizeChange(width, height); topLevelWidget->requestSizeChange(width, height);
} }
else
else if (pData->view != nullptr)
{ {
puglSetSizeAndDefault(pData->view, width, height); puglSetSizeAndDefault(pData->view, width, height);
} }
@@ -315,7 +320,7 @@ void Window::setSize(const Size<uint>& size)


const char* Window::getTitle() const noexcept const char* Window::getTitle() const noexcept
{ {
return puglGetWindowTitle(pData->view);
return pData->view != nullptr ? puglGetWindowTitle(pData->view) : "";
} }


void Window::setTitle(const char* const title) void Window::setTitle(const char* const title)
@@ -326,12 +331,14 @@ void Window::setTitle(const char* const title)


bool Window::isIgnoringKeyRepeat() const noexcept bool Window::isIgnoringKeyRepeat() const noexcept
{ {
return puglGetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT) == PUGL_TRUE;
return pData->view != nullptr
&& puglGetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT) == PUGL_TRUE;
} }


void Window::setIgnoringKeyRepeat(const bool ignore) noexcept void Window::setIgnoringKeyRepeat(const bool ignore) noexcept
{ {
puglSetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT, ignore);
if (pData->view != nullptr)
puglSetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT, ignore);
} }


const void* Window::getClipboard(size_t& dataSize) const void* Window::getClipboard(size_t& dataSize)
@@ -341,12 +348,14 @@ const void* Window::getClipboard(size_t& dataSize)


bool Window::setClipboard(const char* const mimeType, const void* const data, const size_t dataSize) bool Window::setClipboard(const char* const mimeType, const void* const data, const size_t dataSize)
{ {
return puglSetClipboard(pData->view, mimeType != nullptr ? mimeType : "text/plain", data, dataSize) == PUGL_SUCCESS;
return pData->view != nullptr
&& puglSetClipboard(pData->view, mimeType != nullptr ? mimeType : "text/plain", data, dataSize) == PUGL_SUCCESS;
} }


bool Window::setCursor(const MouseCursor cursor) bool Window::setCursor(const MouseCursor cursor)
{ {
return puglSetCursor(pData->view, static_cast<PuglCursor>(cursor)) == PUGL_SUCCESS;
return pData->view != nullptr
&& puglSetCursor(pData->view, static_cast<PuglCursor>(cursor)) == PUGL_SUCCESS;
} }


bool Window::addIdleCallback(IdleCallback* const callback, const uint timerFrequencyInMs) bool Window::addIdleCallback(IdleCallback* const callback, const uint timerFrequencyInMs)
@@ -377,7 +386,7 @@ const GraphicsContext& Window::getGraphicsContext() const noexcept


uintptr_t Window::getNativeWindowHandle() const noexcept uintptr_t Window::getNativeWindowHandle() const noexcept
{ {
return puglGetNativeView(pData->view);
return pData->view != nullptr ? puglGetNativeView(pData->view) : 0;
} }


double Window::getScaleFactor() const noexcept double Window::getScaleFactor() const noexcept
@@ -399,10 +408,8 @@ bool Window::openFileBrowser(const FileBrowserOptions& options)


void Window::repaint() noexcept void Window::repaint() noexcept
{ {
if (pData->view == nullptr)
return;

puglPostRedisplay(pData->view);
if (pData->view != nullptr)
puglPostRedisplay(pData->view);
} }


void Window::repaint(const Rectangle<uint>& rect) noexcept void Window::repaint(const Rectangle<uint>& rect) noexcept
@@ -482,13 +489,17 @@ void Window::setGeometryConstraints(uint minimumWidth,


void Window::setTransientParent(const uintptr_t transientParentWindowHandle) void Window::setTransientParent(const uintptr_t transientParentWindowHandle)
{ {
puglSetTransientParent(pData->view, transientParentWindowHandle);
if (pData->view != nullptr)
puglSetTransientParent(pData->view, transientParentWindowHandle);
} }


std::vector<ClipboardDataOffer> Window::getClipboardDataOfferTypes() std::vector<ClipboardDataOffer> Window::getClipboardDataOfferTypes()
{ {
std::vector<ClipboardDataOffer> offerTypes; std::vector<ClipboardDataOffer> offerTypes;


if (pData->view != nullptr)
return offerTypes;

if (const uint32_t numTypes = puglGetNumClipboardTypes(pData->view)) if (const uint32_t numTypes = puglGetNumClipboardTypes(pData->view))
{ {
offerTypes.reserve(numTypes); offerTypes.reserve(numTypes);
@@ -528,7 +539,8 @@ void Window::onFocus(bool, CrossingMode)


void Window::onReshape(uint, uint) void Window::onReshape(uint, uint)
{ {
puglFallbackOnResize(pData->view);
if (pData->view != nullptr)
puglFallbackOnResize(pData->view);
} }


void Window::onScaleFactorChanged(double) void Window::onScaleFactorChanged(double)


Loading…
Cancel
Save