OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/chrome/browser/ui/context_menu/context_menu_holder.h" | |
6 | |
7 #import "base/logging.h" | |
8 #import "base/mac/scoped_nsobject.h" | |
9 | |
10 @implementation ContextMenuHolder { | |
11 // Backs the property of the same name. | |
12 base::scoped_nsobject<NSString> _menuTitle; | |
13 // Stores the itemTitles of the menu items. | |
14 base::scoped_nsobject<NSMutableArray> _itemTitles; | |
15 // Keep a copy of all the actions blocks. | |
16 base::scoped_nsobject<NSMutableArray> _actions; | |
17 // A set of indices in |_actions| that should cause the context menu to be | |
18 // dismissed without animation when tapped. | |
19 base::scoped_nsobject<NSMutableIndexSet> _dismissImmediatelyActions; | |
20 } | |
21 | |
22 - (instancetype)init { | |
23 self = [super init]; | |
24 if (self) { | |
25 _itemTitles.reset([[NSMutableArray alloc] init]); | |
26 _actions.reset([[NSMutableArray alloc] init]); | |
27 _dismissImmediatelyActions.reset([[NSMutableIndexSet alloc] init]); | |
28 } | |
29 return self; | |
30 } | |
31 | |
32 - (NSString*)menuTitle { | |
33 return _menuTitle; | |
34 } | |
35 | |
36 - (void)setMenuTitle:(NSString*)newTitle { | |
37 _menuTitle.reset([newTitle copy]); | |
38 } | |
39 | |
40 - (NSArray*)itemTitles { | |
41 return [[_itemTitles copy] autorelease]; | |
42 } | |
43 | |
44 - (NSUInteger)itemCount { | |
45 return [_itemTitles count]; | |
46 } | |
47 | |
48 - (void)appendItemWithTitle:(NSString*)title action:(ProceduralBlock)action { | |
49 [self appendItemWithTitle:title action:action dismissImmediately:NO]; | |
50 } | |
51 | |
52 - (void)appendItemWithTitle:(NSString*)title | |
53 action:(ProceduralBlock)action | |
54 dismissImmediately:(BOOL)dismissImmediately { | |
55 DCHECK(title && action); | |
56 [_itemTitles addObject:title]; | |
57 // The action block needs to receive the copy message in order to freeze the | |
58 // stack data it uses. Only then can it be safely retained by the array. | |
59 base::scoped_nsprotocol<id> heapAction([action copy]); | |
60 [_actions addObject:heapAction]; | |
61 if (dismissImmediately) { | |
62 [_dismissImmediatelyActions addIndex:[_actions count] - 1]; | |
63 } | |
64 } | |
65 | |
66 - (void)performActionAtIndex:(NSUInteger)index { | |
67 static_cast<ProceduralBlock>([_actions objectAtIndex:index])(); | |
68 } | |
69 | |
70 - (BOOL)shouldDismissImmediatelyOnClickedAtIndex:(NSUInteger)index { | |
71 return [_dismissImmediatelyActions containsIndex:index]; | |
72 } | |
73 | |
74 @end | |
OLD | NEW |