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 #pragma clang diagnostic push | |
24 #pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
25 for (SEL command : commands) { | |
26 [dispatcher performSelector:command withObject:context]; | |
27 } | |
28 #pragma clang diagnostic pop | |
29 } | |
30 } | 18 } |
31 | 19 |
32 @interface ContextMenuViewController () | 20 @interface ContextMenuViewController () |
33 // The dispatcher passed on initialization. | 21 // The dispatcher passed on initialization. |
34 @property(nonatomic, readonly, weak) id<ContextMenuCommands> dispatcher; | 22 @property(nonatomic, readonly, weak) id<ContextMenuCommands> dispatcher; |
35 // The context passed on initialization. | 23 // The context passed on initialization. |
36 @property(nonatomic, strong) ContextMenuContext* context; | 24 @property(nonatomic, strong) ContextMenuContext* context; |
37 | 25 |
38 // 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 |
39 // appearance. | 27 // appearance. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 style:UIAlertActionStyleCancel]]; | 68 style:UIAlertActionStyleCancel]]; |
81 } | 69 } |
82 | 70 |
83 #pragma mark - | 71 #pragma mark - |
84 | 72 |
85 - (UIAlertAction*)alertActionForItem:(ContextMenuItem*)item | 73 - (UIAlertAction*)alertActionForItem:(ContextMenuItem*)item |
86 style:(UIAlertActionStyle)style { | 74 style:(UIAlertActionStyle)style { |
87 DCHECK(item); | 75 DCHECK(item); |
88 // Create a block that dispatches |item|'s ContextMenuCommands. | 76 // Create a block that dispatches |item|'s ContextMenuCommands. |
89 AlertActionHandler handler = ^(UIAlertAction* action) { | 77 AlertActionHandler handler = ^(UIAlertAction* action) { |
90 DispatchContextMenuCommands(item.commands, self.dispatcher, self.context); | 78 #pragma clang diagnostic push |
| 79 #pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| 80 // TODO: Convert to DCHECK once all commands are implemented. |
| 81 if (item.command) |
| 82 [self.dispatcher performSelector:item.command withObject:self.context]; |
| 83 // If the command opens a new tab, the context menu will be hidden |
| 84 // automatically. If not, dispatch |-hideMenuContext:| to stop the menu UI. |
| 85 if (!item.commandOpensTab) { |
| 86 [self.dispatcher performSelector:@selector(hideContextMenu:) |
| 87 withObject:self.context]; |
| 88 } |
| 89 #pragma clang diagnostic pop |
91 }; | 90 }; |
92 return [UIAlertAction actionWithTitle:item.title style:style handler:handler]; | 91 return [UIAlertAction actionWithTitle:item.title style:style handler:handler]; |
93 } | 92 } |
94 | 93 |
95 @end | 94 @end |
OLD | NEW |