Chromium Code Reviews| 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/tools/tools_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/tools/tools_mediator.h" |
| 6 | 6 |
| 7 #import "ios/clean/chrome/browser/ui/commands/settings_commands.h" | 7 #include "base/macros.h" |
| 8 #import "ios/clean/chrome/browser/ui/tools/tools_actions.h" | |
| 9 #import "ios/clean/chrome/browser/ui/tools/tools_consumer.h" | 8 #import "ios/clean/chrome/browser/ui/tools/tools_consumer.h" |
| 10 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h" | 9 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h" |
| 10 #import "ios/clean/chrome/browser/ui/tools/tools_menu_model.h" | |
| 11 #import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h" | 11 #import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 12 | 13 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
| 15 #endif | 16 #endif |
| 16 | 17 |
| 17 @interface ToolsMediator () | 18 @interface ToolsMediator () |
| 18 @property(nonatomic, strong) id<ToolsConsumer> consumer; | 19 @property(nonatomic, strong) id<ToolsConsumer> consumer; |
| 19 @property(nonatomic, strong) ToolsMenuConfiguration* toolsMenuConfiguration; | 20 @property(nonatomic, strong) ToolsMenuConfiguration* toolsMenuConfiguration; |
| 21 @property(nonatomic, strong) NSMutableArray* menuItems; | |
| 22 // Populates the menuItems array by filtering the model data source based on the | |
| 23 // current ToolsMenuConfiguration. | |
| 24 - (void)populateMenuItems; | |
| 25 // Checks if a specific Menu Item should be visibile for the current | |
|
lpromero
2017/05/23 09:00:43
*visible
sczs
2017/05/23 20:28:18
Done.
| |
| 26 // configuration. | |
| 27 - (BOOL)itemIsVisibleForCurrentConfiguration:(const MenuModelItem)modelItem; | |
| 28 | |
| 20 @end | 29 @end |
| 21 | 30 |
| 22 @implementation ToolsMediator | 31 @implementation ToolsMediator |
| 23 | 32 |
| 24 @synthesize consumer = _consumer; | 33 @synthesize consumer = _consumer; |
| 25 @synthesize toolsMenuConfiguration = _toolsMenuConfiguration; | 34 @synthesize toolsMenuConfiguration = _toolsMenuConfiguration; |
| 35 @synthesize menuItems = _menuItems; | |
| 26 | 36 |
| 27 - (instancetype)initWithConsumer:(id<ToolsConsumer>)consumer | 37 - (instancetype)initWithConsumer:(id<ToolsConsumer>)consumer |
| 28 andConfiguration:(ToolsMenuConfiguration*)menuConfiguration { | 38 andConfiguration:(ToolsMenuConfiguration*)menuConfiguration { |
| 29 self = [super init]; | 39 self = [super init]; |
| 30 if (self) { | 40 if (self) { |
| 31 self.toolsMenuConfiguration = menuConfiguration; | 41 self.toolsMenuConfiguration = menuConfiguration; |
| 32 self.consumer = consumer; | 42 self.consumer = consumer; |
| 33 } | 43 } |
| 34 return self; | 44 return self; |
| 35 } | 45 } |
| 36 | 46 |
| 37 - (void)setConsumer:(id<ToolsConsumer>)consumer { | 47 - (void)setConsumer:(id<ToolsConsumer>)consumer { |
| 38 _consumer = consumer; | 48 _consumer = consumer; |
| 39 | 49 |
| 40 // PLACEHOLDER: Temporary hardcoded consumer model. | 50 [self populateMenuItems]; |
| 41 NSArray<ToolsMenuItem*>* menuItems = [[NSArray alloc] init]; | |
| 42 menuItems = @[ | |
| 43 [[ToolsMenuItem alloc] init], [[ToolsMenuItem alloc] init], | |
| 44 [[ToolsMenuItem alloc] init], [[ToolsMenuItem alloc] init], | |
| 45 [[ToolsMenuItem alloc] init], [[ToolsMenuItem alloc] init], | |
| 46 [[ToolsMenuItem alloc] init], [[ToolsMenuItem alloc] init], | |
| 47 [[ToolsMenuItem alloc] init], [[ToolsMenuItem alloc] init], | |
| 48 [[ToolsMenuItem alloc] init] | |
| 49 ]; | |
| 50 | 51 |
| 51 menuItems[0].title = @"New Tab"; | 52 [_consumer setToolsMenuItems:self.menuItems]; |
| 52 | |
| 53 menuItems[1].title = @"New Incognito Tab"; | |
| 54 | |
| 55 menuItems[2].title = @"Bookmarks"; | |
| 56 | |
| 57 menuItems[3].title = @"Reading List"; | |
| 58 | |
| 59 menuItems[4].title = @"Recent Tabs"; | |
| 60 | |
| 61 menuItems[5].title = @"History"; | |
| 62 | |
| 63 menuItems[6].title = @"Report an Issue"; | |
| 64 | |
| 65 menuItems[7].title = @"Find in Pageā¦"; | |
| 66 menuItems[7].action = @selector(showFindInPage); | |
| 67 | |
| 68 menuItems[8].title = @"Request Desktop Site"; | |
| 69 | |
| 70 menuItems[9].title = @"Settings"; | |
| 71 menuItems[9].action = @selector(showSettings); | |
| 72 | |
| 73 menuItems[10].title = @"Help"; | |
| 74 | |
| 75 [_consumer setToolsMenuItems:menuItems]; | |
| 76 [_consumer | 53 [_consumer |
| 77 setDisplayOverflowControls:!self.toolsMenuConfiguration.isInTabSwitcher]; | 54 setDisplayOverflowControls:!self.toolsMenuConfiguration.isInTabSwitcher]; |
| 78 } | 55 } |
| 79 | 56 |
| 57 #pragma mark - Private Methods | |
| 58 | |
| 59 - (void)populateMenuItems { | |
| 60 self.menuItems = [NSMutableArray array]; | |
| 61 | |
| 62 for (size_t i = 0; i < arraysize(itemsModelList); ++i) { | |
| 63 const MenuModelItem& modelItem = itemsModelList[i]; | |
| 64 | |
| 65 if ([self itemIsVisibleForCurrentConfiguration:modelItem]) { | |
| 66 ToolsMenuItem* menuItem = [[ToolsMenuItem alloc] init]; | |
| 67 menuItem.title = l10n_util::GetNSStringWithFixup(modelItem.title_id); | |
| 68 menuItem.action = NSSelectorFromString(modelItem.selector); | |
| 69 [self.menuItems addObject:menuItem]; | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 - (BOOL)itemIsVisibleForCurrentConfiguration:(const MenuModelItem)modelItem { | |
| 75 // We first check if the item should be visible on the current Incognito | |
|
lpromero
2017/05/23 09:00:43
Nit: avoid "we". Just remove it, here.
sczs
2017/05/23 20:28:18
Done.
| |
| 76 // state. | |
| 77 switch (modelItem.visibility) { | |
| 78 case kVisibleIncognitoOnly: | |
| 79 if (!self.toolsMenuConfiguration.isInIncognito) { | |
| 80 return NO; | |
| 81 } | |
| 82 break; | |
| 83 case kVisibleNotIncognitoOnly: | |
| 84 if (self.toolsMenuConfiguration.isInIncognito) { | |
| 85 return NO; | |
| 86 } | |
| 87 break; | |
| 88 case kVisibleAlways: | |
| 89 break; | |
| 90 default: | |
| 91 return NO; | |
| 92 } | |
| 93 | |
| 94 // We then check if the item should be visibile for the current Toolbar state. | |
|
lpromero
2017/05/23 09:00:43
Idem.
sczs
2017/05/23 20:28:18
Done.
| |
| 95 switch (modelItem.toolbar_types) { | |
| 96 case ToolbarTypeNone: | |
| 97 return NO; | |
| 98 case ToolbarTypeSwitcher: | |
| 99 return self.toolsMenuConfiguration.isInTabSwitcher; | |
| 100 case ToolbarTypeWeb: | |
| 101 return !self.toolsMenuConfiguration.isInTabSwitcher; | |
| 102 case ToolbarTypeAll: | |
| 103 return YES; | |
| 104 default: | |
| 105 return NO; | |
|
lpromero
2017/05/23 09:00:44
Should you remove the default case, so that this b
sczs
2017/05/23 20:28:18
I was trying to do this, but without the default c
| |
| 106 } | |
| 107 } | |
| 108 | |
| 80 @end | 109 @end |
| 110 | |
| 111 // Private implementation for testing. | |
| 112 @implementation ToolsMediator (Testing) | |
| 113 | |
| 114 - (NSArray*)menuItemsArray { | |
| 115 return self.menuItems; | |
| 116 } | |
| 117 | |
| 118 @end | |
| OLD | NEW |