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_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" |
6 | 6 |
7 #import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" | |
7 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_consumer.h" | 8 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_consumer.h" |
8 #import "ios/web/public/web_state/context_menu_params.h" | 9 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_context_impl.h" |
10 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" | |
11 #import "ios/web/public/url_scheme_util.h" | |
12 #include "url/gurl.h" | |
9 | 13 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
12 #endif | 16 #endif |
13 | 17 |
14 @interface ContextMenuMediator () | 18 @interface ContextMenuMediator () |
15 @property(nonatomic, weak) id<ContextMenuConsumer> consumer; | 19 // The consumer for context menu functionality mediated by this class. |
20 @property(nonatomic, readonly, weak) id<ContextMenuConsumer> consumer; | |
edchin
2017/05/25 21:41:53
The consumer is only used in the -init method so t
kkhorimoto
2017/05/26 23:20:09
Done.
| |
21 | |
22 // The ContextMenuItem to use for script menus. | |
23 - (ContextMenuItem*)scriptItem; | |
24 | |
25 // The ContextMenuItems to use for link menus. | |
26 - (NSArray<ContextMenuItem*>*)linkItems; | |
27 | |
28 // The ContextMenuItems to use for image menus. | |
29 - (NSArray<ContextMenuItem*>*)imageItems; | |
30 | |
31 // The cancel item to use. | |
32 - (ContextMenuItem*)cancelItem; | |
33 | |
16 @end | 34 @end |
17 | 35 |
18 @implementation ContextMenuMediator | 36 @implementation ContextMenuMediator |
19 @synthesize consumer = _consumer; | 37 @synthesize consumer = _consumer; |
20 | 38 |
21 - (instancetype)initWithConsumer:(id<ContextMenuConsumer>)consumer { | 39 - (instancetype)initWithConsumer:(id<ContextMenuConsumer>)consumer |
40 context:(ContextMenuContextImpl*)context { | |
22 if ((self = [super init])) { | 41 if ((self = [super init])) { |
42 DCHECK(consumer); | |
23 _consumer = consumer; | 43 _consumer = consumer; |
24 [self updateConsumer]; | 44 DCHECK(context); |
45 // Set the context and menu title. | |
46 [_consumer setContextMenuContext:context]; | |
47 [_consumer setContextMenuTitle:context.menuTitle]; | |
48 // Add the appropriate items. | |
49 NSMutableArray<ContextMenuItem*>* items = [[NSMutableArray alloc] init]; | |
50 if (context.script.size()) | |
51 [items addObject:[self scriptItem]]; | |
52 BOOL showLinkOptions = | |
53 context.linkURL.is_valid() && web::UrlHasWebScheme(context.linkURL); | |
54 if (showLinkOptions) | |
55 [items addObjectsFromArray:[self linkItems]]; | |
56 BOOL showImageOptions = context.imageURL.is_valid(); | |
57 if (showImageOptions) | |
58 [items addObjectsFromArray:[self imageItems]]; | |
59 [_consumer setContextMenuItems:[items copy] cancelItem:[self cancelItem]]; | |
25 } | 60 } |
26 return self; | 61 return self; |
27 } | 62 } |
28 | 63 |
29 // Update the consumer. | 64 #pragma mark - |
30 - (void)updateConsumer { | |
31 // PLACEHOLDER. Fake title. | |
32 [self.consumer setContextMenuTitle:@"http://some/link.html"]; | |
33 NSMutableArray<ContextMenuItem*>* items = | |
34 [[NSMutableArray<ContextMenuItem*> alloc] init]; | |
35 | 65 |
36 // PLACEHOLDER. Two non-functional items. | 66 - (ContextMenuItem*)scriptItem { |
37 [items | 67 std::vector<SEL> commands(2U); |
38 addObject:[ContextMenuItem itemWithTitle:@"Open in New Tab" command:nil]]; | 68 commands[0] = @selector(executeContextMenuScript:); |
39 [items | 69 commands[1] = @selector(hideContextMenu:); |
40 addObject:[ContextMenuItem itemWithTitle:@"Copy Link URL" command:nil]]; | 70 return [ContextMenuItem itemWithTitle:@"Execute Script" commands:commands]; |
41 [self.consumer setContextMenuItems:[items copy]]; | 71 } |
72 | |
73 - (NSArray<ContextMenuItem*>*)linkItems { | |
74 // Opening the link in a new Tab will stop this context menu's coordinator, so | |
75 // there's no need to hide it. | |
76 std::vector<SEL> newTabCommands(1U); | |
77 newTabCommands[0] = @selector(openContextMenuLinkInNewTab:); | |
78 // TODO: Add |-openContextMenuLinkInNewIncognitoTab:| as the first command for | |
79 // "Open In New Incognito Tab" once the incognito tab grid is implemented. | |
80 std::vector<SEL> newIncognitoTabCommands(1U); | |
81 newIncognitoTabCommands[0] = @selector(hideContextMenu:); | |
82 // TODO: Add |-copyContextMenuLink:| as the first command for "Copy Link" once | |
83 // copying to pasteboard is implemented. | |
84 std::vector<SEL> copyLinkCommands(1U); | |
85 newIncognitoTabCommands[0] = @selector(hideContextMenu:); | |
86 return @[ | |
87 [ContextMenuItem itemWithTitle:@"Open In New Tab" commands:newTabCommands], | |
88 [ContextMenuItem itemWithTitle:@"Open In New Incognito Tab" | |
89 commands:newIncognitoTabCommands], | |
90 [ContextMenuItem itemWithTitle:@"Copy Link" commands:copyLinkCommands], | |
91 ]; | |
92 } | |
93 | |
94 - (NSArray<ContextMenuItem*>*)imageItems { | |
95 // TODO: Add |-saveContextMenuImage:| as the first command for "Save Image" | |
96 // once camera roll access has been implemented. | |
97 std::vector<SEL> saveImageCommands(1U); | |
98 saveImageCommands[0] = @selector(hideContextMenu:); | |
99 std::vector<SEL> openImageCommands(2U); | |
100 openImageCommands[0] = @selector(openContextMenuImage:); | |
101 openImageCommands[1] = @selector(hideContextMenu:); | |
102 // Opening the image in a new Tab will stop this context menu's coordinator, | |
103 // so there's no need to hide it. | |
104 std::vector<SEL> openImageInNewTabCommands(1U); | |
105 openImageInNewTabCommands[0] = @selector(openContextMenuImageInNewTab:); | |
106 return @[ | |
107 [ContextMenuItem itemWithTitle:@"Save Image" commands:saveImageCommands], | |
108 [ContextMenuItem itemWithTitle:@"Open Image" commands:openImageCommands], | |
109 [ContextMenuItem itemWithTitle:@"Open Image In New Tab" | |
110 commands:openImageInNewTabCommands], | |
111 ]; | |
112 } | |
113 | |
114 - (ContextMenuItem*)cancelItem { | |
115 std::vector<SEL> cancelCommands(1U); | |
116 cancelCommands[0] = @selector(hideContextMenu:); | |
117 return [ContextMenuItem itemWithTitle:@"Cancel" commands:cancelCommands]; | |
42 } | 118 } |
43 | 119 |
44 @end | 120 @end |
OLD | NEW |