Commit 9c72b63a authored by Tina Wang's avatar Tina Wang Committed by Commit Bot

[SettingsUI] Add enterprise info section to three dot menu

- Added PopupMenuTextItem.h/mm for a general textual TableViewItem in the menu.
- Created a PopupMenuTextItem for enterprise information in popup_menu_mediator.
- Added a section to the popupToolsMenu when:
 - Settings UI flag is enabled
 - One or more prefs is managed by policy.
- Added the action to the enterprise info item. The action will be: doing nothing.

Bug: 1084692
Change-Id: I25158a432b0b319b7f9a2c298e7ab05692fbff63
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2250661
Commit-Queue: Tina Wang <tinazwang@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#780896}
parent 6a67eec7
......@@ -20,6 +20,7 @@ source_set("popup_menu") {
"resources:popup_menu_close_tab",
"resources:popup_menu_downloads",
"resources:popup_menu_edit_bookmark",
"resources:popup_menu_enterprise_icon",
"resources:popup_menu_find_in_page",
"resources:popup_menu_help",
"resources:popup_menu_history",
......@@ -58,6 +59,7 @@ source_set("popup_menu") {
"//ios/chrome/browser/find_in_page",
"//ios/chrome/browser/main:public",
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/policy",
"//ios/chrome/browser/policy:feature_flags",
"//ios/chrome/browser/reading_list",
"//ios/chrome/browser/search_engines",
......@@ -83,6 +85,7 @@ source_set("popup_menu") {
"//ios/chrome/browser/web:feature_flags",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/browser/window_activities",
"//ios/chrome/common/ui/colors",
"//ios/components/webui:url_constants",
"//ios/public/provider/chrome/browser",
"//ios/public/provider/chrome/browser/user_feedback",
......
......@@ -7,6 +7,8 @@ source_set("cells") {
sources = [
"popup_menu_navigation_item.h",
"popup_menu_navigation_item.mm",
"popup_menu_text_item.h",
"popup_menu_text_item.mm",
"popup_menu_tools_item.h",
"popup_menu_tools_item.mm",
]
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_POPUP_MENU_CELLS_POPUP_MENU_TEXT_ITEM_H_
#define IOS_CHROME_BROWSER_UI_POPUP_MENU_CELLS_POPUP_MENU_TEXT_ITEM_H_
#import "ios/chrome/browser/ui/popup_menu/public/cells/popup_menu_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_item.h"
// A non interactable textual item. The text wraps a leading image and
// description message.
@interface PopupMenuTextItem : TableViewItem <PopupMenuItem>
// The leading image name.
@property(nonatomic, copy) NSString* imageName;
// The string of the message.
@property(nonatomic, copy) NSString* message;
@end
// Associated cell for the PopupMenuTextItem.
@interface PopupMenuTextCell : TableViewCell
// Text label for the cell.
@property(nonatomic, strong) UILabel* messageLabel;
// The message of the item.
@property(nonatomic, strong) NSMutableAttributedString* messageAttributedString;
// After this is called, the cell is listening for the
// UIContentSizeCategoryDidChangeNotification notification and updates its font
// size to the new category.
- (void)registerForContentSizeUpdates;
@end
#endif // IOS_CHROME_BROWSER_UI_POPUP_MENU_CELLS_POPUP_MENU_TEXT_ITEM_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/popup_menu/cells/popup_menu_text_item.h"
#include <stdlib.h>
#include "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/popup_menu/public/popup_menu_ui_constants.h"
#import "ios/chrome/browser/ui/reading_list/number_badge_view.h"
#import "ios/chrome/browser/ui/reading_list/text_badge_view.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/common/material_timing.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
const CGFloat kCellHeight = 32;
const CGFloat kMargin = 15;
const CGFloat kTopMargin = 8;
const CGFloat kMaxHeight = 100;
NSMutableAttributedString* GetAttributedString(NSString* imageName,
NSString* message) {
// Add a space to have a distance with the leading icon.
NSString* fullText = [@" " stringByAppendingString:message];
NSDictionary* generalAttributes = @{
NSForegroundColorAttributeName : [UIColor colorNamed:kTextSecondaryColor],
NSFontAttributeName :
[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]
};
NSMutableAttributedString* attributedString =
[[NSMutableAttributedString alloc] initWithString:fullText
attributes:generalAttributes];
// Create the leading enterprise icon.
NSTextAttachment* attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:imageName];
// Making sure the image is well centered vertically relative to the text,
// and also that the image scales with the text size.
CGFloat height = attributedString.size.height;
CGFloat capHeight =
[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote].capHeight;
CGFloat verticalOffset = roundf(capHeight - height) / 2.f;
attachment.bounds = CGRectMake(0, verticalOffset, height, height);
NSAttributedString* attachmentString =
[NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attachmentString atIndex:0];
return attributedString;
}
} // namespace
@implementation PopupMenuTextItem
@synthesize actionIdentifier = _actionIdentifier;
- (instancetype)initWithType:(NSInteger)type {
self = [super initWithType:type];
if (self) {
self.cellClass = [PopupMenuTextCell class];
}
return self;
}
- (void)configureCell:(PopupMenuTextCell*)cell
withStyler:(ChromeTableViewStyler*)styler {
[super configureCell:cell withStyler:styler];
NSMutableAttributedString* StringOfCell =
GetAttributedString(self.imageName, self.message);
cell.messageAttributedString = StringOfCell;
cell.messageLabel.attributedText = StringOfCell;
}
#pragma mark - PopupMenuItem
- (CGSize)cellSizeForWidth:(CGFloat)width {
// TODO(crbug.com/828357): This should be done at the table view level.
static PopupMenuTextCell* cell;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cell = [[PopupMenuTextCell alloc] init];
[cell registerForContentSizeUpdates];
});
[self configureCell:cell withStyler:[[ChromeTableViewStyler alloc] init]];
cell.frame = CGRectMake(0, 0, width, kMaxHeight);
[cell setNeedsLayout];
[cell layoutIfNeeded];
return [cell systemLayoutSizeFittingSize:CGSizeMake(width, kMaxHeight)];
}
@end
#pragma mark - PopupMenuTextCell
@implementation PopupMenuTextCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.userInteractionEnabled = NO;
UIView* selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor =
[UIColor colorNamed:kTableViewRowHighlightColor];
self.selectedBackgroundView = selectedBackgroundView;
_messageLabel = [[UILabel alloc] init];
_messageLabel.numberOfLines = 0;
_messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
_messageLabel.adjustsFontForContentSizeCategory = YES;
[self.contentView addSubview:_messageLabel];
[NSLayoutConstraint activateConstraints:@[
[_messageLabel.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],
[self.contentView.heightAnchor
constraintGreaterThanOrEqualToConstant:kCellHeight],
]];
ApplyVisualConstraintsWithMetrics(
@[
@"H:|-(margin)-[label]-(>=margin)-|",
@"V:|-(>=topMargin)-[label]-(>=topMargin)-|"
],
@{
@"label" : self.messageLabel,
},
@{
@"margin" : @(kMargin),
@"topMargin" : @(kTopMargin),
});
// The height constraint is used to have something as small as possible when
// calculating the size of the prototype cell.
NSLayoutConstraint* heightConstraint =
[self.contentView.heightAnchor constraintEqualToConstant:kCellHeight];
heightConstraint.priority = UILayoutPriorityDefaultLow;
heightConstraint.active = YES;
}
return self;
}
- (void)registerForContentSizeUpdates {
// This is needed because if the cell is static (used for height),
// adjustsFontForContentSizeCategory isn't working.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(preferredContentSizeDidChange:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
#pragma mark - UIView
- (void)layoutSubviews {
[super layoutSubviews];
// Adjust the text label preferredMaxLayoutWidth when the parent's width
// changes, for instance on screen rotation.
CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds);
self.messageLabel.preferredMaxLayoutWidth = parentWidth - kMargin * 2;
// Re-layout with the new preferred width to allow the label to adjust its
// height.
[super layoutSubviews];
}
#pragma mark - UITableViewCell
- (void)prepareForReuse {
[super prepareForReuse];
self.userInteractionEnabled = NO;
self.accessibilityTraits &= ~UIAccessibilityTraitNotEnabled;
}
#pragma mark - Accessibility
- (NSString*)accessibilityLabel {
return self.messageAttributedString.string;
}
#pragma mark - Private
// Callback when the preferred Content Size change.
- (void)preferredContentSizeDidChange:(NSNotification*)notification {
self.messageLabel.attributedText = self.messageAttributedString;
}
@end
......@@ -203,6 +203,8 @@ using base::UserMetricsAction;
}));
break;
}
case PopupMenuActionEnterpriseInfoMessage:
break;
default:
NOTREACHED() << "Unexpected identifier";
break;
......
......@@ -74,5 +74,7 @@ extern NSString* const kToolsMenuQRCodeSearch;
extern NSString* const kToolsMenuCopiedImageSearch;
// Text Zoom item accessibility identifier.
extern NSString* const kToolsMenuTextZoom;
// Text Enterprise info item accessibility identifier.
extern NSString* const kTextMenuEnterpriseInfo;
#endif // IOS_CHROME_BROWSER_UI_POPUP_MENU_POPUP_MENU_CONSTANTS_H_
......@@ -45,3 +45,4 @@ NSString* const kToolsMenuIncognitoSearch = @"kToolsMenuIncognitoSearch";
NSString* const kToolsMenuQRCodeSearch = @"kToolsMenuQRCodeSearch";
NSString* const kToolsMenuCopiedImageSearch = @"kToolsMenuCopiedImageSearch";
NSString* const kToolsMenuTextZoom = @"kToolsMenuTextZoom";
NSString* const kTextMenuEnterpriseInfo = @"kTextMenuEnterpriseInfo";
......@@ -21,11 +21,13 @@
#include "components/prefs/pref_service.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "ios/chrome/browser/application_context.h"
#import "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/find_in_page/find_tab_helper.h"
#import "ios/chrome/browser/overlays/public/overlay_presenter.h"
#import "ios/chrome/browser/overlays/public/overlay_presenter_observer_bridge.h"
#include "ios/chrome/browser/policy/browser_policy_connector_ios.h"
#include "ios/chrome/browser/policy/policy_features.h"
#import "ios/chrome/browser/search_engines/search_engines_util.h"
#import "ios/chrome/browser/translate/chrome_ios_translate_client.h"
......@@ -36,6 +38,7 @@
#import "ios/chrome/browser/ui/list_model/list_model.h"
#import "ios/chrome/browser/ui/ntp_tile_views/ntp_tile_constants.h"
#import "ios/chrome/browser/ui/popup_menu/cells/popup_menu_navigation_item.h"
#import "ios/chrome/browser/ui/popup_menu/cells/popup_menu_text_item.h"
#import "ios/chrome/browser/ui/popup_menu/cells/popup_menu_tools_item.h"
#import "ios/chrome/browser/ui/popup_menu/popup_menu_constants.h"
#import "ios/chrome/browser/ui/popup_menu/public/cells/popup_menu_item.h"
......@@ -50,6 +53,7 @@
#import "ios/chrome/browser/web/font_size_tab_helper.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ios/components/webui/web_ui_url_constants.h"
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
......@@ -86,8 +90,23 @@ PopupMenuToolsItem* CreateTableViewItem(int titleID,
}
return item;
}
PopupMenuTextItem* CreateEnterpriseInfoItem(NSString* imageName,
NSString* message,
PopupMenuAction action,
NSString* accessibilityID) {
PopupMenuTextItem* item =
[[PopupMenuTextItem alloc] initWithType:kItemTypeEnumZero];
item.imageName = imageName;
item.message = message;
item.actionIdentifier = action;
item.accessibilityIdentifier = accessibilityID;
return item;
}
} // namespace
@interface PopupMenuMediator () <BookmarkModelBridgeObserver,
CRWWebStateObserver,
IOSLanguageDetectionTabHelperObserving,
......@@ -813,7 +832,18 @@ PopupMenuToolsItem* CreateTableViewItem(int titleID,
NSArray* collectionActions = [self collectionItems];
self.items = @[ tabActions, collectionActions, browserActions ];
if (base::FeatureList::IsEnabled(kEnableIOSManagedSettingsUI) &&
GetApplicationContext()
->GetBrowserPolicyConnector()
->HasMachineLevelPolicies()) {
// Show enterprise infomation when chrome is managed by policy and the
// settings UI flag is enabled.
NSArray* textActions = [self enterpriseInfoSection];
self.items =
@[ tabActions, collectionActions, browserActions, textActions ];
} else {
self.items = @[ tabActions, collectionActions, browserActions ];
}
}
- (NSArray<TableViewItem*>*)itemsForNewTab {
......@@ -997,6 +1027,16 @@ PopupMenuToolsItem* CreateTableViewItem(int titleID,
return @[ bookmarks, self.readingListItem, recentTabs, history, settings ];
}
// Creates the section for enterprise info.
- (NSArray<TableViewItem*>*)enterpriseInfoSection {
NSString* message = l10n_util::GetNSString(
IDS_IOS_ENTERPRISE_MANAGED_SETTING_DESC_WITHOUT_COMPANY_NAME);
TableViewItem* enterpriseInfoItem = CreateEnterpriseInfoItem(
@"popup_menu_enterprise_icon", message,
PopupMenuActionEnterpriseInfoMessage, kTextMenuEnterpriseInfo);
return @[ enterpriseInfoItem ];
}
// Returns the UserAgentType currently in use.
- (web::UserAgentType)userAgentType {
if (!self.webState)
......
......@@ -54,6 +54,8 @@ typedef NS_ENUM(NSInteger, PopupMenuAction) {
PopupMenuActionShowUpdatePasswordOptions,
PopupMenuActionShowSaveCardOptions,
PopupMenuActionShowTranslateOptions,
// Textual popup menu
PopupMenuActionEnterpriseInfoMessage,
};
// Protocol defining a popup item.
......
......@@ -232,3 +232,11 @@ imageset("popup_menu_text_zoom") {
"popup_menu_text_zoom.imageset/popup_menu_text_zoom@3x.png",
]
}
imageset("popup_menu_enterprise_icon") {
sources = [
"popup_menu_enterprise_icon.imageset/Contents.json",
"popup_menu_enterprise_icon.imageset/popup_menu_enterprise_icon@2x.png",
"popup_menu_enterprise_icon.imageset/popup_menu_enterprise_icon@3x.png",
]
}
{
"images": [
{
"idiom": "universal",
"scale": "2x",
"filename": "popup_menu_enterprise_icon@2x.png"
},
{
"idiom": "universal",
"scale": "3x",
"filename": "popup_menu_enterprise_icon@3x.png"
}
],
"info": {
"version": 1,
"author": "xcode"
}
}
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