views:

208

answers:

1

I am working on an app whose main window has an NSDrawer. It is crucial that the drawer will always open on the right edge, and that is how I have coded it to work. What I would like to know is if there is a way to detect if the drawer will open "off the screen"... Is there a way that I can detect this? If so, how? And additionally how can I move the main window to adjust for the width of drawer that will open?

Thanks in advance.

Nick

EDIT:

Here is the solution thanks to Rob's suggestion.

-(IBAction)toggleDrawer:(id)sender
{

NSRect screenFrame = [[[NSScreen screens] objectAtIndex:0] visibleFrame];
NSRect windowFrame = [window frame];
NSRect drawerFrame = [[[drawer contentView] window] frame];

if ([drawer state] == NSDrawerOpenState)
{
 [drawer close];
}
else
{
 if (windowFrame.size.width + 
     windowFrame.origin.x +
     drawerFrame.size.width > screenFrame.size.width)
 {
  NSLog(@"Will Open Off Screen");

  float offset = (windowFrame.size.width + 
           windowFrame.origin.x + 
           drawerFrame.size.width) - screenFrame.size.width;

  NSRect newRect = NSMakeRect(windowFrame.origin.x - offset, 
                                            windowFrame.origin.y,
                                            windowFrame.size.width,
                                            windowFrame.size.height);

  [window setFrame:newRect display:YES animate:YES];
 }

 [drawer openOnEdge:NSMaxXEdge];  
}
}
+2  A: 

You could use the methods of NSScreen to calculate whether the frame of the expanded drawer would open off-screen, then use the -setFrame:display:animate: to move the window the requisite distance away from the screen edge before opening the drawer.

Rob Keniger
Perfect. Thank you. For anyone else interested I've added the code I used to the question.
nick