-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support textDecorationLine on <Text> components (#709) #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
12ac2cb
5de9c8e
b4a8fe2
bdc0a98
c4b93d3
27f7ce1
fdbb0ec
c05a80e
5b5aa6b
355182a
f35c67a
ced5687
1db84ac
5e842a0
e259858
2e2f60e
4850e9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.Serialization; | ||
| using static System.FormattableString; | ||
|
|
||
| namespace ReactNative.Reflection | ||
|
|
@@ -15,11 +17,7 @@ public static T Parse<T>(string value) | |
| { | ||
| var lookup = s_enumCache.GetOrAdd( | ||
| typeof(T), | ||
| type => Enum.GetValues(type) | ||
| .Cast<object>() | ||
| .ToDictionary( | ||
| e => Normalize(e.ToString()), | ||
| e => e)); | ||
| type => EnumToDictionary(type)); | ||
|
|
||
| var result = default(object); | ||
| if (!lookup.TryGetValue(Normalize(value), out result)) | ||
|
|
@@ -41,6 +39,30 @@ public static T Parse<T>(string value) | |
| return Parse<T>(value); | ||
| } | ||
|
|
||
| private static Dictionary<string, object> EnumToDictionary(Type type) | ||
| { | ||
| var result = new Dictionary<string, object>(); | ||
|
|
||
| var names = Enum.GetNames(type); | ||
| var values = Enum.GetValues(type); | ||
|
|
||
| for (int i = 0; i < values.Length; ++i) | ||
| { | ||
| string name = names[i]; | ||
| object value = values.GetValue(i); | ||
|
|
||
| result.Add(Normalize(name), value); | ||
|
|
||
| var enumMemberAttribute = type.GetField(name).GetCustomAttribute(typeof(EnumMemberAttribute), false); | ||
| if (enumMemberAttribute != null) | ||
| { | ||
| result.Add(Normalize(((EnumMemberAttribute)enumMemberAttribute).Value), value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just curious, does the serialization behavior of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works both ways. For example, |
||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static string Normalize(string value) | ||
| { | ||
| return value.ToLowerInvariant().Replace("-", ""); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using System.Runtime.Serialization; | ||
|
|
||
| namespace ReactNative.Views.Text | ||
| { | ||
| /// <summary> | ||
| /// TextDecorationLine values. | ||
| /// </summary> | ||
| public enum TextDecorationLine | ||
| { | ||
| /// <summary> | ||
| /// Text has no line decoration. | ||
| /// </summary> | ||
| None, | ||
| /// <summary> | ||
| /// Text is Underlined. | ||
| /// </summary> | ||
| Underline, | ||
| /// <summary> | ||
| /// Text is Stroke out. | ||
| /// </summary> | ||
| LineThrough, | ||
| /// <summary> | ||
| /// Text is both Underlined and Stroke out. | ||
| /// </summary> | ||
| [EnumMember(Value = "underline line-through")] | ||
| UnderlineLineThrough | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is cool, this is compatible with the existing EnumHelpers class? We should re-write all the other enums to use this where possible. Ideally we could get rid of that reflection hack.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually don't need this enum, you can just as easily switch case on the string itself. In reply to: 104831419 [](ancestors = 104831419)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will avoid the dependency on System.Runtime.Serialization, and avoid the reflection driven changes to In reply to: 104832645 [](ancestors = 104832645,104831419)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally vote for enum instead of string, if there is a limited number of variants, because it
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I tend to agree that strong typing is usually good, I don't like that we have to add yet another dependency and pay an attribute reflection penalty for all enums across the framework just to support this strong typing. In reply to: 105130501 [](ancestors = 105130501)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When making decision on changing EnumHelpers I was looking onto "textAlign". Logically it is same as "textDecorationLine" and they both should be treated in same way. But "textDecorationLine" contains single value with space in it, which makes it impossible to use existing approach. So, either it must be an exceptional enum, treated differently (How many such enums can we possibly have?) or the approach should be changed. I've chosen the latter. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that we now have to pay a reflection penalty for all other enums where this does not exist. I know this only occurs once, but its a potential performance hit nonetheless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true that Reflection brings in an overhead, but in this case, due to the fact that this method is called only once per enum, I think it's too small to have a significant impact on performance.