1// Copyright 2016 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/icons/chrome_icon.h"
6
7#import <CoreGraphics/CoreGraphics.h>
8
9#include "base/check.h"
10#include "ios/chrome/grit/ios_strings.h"
11#include "ui/base/l10n/l10n_util_mac.h"
12
13#if !defined(__has_feature) || !__has_feature(objc_arc)
14#error "This file requires ARC support."
15#endif
16
17namespace {
18
19NSString* AccessibilityLabelForIconNamed(NSString* name) {
20  if ([name isEqualToString:@"ic_arrow_back"])
21    return l10n_util::GetNSString(IDS_IOS_ICON_ARROW_BACK);
22  if ([name isEqualToString:@"ic_close"])
23    return l10n_util::GetNSString(IDS_IOS_ICON_CLOSE);
24  if ([name isEqualToString:@"ic_info"])
25    return l10n_util::GetNSString(IDS_IOS_ICON_INFO);
26  if ([name isEqualToString:@"ic_search"])
27    return l10n_util::GetNSString(IDS_IOS_ICON_SEARCH);
28  return nil;
29}
30
31UIImage* IconNamed(NSString* name) {
32  UIImage* image = [UIImage imageNamed:name];
33  DCHECK(image);
34  image.accessibilityIdentifier = name;
35  image.accessibilityLabel = AccessibilityLabelForIconNamed(name);
36  return image;
37}
38
39// Wraps -[UIImage imageFlippedForRightToLeftLayoutDirection] to also support
40// porting accessibility properties.
41// TODO(crbug.com/622543): remove this workaround if Apple fixes rdar://26962660
42UIImage* ImageFlippedForRightToLeftLayoutDirection(UIImage* image) {
43  UIImage* imageFlipped = [image imageFlippedForRightToLeftLayoutDirection];
44  imageFlipped.accessibilityIdentifier = image.accessibilityIdentifier;
45  imageFlipped.accessibilityLabel = image.accessibilityLabel;
46  return imageFlipped;
47}
48
49}  // namespace
50
51@implementation ChromeIcon
52
53+ (UIImage*)backIcon {
54  return ImageFlippedForRightToLeftLayoutDirection(IconNamed(@"ic_arrow_back"));
55}
56
57+ (UIImage*)closeIcon {
58  return IconNamed(@"ic_close");
59}
60
61+ (UIImage*)infoIcon {
62  return IconNamed(@"ic_info");
63}
64
65+ (UIImage*)searchIcon {
66  return IconNamed(@"ic_search");
67}
68
69+ (UIImage*)chevronIcon {
70  return ImageFlippedForRightToLeftLayoutDirection(
71      IconNamed(@"ic_chevron_right"));
72}
73
74+ (UIBarButtonItem*)templateBarButtonItemWithImage:(UIImage*)image
75                                            target:(id)target
76                                            action:(SEL)action {
77  UIImage* templateImage =
78      [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
79  UIBarButtonItem* barButtonItem =
80      [[UIBarButtonItem alloc] initWithImage:templateImage
81                                       style:UIBarButtonItemStylePlain
82                                      target:target
83                                      action:action];
84  [barButtonItem setAccessibilityIdentifier:image.accessibilityIdentifier];
85  [barButtonItem setAccessibilityLabel:image.accessibilityLabel];
86  return barButtonItem;
87}
88
89@end
90