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

Side by Side Diff: ios/chrome/browser/ui/toolbar/toolbar_controller.mm

Issue 2602903002: Factor iOS native image loading into a utility function. (Closed)
Patch Set: Fixed BUILD files. Created 3 years, 11 months 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
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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/chrome/browser/ui/toolbar/toolbar_controller.h" 5 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h"
6 6
7 #include <QuartzCore/QuartzCore.h> 7 #include <QuartzCore/QuartzCore.h>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
(...skipping 16 matching lines...) Expand all
27 #include "ios/chrome/browser/ui/toolbar/toolbar_resource_macros.h" 27 #include "ios/chrome/browser/ui/toolbar/toolbar_resource_macros.h"
28 #import "ios/chrome/browser/ui/toolbar/toolbar_tools_menu_button.h" 28 #import "ios/chrome/browser/ui/toolbar/toolbar_tools_menu_button.h"
29 #import "ios/chrome/browser/ui/toolbar/tools_menu_button_observer_bridge.h" 29 #import "ios/chrome/browser/ui/toolbar/tools_menu_button_observer_bridge.h"
30 #import "ios/chrome/browser/ui/tools_menu/tools_menu_context.h" 30 #import "ios/chrome/browser/ui/tools_menu/tools_menu_context.h"
31 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" 31 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h"
32 #import "ios/chrome/browser/ui/uikit_ui_util.h" 32 #import "ios/chrome/browser/ui/uikit_ui_util.h"
33 #import "ios/chrome/common/material_timing.h" 33 #import "ios/chrome/common/material_timing.h"
34 #include "ios/chrome/grit/ios_strings.h" 34 #include "ios/chrome/grit/ios_strings.h"
35 #include "ios/chrome/grit/ios_theme_resources.h" 35 #include "ios/chrome/grit/ios_theme_resources.h"
36 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h" 36 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
37 #include "ui/base/resource/resource_bundle.h"
38 37
39 using base::UserMetricsAction; 38 using base::UserMetricsAction;
40 using ios::material::TimingFunction; 39 using ios::material::TimingFunction;
41 40
42 // Animation key used for stack view transition animations 41 // Animation key used for stack view transition animations
43 NSString* const kToolbarTransitionAnimationKey = @"ToolbarTransitionAnimation"; 42 NSString* const kToolbarTransitionAnimationKey = @"ToolbarTransitionAnimation";
44 43
45 // Externed max tab count. 44 // Externed max tab count.
46 const NSInteger kStackButtonMaxTabCount = 99; 45 const NSInteger kStackButtonMaxTabCount = 99;
47 // Font sizes for the button containing the tab count 46 // Font sizes for the button containing the tab count
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 base::scoped_nsobject<UIButton> shareButton_; 215 base::scoped_nsobject<UIButton> shareButton_;
217 base::scoped_nsobject<NSArray> standardButtons_; 216 base::scoped_nsobject<NSArray> standardButtons_;
218 std::unique_ptr<ToolsMenuButtonObserverBridge> toolsMenuButtonObserverBridge_; 217 std::unique_ptr<ToolsMenuButtonObserverBridge> toolsMenuButtonObserverBridge_;
219 ToolbarControllerStyle style_; 218 ToolbarControllerStyle style_;
220 219
221 // The following is nil if not visible. 220 // The following is nil if not visible.
222 base::scoped_nsobject<ToolsPopupController> toolsPopupController_; 221 base::scoped_nsobject<ToolsPopupController> toolsPopupController_;
223 } 222 }
224 223
225 // Returns the background image that should be used for |style|. 224 // Returns the background image that should be used for |style|.
226 - (const gfx::Image&)getBackgroundImageForStyle:(ToolbarControllerStyle)style; 225 - (UIImage*)getBackgroundImageForStyle:(ToolbarControllerStyle)style;
227 226
228 // Whether the share button should be visible in the toolbar. 227 // Whether the share button should be visible in the toolbar.
229 - (BOOL)shareButtonShouldBeVisible; 228 - (BOOL)shareButtonShouldBeVisible;
230 229
231 // Update share button visibility and |standardButtons_| array. 230 // Update share button visibility and |standardButtons_| array.
232 - (void)updateStandardButtons; 231 - (void)updateStandardButtons;
233 232
234 // Returns an animation for |button| for a toolbar transition animation with 233 // Returns an animation for |button| for a toolbar transition animation with
235 // |style|. |button|'s frame will be interpolated between its layout in the 234 // |style|. |button|'s frame will be interpolated between its layout in the
236 // screen toolbar to the card's tab frame, and will be faded in for 235 // screen toolbar to the card's tab frame, and will be faded in for
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 kToolbarShareButtonIdentifier); 308 kToolbarShareButtonIdentifier);
310 [view_ addSubview:shareButton_]; 309 [view_ addSubview:shareButton_];
311 } 310 }
312 311
313 CGRect shadowFrame = kShadowViewFrame[idiom]; 312 CGRect shadowFrame = kShadowViewFrame[idiom];
314 shadowFrame.origin.y = CGRectGetMaxY(backgroundFrame); 313 shadowFrame.origin.y = CGRectGetMaxY(backgroundFrame);
315 shadowView_.reset([[UIImageView alloc] initWithFrame:shadowFrame]); 314 shadowView_.reset([[UIImageView alloc] initWithFrame:shadowFrame]);
316 [shadowView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 315 [shadowView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
317 [shadowView_ setUserInteractionEnabled:NO]; 316 [shadowView_ setUserInteractionEnabled:NO];
318 [view_ addSubview:shadowView_]; 317 [view_ addSubview:shadowView_];
319 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 318 [shadowView_ setImage:NativeImage(IDR_IOS_TOOLBAR_SHADOW)];
320 gfx::Image shadow = rb.GetNativeImageNamed(IDR_IOS_TOOLBAR_SHADOW);
321 [shadowView_ setImage:shadow.ToUIImage()];
322 319
323 if (idiom == IPHONE_IDIOM) { 320 if (idiom == IPHONE_IDIOM) {
324 // iPad omnibox does not expand to full bleed. 321 // iPad omnibox does not expand to full bleed.
325 CGRect fullBleedShadowFrame = kFullBleedShadowViewFrame; 322 CGRect fullBleedShadowFrame = kFullBleedShadowViewFrame;
326 fullBleedShadowFrame.origin.y = shadowFrame.origin.y; 323 fullBleedShadowFrame.origin.y = shadowFrame.origin.y;
327 fullBleedShadowView_.reset( 324 fullBleedShadowView_.reset(
328 [[UIImageView alloc] initWithFrame:fullBleedShadowFrame]); 325 [[UIImageView alloc] initWithFrame:fullBleedShadowFrame]);
329 [fullBleedShadowView_ 326 [fullBleedShadowView_
330 setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 327 setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
331 [fullBleedShadowView_ setUserInteractionEnabled:NO]; 328 [fullBleedShadowView_ setUserInteractionEnabled:NO];
332 [fullBleedShadowView_ setAlpha:0]; 329 [fullBleedShadowView_ setAlpha:0];
333 [view_ addSubview:fullBleedShadowView_]; 330 [view_ addSubview:fullBleedShadowView_];
334 gfx::Image fullBleedShadow = 331 [fullBleedShadowView_
335 rb.GetNativeImageNamed(IDR_IOS_TOOLBAR_SHADOW_FULL_BLEED); 332 setImage:NativeImage(IDR_IOS_TOOLBAR_SHADOW_FULL_BLEED)];
336 [fullBleedShadowView_ setImage:fullBleedShadow.ToUIImage()];
337 } 333 }
338 334
339 transitionLayers_.reset( 335 transitionLayers_.reset(
340 [[NSMutableArray alloc] initWithCapacity:kTransitionLayerCapacity]); 336 [[NSMutableArray alloc] initWithCapacity:kTransitionLayerCapacity]);
341 337
342 // UIImageViews do not default to userInteractionEnabled:YES. 338 // UIImageViews do not default to userInteractionEnabled:YES.
343 [view_ setUserInteractionEnabled:YES]; 339 [view_ setUserInteractionEnabled:YES];
344 [backgroundView_ setUserInteractionEnabled:YES]; 340 [backgroundView_ setUserInteractionEnabled:YES];
345 341
346 gfx::Image tile = [self getBackgroundImageForStyle:style]; 342 UIImage* tile = [self getBackgroundImageForStyle:style];
347 [[self backgroundView] 343 [[self backgroundView]
348 setImage:StretchableImageFromUIImage(tile.ToUIImage(), 0.0, 3.0)]; 344 setImage:StretchableImageFromUIImage(tile, 0.0, 3.0)];
349 345
350 if (idiom == IPHONE_IDIOM) { 346 if (idiom == IPHONE_IDIOM) {
351 stackButton_.reset( 347 stackButton_.reset(
352 [[ToolbarCenteredButton alloc] initWithFrame:stackButtonFrame]); 348 [[ToolbarCenteredButton alloc] initWithFrame:stackButtonFrame]);
353 [stackButton_ setTag:IDC_TOGGLE_TAB_SWITCHER]; 349 [stackButton_ setTag:IDC_TOGGLE_TAB_SWITCHER];
354 [[stackButton_ titleLabel] 350 [[stackButton_ titleLabel]
355 setFont:[self fontForSize:kFontSizeFewerThanTenTabs]]; 351 setFont:[self fontForSize:kFontSizeFewerThanTenTabs]];
356 [stackButton_ 352 [stackButton_
357 setTitleColor:[UIColor colorWithWhite:kStackButtonNormalColors[style_] 353 setTitleColor:[UIColor colorWithWhite:kStackButtonNormalColors[style_]
358 alpha:1.0] 354 alpha:1.0]
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 474
479 return YES; 475 return YES;
480 } 476 }
481 477
482 - (void)setShareButtonEnabled:(BOOL)enabled { 478 - (void)setShareButtonEnabled:(BOOL)enabled {
483 [shareButton_ setEnabled:enabled]; 479 [shareButton_ setEnabled:enabled];
484 } 480 }
485 481
486 - (UIImage*)imageForImageEnum:(int)imageEnum 482 - (UIImage*)imageForImageEnum:(int)imageEnum
487 forState:(ToolbarButtonUIState)state { 483 forState:(ToolbarButtonUIState)state {
488 int imageId = 484 int imageID =
489 [self imageIdForImageEnum:imageEnum style:[self style] forState:state]; 485 [self imageIdForImageEnum:imageEnum style:[self style] forState:state];
490 DCHECK(imageId); 486 return NativeReversableImage(
491 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 487 imageID, [self imageShouldFlipForRightToLeftLayoutDirection:imageEnum]);
492 gfx::Image tile = rb.GetNativeImageNamed(imageId);
493 UIImage* image = tile.ToUIImage();
494 return (UseRTLLayout() &&
495 [self imageShouldFlipForRightToLeftLayoutDirection:imageEnum])
496 ? [image imageFlippedForRightToLeftLayoutDirection]
497 : image;
498 } 488 }
499 489
500 - (int)imageEnumForButton:(UIButton*)button { 490 - (int)imageEnumForButton:(UIButton*)button {
501 if (button == stackButton_.get()) 491 if (button == stackButton_.get())
502 return ToolbarButtonNameStack; 492 return ToolbarButtonNameStack;
503 return NumberOfToolbarButtonNames; 493 return NumberOfToolbarButtonNames;
504 } 494 }
505 495
506 - (int)imageIdForImageEnum:(int)index 496 - (int)imageIdForImageEnum:(int)index
507 style:(ToolbarControllerStyle)style 497 style:(ToolbarControllerStyle)style
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 }]; 623 }];
634 // reset tabHistoryPopupController_ to prevent -applicationDidEnterBackground 624 // reset tabHistoryPopupController_ to prevent -applicationDidEnterBackground
635 // from posting another kMenuWillHideNotification. 625 // from posting another kMenuWillHideNotification.
636 toolsPopupController_.reset(); 626 toolsPopupController_.reset();
637 627
638 [[NSNotificationCenter defaultCenter] 628 [[NSNotificationCenter defaultCenter]
639 postNotificationName:kMenuWillHideNotification 629 postNotificationName:kMenuWillHideNotification
640 object:nil]; 630 object:nil];
641 } 631 }
642 632
643 - (const gfx::Image&)getBackgroundImageForStyle:(ToolbarControllerStyle)style { 633 - (UIImage*)getBackgroundImageForStyle:(ToolbarControllerStyle)style {
644 int backgroundImageId; 634 int backgroundImageID;
645 if (style == ToolbarControllerStyleLightMode) 635 if (style == ToolbarControllerStyleLightMode)
646 backgroundImageId = IDR_IOS_TOOLBAR_LIGHT_BACKGROUND; 636 backgroundImageID = IDR_IOS_TOOLBAR_LIGHT_BACKGROUND;
647 else 637 else
648 backgroundImageId = IDR_IOS_TOOLBAR_DARK_BACKGROUND; 638 backgroundImageID = IDR_IOS_TOOLBAR_DARK_BACKGROUND;
649 639
650 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 640 return NativeImage(backgroundImageID);
651 return rb.GetNativeImageNamed(backgroundImageId);
652 } 641 }
653 642
654 - (CGRect)specificControlsArea { 643 - (CGRect)specificControlsArea {
655 // Return the rect to the leading side of the leading-most trailing control. 644 // Return the rect to the leading side of the leading-most trailing control.
656 UIView* trailingControl = toolsMenuButton_; 645 UIView* trailingControl = toolsMenuButton_;
657 if (!IsIPadIdiom()) 646 if (!IsIPadIdiom())
658 trailingControl = stackButton_; 647 trailingControl = stackButton_;
659 if ([self shareButtonShouldBeVisible]) 648 if ([self shareButtonShouldBeVisible])
660 trailingControl = shareButton_; 649 trailingControl = shareButton_;
661 LayoutRect trailing = 650 LayoutRect trailing =
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 #pragma mark - 1045 #pragma mark -
1057 #pragma mark PopupMenuDelegate methods. 1046 #pragma mark PopupMenuDelegate methods.
1058 1047
1059 - (void)dismissPopupMenu:(PopupMenuController*)controller { 1048 - (void)dismissPopupMenu:(PopupMenuController*)controller {
1060 if ([controller isKindOfClass:[ToolsPopupController class]] && 1049 if ([controller isKindOfClass:[ToolsPopupController class]] &&
1061 (ToolsPopupController*)controller == toolsPopupController_) 1050 (ToolsPopupController*)controller == toolsPopupController_)
1062 [self dismissToolsMenuPopup]; 1051 [self dismissToolsMenuPopup];
1063 } 1052 }
1064 1053
1065 @end 1054 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698