aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/internal.c28
-rw-r--r--src/internal.h4
-rw-r--r--src/mac.m19
-rw-r--r--src/win.c24
-rw-r--r--src/x11.c18
5 files changed, 49 insertions, 44 deletions
diff --git a/src/internal.c b/src/internal.c
index dd1b7f1..bc5080f 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -95,6 +95,34 @@ puglDecodeUTF8(const uint8_t* buf)
return 0xFFFD;
}
+static inline bool
+isValidSize(const double width, const double height)
+{
+ return width > 0.0 && height > 0.0;
+}
+
+PuglStatus
+puglPreRealize(PuglView* const view)
+{
+ // Ensure that a backend with at least a configure method has been set
+ if (!view->backend || !view->backend->configure) {
+ return PUGL_BAD_BACKEND;
+ }
+
+ // Set the size to the default if it hasn't already been set
+ if (!isValidSize(view->frame.width, view->frame.height)) {
+ const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE];
+ if (!isValidSize(defaultSize.width, defaultSize.height)) {
+ return PUGL_BAD_CONFIGURATION;
+ }
+
+ view->frame.width = defaultSize.width;
+ view->frame.height = defaultSize.height;
+ }
+
+ return PUGL_SUCCESS;
+}
+
PuglStatus
puglDispatchSimpleEvent(PuglView* view, const PuglEventType type)
{
diff --git a/src/internal.h b/src/internal.h
index e3e7924..066dc7e 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -28,6 +28,10 @@ puglSetString(char** dest, const char* string);
uint32_t
puglDecodeUTF8(const uint8_t* buf);
+/// Prepare a view to be realized by the platform implementation if possible
+PuglStatus
+puglPreRealize(PuglView* view);
+
/// Dispatch an event with a simple `type` to `view`
PuglStatus
puglDispatchSimpleEvent(PuglView* view, PuglEventType type);
diff --git a/src/mac.m b/src/mac.m
index 152874a..0ba95ca 100644
--- a/src/mac.m
+++ b/src/mac.m
@@ -1007,13 +1007,16 @@ PuglStatus
puglRealize(PuglView* view)
{
PuglInternals* impl = view->impl;
+ PuglStatus st = PUGL_SUCCESS;
+ // Ensure that we're unrealized
if (impl->wrapperView) {
return PUGL_FAILURE;
}
- if (!view->backend || !view->backend->configure) {
- return PUGL_BAD_BACKEND;
+ // Check that the basic required configuration has been done
+ if ((st = puglPreRealize(view))) {
+ return st;
}
const NSScreen* const screen = [NSScreen mainScreen];
@@ -1053,17 +1056,6 @@ puglRealize(PuglView* view)
CVDisplayLinkRelease(link);
}
- // Set the size to the default if it has not already been set
- if (view->frame.width <= 0.0 || view->frame.height <= 0.0) {
- const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE];
- if (!defaultSize.width || !defaultSize.height) {
- return PUGL_BAD_CONFIGURATION;
- }
-
- view->frame.width = defaultSize.width;
- view->frame.height = defaultSize.height;
- }
-
// Center top-level windows if a position has not been set
if (!view->parent && !view->frame.x && !view->frame.y) {
const double screenWidthPx = [screen frame].size.width * scaleFactor;
@@ -1115,7 +1107,6 @@ puglRealize(PuglView* view)
}
// Create draw view to be rendered to
- PuglStatus st = PUGL_SUCCESS;
if ((st = view->backend->configure(view)) ||
(st = view->backend->create(view))) {
return st;
diff --git a/src/win.c b/src/win.c
index c83c91d..e41222b 100644
--- a/src/win.c
+++ b/src/win.c
@@ -213,10 +213,18 @@ PuglStatus
puglRealize(PuglView* view)
{
PuglInternals* impl = view->impl;
+ PuglStatus st = PUGL_SUCCESS;
+
+ // Ensure that we're unrealized
if (impl->hwnd) {
return PUGL_FAILURE;
}
+ // Check that the basic required configuration has been done
+ if ((st = puglPreRealize(view))) {
+ return st;
+ }
+
// Getting depth from the display mode seems tedious, just set usual values
if (view->hints[PUGL_RED_BITS] == PUGL_DONT_CARE) {
view->hints[PUGL_RED_BITS] = 8;
@@ -242,11 +250,6 @@ puglRealize(PuglView* view)
return PUGL_REGISTRATION_FAILED;
}
- if (!view->backend || !view->backend->configure) {
- return PUGL_BAD_BACKEND;
- }
-
- PuglStatus st = PUGL_SUCCESS;
if ((st = view->backend->configure(view)) ||
(st = view->backend->create(view))) {
return st;
@@ -1372,17 +1375,6 @@ puglWinCreateWindow(PuglView* const view,
const unsigned winFlags = puglWinGetWindowFlags(view);
const unsigned winExFlags = puglWinGetWindowExFlags(view);
- // Set the size to the default if it has not already been set
- if (view->frame.width <= 0.0 || view->frame.height <= 0.0) {
- const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE];
- if (!defaultSize.width || !defaultSize.height) {
- return PUGL_BAD_CONFIGURATION;
- }
-
- view->frame.width = defaultSize.width;
- view->frame.height = defaultSize.height;
- }
-
// Center top-level windows if a position has not been set
if (!view->parent && !view->frame.x && !view->frame.y) {
RECT desktopRect;
diff --git a/src/x11.c b/src/x11.c
index 2bdfc4a..b7b78f2 100644
--- a/src/x11.c
+++ b/src/x11.c
@@ -357,24 +357,14 @@ puglRealize(PuglView* const view)
XSetWindowAttributes attr = PUGL_INIT_STRUCT;
PuglStatus st = PUGL_SUCCESS;
- // Ensure that we're unrealized and that a reasonable backend has been set
+ // Ensure that we're unrealized
if (impl->win) {
return PUGL_FAILURE;
}
- if (!view->backend || !view->backend->configure) {
- return PUGL_BAD_BACKEND;
- }
-
- // Set the size to the default if it has not already been set
- if (view->frame.width <= 0.0 || view->frame.height <= 0.0) {
- const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE];
- if (!defaultSize.width || !defaultSize.height) {
- return PUGL_BAD_CONFIGURATION;
- }
-
- view->frame.width = defaultSize.width;
- view->frame.height = defaultSize.height;
+ // Check that the basic required configuration has been done
+ if ((st = puglPreRealize(view))) {
+ return st;
}
// Center top-level windows if a position has not been set