OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_view_controller.h
" | 5 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_view_controller.h
" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" |
8 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_context.h" | 9 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_context.h" |
9 | 10 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
12 #endif | 13 #endif |
13 | 14 |
14 namespace { | 15 namespace { |
15 // Typedef the block parameter for UIAlertAction for readability. | 16 // Typedef the block parameter for UIAlertAction for readability. |
16 typedef void (^AlertActionHandler)(UIAlertAction*); | 17 typedef void (^AlertActionHandler)(UIAlertAction*); |
17 // Sends |commands| to |dispatcher| using |context| as the menu context. | |
18 void DispatchContextMenuCommands(const std::vector<SEL>& commands, | |
19 id dispatcher, | |
20 ContextMenuContext* context) { | |
21 DCHECK(dispatcher); | |
22 DCHECK(context); | |
23 // |-performSelector:withObject:| throws a warning in ARC because the compiler | |
24 // doesn't know how to handle the memory management of the returned values. | |
25 // Since all ContextMenuCommands return void, these warning can be ignored | |
26 // here. | |
27 #pragma clang diagnostic push | |
28 #pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
29 for (SEL command : commands) { | |
30 [dispatcher performSelector:command withObject:context]; | |
31 } | |
32 #pragma clang diagnostic pop | |
33 } | |
34 } | 18 } |
35 | 19 |
36 @interface ContextMenuViewController () | 20 @interface ContextMenuViewController () |
37 // The dispatcher passed on initialization. | 21 // The dispatcher passed on initialization. |
38 @property(nonatomic, readonly, weak) id<ContextMenuCommands> dispatcher; | 22 @property(nonatomic, readonly, weak) id<ContextMenuCommands> dispatcher; |
39 // The context passed on initialization. | 23 // The context passed on initialization. |
40 @property(nonatomic, strong) ContextMenuContext* context; | 24 @property(nonatomic, strong) ContextMenuContext* context; |
41 | 25 |
42 // Creates an UIAlertAction for |item| using |style| to manage the action's | 26 // Creates an UIAlertAction for |item| using |style| to manage the action's |
43 // appearance. | 27 // appearance. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 style:UIAlertActionStyleCancel]]; | 68 style:UIAlertActionStyleCancel]]; |
85 } | 69 } |
86 | 70 |
87 #pragma mark - | 71 #pragma mark - |
88 | 72 |
89 - (UIAlertAction*)alertActionForItem:(ContextMenuItem*)item | 73 - (UIAlertAction*)alertActionForItem:(ContextMenuItem*)item |
90 style:(UIAlertActionStyle)style { | 74 style:(UIAlertActionStyle)style { |
91 DCHECK(item); | 75 DCHECK(item); |
92 // Create a block that dispatches |item|'s ContextMenuCommands. | 76 // Create a block that dispatches |item|'s ContextMenuCommands. |
93 AlertActionHandler handler = ^(UIAlertAction* action) { | 77 AlertActionHandler handler = ^(UIAlertAction* action) { |
94 DispatchContextMenuCommands(item.commands, self.dispatcher, self.context); | 78 // |-performSelector:withObject:| throws a warning in ARC because the compiler |
| 79 // doesn't know how to handle the memory management of the returned values. |
| 80 // Since all ContextMenuCommands return void, these warning can be ignored |
| 81 // here. |
| 82 #pragma clang diagnostic push |
| 83 #pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| 84 // TODO: Convert to DCHECK once all commands are implemented. |
| 85 if (item.command) |
| 86 [self.dispatcher performSelector:item.command withObject:self.context]; |
| 87 // If the command opens a new tab, the context menu will be hidden |
| 88 // automatically. If not, dispatch |-hideMenuContext:| to stop the menu UI. |
| 89 if (!item.commandOpensTab) { |
| 90 [self.dispatcher performSelector:@selector(hideContextMenu:) |
| 91 withObject:self.context]; |
| 92 } |
| 93 #pragma clang diagnostic pop |
95 }; | 94 }; |
96 return [UIAlertAction actionWithTitle:item.title style:style handler:handler]; | 95 return [UIAlertAction actionWithTitle:item.title style:style handler:handler]; |
97 } | 96 } |
98 | 97 |
99 @end | 98 @end |
OLD | NEW |