|
1 | 1 | // Copyright (c) 2012 Intel Corp |
2 | 2 | // Copyright (c) 2012 The Chromium Authors |
3 | | -// |
4 | | -// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | 5 | // of this software and associated documentation files (the "Software"), to deal |
6 | 6 | // in the Software without restriction, including without limitation the rights |
7 | 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co |
8 | 8 | // pies of the Software, and to permit persons to whom the Software is furnished |
9 | 9 | // to do so, subject to the following conditions: |
10 | | -// |
| 10 | +// |
11 | 11 | // The above copyright notice and this permission notice shall be included in al |
12 | 12 | // l copies or substantial portions of the Software. |
13 | | -// |
| 13 | +// |
14 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM |
15 | 15 | // PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES |
16 | 16 | // S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
|
43 | 43 | std::string label; |
44 | 44 | option.GetString("label", &label); |
45 | 45 |
|
46 | | - menu_item_ = [[NSMenuItem alloc] |
47 | | - initWithTitle:[NSString stringWithUTF8String:label.c_str()] |
48 | | - action: @selector(invoke:) |
49 | | - keyEquivalent: @""]; |
50 | | - |
51 | | - delegate_ = [[MenuItemDelegate alloc] initWithMenuItem:this]; |
52 | | - [menu_item_ setTarget:delegate_]; |
| 46 | + std::string selector; |
| 47 | + option.GetString("selector", &selector); |
| 48 | + |
| 49 | + if(!selector.empty()) { |
| 50 | + menu_item_ = [[NSMenuItem alloc] |
| 51 | + initWithTitle:[NSString stringWithUTF8String:label.c_str()] |
| 52 | + action: NSSelectorFromString([NSString stringWithUTF8String:selector.c_str()]) |
| 53 | + keyEquivalent: @""]; |
| 54 | + delegate_ = [MenuItemDelegate alloc]; |
| 55 | + } else { |
| 56 | + menu_item_ = [[NSMenuItem alloc] |
| 57 | + initWithTitle:[NSString stringWithUTF8String:label.c_str()] |
| 58 | + action: @selector(invoke:) |
| 59 | + keyEquivalent: @""]; |
| 60 | + delegate_ = [[MenuItemDelegate alloc] initWithMenuItem:this]; |
| 61 | + [menu_item_ setTarget:delegate_]; |
| 62 | + } |
53 | 63 |
|
54 | 64 | if (type == "checkbox") { |
55 | 65 | bool checked = false; |
|
69 | 79 | if (option.GetString("tooltip", &tooltip)) |
70 | 80 | SetTooltip(tooltip); |
71 | 81 |
|
| 82 | + std::string key; |
| 83 | + if (option.GetString("key", &key)) |
| 84 | + SetKey(key); |
| 85 | + |
| 86 | + std::string modifiers; |
| 87 | + if (option.GetString("modifiers", &modifiers)) { |
| 88 | + NSUInteger mask = 0; |
| 89 | + NSString* nsmodifiers = [NSString stringWithUTF8String:modifiers.c_str()]; |
| 90 | + if([nsmodifiers rangeOfString:@"shift"].location != NSNotFound) |
| 91 | + mask = mask|NSShiftKeyMask; |
| 92 | + if([nsmodifiers rangeOfString:@"cmd"].location != NSNotFound) |
| 93 | + mask = mask|NSCommandKeyMask; |
| 94 | + if([nsmodifiers rangeOfString:@"alt"].location != NSNotFound) |
| 95 | + mask = mask|NSAlternateKeyMask; |
| 96 | + if([nsmodifiers rangeOfString:@"ctrl"].location != NSNotFound) |
| 97 | + mask = mask|NSControlKeyMask; |
| 98 | + [menu_item_ setKeyEquivalentModifierMask:mask]; |
| 99 | + } |
| 100 | + |
72 | 101 | int menu_id; |
73 | 102 | if (option.GetInteger("submenu", &menu_id)) |
74 | 103 | SetSubmenu(dispatcher_host()->GetApiObject<Menu>(menu_id)); |
|
94 | 123 | [menu_item_ setTitle:[NSString stringWithUTF8String:label.c_str()]]; |
95 | 124 | } |
96 | 125 |
|
| 126 | +void MenuItem::SetKey(const std::string& key) { |
| 127 | + [menu_item_ setKeyEquivalent:[NSString stringWithUTF8String:key.c_str()]]; |
| 128 | +} |
| 129 | + |
97 | 130 | void MenuItem::SetIcon(const std::string& icon) { |
98 | 131 | if (!icon.empty()) { |
99 | 132 | NSImage* image = [[NSImage alloc] |
|
0 commit comments