Commit 46e00cb0 authored by Javier Ernesto Flores Robles's avatar Javier Ernesto Flores Robles Committed by Commit Bot

[iOS][Alert] Add style for actions

Improves the style of the actions to be like the UX mocks.

Bug: 951358, 951300
Change-Id: I535fd2ba711321d782d1ff0c5bb41781523aef88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1578745
Commit-Queue: Javier Ernesto Flores Robles <javierrobles@chromium.org>
Reviewed-by: default avatarKurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653552}
parent f91fbc16
...@@ -8,6 +8,8 @@ source_set("alert_view_controller") { ...@@ -8,6 +8,8 @@ source_set("alert_view_controller") {
"alert_view_controller.mm", "alert_view_controller.mm",
] ]
deps = [ deps = [
"//ios/chrome/browser/ui/elements",
"//ios/chrome/browser/ui/util",
"//ios/chrome/common/ui_util", "//ios/chrome/common/ui_util",
] ]
libs = [ "UIKit.framework" ] libs = [ "UIKit.framework" ]
......
...@@ -15,8 +15,12 @@ ...@@ -15,8 +15,12 @@
// The title for this action. // The title for this action.
@property(nonatomic, readonly) NSString* title; @property(nonatomic, readonly) NSString* title;
// The style for this action. Matches UIAlertAction style.
@property(nonatomic, readonly) UIAlertActionStyle style;
// Initializes an action with |title| and |handler|. // Initializes an action with |title| and |handler|.
+ (instancetype)actionWithTitle:(NSString*)title + (instancetype)actionWithTitle:(NSString*)title
style:(UIAlertActionStyle)style
handler:(void (^)(AlertAction* action))handler; handler:(void (^)(AlertAction* action))handler;
@end @end
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#import "ios/chrome/browser/ui/alert_view_controller/alert_view_controller.h" #import "ios/chrome/browser/ui/alert_view_controller/alert_view_controller.h"
#import "ios/chrome/browser/ui/elements/gray_highlight_button.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/common/ui_util/constraints_ui_util.h" #import "ios/chrome/common/ui_util/constraints_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -36,12 +38,21 @@ constexpr CGFloat kMessageInsetTop = 4; ...@@ -36,12 +38,21 @@ constexpr CGFloat kMessageInsetTop = 4;
constexpr CGFloat kMessageInsetLeading = 20; constexpr CGFloat kMessageInsetLeading = 20;
constexpr CGFloat kMessageInsetBottom = 20; constexpr CGFloat kMessageInsetBottom = 20;
constexpr CGFloat kMessageInsetTrailing = 20; constexpr CGFloat kMessageInsetTrailing = 20;
constexpr CGFloat kButtonInsetTop = 20;
constexpr CGFloat kButtonInsetLeading = 20;
constexpr CGFloat kButtonInsetBottom = 20;
constexpr CGFloat kButtonInsetTrailing = 20;
// Colors for the action buttons.
constexpr int kButtonTextDefaultColor = 0x0579ff;
constexpr int kButtonTextDestructiveColor = 0xdf322f;
} // namespace } // namespace
@interface AlertAction () @interface AlertAction ()
@property(nonatomic, readwrite) NSString* title; @property(nonatomic, readwrite) NSString* title;
@property(nonatomic, readwrite) UIAlertActionStyle style;
// The unique identifier for the actions created. // The unique identifier for the actions created.
@property(nonatomic) NSInteger uniqueIdentifier; @property(nonatomic) NSInteger uniqueIdentifier;
...@@ -54,12 +65,14 @@ constexpr CGFloat kMessageInsetTrailing = 20; ...@@ -54,12 +65,14 @@ constexpr CGFloat kMessageInsetTrailing = 20;
@implementation AlertAction @implementation AlertAction
+ (instancetype)actionWithTitle:(NSString*)title + (instancetype)actionWithTitle:(NSString*)title
style:(UIAlertActionStyle)style
handler:(void (^)(AlertAction* action))handler { handler:(void (^)(AlertAction* action))handler {
AlertAction* action = [[AlertAction alloc] init]; AlertAction* action = [[AlertAction alloc] init];
static NSInteger actionIdentifier = 0; static NSInteger actionIdentifier = 0;
action.uniqueIdentifier = ++actionIdentifier; action.uniqueIdentifier = ++actionIdentifier;
action.title = title; action.title = title;
action.handler = handler; action.handler = handler;
action.style = style;
return action; return action;
} }
...@@ -102,6 +115,7 @@ constexpr CGFloat kMessageInsetTrailing = 20; ...@@ -102,6 +115,7 @@ constexpr CGFloat kMessageInsetTrailing = 20;
[[UIColor blackColor] colorWithAlphaComponent:kBackgroundAlpha]; [[UIColor blackColor] colorWithAlphaComponent:kBackgroundAlpha];
self.contentView = [[UIView alloc] init]; self.contentView = [[UIView alloc] init];
self.contentView.clipsToBounds = YES;
self.contentView.backgroundColor = [UIColor whiteColor]; self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.cornerRadius = kCornerRadius; self.contentView.layer.cornerRadius = kCornerRadius;
self.contentView.layer.shadowOffset = self.contentView.layer.shadowOffset =
...@@ -194,23 +208,53 @@ constexpr CGFloat kMessageInsetTrailing = 20; ...@@ -194,23 +208,53 @@ constexpr CGFloat kMessageInsetTrailing = 20;
self.buttonAlertActionsDictionary = [[NSMutableDictionary alloc] init]; self.buttonAlertActionsDictionary = [[NSMutableDictionary alloc] init];
for (AlertAction* action in self.actions) { for (AlertAction* action in self.actions) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem]; GrayHighlightButton* button = [[GrayHighlightButton alloc] init];
UIFont* font = nil;
UIColor* textColor = nil;
if (action.style == UIAlertActionStyleDefault) {
font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
textColor = UIColorFromRGB(kButtonTextDefaultColor);
} else if (action.style == UIAlertActionStyleCancel) {
font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
textColor = UIColorFromRGB(kButtonTextDefaultColor);
} else { // Style is UIAlertActionStyleDestructive
font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
textColor = UIColorFromRGB(kButtonTextDestructiveColor);
}
[button.titleLabel setFont:font];
[button setTitleColor:textColor forState:UIControlStateNormal];
[button setTitle:action.title forState:UIControlStateNormal]; [button setTitle:action.title forState:UIControlStateNormal];
button.contentHorizontalAlignment = button.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter; UIControlContentHorizontalAlignmentCenter;
[button addTarget:self [button addTarget:self
action:@selector(didSelectActionForButton:) action:@selector(didSelectActionForButton:)
forControlEvents:UIControlEventTouchUpInside]; forControlEvents:UIControlEventTouchUpInside];
button.translatesAutoresizingMaskIntoConstraints = NO; button.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:button]; button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonInsetTop, kButtonInsetLeading,
[NSLayoutConstraint activateConstraints:@[ kButtonInsetBottom, kButtonInsetTrailing);
[button.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor], UIView* hairline = [[UIView alloc] init];
[button.leadingAnchor hairline.backgroundColor = [UIColor lightGrayColor];
constraintEqualToAnchor:self.contentView.leadingAnchor], hairline.translatesAutoresizingMaskIntoConstraints = NO;
]];
[stackView addArrangedSubview:button]; UIView* buttonContainer = [[UIView alloc] init];
[buttonContainer addSubview:button];
[buttonContainer addSubview:hairline];
buttonContainer.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addArrangedSubview:buttonContainer];
CGFloat pixelHeight = 1.0 / [UIScreen mainScreen].scale;
[hairline.heightAnchor constraintEqualToConstant:pixelHeight].active = YES;
AddSameConstraintsToSides(
hairline, buttonContainer,
(LayoutSides::kTrailing | LayoutSides::kTop | LayoutSides::kLeading));
AddSameConstraints(button, buttonContainer);
AddSameConstraintsToSides(buttonContainer, self.contentView,
LayoutSides::kTrailing | LayoutSides::kLeading);
button.tag = action.uniqueIdentifier; button.tag = action.uniqueIdentifier;
self.buttonAlertActionsDictionary[@(action.uniqueIdentifier)] = action; self.buttonAlertActionsDictionary[@(action.uniqueIdentifier)] = action;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
@interface SCAlertCoordinator () @interface SCAlertCoordinator ()
@property(nonatomic, strong) UIViewController* containerViewController; @property(nonatomic, strong) UIViewController* containerViewController;
@property(nonatomic, strong) UISwitch* blockAlertSwitch;
@end @end
@implementation SCAlertCoordinator @implementation SCAlertCoordinator
...@@ -50,17 +51,31 @@ ...@@ -50,17 +51,31 @@
action:@selector(showHTTPAuth) action:@selector(showHTTPAuth)
forControlEvents:UIControlEventTouchUpInside]; forControlEvents:UIControlEventTouchUpInside];
UIStackView* stack = [[UIStackView alloc] initWithArrangedSubviews:@[ UILabel* blockAlertsLabel = [[UILabel alloc] init];
alertButton, promptButton, confirmButton, authButton blockAlertsLabel.text = @"Show \"Block Alerts Button\"";
self.blockAlertSwitch = [[UISwitch alloc] init];
UIStackView* switchStack = [[UIStackView alloc]
initWithArrangedSubviews:@[ self.blockAlertSwitch, blockAlertsLabel ]];
switchStack.axis = UILayoutConstraintAxisHorizontal;
switchStack.spacing = 16;
switchStack.translatesAutoresizingMaskIntoConstraints = NO;
UIStackView* verticalStack = [[UIStackView alloc] initWithArrangedSubviews:@[
alertButton, promptButton, confirmButton, authButton, switchStack
]]; ]];
stack.axis = UILayoutConstraintAxisVertical; verticalStack.axis = UILayoutConstraintAxisVertical;
stack.spacing = 30; verticalStack.spacing = 30;
stack.translatesAutoresizingMaskIntoConstraints = NO; verticalStack.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:stack]; verticalStack.distribution = UIStackViewDistributionFillEqually;
[containerView addSubview:verticalStack];
[NSLayoutConstraint activateConstraints:@[ [NSLayoutConstraint activateConstraints:@[
[stack.centerXAnchor constraintEqualToAnchor:containerView.centerXAnchor], [verticalStack.centerXAnchor
[stack.centerYAnchor constraintEqualToAnchor:containerView.centerYAnchor], constraintEqualToAnchor:containerView.centerXAnchor],
[verticalStack.centerYAnchor
constraintEqualToAnchor:containerView.centerYAnchor],
]]; ]];
[self.baseViewController pushViewController:self.containerViewController [self.baseViewController pushViewController:self.containerViewController
animated:YES]; animated:YES];
...@@ -73,6 +88,7 @@ ...@@ -73,6 +88,7 @@
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
AlertAction* action = AlertAction* action =
[AlertAction actionWithTitle:@"OK" [AlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -92,6 +108,7 @@ ...@@ -92,6 +108,7 @@
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
AlertAction* OKAction = AlertAction* OKAction =
[AlertAction actionWithTitle:@"OK" [AlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -100,6 +117,7 @@ ...@@ -100,6 +117,7 @@
[alert addAction:OKAction]; [alert addAction:OKAction];
AlertAction* cancelAction = AlertAction* cancelAction =
[AlertAction actionWithTitle:@"Cancel" [AlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -116,6 +134,7 @@ ...@@ -116,6 +134,7 @@
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
AlertAction* OKAction = AlertAction* OKAction =
[AlertAction actionWithTitle:@"OK" [AlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -124,6 +143,7 @@ ...@@ -124,6 +143,7 @@
[alert addAction:OKAction]; [alert addAction:OKAction];
AlertAction* cancelAction = AlertAction* cancelAction =
[AlertAction actionWithTitle:@"Cancel" [AlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -148,6 +168,7 @@ ...@@ -148,6 +168,7 @@
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
AlertAction* OKAction = AlertAction* OKAction =
[AlertAction actionWithTitle:@"Sign In" [AlertAction actionWithTitle:@"Sign In"
style:UIAlertActionStyleDefault
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -156,6 +177,7 @@ ...@@ -156,6 +177,7 @@
[alert addAction:OKAction]; [alert addAction:OKAction];
AlertAction* cancelAction = AlertAction* cancelAction =
[AlertAction actionWithTitle:@"Cancel" [AlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
[weakSelf.containerViewController [weakSelf.containerViewController
dismissViewControllerAnimated:YES dismissViewControllerAnimated:YES
...@@ -166,6 +188,18 @@ ...@@ -166,6 +188,18 @@
} }
- (void)presentAlertViewController:(AlertViewController*)alertViewController { - (void)presentAlertViewController:(AlertViewController*)alertViewController {
if (self.blockAlertSwitch.isOn) {
__weak __typeof(self) weakSelf = self;
AlertAction* blockAction =
[AlertAction actionWithTitle:@"Block Dialogs"
style:UIAlertActionStyleDestructive
handler:^(AlertAction* action) {
[weakSelf.containerViewController
dismissViewControllerAnimated:YES
completion:nil];
}];
[alertViewController addAction:blockAction];
}
alertViewController.modalTransitionStyle = alertViewController.modalTransitionStyle =
UIModalTransitionStyleCrossDissolve; UIModalTransitionStyleCrossDissolve;
alertViewController.modalPresentationStyle = alertViewController.modalPresentationStyle =
......
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