Instantly share code, notes, and snippets.
Last active
May 31, 2020 23:55
-
Star
(3)
3
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save mmackh/38ea565914292b9bc141601b22fe7fc3 to your computer and use it in GitHub Desktop.
NSMenu in Catalyst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// IPDFContextMenu.h | |
// InstaPDF | |
// | |
// Created by Maximilian Mackh on 02/02/15. | |
// Copyright (c) 2015 mackh ag. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
NS_ASSUME_NONNULL_BEGIN | |
@interface IPDFContextMenu : NSObject | |
+ (instancetype)menuWithActions:(NSArray *)actions; | |
- (void)show; | |
@end | |
@interface IPDFContextMenuAction : NSObject | |
+ (instancetype)actionWithSeparator; | |
+ (instancetype)actionWithTitle:(NSString *)title handler:(void(^)(void))handler; | |
+ (instancetype)actionWithTitle:(NSString *)title keyEquivilant:(NSString *)keyEquivilant modifierMask:(NSUInteger)modifierMask handler:(void(^)(void))handler; | |
@property (nonatomic, assign) BOOL disableItem; | |
+ (NSString *)backspaceCharacter; | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// IPDFContextMenu.m | |
// InstaPDF | |
// | |
// Created by Maximilian Mackh on 02/02/15. | |
// Copyright (c) 2015 mackh ag. All rights reserved. | |
// | |
#import "IPDFContextMenu.h" | |
@interface NSMenu_Catalyst : NSObject | |
@property (nonatomic) BOOL autoenablesItems; | |
- (void)addItem:(id)item; | |
+ (void)popUpContextMenu:(id)menu withEvent:(id)event forView:(id)view; | |
- (id)currentEvent; | |
@end | |
@interface NSMenuItem_Catalyst : NSObject | |
+ (instancetype)separatorItem; | |
- (instancetype)initWithTitle:(NSString *)title action:(nonnull SEL)action keyEquivalent:(NSString *)keyEquivilant; | |
- (void)setTarget:(id)target; | |
- (void)setKeyEquivalentModifierMask:(NSInteger)keyEquivalentModifierMask; | |
@property (nonatomic) BOOL enabled; | |
@end | |
typedef void(^IPDFContextMenuActionHandler)(void); | |
@interface IPDFContextMenuAction () | |
@property (nonatomic,strong) IPDFContextMenuActionHandler handler; | |
@property (nonatomic) NSMenuItem_Catalyst *attachedItem; | |
@end | |
@interface IPDFContextMenu () | |
@property (nonatomic) NSArray *actions; | |
@property (nonatomic) NSMenu_Catalyst *attachedMenu; | |
@end | |
@implementation IPDFContextMenu | |
+ (instancetype)menuWithActions:(NSArray *)actions | |
{ | |
IPDFContextMenu *cM = [IPDFContextMenu new]; | |
cM.actions = actions; | |
cM.attachedMenu = [[NSClassFromString(@"NSMenu") alloc] init]; | |
cM.attachedMenu.autoenablesItems = NO; | |
for (IPDFContextMenuAction *action in actions) | |
{ | |
[cM.attachedMenu addItem:action.attachedItem]; | |
} | |
return cM; | |
} | |
- (void)show | |
{ | |
id app = [NSClassFromString(@"NSApplication") performSelector:@selector(sharedApplication)]; | |
NSArray *windows = [app performSelector:@selector(windows)]; | |
if (!windows.count) return; | |
id window = windows.firstObject; | |
id currentEvent = [window performSelector:@selector(currentEvent)]; | |
[NSClassFromString(@"NSMenu") popUpContextMenu:self.attachedMenu withEvent:currentEvent forView:nil]; | |
} | |
@end | |
@implementation IPDFContextMenuAction | |
+ (instancetype)actionWithSeparator | |
{ | |
IPDFContextMenuAction *cMA = [IPDFContextMenuAction new]; | |
cMA.attachedItem = [NSClassFromString(@"NSMenuItem") separatorItem]; | |
return cMA; | |
} | |
+ (instancetype)actionWithTitle:(NSString *)title handler:(void(^)(void))handler | |
{ | |
return [self actionWithTitle:title keyEquivilant:@"" modifierMask:0 handler:handler]; | |
} | |
+ (instancetype)actionWithTitle:(NSString *)title keyEquivilant:(NSString *)keyEquivilant modifierMask:(NSUInteger)modifierMask handler:(void(^)(void))handler; | |
{ | |
IPDFContextMenuAction *cMA = [IPDFContextMenuAction new]; | |
cMA.attachedItem = [[NSClassFromString(@"NSMenuItem") alloc] initWithTitle:title action:@selector(action) keyEquivalent:keyEquivilant]; | |
[cMA.attachedItem setTarget:cMA]; | |
[cMA.attachedItem setKeyEquivalentModifierMask:modifierMask]; | |
cMA.handler = handler; | |
return cMA; | |
} | |
- (void)action | |
{ | |
if(self.handler) self.handler(); | |
} | |
- (void)setDisableItem:(BOOL)disableItem | |
{ | |
self.attachedItem.enabled = !disableItem; | |
} | |
+ (NSString *)backspaceCharacter | |
{ | |
unichar backspaceChar = 0x0008; | |
return [NSString stringWithCharacters:&backspaceChar length:1]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment