Skip to content

Commit

Permalink
win/mac: consider min/maxSize
Browse files Browse the repository at this point in the history
  • Loading branch information
osch committed Aug 1, 2021
1 parent c198119 commit ca650e1
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 21 deletions.
173 changes: 153 additions & 20 deletions pugl-repo/src/mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ @implementation PuglWrapperView {
NSMutableAttributedString* markedText;
bool reshaped;
PuglEventKey* processingKeyEvent;

NSLayoutConstraint* minWidthConstraint;
NSLayoutConstraint* minHeightConstraint;
NSLayoutConstraint* maxWidthConstraint;
NSLayoutConstraint* maxHeightConstraint;
}

- (void)dispatchExpose:(NSRect)rect
Expand Down Expand Up @@ -1215,19 +1220,6 @@ PuglWorldFlags PUGL_UNUSED(flags))
return impl;
}

static NSLayoutConstraint*
puglConstraint(id item, NSLayoutAttribute attribute, float constant)
{
return
[NSLayoutConstraint constraintWithItem:item
attribute:attribute
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)constant];
}

PuglStatus
puglRealize(PuglView* view)
{
Expand Down Expand Up @@ -1287,12 +1279,51 @@ PuglWorldFlags PUGL_UNUSED(flags))
impl->wrapperView->markedText = [[NSMutableAttributedString alloc] init];
[impl->wrapperView setAutoresizesSubviews:YES];
[impl->wrapperView initWithFrame:framePt];
[impl->wrapperView addConstraint:puglConstraint(impl->wrapperView,
NSLayoutAttributeWidth,
view->minWidth)];
[impl->wrapperView addConstraint:puglConstraint(impl->wrapperView,
NSLayoutAttributeHeight,
view->minHeight)];
if (view->minWidth) {
impl->wrapperView->minWidthConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->minWidth / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->minWidthConstraint];
}
if (view->minHeight) {
impl->wrapperView->minHeightConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->minHeight / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->minHeightConstraint];
}
if (view->maxWidth > 0) {
impl->wrapperView->maxWidthConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->maxWidth / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->maxWidthConstraint];
}
if (view->maxHeight > 0) {
impl->wrapperView->maxHeightConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->maxHeight / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->maxHeightConstraint];
}

[impl->wrapperView setReshaped];

// Create draw view to be rendered to
Expand Down Expand Up @@ -1347,6 +1378,16 @@ PuglWorldFlags PUGL_UNUSED(flags))
[window
setContentMinSize:sizePoints(view, view->minWidth, view->minHeight)];
}
if (view->hints[PUGL_RESIZABLE] && (view->maxWidth || view->maxHeight)) {
NSSize scaled = sizePoints(view, view->maxWidth, view->maxHeight);
if (view->maxWidth < 0) {
scaled.width = CGFLOAT_MAX;
}
if (view->maxHeight < 0) {
scaled.height = CGFLOAT_MAX;
}
[view->impl->window setContentMaxSize:scaled];
}
impl->window = window;

