diff options
author | David Robillard <d@drobilla.net> | 2022-05-20 19:44:23 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-05-21 17:05:51 -0400 |
commit | 82405e8c177de446d03c340b4b6876370d79883a (patch) | |
tree | 8b58d94ce03810d95558d725f9a3394d2e7d0fc7 /src/mac.m | |
parent | a136586a339032126540ceb24f7c3f15eaba68c8 (diff) | |
download | pugl-82405e8c177de446d03c340b4b6876370d79883a.tar.gz pugl-82405e8c177de446d03c340b4b6876370d79883a.tar.bz2 pugl-82405e8c177de446d03c340b4b6876370d79883a.zip |
Add puglSetPosition() and puglSetSize()
These are redundant with puglSetFrame in a sense, but allow setting the size of
a view without the position, or vice-versa. This API also maps more nicely to
Wayland, where applications can not position themselves (but can resize).
Diffstat (limited to 'src/mac.m')
-rw-r--r-- | src/mac.m | 82 |
1 files changed, 71 insertions, 11 deletions
@@ -1368,28 +1368,88 @@ puglSetWindowTitle(PuglView* view, const char* title) PuglStatus puglSetFrame(PuglView* view, const PuglRect frame) { - PuglInternals* const impl = view->impl; + PuglInternals* const impl = view->impl; + const NSRect framePx = rectToNsRect(frame); + const NSRect framePt = nsRectToPoints(view, framePx); - // Update view frame to exactly the requested frame in Pugl coordinates + // Update view frame to exactly the requested frame view->frame = frame; - const NSRect framePx = rectToNsRect(frame); - const NSRect framePt = nsRectToPoints(view, framePx); if (impl->window) { - // Resize window to fit new content rect const NSRect screenPt = rectToScreen(viewScreen(view), framePt); - const NSRect winFrame = [impl->window frameRectForContentRect:screenPt]; + // Move and resize window to fit new content rect + const NSRect winFrame = [impl->window frameRectForContentRect:screenPt]; [impl->window setFrame:winFrame display:NO]; + + // Resize views + const NSRect sizePx = NSMakeRect(0, 0, frame.width, frame.height); + const NSRect sizePt = [impl->drawView convertRectFromBacking:sizePx]; + [impl->wrapperView setFrame:sizePt]; + [impl->drawView setFrame:sizePt]; + } else { + // Resize view + const NSRect sizePx = NSMakeRect(0, 0, frame.width, frame.height); + const NSRect sizePt = [impl->drawView convertRectFromBacking:sizePx]; + + [impl->wrapperView setFrame:framePt]; + [impl->drawView setFrame:sizePt]; + } + + return PUGL_SUCCESS; +} + +PuglStatus +puglSetPosition(PuglView* const view, const int x, const int y) +{ + if (x > INT16_MAX || y > INT16_MAX) { + return PUGL_BAD_PARAMETER; + } + + const PuglRect frame = { + (PuglCoord)x, (PuglCoord)y, view->frame.height, view->frame.height}; + + PuglInternals* const impl = view->impl; + if (impl->window) { + return puglSetFrame(view, frame); } - // Resize views - const NSRect sizePx = NSMakeRect(0, 0, frame.width, frame.height); - const NSRect sizePt = [impl->drawView convertRectFromBacking:sizePx]; + const NSRect framePx = rectToNsRect(frame); + const NSRect framePt = nsRectToPoints(view, framePx); + [impl->wrapperView setFrameOrigin:framePt.origin]; + + const NSRect drawPx = NSMakeRect(0, 0, frame.width, frame.height); + const NSRect drawPt = [impl->drawView convertRectFromBacking:drawPx]; + [impl->drawView setFrameOrigin:drawPt.origin]; - [impl->wrapperView setFrame:(impl->window ? sizePt : framePt)]; - [impl->drawView setFrame:sizePt]; + view->frame = frame; + return PUGL_SUCCESS; +} + +PuglStatus +puglSetSize(PuglView* const view, const unsigned width, const unsigned height) +{ + if (width > INT16_MAX || height > INT16_MAX) { + return PUGL_BAD_PARAMETER; + } + const PuglRect frame = { + view->frame.x, view->frame.y, (PuglSpan)width, (PuglSpan)height}; + + PuglInternals* const impl = view->impl; + if (impl->window) { + return puglSetFrame(view, frame); + } + + const NSRect framePx = rectToNsRect(frame); + const NSRect framePt = nsRectToPoints(view, framePx); + [impl->wrapperView setFrameSize:framePt.size]; + + const NSRect drawPx = NSMakeRect(0, 0, frame.width, frame.height); + const NSRect drawPt = [impl->drawView convertRectFromBacking:drawPx]; + [impl->drawView setFrameSize:drawPt.size]; + + view->frame = frame; return PUGL_SUCCESS; } |