-
Notifications
You must be signed in to change notification settings - Fork 3
/
IPDFContextMenu.m
124 lines (90 loc) · 3.13 KB
/
IPDFContextMenu.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// IPDFContextMenu.m
// InstaPDF
//
// Created by Maximilian Mackh on 02/02/15.
// Copyright (c) 2015 mackh ag. All rights reserved.
//
#import "IPDFContextMenu.h"
#import "IPDFMacEventBus.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;
@property (nonatomic) BOOL allowsContextMenuPlugIns;
@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;
cM.attachedMenu.allowsContextMenuPlugIns = NO;
for (IPDFContextMenuAction *action in actions)
{
[cM.attachedMenu addItem:action.attachedItem];
}
return cM;
}
- (void)setAllowsContextMenuPlugIns:(BOOL)allowsContextMenuPlugIns
{
_allowsContextMenuPlugIns = allowsContextMenuPlugIns;
self.attachedMenu.allowsContextMenuPlugIns = allowsContextMenuPlugIns;
}
- (void)show
{
[NSClassFromString(@"NSMenu") popUpContextMenu:self.attachedMenu withEvent:[IPDFMacEventBus currentEvent].underlyingEvent 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