2011-07-25 17 views
4

私は、左クリックと右クリックを持つステータスバーアプリケーションに取り組んでいます。私は他のポストのヒントに従うことでこの作業を開始しましたが、右クリックでメニューを表示する方法についてはわかりません。NSStatusItem右クリックメニュー

私は私のNSStatusItemのカスタムビューとしてサブクラスのNSViewを使用して、右と異なる機能を実行し、左クリックを持つ

- (void)mouseDown:(NSEvent *)theEvent{ 
    [super mouseDown:theEvent]; 
    if ([theEvent modifierFlags] & NSCommandKeyMask){ 
     [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; 
    }else{ 
     [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO]; 
    } 
} 

- (void)rightMouseDown:(NSEvent *)theEvent{ 
    [super rightMouseDown:theEvent]; 
    [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; 
} 

私は、右クリックのメニュー、標準NSStatusItem同じように表示することができますどのように左クリックしますか?

答えて

19

NSStatusItem popUpStatusItemMenu:トリックをしました。私は右クリックアクションから呼び出して、表示したいメニューを渡して表示しています!これは私がこの機能が期待するものではありませんが、機能しています。ここで

は私のコードがどのように見えるかの重要な部分です。ここで

- (void)showMenu{ 
    // check if we are showing the highlighted state of the custom status item view 
    if(self.statusItemView.clicked){ 
     // show the right click menu 
     [self.statusItem popUpStatusItemMenu:self.rightClickMenu]; 
    } 
} 

// menu delegate method to unhighlight the custom status bar item view 
- (void)menuDidClose:(NSMenu *)menu{ 
    [self.statusItemView setHighlightState:NO]; 
} 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{ 
    // setup custom view that implements mouseDown: and rightMouseDown: 
    self.statusItemView = [[ISStatusItemView alloc] init]; 
    self.statusItemView.image = [NSImage imageNamed:@"menu.png"]; 
    self.statusItemView.alternateImage = [NSImage imageNamed:@"menu_alt.png"];  
    self.statusItemView.target = self; 
    self.statusItemView.action = @selector(mainAction); 
    self.statusItemView.rightAction = @selector(showMenu); 

    // set menu delegate 
    [self.rightClickMenu setDelegate:self]; 

    // use the custom view in the status bar item 
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; 
    [self.statusItem setView:self.statusItemView]; 
} 

は、カスタムビューの実装です:

@implementation ISStatusItemView 

@synthesize image = _image; 
@synthesize alternateImage = _alternateImage; 
@synthesize clicked = _clicked; 
@synthesize action = _action; 
@synthesize rightAction = _rightAction; 
@synthesize target = _target; 

- (void)setHighlightState:(BOOL)state{ 
    if(self.clicked != state){ 
     self.clicked = state; 
     [self setNeedsDisplay:YES]; 
    } 
} 

- (void)drawImage:(NSImage *)aImage centeredInRect:(NSRect)aRect{ 
    NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aImage.size.width*0.5f), 
            (CGFloat)round(aRect.size.height*0.5f-aImage.size.height*0.5f), 
            aImage.size.width, 
            aImage.size.height); 
    [aImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; 
} 

- (void)drawRect:(NSRect)rect{ 
    if(self.clicked){ 
     [[NSColor selectedMenuItemColor] set]; 
     NSRectFill(rect);   
     if(self.alternateImage){ 
      [self drawImage:self.alternateImage centeredInRect:rect]; 
     }else if(self.image){ 
      [self drawImage:self.image centeredInRect:rect]; 
     } 
    }else if(self.image){ 
     [self drawImage:self.image centeredInRect:rect]; 
    } 
} 

- (void)mouseDown:(NSEvent *)theEvent{ 
    [super mouseDown:theEvent]; 
    [self setHighlightState:!self.clicked]; 
    if ([theEvent modifierFlags] & NSCommandKeyMask){ 
     [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; 
    }else{ 
     [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO]; 
    } 
} 

- (void)rightMouseDown:(NSEvent *)theEvent{ 
    [super rightMouseDown:theEvent]; 
    [self setHighlightState:!self.clicked]; 
    [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; 
} 

- (void)dealloc{ 
    self.target = nil; 
    self.action = nil; 
    self.rightAction = nil; 
    [super dealloc]; 
} 

@end 
+2

あなたの答えについての素晴らしいことは、それは私の全く関係のない質問を解決したことです:)ありがとう。 NSStatusItemにメニューが設定されている場合、アクションはターゲット上で呼び出されません。したがって、NSMenuを設定することはできませんでした。その後、popUpStatusItemMenuメソッドを使用してメニューを表示します:) – Mazyod

3

1つのオプションはダウンだけで偽のマウスの左にある:

- (void)rightMouseDown: (NSEvent *)event { 
    NSEvent * newEvent; 
    newEvent = [NSEvent mouseEventWithType:NSLeftMouseDown 
            location:[event locationInWindow] 
          modifierFlags:[event modifierFlags] 
           timestamp:CFAbsoluteTimeGetCurrent() 
           windowNumber:[event windowNumber] 
            context:[event context] 
           eventNumber:[event eventNumber] 
           clickCount:[event clickCount] 
            pressure:[event pressure]]; 
    [self mouseDown:newEvent]; 
} 
+0

非常にトリッキーですが、私の左クリックがacを実行するため、これは私のためにはうまくいかないと思いますメニューは表示されません。 – keegan3d

+0

はい、あなたのコードと説明でちょっと混乱していました。今どのようにメニューを表示していますか? –

+0

混乱して申し訳ありませんが、私はもっと掘り下げて実験をしています。あなたのスニペットをありがとう、私は有用な一日に来ると確信しています:) – keegan3d

0

は、ビュー

にあなたがタイトルを必要とするときのために少し何かを追加しました
- (void)drawRect:(NSRect)rect{ 
    if(self.clicked){ 
     [[NSColor selectedMenuItemColor] set]; 
     NSRectFill(rect); 
     if(self.alternateImage){ 
      [self drawImage:self.alternateImage centeredInRect:rect]; 
     }else if(self.image){ 
      [self drawImage:self.image centeredInRect:rect]; 
     } else { 
      [self drawTitleInRect:rect]; 
     } 
    } else if(self.image){ 
     [self drawImage:self.image centeredInRect:rect]; 
    } else { 
     [self drawTitleInRect:rect]; 
    } 

} 

-(void)drawTitleInRect:(CGRect)rect 
{ 
    CGSize size = [_title sizeWithAttributes:nil]; 

    CGRect newRect = CGRectMake(MAX((rect.size.width - size.width)/2.f,0.f), 
           MAX((rect.size.height - size.height)/2.f,0.f), 
           size.width, 
           size.height); 

    NSDictionary *attributes = @{NSForegroundColorAttributeName : self.clicked?[NSColor highlightColor]:[NSColor textColor] 
           }; 
    [_title drawInRect:newRect withAttributes:attributes]; 

} 
+0

別の質問に答えるつもりでしたか?この質問は、ステータス項目を右クリックしたときのポップアップメニューの表示についてです。 –

+0

たぶんそれはコメント(深夜coindg)であるべきです。それは受け入れられた答えに少し追加されています。いつか誰かを助けるかもしれないと思った。 –

関連する問題