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 #import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" |
| 8 |
7 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
8 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
9 #endif | 11 #endif |
10 | 12 |
11 namespace { | 13 namespace { |
12 // Typedef the block parameter for UIAlertAction for readability. | 14 // Typedef the block parameter for UIAlertAction for readability. |
13 typedef void (^AlertActionHandler)(UIAlertAction*); | 15 typedef void (^AlertActionHandler)(UIAlertAction*); |
14 } | 16 } |
15 | 17 |
16 @interface ContextMenuViewController () | 18 @interface ContextMenuViewController () |
17 // Object to which command messages will be sent. | 19 // Object to which command messages will be sent. |
18 @property(nonatomic, weak) id dispatcher; | 20 @property(nonatomic, weak) id dispatcher; |
19 @end | 21 @end |
20 | 22 |
21 @implementation ContextMenuViewController | 23 @implementation ContextMenuViewController |
22 @synthesize dispatcher = _dispatcher; | 24 @synthesize dispatcher = _dispatcher; |
23 | 25 |
24 - (instancetype)initWithDispatcher:(id)dispatcher { | 26 - (instancetype)initWithDispatcher:(id<ContextMenuCommands>)dispatcher { |
25 self = | 27 self = |
26 [[self class] alertControllerWithTitle:nil | 28 [[self class] alertControllerWithTitle:nil |
27 message:nil | 29 message:nil |
28 preferredStyle:UIAlertControllerStyleActionSheet]; | 30 preferredStyle:UIAlertControllerStyleActionSheet]; |
29 if (self) { | 31 if (self) { |
30 _dispatcher = dispatcher; | 32 _dispatcher = dispatcher; |
31 } | 33 } |
32 return self; | 34 return self; |
33 } | 35 } |
34 | 36 |
35 #pragma mark - ContextMenuConsumer | 37 #pragma mark - ContextMenuConsumer |
36 | 38 |
37 - (void)setContextMenuTitle:(NSString*)title { | 39 - (void)setContextMenuTitle:(NSString*)title { |
38 self.title = title; | 40 self.title = title; |
39 } | 41 } |
40 | 42 |
41 - (void)setContextMenuItems:(NSArray<ContextMenuItem*>*)items { | 43 - (void)setContextMenuItems:(NSArray<ContextMenuItem*>*)items { |
42 for (ContextMenuItem* item in items) { | 44 for (ContextMenuItem* item in items) { |
43 // Create a block that sends the invocation passed in with the item's | 45 // Create a block that sends the invocation passed in with the item's |
44 // configuration to the dispatcher. | 46 // configuration to the dispatcher. |
45 AlertActionHandler handler = ^(UIAlertAction* action) { | 47 AlertActionHandler handler = ^(UIAlertAction* action) { |
46 [item.command invokeWithTarget:self.dispatcher]; | 48 [item.command invokeWithTarget:self.dispatcher]; |
47 }; | 49 }; |
48 [self addAction:[UIAlertAction actionWithTitle:item.title | 50 [self addAction:[UIAlertAction actionWithTitle:item.title |
49 style:UIAlertActionStyleDefault | 51 style:UIAlertActionStyleDefault |
50 handler:handler]]; | 52 handler:handler]]; |
51 } | 53 } |
52 | 54 |
53 // Always add a cancel action. | 55 // Always add a cancel action. |
| 56 AlertActionHandler cancelHandler = ^(UIAlertAction* action) { |
| 57 [self.dispatcher cancelContextMenu]; |
| 58 }; |
54 [self addAction:[UIAlertAction actionWithTitle:@"Cancel" | 59 [self addAction:[UIAlertAction actionWithTitle:@"Cancel" |
55 style:UIAlertActionStyleCancel | 60 style:UIAlertActionStyleCancel |
56 handler:nil]]; | 61 handler:cancelHandler]]; |
57 } | 62 } |
58 | 63 |
59 @end | 64 @end |
OLD | NEW |