2012-10-03 9 views
5

Apple's sample codeから読み、the docsNSPathControlが、例えば、と同じように動作するように設定する方法はありません。 Xcodeのエディタウィンドウで 'ジャンプバー':パスの各コンポーネントのポップアップを含むNSPathControl?

Xcode Editor custom path control

すなわち、パス(または他の種類の階層)を表し、パスの各コンポーネントをクリック可能なポップアップにして階層をナビゲートします。

クリックを聞いて一時的なウィンドウにメニューを表示すると、このような振る舞いを偽っている人は誰ですか?コンテキストメニューをポップアップするために:私はmouseDownイベントを使用できるように、私はNSPathControlのサブクラスを作っ

答えて

3

..しかし、まだそれをグーグルでは、このような運 -

は1つがさらにいくつかのOSSの実装を期待する一般的なデザインのように思えます正しい位置にあるコンポーネントセルの数。私は、必要に応じてより深いメニューを作成するために、メニューに代理人を追加しました。

- (void)mouseDown:(NSEvent *)event { 

    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; 


    NSPathCell *cell = self.cell; 
    NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point 
                   withFrame:self.bounds 
                   inView:self]; 

    NSRect componentRect = [cell rectOfPathComponentCell:componentCell 
               withFrame:self.bounds 
                inView:self]; 

    NSMenu *menu = [componentCell menuForEvent:event 
             inRect:componentRect 
             ofView:self]; 

    if (menu.numberOfItems > 0) { 
     NSUInteger selectedMenuItemIndex = 0; 
     for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) { 
      if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) { 
       selectedMenuItemIndex = menuItemIndex; 
       break; 
      } 
     } 

     NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex]; 
     [menu popUpMenuPositioningItem:selectedMenuItem 
          atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2) 
           inView:self]; 
    } 
} 

- (NSMenu *)menuForEvent:(NSEvent *)event { 
    if (event.type != NSLeftMouseDown) { 
     return nil; 
    } 
    return [super menuForEvent:event]; 
} 
1

私は、メニューアイテムの遅延読み込みに対応するためにStephanの答えを少し拡張しました。

#import "NSPathControlExtended.h" 

@implementation NSPathControlExtended 

@synthesize delegate; 

- (instancetype)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
    } 
    return self; 
} 

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect]; 

    // Drawing code here. 
} 

- (void)mouseDown:(NSEvent *)event { 

    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; 


    NSPathCell *cell = self.cell; 
    NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point 
                   withFrame:self.bounds 
                   inView:self]; 

    NSRect componentRect = [cell rectOfPathComponentCell:componentCell 
               withFrame:self.bounds 
                inView:self]; 

    NSMenu *menu = [delegate pathControl:self menuForCell:componentCell]; 

    if (menu.numberOfItems > 0) { 
     NSUInteger selectedMenuItemIndex = 0; 
     for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) { 
      if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) { 
       selectedMenuItemIndex = menuItemIndex; 
       break; 
      } 
     } 

     NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex]; 
     [menu popUpMenuPositioningItem:selectedMenuItem 
          atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2) 
           inView:self]; 
    } 
} 

- (NSMenu *)menuForEvent:(NSEvent *)event { 
    if (event.type != NSLeftMouseDown) { 
     return nil; 
    } 
    return [super menuForEvent:event]; 
} 

@end 
NSPathControlExtended.m

NSPathControlExtended.h

@protocol NSPathControlExtendedDelegate <NSPathControlDelegate> 

@required 
- (NSMenu *)pathControl:(NSPathControl *)pathControl menuForCell:(NSPathComponentCell *)cell; 

@end 

@interface NSPathControlExtended : NSPathControl 

@property (weak) id <NSPathControlExtendedDelegate> delegate; 

@end 

:私はむしろ、各セルのための時間のメニューの控えを構築するより、メニューのために呼び出すために小さなプロトコルを作成しました

関連する問題