Commit 0825547d authored by sczs's avatar sczs Committed by Commit Bot

[ios clean] Disables ToolsMenu items based on ToolsMenuConfig

This CL disables certain items on the ToolsMenu based on the following 
criteria:
-No Tabs Opened. (Will disable Close All Tabs/Incognito Tabs)
-In New Tab Page. (Will disable Find in Page and Request Desktop/Mobile
sites).

In order to do this a new Bitmask is added to the ToolsMenuModel and it
is used by the ToolsMediator to check if the item should be enabled or
not.

Bug: 682880
Change-Id: I2318f7c3d6308317cb9c07e6b5a4692a4fdd9a5e
Reviewed-on: https://chromium-review.googlesource.com/571922Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488442}
parent e9780e62
...@@ -202,6 +202,8 @@ ...@@ -202,6 +202,8 @@
ToolsMenuConfiguration* menuConfiguration = ToolsMenuConfiguration* menuConfiguration =
[[ToolsMenuConfiguration alloc] initWithDisplayView:nil]; [[ToolsMenuConfiguration alloc] initWithDisplayView:nil];
menuConfiguration.inTabSwitcher = YES; menuConfiguration.inTabSwitcher = YES;
menuConfiguration.noOpenedTabs = self.browser->web_state_list().empty();
menuConfiguration.inNewTabPage = NO;
toolsCoordinator.toolsMenuConfiguration = menuConfiguration; toolsCoordinator.toolsMenuConfiguration = menuConfiguration;
[toolsCoordinator start]; [toolsCoordinator start];
self.toolsMenuCoordinator = toolsCoordinator; self.toolsMenuCoordinator = toolsCoordinator;
......
...@@ -15,6 +15,7 @@ source_set("toolbar") { ...@@ -15,6 +15,7 @@ source_set("toolbar") {
deps = [ deps = [
":toolbar_ui", ":toolbar_ui",
"//base", "//base",
"//ios/chrome/browser",
"//ios/chrome/browser/web_state_list", "//ios/chrome/browser/web_state_list",
"//ios/clean/chrome/browser/ui/commands", "//ios/clean/chrome/browser/ui/commands",
"//ios/clean/chrome/browser/ui/omnibox", "//ios/clean/chrome/browser/ui/omnibox",
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.h" #import "ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h" #import "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h"
#import "ios/clean/chrome/browser/ui/omnibox/location_bar_coordinator.h" #import "ios/clean/chrome/browser/ui/omnibox/location_bar_coordinator.h"
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h" #import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h"
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
#import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h"
#import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.h" #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.h"
#import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h" #import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h"
#import "ios/web/public/web_state/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -105,6 +107,10 @@ ...@@ -105,6 +107,10 @@
ToolsMenuConfiguration* menuConfiguration = ToolsMenuConfiguration* menuConfiguration =
[[ToolsMenuConfiguration alloc] initWithDisplayView:nil]; [[ToolsMenuConfiguration alloc] initWithDisplayView:nil];
menuConfiguration.inTabSwitcher = NO; menuConfiguration.inTabSwitcher = NO;
menuConfiguration.noOpenedTabs = NO;
menuConfiguration.inNewTabPage =
(self.webState->GetLastCommittedURL() == GURL(kChromeUINewTabURL));
toolsCoordinator.toolsMenuConfiguration = menuConfiguration; toolsCoordinator.toolsMenuConfiguration = menuConfiguration;
toolsCoordinator.webState = self.webState; toolsCoordinator.webState = self.webState;
[toolsCoordinator start]; [toolsCoordinator start];
......
...@@ -134,6 +134,7 @@ const CGFloat kCloseButtonHeight = 44.0; ...@@ -134,6 +134,7 @@ const CGFloat kCloseButtonHeight = 44.0;
0, kMenuItemLeadingEdgeInset, 0, 0)]; 0, kMenuItemLeadingEdgeInset, 0, 0)];
[menuButton.titleLabel setFont:[MDCTypography subheadFont]]; [menuButton.titleLabel setFont:[MDCTypography subheadFont]];
[menuButton.titleLabel setTextAlignment:NSTextAlignmentNatural]; [menuButton.titleLabel setTextAlignment:NSTextAlignmentNatural];
menuButton.enabled = item.enabled;
[menuButton addTarget:self.dispatcher [menuButton addTarget:self.dispatcher
action:@selector(closeToolsMenu) action:@selector(closeToolsMenu)
forControlEvents:UIControlEventTouchUpInside]; forControlEvents:UIControlEventTouchUpInside];
......
...@@ -87,11 +87,11 @@ ...@@ -87,11 +87,11 @@
for (size_t i = 0; i < arraysize(itemsModelList); ++i) { for (size_t i = 0; i < arraysize(itemsModelList); ++i) {
const MenuModelItem& modelItem = itemsModelList[i]; const MenuModelItem& modelItem = itemsModelList[i];
if ([self itemIsVisibleForCurrentConfiguration:modelItem]) { if ([self itemIsVisibleForCurrentConfiguration:modelItem]) {
ToolsMenuItem* menuItem = [[ToolsMenuItem alloc] init]; ToolsMenuItem* menuItem = [[ToolsMenuItem alloc] init];
menuItem.title = l10n_util::GetNSStringWithFixup(modelItem.title_id); menuItem.title = l10n_util::GetNSStringWithFixup(modelItem.title_id);
menuItem.action = NSSelectorFromString(modelItem.selector); menuItem.action = NSSelectorFromString(modelItem.selector);
menuItem.enabled = [self itemIsEnabledForCurrentConfiguration:modelItem];
[self.menuItems addObject:menuItem]; [self.menuItems addObject:menuItem];
} }
} }
...@@ -132,6 +132,20 @@ ...@@ -132,6 +132,20 @@
} }
} }
// Returns true if item should be enabled on the current configuration.
- (BOOL)itemIsEnabledForCurrentConfiguration:(const MenuModelItem)modelItem {
switch (modelItem.enabled) {
case ItemEnabledAlways:
return YES;
case ItemEnabledNotInNTP:
return !self.toolsMenuConfiguration.inNewTabPage;
case ItemEnabledWhenOpenTabs:
return !self.toolsMenuConfiguration.hasNoOpenedTabs;
default:
return YES;
}
}
@end @end
// Private implementation for testing. // Private implementation for testing.
......
...@@ -103,6 +103,84 @@ TEST_F(ToolsMediatorTest, TestMenuItemsForTabSwitcherNonIncognito) { ...@@ -103,6 +103,84 @@ TEST_F(ToolsMediatorTest, TestMenuItemsForTabSwitcherNonIncognito) {
EXPECT_EQ(5ul, [mediator_.menuItemsArray count]); EXPECT_EQ(5ul, [mediator_.menuItemsArray count]);
} }
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForOpenTabs) {
configuration_.inTabSwitcher = YES;
configuration_.inIncognito = NO;
configuration_.noOpenedTabs = NO;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* closeAllTabsItem = mediator_.menuItemsArray[2];
EXPECT_NSEQ(@"Close All Tabs", closeAllTabsItem.title);
EXPECT_EQ(true, closeAllTabsItem.enabled);
}
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForNoOpenTabs) {
configuration_.inTabSwitcher = YES;
configuration_.inIncognito = NO;
configuration_.noOpenedTabs = YES;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* closeAllTabsItem = mediator_.menuItemsArray[2];
EXPECT_NSEQ(@"Close All Tabs", closeAllTabsItem.title);
EXPECT_EQ(false, closeAllTabsItem.enabled);
}
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForOpenTabsIncognito) {
configuration_.inTabSwitcher = YES;
configuration_.inIncognito = YES;
configuration_.noOpenedTabs = NO;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* closeAllTabsItem = mediator_.menuItemsArray[2];
EXPECT_NSEQ(@"Close All Incognito Tabs", closeAllTabsItem.title);
EXPECT_EQ(true, closeAllTabsItem.enabled);
}
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForNoOpenTabsIncognito) {
configuration_.inTabSwitcher = YES;
configuration_.inIncognito = YES;
configuration_.noOpenedTabs = YES;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* closeAllTabsItem = mediator_.menuItemsArray[2];
EXPECT_NSEQ(@"Close All Incognito Tabs", closeAllTabsItem.title);
EXPECT_EQ(false, closeAllTabsItem.enabled);
}
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForInNTP) {
configuration_.inTabSwitcher = NO;
configuration_.inIncognito = NO;
configuration_.inNewTabPage = YES;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* findInPageItem = mediator_.menuItemsArray[4];
EXPECT_NSEQ(@"Find in Page…", findInPageItem.title);
EXPECT_EQ(false, findInPageItem.enabled);
}
TEST_F(ToolsMediatorTest, TestEnabledMenuItemsForNotInNTP) {
configuration_.inTabSwitcher = NO;
configuration_.inIncognito = NO;
configuration_.inNewTabPage = NO;
mediator_ = [[ToolsMediator alloc] initWithConsumer:consumer_
configuration:configuration_];
ToolsMenuItem* findInPageItem = mediator_.menuItemsArray[4];
EXPECT_NSEQ(@"Find in Page…", findInPageItem.title);
EXPECT_EQ(true, findInPageItem.enabled);
}
TEST_F(ToolsMediatorTest, TestDontUpdateConsumerLoadingState) { TEST_F(ToolsMediatorTest, TestDontUpdateConsumerLoadingState) {
configuration_.inTabSwitcher = NO; configuration_.inTabSwitcher = NO;
[[consumer_ reject] setIsLoading:YES]; [[consumer_ reject] setIsLoading:YES];
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
@interface ToolsMenuItem : NSObject @interface ToolsMenuItem : NSObject
@property(nonatomic, copy) NSString* title; @property(nonatomic, copy) NSString* title;
@property(nonatomic) SEL action; @property(nonatomic) SEL action;
@property(nonatomic) BOOL enabled;
@end @end
#endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLS_TOOLS_MENU_ITEM_H_ #endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLS_TOOLS_MENU_ITEM_H_
...@@ -11,4 +11,5 @@ ...@@ -11,4 +11,5 @@
@implementation ToolsMenuItem @implementation ToolsMenuItem
@synthesize title = _title; @synthesize title = _title;
@synthesize action = _action; @synthesize action = _action;
@synthesize enabled = _enabled;
@end @end
...@@ -26,6 +26,8 @@ struct MenuModelItem { ...@@ -26,6 +26,8 @@ struct MenuModelItem {
// The selector name which will be called on the dispatcher, when the // The selector name which will be called on the dispatcher, when the
// item is selected. // item is selected.
NSString* selector; NSString* selector;
// If visible, this is the condition in which the item will be enabled.
int enabled;
}; };
// Menu items can be marked as visible or not when Incognito is enabled. // Menu items can be marked as visible or not when Incognito is enabled.
...@@ -38,6 +40,16 @@ typedef NS_OPTIONS(NSUInteger, ItemVisible) { ...@@ -38,6 +40,16 @@ typedef NS_OPTIONS(NSUInteger, ItemVisible) {
// clang-format on // clang-format on
}; };
// Menu items can be enabled or not based on the ToolsMenuConfiguration.
// The following bits are used for |enabled| field in |MenuModelItem|.
typedef NS_OPTIONS(NSUInteger, ItemEnable) {
// clang-format off
ItemEnabledAlways = 0,
ItemEnabledWhenOpenTabs = 1 << 0,
ItemEnabledNotInNTP = 1 << 1,
// clang-format on
};
// Flags for different toolbar types. // Flags for different toolbar types.
typedef NS_OPTIONS(NSUInteger, ToolbarType) { typedef NS_OPTIONS(NSUInteger, ToolbarType) {
// clang-format off // clang-format off
......
...@@ -14,51 +14,51 @@ ...@@ -14,51 +14,51 @@
// the value of kToolsMenuNumberOfItems with the new total count. // the value of kToolsMenuNumberOfItems with the new total count.
const MenuModelItem itemsModelList[kToolsMenuNumberOfItems] = { const MenuModelItem itemsModelList[kToolsMenuNumberOfItems] = {
// clang-format off // clang-format off
{ IDS_IOS_TOOLS_MENU_NEW_TAB, kToolsMenuNewTabId, { IDS_IOS_TOOLS_MENU_NEW_TAB, kToolsMenuNewTabId,
ToolbarTypeAll, ItemVisibleAlways, ToolbarTypeAll, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_NEW_INCOGNITO_TAB, kToolsMenuNewIncognitoTabId, { IDS_IOS_TOOLS_MENU_NEW_INCOGNITO_TAB, kToolsMenuNewIncognitoTabId,
ToolbarTypeAll, ItemVisibleAlways, ToolbarTypeAll, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_CLOSE_ALL_TABS, kToolsMenuCloseAllTabsId, { IDS_IOS_TOOLS_MENU_CLOSE_ALL_TABS, kToolsMenuCloseAllTabsId,
ToolbarTypeSwitcher, ItemVisibleNotIncognitoOnly, ToolbarTypeSwitcher, ItemVisibleNotIncognitoOnly,
nil }, nil, ItemEnabledWhenOpenTabs},
{ IDS_IOS_TOOLS_MENU_CLOSE_ALL_INCOGNITO_TABS, { IDS_IOS_TOOLS_MENU_CLOSE_ALL_INCOGNITO_TABS,
kToolsMenuCloseAllIncognitoTabsId, kToolsMenuCloseAllIncognitoTabsId,
ToolbarTypeSwitcher, ItemVisibleIncognitoOnly, ToolbarTypeSwitcher, ItemVisibleIncognitoOnly,
nil }, nil, ItemEnabledWhenOpenTabs},
{ IDS_IOS_TOOLS_MENU_BOOKMARKS, kToolsMenuBookmarksId, { IDS_IOS_TOOLS_MENU_BOOKMARKS, kToolsMenuBookmarksId,
ToolbarTypeNone, ItemVisibleAlways, ToolbarTypeNone, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_READING_LIST, kToolsMenuReadingListId, { IDS_IOS_TOOLS_MENU_READING_LIST, kToolsMenuReadingListId,
ToolbarTypeNone, ItemVisibleAlways, ToolbarTypeNone, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_RECENT_TABS, kToolsMenuOtherDevicesId, { IDS_IOS_TOOLS_MENU_RECENT_TABS, kToolsMenuOtherDevicesId,
ToolbarTypeNone, ItemVisibleNotIncognitoOnly, ToolbarTypeNone, ItemVisibleNotIncognitoOnly,
nil }, nil, ItemEnabledAlways},
{ IDS_HISTORY_SHOW_HISTORY, kToolsMenuHistoryId, { IDS_HISTORY_SHOW_HISTORY, kToolsMenuHistoryId,
ToolbarTypeWeb, ItemVisibleAlways, ToolbarTypeWeb, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_OPTIONS_REPORT_AN_ISSUE, kToolsMenuReportAnIssueId, { IDS_IOS_OPTIONS_REPORT_AN_ISSUE, kToolsMenuReportAnIssueId,
ToolbarTypeAll, ItemVisibleAlways, ToolbarTypeAll, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_FIND_IN_PAGE, kToolsMenuFindInPageId, { IDS_IOS_TOOLS_MENU_FIND_IN_PAGE, kToolsMenuFindInPageId,
ToolbarTypeWeb, ItemVisibleAlways, ToolbarTypeWeb, ItemVisibleAlways,
NSStringFromSelector(@selector(showFindInPage)) }, NSStringFromSelector(@selector(showFindInPage)),ItemEnabledNotInNTP},
{ IDS_IOS_TOOLS_MENU_REQUEST_DESKTOP_SITE, kToolsMenuRequestDesktopId, { IDS_IOS_TOOLS_MENU_REQUEST_DESKTOP_SITE, kToolsMenuRequestDesktopId,
ToolbarTypeNone, ItemVisibleAlways, ToolbarTypeNone, ItemVisibleAlways,
nil }, nil, ItemEnabledNotInNTP},
{ IDS_IOS_TOOLS_MENU_REQUEST_MOBILE_SITE, kToolsMenuRequestMobileId, { IDS_IOS_TOOLS_MENU_REQUEST_MOBILE_SITE, kToolsMenuRequestMobileId,
ToolbarTypeNone, ItemVisibleAlways, ToolbarTypeNone, ItemVisibleAlways,
nil }, nil, ItemEnabledNotInNTP},
{ IDS_IOS_TOOLS_MENU_READER_MODE, kToolsMenuReaderMode, { IDS_IOS_TOOLS_MENU_READER_MODE, kToolsMenuReaderMode,
ToolbarTypeNone, ItemVisibleAlways, ToolbarTypeNone, ItemVisibleAlways,
nil }, nil, ItemEnabledNotInNTP},
{ IDS_IOS_TOOLS_MENU_SETTINGS, kToolsMenuSettingsId, { IDS_IOS_TOOLS_MENU_SETTINGS, kToolsMenuSettingsId,
ToolbarTypeAll, ItemVisibleAlways, ToolbarTypeAll, ItemVisibleAlways,
NSStringFromSelector(@selector(showSettings)) }, NSStringFromSelector(@selector(showSettings)), ItemEnabledAlways},
{ IDS_IOS_TOOLS_MENU_HELP_MOBILE, kToolsMenuHelpId, { IDS_IOS_TOOLS_MENU_HELP_MOBILE, kToolsMenuHelpId,
ToolbarTypeWeb, ItemVisibleAlways, ToolbarTypeWeb, ItemVisibleAlways,
nil }, nil, ItemEnabledAlways},
// clang-format on // clang-format on
}; };
...@@ -24,6 +24,8 @@ enum class UserAgentType : short; ...@@ -24,6 +24,8 @@ enum class UserAgentType : short;
@property(nonatomic, getter=hasNoOpenedTabs) BOOL noOpenedTabs; @property(nonatomic, getter=hasNoOpenedTabs) BOOL noOpenedTabs;
// Indicates that the menu is being shown while in incognito mode. // Indicates that the menu is being shown while in incognito mode.
@property(nonatomic, getter=isInIncognito) BOOL inIncognito; @property(nonatomic, getter=isInIncognito) BOOL inIncognito;
// Indicates that the menu is being shown while in the New Tab Page.
@property(nonatomic, getter=isInNewTabPage) BOOL inNewTabPage;
// Indicates that the menu is being shown while user agent is |userAgentType|. // Indicates that the menu is being shown while user agent is |userAgentType|.
// If NONE, shows "Request Desktop Site" in disabled state. // If NONE, shows "Request Desktop Site" in disabled state.
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
@synthesize inIncognito = _inIncognito; @synthesize inIncognito = _inIncognito;
@synthesize userAgentType = _userAgentType; @synthesize userAgentType = _userAgentType;
@synthesize requestStartTime = _requestStartTime; @synthesize requestStartTime = _requestStartTime;
@synthesize inNewTabPage = _inNewTabPage;
- (instancetype)initWithDisplayView:(UIView*)displayView { - (instancetype)initWithDisplayView:(UIView*)displayView {
if (self = [super init]) { if (self = [super init]) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment