I have an instance of NSWindow that I resize programatically (with setFrame). At its creation, it has a default size of 50x50. I then call setFrame to resize it to 350x450: everything is fine, I can drag my window all I want. Then, later in the program, I resize it to 1024x768. At this point, the only part of the title bar that I can drag is the first 350 pixels: the left part. Nothing happens if 开发者_开发百科I drag the right part. As if the title bar still thinks the window has its previous size.
Updated (Added the code that sets the frame):
void CGLWindowMac::_setSize(int width, int height)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSRect sFrame;
sFrame = [NSWindow contentRectForFrameRect:[mObjcImp frame] styleMask:[mObjcImp styleMask]];
sFrame.origin.y += sFrame.size.height;
sFrame.origin.y -= height;
sFrame.size.height = height;
sFrame.size.width = width;
sFrame = [NSWindow frameRectForContentRect:sFrame
styleMask:[mObjcImp styleMask]];
NSLog(@"Frame Before setsize: cur Size(%f, %f) ", [mObjcImp frame].size.width, [mObjcImp frame].size.height);
[mObjcImp setFrame:sFrame display:YES animate:NO];
NSLog(@"Frame After setsize: cur Size(%f, %f) new val(%d, %d)", [mObjcImp frame].size.width, [mObjcImp frame].size.height, width, height);
// Tell the application that the window size has change.
onSize(width, height);
[pool release];
}
And here is the output:
2011-05-04 08:50:47.313 ClientProMac[2461:a0f] Frame Before setsize: cur Size(50.000000, 72.000000)
2011-05-04 08:50:47.340 ClientProMac[2461:a0f] Frame After setsize: cur Size(350.000000, 472.000000) new val(350, 450)
2011-05-04 08:50:49.148 ClientProMac[2461:7003] Frame Before setsize: cur Size(350.000000, 472.000000)
2011-05-04 08:50:49.160 ClientProMac[2461:7003] Frame After setsize: cur Size(1024.000000, 790.000000) new val(1024, 768)
The weird thing is that if I then resize the window by dragging its corner, the full title bar becomes draggable again: the bug disappear.
Anyone saw this behavior before? What causes it and how can I fix it.
Thanks.
NSWindow (and most GUI operations in cocoa) are not thread safe. Make sure you're resizing the window on the main thread.
For example:
NSRect frame = NSMakeRect(42, 42, 420, 420);
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[window setFrame:frame];
[pool release];
});
If you need backwards compatibility and don't want to use GCD, you can use -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:].
精彩评论