From: Bowon Ryu Date: Mon, 13 Dec 2021 09:51:43 +0000 (+0900) Subject: [NUI] refactoring TextMapHelper class X-Git-Tag: accepted/tizen/unified/20231205.024657~1249 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0ca64b45e8395c06fd6934e4a4873e53d08a0d1;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] refactoring TextMapHelper class - remove duplicate code through function extraction for better readability. - using Add() from PropertyMapSetterHelper class for reduce memory wastage - or call Dispose(); Signed-off-by: Bowon Ryu --- diff --git a/src/Tizen.NUI/src/public/BaseComponents/TextMapHelper.cs b/src/Tizen.NUI/src/public/BaseComponents/TextMapHelper.cs index 75ff2c7..d8d8cd0 100644 --- a/src/Tizen.NUI/src/public/BaseComponents/TextMapHelper.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/TextMapHelper.cs @@ -27,18 +27,6 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static class TextMapHelper { - private static string GetCamelCase(string pascalCase) - { - if (!string.IsNullOrEmpty(pascalCase)) - { - char[] charArray = pascalCase.ToCharArray(); - charArray[0] = Char.ToLower(charArray[0]); - pascalCase = new string(charArray); - } - - return pascalCase; - } - /// /// It returns a string value according to FontWidthType. /// The returned value can be used for FontStyle PropertyMap. @@ -101,7 +89,6 @@ namespace Tizen.NUI.BaseComponents public static FontWidthType GetFontWidthType(string fontWidthString) { FontWidthType value; - if (!(Enum.TryParse(fontWidthString, true, out value) && Enum.IsDefined(typeof(FontWidthType), value))) { value = FontWidthType.None; // If parsing fails, set a default value. @@ -120,7 +107,6 @@ namespace Tizen.NUI.BaseComponents public static FontWeightType GetFontWeightType(string fontWeightString) { FontWeightType value; - if (!(Enum.TryParse(fontWeightString, true, out value) && Enum.IsDefined(typeof(FontWeightType), value))) { value = FontWeightType.None; // If parsing fails, set a default value. @@ -139,7 +125,6 @@ namespace Tizen.NUI.BaseComponents public static FontSlantType GetFontSlantType(string fontSlantString) { FontSlantType value; - if (!(Enum.TryParse(fontSlantString, true, out value) && Enum.IsDefined(typeof(FontSlantType), value))) { value = FontSlantType.None; // If parsing fails, set a default value. @@ -149,6 +134,24 @@ namespace Tizen.NUI.BaseComponents } /// + /// It returns a FontSizeType value according to fontSizeString. + /// The returned value can be used for FontStyle PropertyMap. + /// The FontSizeType string value. + /// A FontSizeType value for TextFit.FontSizeType property. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static FontSizeType GetFontSizeType(string fontSizeString) + { + FontSizeType value; + if (!(Enum.TryParse(fontSizeString, true, out value) && Enum.IsDefined(typeof(FontSizeType), value))) + { + value = FontSizeType.PointSize; // If parsing fails, set a default value. + } + + return value; + } + + /// /// This method converts a FontStyle struct to a PropertyMap and returns it. /// The returned map can be used for set FontStyle PropertyMap in the SetFontStyle method. /// The FontStyle struct value. @@ -158,13 +161,9 @@ namespace Tizen.NUI.BaseComponents public static PropertyMap GetFontStyleMap(FontStyle fontStyle) { var map = new PropertyMap(); - var width = new PropertyValue(GetFontWidthString(fontStyle.Width)); - var weight = new PropertyValue(GetFontWeightString(fontStyle.Weight)); - var slant = new PropertyValue(GetFontSlantString(fontStyle.Slant)); - - map.Add("width", width); - map.Add("weight", weight); - map.Add("slant", slant); + map.Add("width", GetFontWidthString(fontStyle.Width)); + map.Add("weight", GetFontWeightString(fontStyle.Weight)); + map.Add("slant", GetFontSlantString(fontStyle.Slant)); return map; } @@ -178,17 +177,12 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static FontStyle GetFontStyleStruct(PropertyMap map) { - string width = "none"; - string weight = "none"; - string slant = "none"; - map.Find(0, "width")?.Get(out width); - map.Find(0, "weight")?.Get(out weight); - map.Find(0, "slant")?.Get(out slant); + var defaultValue = "none"; var fontStyle = new FontStyle(); - fontStyle.Width = GetFontWidthType(width); - fontStyle.Weight = GetFontWeightType(weight); - fontStyle.Slant = GetFontSlantType(slant); + fontStyle.Width = GetFontWidthType(GetStringFromMap(map, "width", defaultValue)); + fontStyle.Weight = GetFontWeightType(GetStringFromMap(map, "weight", defaultValue)); + fontStyle.Slant = GetFontSlantType(GetStringFromMap(map, "slant", defaultValue)); return fontStyle; } @@ -202,9 +196,11 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static PropertyMap GetInputFilterMap(InputFilter inputFilter) { + var defaultValue = ""; + var map = new PropertyMap(); - var accepted = inputFilter.Accepted != null ? new PropertyValue(inputFilter.Accepted) : new PropertyValue(""); - var rejected = inputFilter.Rejected != null ? new PropertyValue(inputFilter.Rejected) : new PropertyValue(""); + var accepted = inputFilter.Accepted == null ? defaultValue : inputFilter.Accepted; + var rejected = inputFilter.Rejected == null ? defaultValue : inputFilter.Rejected; map.Add(0, accepted); map.Add(1, rejected); @@ -220,17 +216,13 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static InputFilter GetInputFilterStruct(PropertyMap map) { - string accepted = ""; - string rejected = ""; - map.Find(0)?.Get(out accepted); - map.Find(1)?.Get(out rejected); + var defaultValue = ""; var inputFilter = new InputFilter(); - inputFilter.Accepted = accepted; - inputFilter.Rejected = rejected; + inputFilter.Accepted = GetStringFromMap(map, 0, defaultValue); + inputFilter.Rejected = GetStringFromMap(map, 1, defaultValue); return inputFilter; - } /// @@ -244,13 +236,13 @@ namespace Tizen.NUI.BaseComponents { var map = new PropertyMap(); - map.Add("enable", new PropertyValue(underline.Enable)); + map.Add("enable", underline.Enable); if (underline.Color != null) - map.Add("color", new PropertyValue(underline.Color)); + map.Add("color", underline.Color); if (underline.Height != null) - map.Add("height", new PropertyValue((float)underline.Height)); + map.Add("height", (float)underline.Height); return map; } @@ -264,15 +256,10 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static Underline GetUnderlineStruct(PropertyMap map) { - Color color = new Color(); - map.Find(0, "enable").Get(out bool enable); - map.Find(0, "color").Get(color); - map.Find(0, "height").Get(out float height); - var underline = new Underline(); - underline.Enable = enable; - underline.Color = color; - underline.Height = height; + underline.Enable = GetBoolFromMap(map, "enable", false); + underline.Color = GetColorFromMap(map, "color", Color.Black); + underline.Height = GetFloatFromMap(map, "height", 0.0f); return underline; } @@ -289,13 +276,13 @@ namespace Tizen.NUI.BaseComponents var map = new PropertyMap(); if (shadow.Offset != null) - map.Add("offset", new PropertyValue(shadow.Offset)); + map.Add("offset", shadow.Offset); if (shadow.Color != null) - map.Add("color", new PropertyValue(shadow.Color)); + map.Add("color", shadow.Color); if (shadow.BlurRadius != null) - map.Add("blurRadius", new PropertyValue((float)shadow.BlurRadius)); + map.Add("blurRadius", (float)shadow.BlurRadius); return map; } @@ -309,16 +296,13 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static Tizen.NUI.Text.Shadow GetShadowStruct(PropertyMap map) { - Vector2 offset = new Vector2(); - Color color = new Color(); - map.Find(0, "offset").Get(offset); - map.Find(0, "color").Get(color); - map.Find(0, "blurRadius").Get(out float blurRadius); + var defaultValue = new Vector2(0, 0); var shadow = new Tizen.NUI.Text.Shadow(); - shadow.Offset = offset; - shadow.Color = color; - shadow.BlurRadius = blurRadius; + shadow.Offset = GetVector2FromMap(map, "offset", defaultValue); + defaultValue.Dispose(); + shadow.Color = GetColorFromMap(map, "color", Color.Black); + shadow.BlurRadius = GetFloatFromMap(map, "blurRadius", 0.0f); return shadow; } @@ -335,10 +319,10 @@ namespace Tizen.NUI.BaseComponents var map = new PropertyMap(); if (outline.Color != null) - map.Add("color", new PropertyValue(outline.Color)); + map.Add("color", outline.Color); if (outline.Width != null) - map.Add("width", new PropertyValue((float)outline.Width)); + map.Add("width", (float)outline.Width); return map; } @@ -352,37 +336,14 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static Outline GetOutlineStruct(PropertyMap map) { - Color color = new Color(); - map.Find(0, "color").Get(color); - map.Find(0, "width").Get(out float width); - var outline = new Outline(); - outline.Color = color; - outline.Width = width; + outline.Color = GetColorFromMap(map, "color", Color.Black); + outline.Width = GetFloatFromMap(map, "width", 0.0f); return outline; } /// - /// It returns a FontSizeType value according to fontSizeString. - /// The returned value can be used for FontStyle PropertyMap. - /// The FontSizeType string value. - /// A FontSizeType value for TextFit.FontSizeType property. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public static FontSizeType GetFontSizeType(string fontSizeString) - { - FontSizeType value; - - if (!(Enum.TryParse(fontSizeString, true, out value) && Enum.IsDefined(typeof(FontSizeType), value))) - { - value = FontSizeType.PointSize; // If parsing fails, set a default value. - } - - return value; - } - - /// /// This method converts a TextFit struct to a PropertyMap and returns it. /// The returned map can be used for set TextFit PropertyMap in the SetTextFit method. /// The TextFit struct value. @@ -392,17 +353,17 @@ namespace Tizen.NUI.BaseComponents public static PropertyMap GetTextFitMap(TextFit textFit) { var map = new PropertyMap(); - map.Add("enable", new PropertyValue(textFit.Enable)); - map.Add("fontSizeType", new PropertyValue(GetFontSizeString(textFit.FontSizeType))); + map.Add("enable", textFit.Enable); + map.Add("fontSizeType", GetFontSizeString(textFit.FontSizeType)); if (textFit.MinSize != null) - map.Add("minSize", new PropertyValue((float)textFit.MinSize)); + map.Add("minSize", (float)textFit.MinSize); if (textFit.MaxSize != null) - map.Add("maxSize", new PropertyValue((float)textFit.MaxSize)); + map.Add("maxSize", (float)textFit.MaxSize); if (textFit.StepSize != null) - map.Add("stepSize", new PropertyValue((float)textFit.StepSize)); + map.Add("stepSize", (float)textFit.StepSize); return map; } @@ -416,26 +377,15 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static TextFit GetTextFitStruct(PropertyMap map) { - bool enable = false; - float minSize = 0.0f; - float maxSize = 0.0f; - float stepSize = 0.0f; - float fontSize = 0.0f; - string fontSizeType = null; - map.Find(0, "enable")?.Get(out enable); - map.Find(0, "minSize")?.Get(out minSize); - map.Find(0, "maxSize")?.Get(out maxSize); - map.Find(0, "stepSize")?.Get(out stepSize); - map.Find(0, "fontSize")?.Get(out fontSize); - map.Find(0, "fontSizeType")?.Get(out fontSizeType); + var defaultValue = "PointSize"; var textFit = new TextFit(); - textFit.Enable = enable; - textFit.MinSize = minSize; - textFit.MaxSize = maxSize; - textFit.StepSize = stepSize; - textFit.FontSize = fontSize; - textFit.FontSizeType = GetFontSizeType(fontSizeType); + textFit.Enable = GetBoolFromMap(map, "enable", false); + textFit.MinSize = GetFloatFromMap(map, "minSize", 0.0f); + textFit.MaxSize = GetFloatFromMap(map, "maxSize", 0.0f); + textFit.StepSize = GetFloatFromMap(map, "stepSize", 0.0f); + textFit.FontSize = GetFloatFromMap(map, "fontSize", 0.0f); + textFit.FontSizeType = GetFontSizeType(GetStringFromMap(map, "fontSizeType", defaultValue)); return textFit; } @@ -452,30 +402,34 @@ namespace Tizen.NUI.BaseComponents var map = new PropertyMap(); if (placeholder.Text != null) - map.Add("text", new PropertyValue(placeholder.Text)); + map.Add("text", placeholder.Text); if (placeholder.TextFocused != null) - map.Add("textFocused", new PropertyValue(placeholder.TextFocused)); + map.Add("textFocused", placeholder.TextFocused); if (placeholder.Color != null) - map.Add("color", new PropertyValue(placeholder.Color)); + map.Add("color", placeholder.Color); if (placeholder.FontFamily != null) - map.Add("fontFamily", new PropertyValue(placeholder.FontFamily)); + map.Add("fontFamily", placeholder.FontFamily); if (placeholder.FontStyle != null) - map.Add("fontStyle", new PropertyValue(GetFontStyleMap((FontStyle)placeholder.FontStyle))); + { + var fontStyleValue = new PropertyValue(GetFontStyleMap((FontStyle)placeholder.FontStyle)); + map.Add("fontStyle", fontStyleValue); + fontStyleValue.Dispose(); + } if (placeholder.PointSize != null && placeholder.PixelSize != null) - map.Add("pointSize", new PropertyValue((float)placeholder.PointSize)); + map.Add("pointSize", (float)placeholder.PointSize); else if (placeholder.PointSize != null) - map.Add("pointSize", new PropertyValue((float)placeholder.PointSize)); + map.Add("pointSize", (float)placeholder.PointSize); else if (placeholder.PixelSize != null) - map.Add("pixelSize", new PropertyValue((float)placeholder.PixelSize)); + map.Add("pixelSize", (float)placeholder.PixelSize); - map.Add("ellipsis", new PropertyValue(placeholder.Ellipsis)); + map.Add("ellipsis", placeholder.Ellipsis); return map; } @@ -489,43 +443,19 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static Placeholder GetPlaceholderStruct(PropertyMap map) { - string text = ""; - string textFocused = ""; - Color color = new Color(); - string fontFamily = null; - var fontStyle = new PropertyMap(); - PropertyValue pointSizeValue = null; - PropertyValue pixelSizeValue = null; - bool ellipsis = false; - - map.Find(0)?.Get(out text); - map.Find(1)?.Get(out textFocused); - map.Find(2)?.Get(color); - map.Find(3)?.Get(out fontFamily); - map.Find(4)?.Get(fontStyle); - pointSizeValue = map.Find(5); - pixelSizeValue = map.Find(6); - map.Find(7)?.Get(out ellipsis); + var defaultText = ""; + var defaultFontStyle = new PropertyMap(); var placeholder = new Placeholder(); - placeholder.Text = text; - placeholder.TextFocused = textFocused; - placeholder.Color = color; - placeholder.FontFamily = fontFamily; - placeholder.Ellipsis = ellipsis; - placeholder.FontStyle = GetFontStyleStruct(fontStyle); - - if (pointSizeValue != null) - { - pointSizeValue.Get(out float pointSize); - placeholder.PointSize = pointSize; - } - - if (pixelSizeValue != null) - { - pixelSizeValue.Get(out float pixelSize); - placeholder.PixelSize = pixelSize; - } + placeholder.Text = GetStringFromMap(map, 0, defaultText); + placeholder.TextFocused = GetStringFromMap(map, 1, defaultText); + placeholder.Color = GetColorFromMap(map, 2, Color.Black); + placeholder.FontFamily = GetStringFromMap(map, 3, defaultText); + placeholder.FontStyle = GetFontStyleStruct(GetMapFromMap(map, 4, defaultFontStyle)); + defaultFontStyle.Dispose(); + placeholder.PointSize = GetNullableFloatFromMap(map, 5); + placeholder.PixelSize = GetNullableFloatFromMap(map, 6); + placeholder.Ellipsis = GetBoolFromMap(map, 7, false); return placeholder; } @@ -541,16 +471,16 @@ namespace Tizen.NUI.BaseComponents { var map = new PropertyMap(); - map.Add(0, new PropertyValue((int)hiddenInput.Mode)); + map.Add(0, (int)hiddenInput.Mode); if (hiddenInput.SubstituteCharacter != null) - map.Add(1, new PropertyValue(Convert.ToInt32(hiddenInput.SubstituteCharacter))); + map.Add(1, Convert.ToInt32(hiddenInput.SubstituteCharacter)); if (hiddenInput.SubstituteCount != null) - map.Add(2, new PropertyValue((int)hiddenInput.SubstituteCount)); + map.Add(2, (int)hiddenInput.SubstituteCount); if (hiddenInput.ShowLastCharacterDuration != null) - map.Add(3, new PropertyValue((int)hiddenInput.ShowLastCharacterDuration)); + map.Add(3, (int)hiddenInput.ShowLastCharacterDuration); return map; } @@ -564,37 +494,17 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static HiddenInput GetHiddenInputStruct(PropertyMap map) { - PropertyValue value = null; + int defaultVlaue = 0; var hiddenInput = new HiddenInput(); + hiddenInput.Mode = (HiddenInputModeType)GetIntFromMap(map, 0, defaultVlaue); - value = map.Find(0); - if (value != null) - { - value.Get(out int mode); - hiddenInput.Mode = (HiddenInputModeType)mode; - } - - value = map.Find(1); - if (value != null) - { - value.Get(out int substituteCharacter); + int? substituteCharacter = GetNullableIntFromMap(map, 1); + if (substituteCharacter != null) hiddenInput.SubstituteCharacter = Convert.ToChar(substituteCharacter); - } - value = map.Find(2); - if (value != null) - { - value.Get(out int substituteCount); - hiddenInput.SubstituteCount = substituteCount; - } - - value = map.Find(3); - if (value != null) - { - value.Get(out int showLastCharacterDuration); - hiddenInput.ShowLastCharacterDuration = showLastCharacterDuration; - } + hiddenInput.SubstituteCount = GetNullableIntFromMap(map, 2); + hiddenInput.ShowLastCharacterDuration = GetNullableIntFromMap(map, 3); return hiddenInput; } @@ -608,7 +518,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static PropertyMap GetFileNameMap(string fileName) { - return new PropertyMap().Add("filename", new PropertyValue(fileName)); + return new PropertyMap().Add("filename", fileName); } /// @@ -621,18 +531,115 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public static SelectionHandleImage GetSelectionHandleImageStruct(PropertyMap leftImageMap, PropertyMap rightImageMap) { - string leftImageUrl = null; - string rightImageUrl = null; + var defaultValue = ""; var selectionHandleImage = new SelectionHandleImage(); + selectionHandleImage.LeftImageUrl = GetStringFromMap(leftImageMap, "filename", defaultValue); + selectionHandleImage.RightImageUrl = GetStringFromMap(rightImageMap, "filename", defaultValue); - leftImageMap.Find(0, "filename")?.Get(out leftImageUrl); - rightImageMap.Find(0, "filename")?.Get(out rightImageUrl); + return selectionHandleImage; + } - selectionHandleImage.LeftImageUrl = leftImageUrl; - selectionHandleImage.RightImageUrl = rightImageUrl; + private static string GetCamelCase(string pascalCase) + { + if (!string.IsNullOrEmpty(pascalCase)) + { + char[] charArray = pascalCase.ToCharArray(); + charArray[0] = Char.ToLower(charArray[0]); + pascalCase = new string(charArray); + } - return selectionHandleImage; + return pascalCase; + } + + private static string GetStringFromMap(PropertyMap map, string key, string defaultValue) + { + string value = defaultValue; + map.Find(0, key)?.Get(out value); + return value; + } + + private static string GetStringFromMap(PropertyMap map, int key, string defaultValue) + { + string value = defaultValue; + map.Find(key)?.Get(out value); + return value; + } + + private static bool GetBoolFromMap(PropertyMap map, string key, bool defaultValue) + { + bool value = defaultValue; + map.Find(0, key)?.Get(out value); + return value; + } + + private static bool GetBoolFromMap(PropertyMap map, int key, bool defaultValue) + { + bool value = defaultValue; + map.Find(key)?.Get(out value); + return value; + } + + private static int GetIntFromMap(PropertyMap map, int key, int defaultValue) + { + int value = defaultValue; + map.Find(key)?.Get(out value); + return value; + } + + private static float GetFloatFromMap(PropertyMap map, string key, float defaultValue) + { + float value = defaultValue; + map.Find(0, key)?.Get(out value); + return value; + } + + private static Color GetColorFromMap(PropertyMap map, string key, Color defaultValue) + { + Color value = new Color(defaultValue); + map.Find(0, key)?.Get(value); + return value; + } + + private static Color GetColorFromMap(PropertyMap map, int key, Color defaultValue) + { + Color value = new Color(defaultValue); + map.Find(key)?.Get(value); + return value; + } + + private static Vector2 GetVector2FromMap(PropertyMap map, string key, Vector2 defaultValue) + { + Vector2 value = new Vector2(defaultValue); + map.Find(0, key)?.Get(value); + return value; + } + + private static PropertyMap GetMapFromMap(PropertyMap map, int key, PropertyMap defaultValue) + { + PropertyMap value = new PropertyMap(defaultValue); + map.Find(key)?.Get(value); + return value; + } + + private static int? GetNullableIntFromMap(PropertyMap map, int key) + { + PropertyValue propertyValue = map.Find(key); + if (propertyValue == null) + return null; + + propertyValue.Get(out int value); + return value; + } + + private static float? GetNullableFloatFromMap(PropertyMap map, int key) + { + PropertyValue propertyValue = map.Find(key); + if (propertyValue == null) + return null; + + propertyValue.Get(out float value); + return value; } } } \ No newline at end of file