< Summary

Class:DCL.UIElements.TextFieldPlaceholder
Assembly:ECS7.UIElements
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/UIElements/TextFieldPlaceholder.cs
Covered lines:41
Uncovered lines:15
Coverable lines:56
Total lines:145
Line coverage:73.2% (41 of 56)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:13
Method coverage:84.6% (11 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TextFieldPlaceholder(...)0%110100%
SetPlaceholder(...)0%220100%
SetNormalColor(...)0%3.143075%
SetReadOnly(...)0%110100%
SetPlaceholderColor(...)0%220100%
UpdateIfFocusStateIs(...)0%3.333066.67%
SetNormalStyle()0%110100%
SetPlaceholderStyle()0%110100%
OnFocusIn(...)0%12300%
OnFocusOut(...)0%3.013088.89%
InputIsNullOrEmpty()0%110100%
OnValueChanged(...)0%220100%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/UIElements/TextFieldPlaceholder.cs

#LineLine coverage
 1using DCL.ECSComponents;
 2using Decentraland.Common;
 3using System;
 4using UnityEngine;
 5using UnityEngine.UIElements;
 6
 7namespace DCL.UIElements
 8{
 9    /// <summary>
 10    /// Adds placeholder to the `TextField`;
 11    /// In Unity 2023 `Placeholder` is included in the framework
 12    /// </summary>
 13    public class TextFieldPlaceholder : IDisposable
 14    {
 15        private string placeholder;
 16        private Color placeholderColor;
 17
 18        private Color normalColor;
 19
 20        private readonly TextField textField;
 21
 22        private bool isPlaceholder;
 23        private bool isFocused;
 24
 25        private bool isReadonly;
 26
 427        public TextFieldPlaceholder(TextField textField)
 28        {
 429            this.textField = textField;
 30
 431            textField.RegisterCallback<FocusInEvent>(OnFocusIn);
 432            textField.RegisterCallback<FocusOutEvent>(OnFocusOut);
 33            // To support changing the value from code
 434            textField.RegisterValueChangedCallback(OnValueChanged);
 35
 436            OnFocusOut(null);
 437        }
 38
 39        public void SetPlaceholder(string placeholder)
 40        {
 441            this.placeholder = placeholder;
 42
 443            if (isPlaceholder)
 444                UpdateIfFocusStateIs(false);
 445        }
 46
 47        public void SetNormalColor(Color4 color)
 48        {
 449            normalColor = color.ToUnityColor();
 50
 451            if (!isPlaceholder || isFocused)
 052                SetNormalStyle();
 453        }
 54
 55        public void SetReadOnly(bool isReadOnly)
 56        {
 457            this.isReadonly = isReadOnly;
 458        }
 59
 60        public void SetPlaceholderColor(Color4 color)
 61        {
 462            placeholderColor = color.ToUnityColor();
 63
 464            if (isPlaceholder)
 465                UpdateIfFocusStateIs(false);
 466        }
 67
 68        private void UpdateIfFocusStateIs(bool focusState)
 69        {
 870            if (isFocused != focusState)
 071                return;
 72
 873            if (focusState)
 74            {
 075                SetNormalStyle();
 76            }
 77            else
 78            {
 879                SetPlaceholderStyle();
 80            }
 881        }
 82
 83        private void SetNormalStyle()
 84        {
 185            textField.style.color = normalColor;
 186        }
 87
 88        private void SetPlaceholderStyle()
 89        {
 1290            textField.style.color = placeholderColor;
 1291            textField.SetValueWithoutNotify(placeholder);
 1292        }
 93
 94        private void OnFocusIn(FocusInEvent _)
 95        {
 096            if (isReadonly)
 097                return;
 98
 099            if (isPlaceholder)
 100            {
 0101                textField.SetValueWithoutNotify(string.Empty);
 0102                SetNormalStyle();
 103            }
 104
 0105            isFocused = true;
 0106        }
 107
 108        private void OnFocusOut(FocusOutEvent _)
 109        {
 5110            if (isReadonly)
 0111                return;
 112
 5113            if (InputIsNullOrEmpty())
 114            {
 4115                SetPlaceholderStyle();
 4116                isPlaceholder = true;
 117            }
 118            else
 119            {
 1120                SetNormalStyle();
 1121                isPlaceholder = false;
 122            }
 123
 5124            isFocused = false;
 5125        }
 126
 127        private bool InputIsNullOrEmpty() =>
 5128            string.IsNullOrEmpty(textField.text);
 129
 130        private void OnValueChanged(ChangeEvent<string> newValue)
 131        {
 1132            if (!isFocused)
 133            {
 1134                OnFocusOut(null);
 135            }
 1136        }
 137
 138        public void Dispose()
 139        {
 0140            textField.UnregisterCallback<FocusInEvent>(OnFocusIn);
 0141            textField.UnregisterCallback<FocusOutEvent>(OnFocusOut);
 0142            textField.UnregisterValueChangedCallback(OnValueChanged);
 0143        }
 144    }
 145}