2012-10-03 8 views
5

Od Apple's sample code i czytając the docs Nie widzę sposobu, aby skonfigurować NSPathControl, aby zachowywać się podobnie do np. The 'Pasek' w oknie edytora Xcode:NSPathControl z wyskakującymi okienkami dla każdego komponentu ścieżki?

Xcode Editor custom path control

Tj czy reprezentuje ścieżkę (lub inny rodzaj hierarchii) i czy każdy komponent ścieżki jest klikalnym wyskakującym okienkiem nawigacyjnym w hierarchii ...?

Ktoś mający szczęście udaje takie zachowanie, słuchając kliknięć i pokazując menu w oknie tymczasowym?

Wygląda wspólnego projektu, gdzie można by nawet spodziewać się realizacji OSS - ale nie ma takiego szczęścia jeszcze googlowania dla niego ..

Odpowiedz

3

zrobiłem podklasę NSPathControl tak, że można używać mouseDown: popup menu kontekstowe komórek składowych we właściwej pozycji. Dodałem również delegata do menu, aby stworzyć głębsze menu na żądanie.

- (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

Rozszerzyłem nieco odpowiedź Stephana, aby umożliwić leniwe ładowanie pozycji menu. Stworzyłem małą protokół zadzwonić do menu zamiast budować menu użytkownika z wyprzedzeniem dla każdej komórki:

NSPathControlExtended.h

@protocol NSPathControlExtendedDelegate <NSPathControlDelegate> 

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

@end 

@interface NSPathControlExtended : NSPathControl 

@property (weak) id <NSPathControlExtendedDelegate> delegate; 

@end 

NSPathControlExtended.m

#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 
Powiązane problemy