PuglWindowDelegate* delegate =
Expand Down Expand Up @@ -1464,6 +1505,22 @@ PuglWorldFlags PUGL_UNUSED(flags))
[wrapperView->markedText release];
wrapperView->markedText = NULL;
}
if (wrapperView->minWidthConstraint) {
[wrapperView->minWidthConstraint release];
wrapperView->minWidthConstraint = NULL;
}
if (wrapperView->minHeightConstraint) {
[wrapperView->minHeightConstraint release];
wrapperView->minHeightConstraint = NULL;
}
if (wrapperView->maxWidthConstraint) {
[wrapperView->maxWidthConstraint release];
wrapperView->maxWidthConstraint = NULL;
}
if (wrapperView->maxHeightConstraint) {
[wrapperView->maxHeightConstraint release];
wrapperView->maxHeightConstraint = NULL;
}
[wrapperView removeFromSuperview];
wrapperView->puglview = NULL;
[wrapperView release];
Expand Down Expand Up @@ -1803,20 +1860,61 @@ PuglWorldFlags PUGL_UNUSED(flags))
PuglStatus
puglSetMinSize(PuglView* const view, const int width, const int height)
{
const NSScreen* const screen = [NSScreen mainScreen];
const double scaleFactor = [screen backingScaleFactor];

view->minWidth = width;
view->minHeight = height;

if (view->impl->window && (view->minWidth || view->minHeight)) {
[view->impl->window
setContentMinSize:sizePoints(view, view->minWidth, view->minHeight)];
}

PuglInternals* impl = view->impl;
if (impl->wrapperView) {
if (impl->wrapperView->minHeightConstraint) {
[impl->wrapperView removeConstraint: impl->wrapperView->minHeightConstraint];
[impl->wrapperView->minHeightConstraint release];
impl->wrapperView->minHeightConstraint = NULL;
}

if (impl->wrapperView->minWidthConstraint) {
[impl->wrapperView removeConstraint: impl->wrapperView->minWidthConstraint];
[impl->wrapperView->minWidthConstraint release];
impl->wrapperView->minWidthConstraint = NULL;
}
if (width > 0) {
impl->wrapperView->minWidthConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->minWidth / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->minWidthConstraint];
}
if (height > 0) {
impl->wrapperView->minHeightConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->minHeight / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->minHeightConstraint];
}
}
return PUGL_SUCCESS;
}

PuglStatus
puglSetMaxSize(PuglView* const view, const int width, const int height)
{
const NSScreen* const screen = [NSScreen mainScreen];
const double scaleFactor = [screen backingScaleFactor];

view->maxWidth = width;
view->maxHeight = height;

Expand All @@ -1832,6 +1930,41 @@ PuglWorldFlags PUGL_UNUSED(flags))
[view->impl->window setContentMaxSize:scaled];
}

PuglInternals* impl = view->impl;
if (impl->wrapperView) {
if (impl->wrapperView->maxHeightConstraint) {
[impl->wrapperView removeConstraint: impl->wrapperView->maxHeightConstraint];
[impl->wrapperView->maxHeightConstraint release];
impl->wrapperView->maxHeightConstraint = NULL;
}
if (impl->wrapperView->maxWidthConstraint) {
[impl->wrapperView removeConstraint: impl->wrapperView->maxWidthConstraint];
[impl->wrapperView->maxWidthConstraint release];
impl->wrapperView->maxWidthConstraint = NULL;
}
if (width > 0) {
impl->wrapperView->maxWidthConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->maxWidth / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->maxWidthConstraint];
}
if (height > 0) {
impl->wrapperView->maxHeightConstraint =
[[NSLayoutConstraint constraintWithItem:impl->wrapperView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(CGFloat)view->maxHeight / scaleFactor] retain];
[impl->wrapperView addConstraint:impl->wrapperView->maxHeightConstraint];
}
}
return PUGL_SUCCESS;
}

Expand Down
13 changes: 13 additions & 0 deletions pugl-repo/src/win.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,13 @@ puglSetMinSize(PuglView* const view, const int width, const int height)
{
view->minWidth = width;
view->minHeight = height;

PuglRect r = puglGetFrame(view);
if (r.width < width || r.height < height) {
int w = (r.width > width) ? r.width : width;
int h = (r.height > height) ? r.height : height;
puglSetSize(view, w, h);
}
return PUGL_SUCCESS;
}

Expand All @@ -1458,6 +1465,12 @@ puglSetMaxSize(PuglView* const view, const int width, const int height)
{
view->maxWidth = width;
view->maxHeight = height;
PuglRect r = puglGetFrame(view);
if ((width > 0 && r.width > width) || (height > 0 && r.height > height)) {
int w = (width <= 0 || r.width < width) ? r.width : width;
int h = (height <= 0 || r.height < height) ? r.height : height;
puglSetSize(view, w, h);
}
return PUGL_SUCCESS;
}

Expand Down
4 changes: 3 additions & 1 deletion thirdparty/nanovg-0.1.2-2.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ build = {
sources = {
"lua-nanovg.c",
"nanovg/src/nanovg.c"
}
},
incdirs = { "include", "$(GL_INCDIR)" },
libdirs = { "$(GL_LIBDIR)" },
}
},
platforms = {
Expand Down

0 comments on commit ca650e1

Please sign in to comment.