Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_overlay_view.mm

Issue 2588733002: Upstream Chrome on iOS source code [9/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_overlay_view.h"
6
7 #import "base/mac/scoped_nsobject.h"
8 #include "base/metrics/user_metrics.h"
9 #include "base/metrics/user_metrics_action.h"
10 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
11 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
12 #include "ios/chrome/browser/ui/commands/ios_command_ids.h"
13 #import "ios/chrome/browser/ui/commands/show_signin_command.h"
14 #import "ios/chrome/browser/ui/material_components/activity_indicator.h"
15 #import "ios/chrome/browser/ui/sync/sync_util.h"
16 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_model.h"
17 #import "ios/chrome/browser/ui/uikit_ui_util.h"
18 #include "ios/chrome/grit/ios_chromium_strings.h"
19 #include "ios/chrome/grit/ios_strings.h"
20 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
21 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato r/src/MaterialActivityIndicator.h"
22 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h"
23 #import "ios/third_party/material_components_ios/src/components/ShadowElevations /src/MaterialShadowElevations.h"
24 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 TabSwitcherPanelOverlayType PanelOverlayTypeFromSignInPanelsType(
28 TabSwitcherSignInPanelsType signInPanelType) {
29 switch (signInPanelType) {
30 case TabSwitcherSignInPanelsType::PANEL_USER_SIGNED_OUT:
31 return TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_OUT;
32 case TabSwitcherSignInPanelsType::PANEL_USER_SIGNED_IN_SYNC_OFF:
33 return TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_IN_SYNC_OFF;
34 case TabSwitcherSignInPanelsType::PANEL_USER_SIGNED_IN_SYNC_IN_PROGRESS:
35 return TabSwitcherPanelOverlayType::
36 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_IN_PROGRESS;
37 case TabSwitcherSignInPanelsType::PANEL_USER_SIGNED_IN_SYNC_ON_NO_SESSIONS:
38 return TabSwitcherPanelOverlayType::
39 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_ON_NO_SESSIONS;
40 case TabSwitcherSignInPanelsType::NO_PANEL:
41 return TabSwitcherPanelOverlayType::OVERLAY_PANEL_EMPTY;
42 }
43 }
44
45 namespace {
46 const CGFloat kContainerOriginYOffset = -58.0;
47 const CGFloat kContainerWidth = 400.0;
48 const CGFloat kTitleMinimumLineHeight = 32.0;
49 const CGFloat kSubtitleMinimunLineHeight = 24.0;
50 }
51
52 @interface TabSwitcherPanelOverlayView ()
53
54 // Updates the texts of labels and button according to the current
55 // |overlayType|.
56 - (void)updateText;
57 // Updates the button target and tag according to the current |overlayType|.
58 - (void)updateButtonTarget;
59 // Sends a SignIn chrome command.
60 - (void)showSignIn;
61
62 @end
63
64 @implementation TabSwitcherPanelOverlayView {
65 ios::ChromeBrowserState* _browserState; // Weak.
66 base::scoped_nsobject<UIView> _container;
67 base::scoped_nsobject<UILabel> _titleLabel;
68 base::scoped_nsobject<UILabel> _subtitleLabel;
69 base::scoped_nsobject<MDCButton> _textButton;
70 base::scoped_nsobject<MDCButton> _floatingButton;
71 base::scoped_nsobject<MDCActivityIndicator> _activityIndicator;
72 std::string _recordedMetricString;
73 }
74
75 @synthesize overlayType = _overlayType;
76
77 - (instancetype)initWithFrame:(CGRect)frame
78 browserState:(ios::ChromeBrowserState*)browserState {
79 self = [super initWithFrame:frame];
80 if (self) {
81 _browserState = browserState;
82 // Create and add container. Will be vertically and horizontally centered.
83 _container.reset([[UIView alloc] initWithFrame:CGRectZero]);
84 [_container setTranslatesAutoresizingMaskIntoConstraints:NO];
85 [self addSubview:_container];
86
87 // Create and add title label to the container.
88 _titleLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]);
89 [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
90 [_titleLabel setFont:[MDCTypography headlineFont]];
91 [_titleLabel
92 setTextColor:[UIColor
93 colorWithWhite:1
94 alpha:[MDCTypography headlineFontOpacity]]];
95 [_titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
96 [_titleLabel setNumberOfLines:0];
97 [_titleLabel setTextAlignment:NSTextAlignmentCenter];
98 [_container addSubview:_titleLabel];
99
100 // Create and add subtitle label to the container.
101 _subtitleLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]);
102 [_subtitleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
103 [_subtitleLabel setFont:[MDCTypography subheadFont]];
104 [_subtitleLabel
105 setTextColor:[UIColor
106 colorWithWhite:1
107 alpha:[MDCTypography display1FontOpacity]]];
108 [_subtitleLabel setNumberOfLines:0];
109 [_container addSubview:_subtitleLabel];
110
111 // Create and add button to the container.
112 _textButton.reset([[MDCRaisedButton alloc] init]);
113 [_textButton setElevation:MDCShadowElevationNone
114 forState:UIControlStateNormal];
115 MDCPalette* buttonPalette = [MDCPalette cr_bluePalette];
116 [_textButton
117 setInkColor:[[buttonPalette tint300] colorWithAlphaComponent:0.5f]];
118 [_textButton setBackgroundColor:[buttonPalette tint500]
119 forState:UIControlStateNormal];
120 [_textButton setBackgroundColor:[UIColor colorWithWhite:0.8f alpha:1.0f]
121 forState:UIControlStateDisabled];
122 [_textButton setTranslatesAutoresizingMaskIntoConstraints:NO];
123 [[_textButton imageView] setTintColor:[UIColor whiteColor]];
124 [_textButton setCustomTitleColor:[UIColor whiteColor]];
125 [_container addSubview:_textButton];
126
127 // Create and add floatingButton to the container.
128 _floatingButton.reset([[MDCFloatingButton alloc] init]);
129 [_floatingButton setTranslatesAutoresizingMaskIntoConstraints:NO];
130 [[_floatingButton imageView] setTintColor:[UIColor whiteColor]];
131 [_container addSubview:_floatingButton];
132
133 // Create and add activity indicator to the container.
134 _activityIndicator.reset(
135 [[MDCActivityIndicator alloc] initWithFrame:CGRectZero]);
136 [_activityIndicator setCycleColors:ActivityIndicatorBrandedCycleColors()];
137 [_activityIndicator setTranslatesAutoresizingMaskIntoConstraints:NO];
138 [_container addSubview:_activityIndicator];
139
140 // Set constraints on all of the container's subviews.
141 AddSameCenterXConstraint(_container, _titleLabel);
142 AddSameCenterXConstraint(_container, _subtitleLabel);
143 NSDictionary* viewsDictionary = @{
144 @"title" : _titleLabel.get(),
145 @"subtitle" : _subtitleLabel.get(),
146 @"button" : _textButton.get(),
147 @"floatingButton" : _floatingButton.get(),
148 @"activityIndicator" : _activityIndicator.get(),
149 };
150 AddSameCenterXConstraint(_container, _textButton);
151 AddSameCenterXConstraint(_container, _floatingButton);
152 AddSameCenterXConstraint(_container, _activityIndicator);
153 NSArray* constraints = @[
154 @"V:|-0-[title]-12-[subtitle]-48-[button]-0-|",
155 @"V:[subtitle]-35-[floatingButton(==48)]-0-|",
156 @"V:[subtitle]-24-[activityIndicator]", @"H:|-[title]-|",
157 @"H:|-[subtitle]-|", @"H:[button(==180)]", @"H:[floatingButton(==48)]"
158 ];
159 ApplyVisualConstraints(constraints, viewsDictionary, _container);
160
161 // Sets the container's width relative to the parent.
162 ApplyVisualConstraintsWithMetrics(
163 @[
164 @"H:|-(>=0)-[container(==containerWidth@999)]-(>=0)-|",
165 ],
166 @{ @"container" : _container.get() },
167 @{ @"containerWidth" : @(kContainerWidth) }, self);
168 }
169 return self;
170 }
171
172 - (void)layoutSubviews {
173 [super layoutSubviews];
174 CGRect containerFrame = [_container frame];
175 containerFrame.origin.x =
176 (self.frame.size.width - containerFrame.size.width) / 2;
177 containerFrame.origin.y =
178 (self.frame.size.height - containerFrame.size.height) / 2 +
179 kContainerOriginYOffset;
180 [_container setFrame:containerFrame];
181 }
182
183 - (void)setOverlayType:(TabSwitcherPanelOverlayType)overlayType {
184 _overlayType = overlayType;
185 [self updateText];
186 [self updateButtonTarget];
187 }
188
189 #pragma mark - Private
190
191 - (void)updateText {
192 NSMutableAttributedString* titleString = nil;
193 NSMutableAttributedString* subtitleString = nil;
194
195 NSString* buttonTitle = nil;
196 UIImage* buttonImage = nil;
197 NSString* buttonAccessibilityLabel = nil;
198 UIColor* floatingButtonNormalBackgroundColor = nil;
199 UIColor* floatingButtonDisabledBackgroundColor = nil;
200 UIColor* floatingButtonInkColor = nil;
201 BOOL spinnerIsHidden = YES;
202 switch (self.overlayType) {
203 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_EMPTY:
204 break;
205 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_OUT:
206 titleString = [[[NSMutableAttributedString alloc]
207 initWithString:l10n_util::GetNSString(
208 IDS_IOS_TAB_SWITCHER_SIGN_IN_ACCOUNT_TITLE)]
209 autorelease];
210 subtitleString = [[[NSMutableAttributedString alloc]
211 initWithString:l10n_util::GetNSString(
212 IDS_IOS_TAB_SWITCHER_SIGN_IN_ACCOUNT_PROMO)]
213 autorelease];
214 buttonTitle =
215 l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_SIGN_IN_ACCOUNT_BUTTON);
216 break;
217 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_IN_SYNC_OFF:
218 titleString = [[[NSMutableAttributedString alloc]
219 initWithString:l10n_util::GetNSString(
220 IDS_IOS_TAB_SWITCHER_ENABLE_SYNC_TITLE)]
221 autorelease];
222 subtitleString = [[[NSMutableAttributedString alloc]
223 initWithString:l10n_util::GetNSString(
224 IDS_IOS_TAB_SWITCHER_SYNC_IS_OFF)] autorelease];
225 buttonTitle =
226 l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_ENABLE_SYNC_BUTTON);
227 break;
228 case TabSwitcherPanelOverlayType::
229 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_ON_NO_SESSIONS:
230 titleString = [[[NSMutableAttributedString alloc]
231 initWithString:l10n_util::GetNSString(
232 IDS_IOS_TAB_SWITCHER_NO_TABS_TO_SYNC_PROMO)]
233 autorelease];
234 subtitleString = [[[NSMutableAttributedString alloc]
235 initWithString:l10n_util::GetNSString(
236 IDS_IOS_OPEN_TABS_NO_SESSION_INSTRUCTIONS)]
237 autorelease];
238 break;
239 case TabSwitcherPanelOverlayType::
240 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_IN_PROGRESS:
241 titleString = [[[NSMutableAttributedString alloc]
242 initWithString:l10n_util::GetNSString(
243 IDS_IOS_TAB_SWITCHER_SYNC_IN_PROGRESS_PROMO)]
244 autorelease];
245 subtitleString = [[[NSMutableAttributedString alloc]
246 initWithString:l10n_util::GetNSString(
247 IDS_IOS_TAB_SWITCHER_SYNC_IS_OFF)] autorelease];
248 spinnerIsHidden = NO;
249 break;
250 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_NO_OPEN_TABS:
251 titleString = [[[NSMutableAttributedString alloc]
252 initWithString:
253 l10n_util::GetNSString(
254 IDS_IOS_TAB_SWITCHER_NO_LOCAL_NON_INCOGNITO_TABS_TITLE)]
255 autorelease];
256 subtitleString = [[[NSMutableAttributedString alloc]
257 initWithString:l10n_util::GetNSString(
258 IDS_IOS_TAB_SWITCHER_NO_LOCAL_NON_INCOGNITO_TABS)]
259 autorelease];
260 buttonImage = [UIImage imageNamed:@"tabswitcher_new_tab_fab"];
261 buttonImage = [buttonImage
262 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
263 floatingButtonInkColor =
264 [[[MDCPalette cr_bluePalette] tint300] colorWithAlphaComponent:0.5f];
265 floatingButtonNormalBackgroundColor =
266 [[MDCPalette cr_bluePalette] tint500];
267 floatingButtonDisabledBackgroundColor =
268 [UIColor colorWithWhite:0.8f alpha:1.0f];
269 buttonAccessibilityLabel =
270 l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_CREATE_NEW_TAB);
271 break;
272 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_NO_INCOGNITO_TABS:
273 titleString = [[[NSMutableAttributedString alloc]
274 initWithString:
275 l10n_util::GetNSString(
276 IDS_IOS_TAB_SWITCHER_NO_LOCAL_INCOGNITO_TABS_PROMO)]
277 autorelease];
278 subtitleString = [[[NSMutableAttributedString alloc]
279 initWithString:l10n_util::GetNSString(
280 IDS_IOS_TAB_SWITCHER_NO_LOCAL_INCOGNITO_TABS)]
281 autorelease];
282 buttonImage = [UIImage imageNamed:@"tabswitcher_new_tab_fab"];
283 buttonImage = [buttonImage
284 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
285
286 floatingButtonInkColor =
287 [[[MDCPalette greyPalette] tint300] colorWithAlphaComponent:0.25f];
288 floatingButtonNormalBackgroundColor = [[MDCPalette greyPalette] tint500];
289 floatingButtonDisabledBackgroundColor =
290 [UIColor colorWithWhite:0.8f alpha:1.0f];
291 buttonAccessibilityLabel =
292 l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_CREATE_NEW_INCOGNITO_TAB);
293 break;
294 };
295
296 base::scoped_nsobject<NSMutableParagraphStyle> titleStyle(
297 [[NSMutableParagraphStyle alloc] init]);
298 [titleStyle setMinimumLineHeight:kTitleMinimumLineHeight];
299 [titleStyle setAlignment:NSTextAlignmentCenter];
300 [titleStyle setLineBreakMode:NSLineBreakByWordWrapping];
301 [titleString addAttribute:NSParagraphStyleAttributeName
302 value:titleStyle
303 range:NSMakeRange(0, [titleString length])];
304 [_titleLabel setAttributedText:titleString];
305
306 base::scoped_nsobject<NSMutableParagraphStyle> subtitleStyle(
307 [[NSMutableParagraphStyle alloc] init]);
308 [subtitleStyle setMinimumLineHeight:kSubtitleMinimunLineHeight];
309 [subtitleStyle setAlignment:NSTextAlignmentCenter];
310 [subtitleStyle setLineBreakMode:NSLineBreakByWordWrapping];
311 [subtitleString addAttribute:NSParagraphStyleAttributeName
312 value:subtitleStyle
313 range:NSMakeRange(0, [subtitleString length])];
314 [_subtitleLabel setAttributedText:subtitleString];
315
316 [_textButton setTitle:buttonTitle forState:UIControlStateNormal];
317 [_textButton setImage:buttonImage forState:UIControlStateNormal];
318 [_floatingButton setTitle:buttonTitle forState:UIControlStateNormal];
319 [_floatingButton setImage:buttonImage forState:UIControlStateNormal];
320 [_floatingButton setInkColor:floatingButtonInkColor];
321 [_floatingButton setBackgroundColor:floatingButtonNormalBackgroundColor
322 forState:UIControlStateNormal];
323 [_floatingButton setBackgroundColor:floatingButtonDisabledBackgroundColor
324 forState:UIControlStateDisabled];
325
326 [_floatingButton setAccessibilityLabel:buttonAccessibilityLabel];
327 [_activityIndicator setHidden:spinnerIsHidden];
328 if (spinnerIsHidden) {
329 [_activityIndicator stopAnimating];
330 } else {
331 [_activityIndicator startAnimating];
332 }
333 }
334
335 - (void)updateButtonTarget {
336 NSInteger tag = 0;
337 SEL selector = nil;
338 _recordedMetricString = "";
339
340 BOOL shouldShowTextButton = YES;
341 BOOL shouldShowFloatingButton = NO;
342 switch (self.overlayType) {
343 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_EMPTY:
344 shouldShowTextButton = NO;
345 break;
346 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_OUT:
347 selector = @selector(showSignIn);
348 _recordedMetricString = "MobileTabSwitcherSignIn";
349 break;
350 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_SIGNED_IN_SYNC_OFF:
351 selector = @selector(showSyncSettings);
352 _recordedMetricString = "MobileTabSwitcherEnableSync";
353 break;
354 case TabSwitcherPanelOverlayType::
355 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_ON_NO_SESSIONS:
356 shouldShowTextButton = NO;
357 break;
358 case TabSwitcherPanelOverlayType::
359 OVERLAY_PANEL_USER_SIGNED_IN_SYNC_IN_PROGRESS:
360 shouldShowTextButton = NO;
361 break;
362 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_NO_OPEN_TABS:
363 tag = IDC_NEW_TAB;
364 selector = @selector(rootViewControllerChromeCommand:);
365 _recordedMetricString = "MobileTabSwitcherCreateNonIncognitoTab";
366 shouldShowTextButton = NO;
367 shouldShowFloatingButton = YES;
368 break;
369 case TabSwitcherPanelOverlayType::OVERLAY_PANEL_USER_NO_INCOGNITO_TABS:
370 tag = IDC_NEW_INCOGNITO_TAB;
371 selector = @selector(rootViewControllerChromeCommand:);
372 _recordedMetricString = "MobileTabSwitcherCreateIncognitoTab";
373 shouldShowTextButton = NO;
374 shouldShowFloatingButton = YES;
375 break;
376 }
377
378 [_textButton setTag:tag];
379 [_textButton addTarget:self
380 action:selector
381 forControlEvents:UIControlEventTouchUpInside];
382 [_textButton addTarget:self
383 action:@selector(recordMetrics)
384 forControlEvents:UIControlEventTouchUpInside];
385 [_textButton setHidden:!shouldShowTextButton];
386 [_floatingButton setTag:tag];
387 [_floatingButton addTarget:self
388 action:selector
389 forControlEvents:UIControlEventTouchUpInside];
390 [_floatingButton addTarget:self
391 action:@selector(recordMetrics)
392 forControlEvents:UIControlEventTouchUpInside];
393 [_floatingButton setHidden:!shouldShowFloatingButton];
394 }
395
396 - (void)showSignIn {
397 base::RecordAction(base::UserMetricsAction("Signin_Signin_FromTabSwitcher"));
398 base::scoped_nsobject<ShowSigninCommand> command([[ShowSigninCommand alloc]
399 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN
400 signInAccessPoint:signin_metrics::AccessPoint::
401 ACCESS_POINT_TAB_SWITCHER]);
402 [self chromeExecuteCommand:command];
403 }
404
405 - (void)showSyncSettings {
406 [self chromeExecuteCommand:ios_internal::sync::GetSyncCommandForBrowserState(
407 _browserState)];
408 }
409
410 - (void)rootViewControllerChromeCommand:(id)command {
411 [self chromeExecuteCommand:command];
412 }
413
414 - (void)recordMetrics {
415 if (!_recordedMetricString.length())
416 return;
417 base::RecordAction(base::UserMetricsAction(_recordedMetricString.c_str()));
418 }
419
420 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698