2009-09-01 11 views
2

メインウィンドウにNSDrawerがあるアプリケーションで作業しています。引き出しが常に右端に開かれることが重要です。それは、それがどのように動作するようにコード化したかです。私が知りたいのは、引き出しが「画面外」で開くかどうかを検出する方法があるかどうかです...これを検出できる方法はありますか?もしそうなら、どうですか?さらに、メインウィンドウをどのように動かすことで、開くことができる引き出しの幅に合わせることができますか?NSDrawerが画面から開くかどうかを検出する方法

ありがとうございます。

ニック

EDIT:

ここでは、ソリューションは、ロブの提案のおかげです。

-(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];  
} 
} 

答えて

4

あなたは、その後、-setFrame使用拡大引き出しのフレームがオフスクリーンを開くかどうかを計算するNSScreenの方法を使用できます。画面から必要な距離離れてウィンドウを移動する:ディスプレイ:アニメーションを引き出しを開く前に端を確認してください。

+1

パーフェクト。ありがとうございました。私が質問に使用したコードを追加しました。 – nrj

+0

すてきな解決策、皆さん! 'newRect'の' offset'を原点ではなく幅から減算することもできます。 –

関連する問題