Commit bc3a74d7 authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Adds support for asset catalog colorsets.

Adds an example use of colorsets in the settings table view cell
catalog.

Adds debug code to DCHECK() that named colors are present in the asset
catalog.

BUG=974255

Change-Id: I870a8b5c4f5af74d7839d685d46fb1c79ebfbe0d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1662068
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672539}
parent 5a2cccc8
......@@ -134,6 +134,12 @@ template("appiconset") {
asset_type = "appiconset"
}
}
template("colorset") {
asset_catalog(target_name) {
forward_variables_from(invoker, "*", [ "asset_type" ])
asset_type = "colorset"
}
}
template("imageset") {
asset_catalog(target_name) {
forward_variables_from(invoker, "*", [ "asset_type" ])
......
......@@ -18,9 +18,34 @@ namespace {
#if !defined(NDEBUG)
// Swizzles [UIColor colorNamed:] to trigger a DCHECK if an invalid color is
// attempted to be loaded.
void SwizzleUIColorColorNamed() {
// The original implementation of [UIColor colorNamed:].
// Called by the new implementation.
static IMP originalImp;
IMP* originalImpPtr = &originalImp;
id swizzleBlock = ^(id self, NSString* colorName) {
// Call the original [UIColor colorNamed:] method.
UIColor* (*imp)(id, SEL, id) =
(UIColor * (*)(id, SEL, id)) * originalImpPtr;
Class aClass = objc_getClass("UIColor");
UIColor* color = imp(aClass, @selector(colorNamed:), colorName);
DCHECK(color) << "Missing color: " << base::SysNSStringToUTF8(colorName);
return color;
};
Method method = class_getClassMethod([UIColor class], @selector(colorNamed:));
DCHECK(method);
IMP blockImp = imp_implementationWithBlock(swizzleBlock);
originalImp = method_setImplementation(method, blockImp);
}
// Swizzles [UIImage imageNamed:] to trigger a DCHECK if an invalid image is
// attempted to be loaded.
void swizzleUIImageImageNamed() {
void SwizzleUIImageImageNamed() {
// Retained by the swizzle block.
NSMutableSet* whiteList = [NSMutableSet set];
......@@ -61,7 +86,7 @@ void swizzleUIImageImageNamed() {
originalImp = method_setImplementation(method, blockImp);
}
#endif // !defined(NDEBUG) && TARGET_IPHONE_SIMULATOR
#endif // !defined(NDEBUG)
} // namespace
......@@ -75,8 +100,9 @@ void swizzleUIImageImageNamed() {
#endif
#if !defined(NDEBUG)
// Enable the detection of missing image assets.
swizzleUIImageImageNamed();
// Enable the detection of missing assets.
SwizzleUIColorColorNamed();
SwizzleUIImageImageNamed();
#endif
}
......
......@@ -80,6 +80,7 @@ source_set("settings") {
"resources:settings_addresses",
"resources:settings_article_suggestions",
"resources:settings_bandwidth",
"resources:settings_catalog_example_text",
"resources:settings_content_settings",
"resources:settings_debug",
"resources:settings_error",
......
......@@ -4,6 +4,12 @@
import("//build/config/ios/asset_catalog.gni")
colorset("settings_catalog_example_text") {
sources = [
"settings_catalog_example_text.colorset/Contents.json",
]
}
imageset("app_icon_placeholder") {
sources = [
"app_icon_placeholder.imageset/Contents.json",
......
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "universal",
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.000",
"blue" : "0.000",
"green" : "0.000",
"alpha" : "1.000"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "1.000",
"blue" : "1.000",
"green" : "1.000",
"alpha" : "1.000"
}
}
}
]
}
......@@ -192,7 +192,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
textActionButtonColorItem.text = @"Hello, you should do something.";
textActionButtonColorItem.disableButtonIntrinsicWidth = YES;
textActionButtonColorItem.buttonBackgroundColor = [UIColor lightGrayColor];
textActionButtonColorItem.buttonTextColor = [UIColor greenColor];
textActionButtonColorItem.buttonTextColor =
[UIColor colorNamed:@"settings_catalog_example_text"];
textActionButtonColorItem.buttonText = @"Do something, different Colors";
[model addItem:textActionButtonColorItem
toSectionWithIdentifier:SectionIdentifierText];
......
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