Show NSMenu only on NSStatusBarButton right click?

前端 未结 4 1112
一向
一向 2020-12-18 01:23

I have the following code: (can be copy-pasted to New macOS project)

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSAppl         


        
4条回答
  •  余生分开走
    2020-12-18 01:43

    I'm using this code on macOS Catalina. 10.15.2. ( Xcode 11.3).
    On left click It trigger action.
    On right click it show menu.

    //HEADER FILE
    #import 
    #import 
    NS_ASSUME_NONNULL_BEGIN
    
    @protocol MOSMainStatusBarDelegate
    
    - (void) menuBarControllerStatusChanged: (BOOL) active;
    
    @end
    
    
    @interface MOSMainStatusBar : NSObject
    
    @property (strong) NSMenu *menu;
    @property (strong, nonatomic) NSImage *image;
    @property (unsafe_unretained, nonatomic) id delegate;
    
    - (instancetype) initWithImage: (NSImage *) image menu: (NSMenu *) menu;
    - (NSStatusBarButton *) statusItemView;
    - (void) showStatusItem;
    - (void) hideStatusItem;
    
    
    @end
    
    //IMPLEMANTION FILE. 
    
    #import "MOSMainStatusBar.h"
    
    @interface MOSMainStatusBar ()
    
    @property (strong, nonatomic) NSStatusItem *statusItem;
    
    @end
    
    @implementation MOSMainStatusBar
    
    - (instancetype) initWithImage: (NSImage *) image menu: (NSMenu *) menu {
        self = [super init];
        if (self) {
            self.image = image;
            self.menu = menu;
        }
        return self;
    }
    
    - (void) setImage: (NSImage *) image {
        _image = image;
        self.statusItem.button.image = image;
    
    }
    
    - (NSStatusBarButton *) statusItemView {
        return self.statusItem.button;
    }
    
    - (void) showStatusItem {
        if (!self.statusItem) {
            self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
    
            [self initStatusItem10];
    
        }
    }
    
    - (void) hideStatusItem {
        if (self.statusItem) {
            [[NSStatusBar systemStatusBar] removeStatusItem:self.statusItem];
            self.statusItem = nil;
        }
    }
    
    
    
    - (void) initStatusItem10 {
    
        self.statusItem.button.image = self.image;
    
    
        self.statusItem.button.imageScaling =  NSImageScaleAxesIndependently;
    
        self.statusItem.button.appearsDisabled = NO;
        self.statusItem.button.target = self;
        self.statusItem.button.action = @selector(leftClick10:);
    
        __unsafe_unretained MOSMainStatusBar *weakSelf = self;
    
        [NSEvent addLocalMonitorForEventsMatchingMask:
         (NSEventMaskRightMouseDown | NSEventModifierFlagOption | NSEventMaskLeftMouseDown) handler:^(NSEvent *incomingEvent) {
    
             if (incomingEvent.type == NSEventTypeLeftMouseDown) {
                 weakSelf.statusItem.menu = nil;
             }
    
             if (incomingEvent.type == NSEventTypeRightMouseDown || [incomingEvent modifierFlags] & NSEventModifierFlagOption) {
    
                 weakSelf.statusItem.menu = weakSelf.menu;
             }
    
             return incomingEvent;
         }];
    }
    
    - (void)leftClick10:(id)sender {
    
        [self.delegate menuBarControllerStatusChanged:YES];
    }
    

提交回复
热议问题