summaryrefslogtreecommitdiff
path: root/ext/mac-ui-macgap1-wrapper/src/MacGap/Classes/Commands/MenuProxy.m
blob: 5bc10a763b1fd296c053b384a21130f181b531b5 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
//  MenuProxy.m
//  MacGap
//
//  Created by Joe Hildebrand on 1/14/12.
//  Copyright (c) 2012 Twitter. All rights reserved.
//

#import <objc/runtime.h>
#import <JavaScriptCore/JavaScript.h>

#import "MenuProxy.h"
#import "MenuItemProxy.h"

static char REPRESENTED_OBJECT;

@interface NSMenu (represented)
@property (strong) id representedObject;
@end

@implementation NSMenu (represented)

- (id) representedObject
{
    return objc_getAssociatedObject(self, &REPRESENTED_OBJECT);
}

- (void) setRepresentedObject:(id)representedObject
{
    objc_setAssociatedObject(self, 
                             &REPRESENTED_OBJECT,
                             representedObject, 
                             OBJC_ASSOCIATION_RETAIN);
}

@end

@implementation MenuProxy

- (id) initWithContext:(JSContextRef)aContext andMenu:(NSMenu*)aMenu
{
    self = [super initWithContext:aContext];
    if (!self)
        return nil;
    menu = aMenu;
    menu.representedObject = self;
    return self;
}

+ (MenuProxy*)proxyWithContext:(JSContextRef)aContext andMenu:(NSMenu*)aMenu
{
    // singleton-ish.
    MenuProxy *ret = [aMenu representedObject];
    if (ret)
    {
        NSLog(@"MP cache hit");
        return ret;
    }
    return [[MenuProxy alloc] initWithContext:aContext andMenu:aMenu];
}

- (void) dealloc
{
    menu.representedObject = nil;
}

- (NSString*) description
{
    return [menu description];
}

static BOOL isNullish(id o)
{
    if (!o)
        return YES;
    if ([o isKindOfClass:[WebUndefined class]])
        return YES;
    return NO;
}

- (MenuItemProxy*)addItemWithTitle:(NSString*)title
                     keyEquivalent:(NSString*)keyCommand
                          callback:(WebScriptObject*)aCallback
                           atIndex:(NSInteger)index
{
    if (isNullish(title))
        title = @"";
    
    NSString *aKey = [MenuProxy getKeyFromString:keyCommand];
    NSMenuItem *item = nil;
    
    if(index) {
        item = [menu insertItemWithTitle:title action:nil keyEquivalent:aKey atIndex:index ];
    } else {
        item = [menu addItemWithTitle:title action:nil keyEquivalent:aKey ];
        
    }
    
    // Set the modifiers.
    NSUInteger modifiers = [MenuProxy getModifiersFromString:keyCommand];
    [item setKeyEquivalentModifierMask:modifiers];
   
    if(!menu.supermenu) {
        NSMenu *s = [[NSMenu alloc] initWithTitle:title];
        [item setSubmenu:s];
    }
   
    MenuItemProxy *mip = [MenuItemProxy proxyWithContext:context andMenuItem:item];
    if (!isNullish(aCallback))
        [mip setCallback:aCallback];
    
   
    return mip;
}

+ (NSString*)getKeyFromString:(NSString*)keyCommand {
    if (isNullish(keyCommand))
        keyCommand = @"";

    // Obtain the key (if there are modifiers, it will be the last character).
    NSString *aKey = @"";
    if ([keyCommand length] > 0) {
        aKey = [keyCommand substringFromIndex:[keyCommand length] - 1];
    }

    return aKey;
}

+ (NSUInteger*)getModifiersFromString:(NSString*)keyCommand {
    // aKeys may optionally specify one or more modifiers.
    NSUInteger modifiers = 0;
    
    if ([keyCommand rangeOfString:@"caps"].location != NSNotFound) modifiers += NSAlphaShiftKeyMask;
    if ([keyCommand rangeOfString:@"shift"].location != NSNotFound) modifiers += NSShiftKeyMask;
    if ([keyCommand rangeOfString:@"cmd"].location != NSNotFound) modifiers += NSCommandKeyMask;
    if ([keyCommand rangeOfString:@"ctrl"].location != NSNotFound) modifiers += NSControlKeyMask;
    if ([keyCommand rangeOfString:@"opt"].location != NSNotFound) modifiers += NSAlternateKeyMask;
    if ([keyCommand rangeOfString:@"alt"].location != NSNotFound) modifiers += NSAlternateKeyMask;

    return modifiers;
}

- (MenuItemProxy*)addSeparator
{
    NSMenuItem *sep = [NSMenuItem separatorItem];
    [menu addItem:sep];
    return [MenuItemProxy proxyWithContext:context andMenuItem:sep];
}

- (MenuItemProxy*)itemForKey:(id)key
{
    if (isNullish(key))
        return nil;
    NSMenuItem *item = nil;
    if ([key isKindOfClass:[NSNumber class]])
    {
        item = [menu itemAtIndex:[key intValue]];
    }
    else if ([key isKindOfClass:[NSString class]])
    {
        item = [menu itemWithTitle:key];
        if (!item)
        {
            // Try again, with ... appended. e.g. "Save..."
            item = [menu itemWithTitle:
                    [key stringByAppendingString:@"\u2026"]];
        }
    }
    if (!item)
        return nil;

    return [MenuItemProxy proxyWithContext:context andMenuItem:item];    
}

- (MenuProxy*)removeItem:(id)key
{
    if (isNullish(key))
        return nil;
    
    NSMenuItem *item = nil;
    if ([key isKindOfClass:[NSNumber class]])
    {
        item = [menu itemAtIndex:[key intValue]];
    }
    else if ([key isKindOfClass:[NSString class]])
    {
        item = [menu itemWithTitle:key];
        if (!item)
        {
            // Try again, with ... appended. e.g. "Save..."
            item = [menu itemWithTitle:
                    [key stringByAppendingString:@"\u2026"]];
        }
    }
    if (!item)
        return nil;
    
    [menu removeItem:item];
    return [MenuProxy proxyWithContext:context andMenu:menu];
}

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
    return [self webScriptNameForSelector:selector] == nil;
}

+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
	return YES;
}

+ (NSString*) webScriptNameForSelector:(SEL)selector
{
	id	result = nil;
   
    if (selector == @selector(addItemWithTitle:keyEquivalent:callback:atIndex:)) {
		result = @"addItem";
	}
    else if (selector == @selector(addSeparator)) {
        result = @"addSeparator";
    }
	else if (selector == @selector(itemForKey:)) {
		result = @"getItem";
	}
    else if (selector == @selector(removeItem:)) {
		result = @"removeMenu";
	}
   
	return result;
}


@end