How to determine if NSDrawer will open the screen

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

Thanks in advance.

Nick

EDIT:

Here is a 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


source to share


1 answer


You can use the NSScreen methods to calculate whether the extended drawer frame will open off-screen and then use -setFrame: display: animimate: move the window the required distance from the edge of the screen to the opening of the drawer.



+4


source







All Articles