4
votes

Let me rephrase the question:

How can I keep a child NSWindow (added to a main NSWindow using addChildWindow) the same size as the main window?

Old question:

I have two NSWindows: an uiWindow and an openglWindow. The uiWindow is borderless and initially the same size as the openglWindow. I add it as a child window. I would like to make the uiWindow follow any size changes that the openglWindow undergoes. To do that, I've subscribed to the delegate of openglWindow and I'm listening to the windowWillResize method. However, I'm confused now. I don't know which function to call on uiWindow to resize it. There are quite a few options:

  • setFrame: display: `
  • contentRectForFrameRect

  • frameRectForContentRect

Below is the initialization code for the child window.

NSRect uiWindowRect = [self.openglWindow convertRectToScreen:((NSView*)self.openglWindow.contentView).bounds];
NSWindow* uiWindow = [[NSWindow alloc] initWithContentRect:uiWindowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained defer:NO];

[self.openglWindow addChildWindow:uiWindow ordered:NSWindowAbove];
1

1 Answers

3
votes

I found a way that does exactly what I was looking for. Here's the code for reference:

-(void)windowDidResize:(NSNotification *)notification{
    CGRect frame = CGRectMake(self.openglWindow.frame.origin.x,self.openglWindow.frame.origin.y, ((NSView*)self.openglWindow.contentView).frame.size.width, ((NSView*)self.openglWindow.contentView).frame.size.height);
    [self.window setFrame:frame display:YES];
    [self.window setFrameOrigin:NSMakePoint(self.openglWindow.frame.origin.x,self.openglWindow.frame.origin.y)];
    [self.window viewsNeedDisplay];
}