blob: 8682485c2165a85693823f12a693e5ecefeba9fc [file] [log] [blame]
// Copyright 2016 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/icons/chrome_icon.h"
#import <CoreGraphics/CoreGraphics.h>
#include "base/logging.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
NSString* AccessibilityLabelForIconNamed(NSString* name) {
if ([name isEqualToString:@"ic_arrow_back"])
return l10n_util::GetNSString(IDS_IOS_ICON_ARROW_BACK);
if ([name isEqualToString:@"ic_close"])
return l10n_util::GetNSString(IDS_IOS_ICON_CLOSE);
if ([name isEqualToString:@"ic_info"])
return l10n_util::GetNSString(IDS_IOS_ICON_INFO);
if ([name isEqualToString:@"ic_search"])
return l10n_util::GetNSString(IDS_IOS_ICON_SEARCH);
return nil;
}
UIImage* IconNamed(NSString* name) {
UIImage* image = [UIImage imageNamed:name];
DCHECK(image);
image.accessibilityIdentifier = name;
image.accessibilityLabel = AccessibilityLabelForIconNamed(name);
return image;
}
// Wraps -[UIImage imageFlippedForRightToLeftLayoutDirection] to also support
// porting accessibility properties.
// TODO(crbug.com/622543): remove this workaround if Apple fixes rdar://26962660
UIImage* ImageFlippedForRightToLeftLayoutDirection(UIImage* image) {
UIImage* imageFlipped = [image imageFlippedForRightToLeftLayoutDirection];
imageFlipped.accessibilityIdentifier = image.accessibilityIdentifier;
imageFlipped.accessibilityLabel = image.accessibilityLabel;
return imageFlipped;
}
} // namespace
@implementation ChromeIcon
+ (UIImage*)backIcon {
return ImageFlippedForRightToLeftLayoutDirection(IconNamed(@"ic_arrow_back"));
}
+ (UIImage*)closeIcon {
return IconNamed(@"ic_close");
}
+ (UIImage*)infoIcon {
return IconNamed(@"ic_info");
}
+ (UIImage*)searchIcon {
return IconNamed(@"ic_search");
}
+ (UIImage*)chevronIcon {
return ImageFlippedForRightToLeftLayoutDirection(
IconNamed(@"ic_chevron_right"));
}
+ (UIBarButtonItem*)templateBarButtonItemWithImage:(UIImage*)image
target:(id)target
action:(SEL)action {
UIImage* templateImage =
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIBarButtonItem* barButtonItem =
[[UIBarButtonItem alloc] initWithImage:templateImage
style:UIBarButtonItemStylePlain
target:target
action:action];
[barButtonItem setAccessibilityIdentifier:image.accessibilityIdentifier];
[barButtonItem setAccessibilityLabel:image.accessibilityLabel];
return barButtonItem;
}
@end