From 44e65f78fca1597471d31a96907d5be465ca1ec1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 22 Jan 2025 18:42:13 -0500 Subject: Factor out puglIsValidPosition() --- src/common.c | 3 +-- src/internal.c | 6 ++++++ src/internal.h | 4 ++++ src/mac.m | 4 ++-- src/win.c | 4 ++-- src/x11.c | 4 ++-- 6 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/common.c b/src/common.c index 8c2268b..2e6e0e6 100644 --- a/src/common.c +++ b/src/common.c @@ -12,7 +12,6 @@ #include #include -#include #include #include @@ -302,7 +301,7 @@ puglGetFrame(const PuglView* view) // Get the default position if set, or fallback to (0, 0) int x = view->defaultX; int y = view->defaultY; - if (x < INT16_MIN || x > INT16_MAX || y < INT16_MIN || y > INT16_MAX) { + if (!puglIsValidPosition(x, y)) { x = 0; y = 0; } diff --git a/src/internal.c b/src/internal.c index 1a7b61f..3799e97 100644 --- a/src/internal.c +++ b/src/internal.c @@ -12,6 +12,12 @@ #include #include +bool +puglIsValidPosition(const int x, const int y) +{ + return x >= INT16_MIN && x <= INT16_MAX && y >= INT16_MIN && y <= INT16_MAX; +} + bool puglIsValidArea(const PuglArea size) { diff --git a/src/internal.h b/src/internal.h index e4296c8..3a2663b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -18,6 +18,10 @@ PUGL_BEGIN_DECLS +/// Return true if `x`,`y` is a valid position +bool +puglIsValidPosition(int x, int y); + /// Return true if `size` is a valid view size bool puglIsValidArea(PuglArea size); diff --git a/src/mac.m b/src/mac.m index 368b137..180db83 100644 --- a/src/mac.m +++ b/src/mac.m @@ -1167,7 +1167,7 @@ getInitialFrame(PuglView* const view) const int x = view->defaultX; const int y = view->defaultY; - if (x >= INT16_MIN && x <= INT16_MAX && y >= INT16_MIN && y <= INT16_MAX) { + if (puglIsValidPosition(x, y)) { // Use the default position set with puglSetPosition while unrealized const PuglRect frame = {(PuglCoord)x, (PuglCoord)y, @@ -1773,7 +1773,7 @@ puglSetFrame(PuglView* view, const PuglRect frame) PuglStatus puglSetPosition(PuglView* const view, const int x, const int y) { - if (x < INT16_MIN || x > INT16_MAX || y < INT16_MIN || y > INT16_MAX) { + if (!puglIsValidPosition(x, y)) { return PUGL_BAD_PARAMETER; } diff --git a/src/win.c b/src/win.c index 0f5644e..ed18a39 100644 --- a/src/win.c +++ b/src/win.c @@ -1302,7 +1302,7 @@ puglSetFrame(PuglView* view, const PuglRect frame) PuglStatus puglSetPosition(PuglView* const view, const int x, const int y) { - if (x < INT16_MIN || x > INT16_MAX || y < INT16_MIN || y > INT16_MAX) { + if (!puglIsValidPosition(x, y)) { return PUGL_BAD_PARAMETER; } @@ -1592,7 +1592,7 @@ getInitialFrame(PuglView* const view) const PuglSpan defaultHeight = view->sizeHints[PUGL_DEFAULT_SIZE].height; const int x = view->defaultX; const int y = view->defaultY; - if (x >= INT16_MIN && x <= INT16_MAX && y >= INT16_MIN && y <= INT16_MAX) { + if (puglIsValidPosition(x, y)) { // Use the default position set with puglSetPosition while unrealized const PuglRect frame = { (PuglCoord)x, (PuglCoord)y, defaultWidth, defaultHeight}; diff --git a/src/x11.c b/src/x11.c index ba0f840..b7401f1 100644 --- a/src/x11.c +++ b/src/x11.c @@ -526,7 +526,7 @@ getInitialFrame(PuglView* const view) const PuglSpan defaultHeight = view->sizeHints[PUGL_DEFAULT_SIZE].height; const int x = view->defaultX; const int y = view->defaultY; - if (x >= INT16_MIN && x <= INT16_MAX && y >= INT16_MIN && y <= INT16_MAX) { + if (puglIsValidPosition(x, y)) { // Use the default position set with puglSetPosition while unrealized const PuglRect frame = { (PuglCoord)x, (PuglCoord)y, defaultWidth, defaultHeight}; @@ -1999,7 +1999,7 @@ puglSetPosition(PuglView* const view, const int x, const int y) { Display* const display = view->world->impl->display; - if (x < INT16_MIN || x > INT16_MAX || y < INT16_MIN || y > INT16_MAX) { + if (!puglIsValidPosition(x, y)) { return PUGL_BAD_PARAMETER; } -- cgit v1.2.1