Repository navigation
[ios, macos] Add to-rgba expression operator. - #11725
Conversation
| `rgba` | `+[UIColor colorWithRed:green:blue:alpha:]` | | ||
| <% } -%> | ||
| `to-rgba` | | | ||
| `to-rgba` | | `CAST(var, 'UIColor')` or `CAST(var, 'NSColor')` |
There was a problem hiding this comment.
Within this particular .ejs file, you can use the cocoaPrefix variable to switch between “UI” and “NS” as appropriate:
We don’t have the “Predicates and Expressions” guide set up this way yet, since the platform-specific content is so minimal in that guide.
| } | ||
| #if TARGET_OS_IPHONE | ||
| else if ([type isEqualToString:@"UIColor"]) { | ||
| return @[@"to-rgba", object]; |
There was a problem hiding this comment.
I had things backwards in #11011. to-string turns its argument into a string, but oddly enough, to-rgba turns a color into an array, not the other way around. So CAST(…, 'UIColor') would more or less correspond to the rgba operator, and the to-rgba operator would correspond to CAST(…, 'NSArray'). Both coercions would be useful.
6aabfc0 to
38623c8
Compare
1ec5
left a comment
There was a problem hiding this comment.
In addition to the comments below, remember to add a blurb to the iOS and macOS changelogs about the newly supported casting types.
| `to-boolean` | `boolValue` | | ||
| `to-color` | | | ||
| <% if (macOS) { -%> | ||
| `to-color` | | `CAST(var, 'NScolor')` |
There was a problem hiding this comment.
Typo: NSColor, not NScolor. Also consider using the cocoaPrefix variable so you don’t have to duplicate the row with an if statement.
| NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject]; | ||
| return [NSExpression expressionWithFormat:@"CAST(%@, 'NSString')", operand]; | ||
| } else if ([op isEqualToString:@"to-color"]) { | ||
| NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject]; |
There was a problem hiding this comment.
to-color can take multiple arguments, similar to to-number. I’m not a huge fan of mgl_numberWithFallbackValues:, so I won’t request an mgl_colorWithFallbackValues:. However, if there is more than one argument to this expression, we should at least fall back to MGL_FUNCTION() instead of losing all the fallback values in translation.
| } | ||
| #endif | ||
| else if ([type isEqualToString:@"NSArray"]) { | ||
| return @[@"to-rgba", object]; |
There was a problem hiding this comment.
to-rgba is only appropriate if the object is a color. If it’s a constant UIColor or NSColor, we can use to-rgba for sure. If it’s a different kind of constant value, such as an NSString, then we should raise an exception since that kind of conversion is currently unsupported. For non-constant expressions, we don’t know the resolved type for sure, so we can use to-rgba in that case.
| } | ||
| #endif | ||
| else if ([type isEqualToString:@"NSArray"]) { | ||
| return @[@"to-rgba", object]; |
There was a problem hiding this comment.
NSExpression is a bit aggressive in evaluating a constant in a cast even during parsing, so just initializing an NSExpression with a format string of CAST(%@, 'NSArray') would crash. However, a format string of CAST(noindex(%@), 'NSArray') should convert to the expected JSON without crashing.
80e63e8 to
40564fd
Compare
| XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); | ||
| } | ||
| { | ||
| NSExpression *expression = [NSExpression expressionWithFormat:@"CAST(noindex(x), 'NSArray')"]; |
There was a problem hiding this comment.
Developers won’t think to use noindex() for this purpose – I didn’t think of it until after much head-scratching. So let’s add a note to the “Predicates and Expressions” guide that points out the need to use noindex() when casting a constant expression to NSArray or NSColor/UIColor.
There was a problem hiding this comment.
noindex() is only required when using the operand to-rgba. I am adding the note for how to cast each:
* To cast a value to a color, use `CAST(key, 'UIColor')`.
* To cast a value to an rgba color, use `CAST(noindex(var), 'NSArray')`.
| green:[components[1].constantValue doubleValue] / 255.0 | ||
| blue:[components[2].constantValue doubleValue] / 255.0 | ||
| alpha:components.count == 3 ? [components[3].constantValue doubleValue] : 1.0]; | ||
| alpha:components.count == 3 ? 1.0 : [components[3].constantValue doubleValue]]; |
There was a problem hiding this comment.
Good catch! Let’s add a mention to the changelogs that this PR fixes an issue where certain colors were being misrepresented in NSExpressions obtained from MGLStyleLayer getters.
40564fd to
78cd0b2
Compare
There was a problem hiding this comment.
This doesn’t cast a value to a color, it casts a color to an array.
To cast an
NSColororUIColorobject to an array, useCAST(noindex(color), 'NSArray').
There was a problem hiding this comment.
To cast a value to a color, use
CAST(key, 'UIColor')on iOS andCAST(key, 'NSColor')on macOS.
78cd0b2 to
1f8d266
Compare
Fixes #11011