< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TextFieldPlaceholder(...)0%2100%
SetPlaceholder(...)0%6200%
SetNormalColor(...)0%12300%
SetReadOnly(...)0%2100%
SetPlaceholderColor(...)0%6200%
UpdateIfFocusStateIs(...)0%12300%
SetNormalStyle()0%2100%
SetPlaceholderStyle()0%2100%
OnFocusIn(...)0%12300%
OnFocusOut(...)0%12300%
InputIsNullOrEmpty()0%2100%
OnValueChanged(...)0%6200%
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
 027        public TextFieldPlaceholder(TextField textField)
 28        {
 029            this.textField = textField;
 30
 031            textField.RegisterCallback<FocusInEvent>(OnFocusIn);
 032            textField.RegisterCallback<FocusOutEvent>(OnFocusOut);
 33            // To support changing the value from code
 034            textField.RegisterValueChangedCallback(OnValueChanged);
 35
 036            OnFocusOut(null);
 037        }
 38
 39        public void SetPlaceholder(string placeholder)
 40        {
 041            this.placeholder = placeholder;
 42
 043            if (isPlaceholder)
 044                UpdateIfFocusStateIs(false);
 045        }
 46
 47        public void SetNormalColor(Color4 color)
 48        {
 049            normalColor = color.ToUnityColor();
 50
 051            if (!isPlaceholder || isFocused)
 052                SetNormalStyle();
 053        }
 54
 55        public void SetReadOnly(bool isReadOnly)
 56        {
 057            this.isReadonly = isReadOnly;
 058        }
 59
 60        public void SetPlaceholderColor(Color4 color)
 61        {
 062            placeholderColor = color.ToUnityColor();
 63
 064            if (isPlaceholder)
 065                UpdateIfFocusStateIs(false);
 066        }
 67
 68        private void UpdateIfFocusStateIs(bool focusState)
 69        {
 070            if (isFocused != focusState)
 071                return;
 72
 073            if (focusState)
 74            {
 075                SetNormalStyle();
 76            }
 77            else
 78            {
 079                SetPlaceholderStyle();
 80            }
 081        }
 82
 83        private void SetNormalStyle()
 84        {
 085            textField.style.color = normalColor;
 086        }
 87
 88        private void SetPlaceholderStyle()
 89        {
 090            textField.style.color = placeholderColor;
 091            textField.SetValueWithoutNotify(placeholder);
 092        }
 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        {
 0110            if (isReadonly)
 0111                return;
 112
 0113            if (InputIsNullOrEmpty())
 114            {
 0115                SetPlaceholderStyle();
 0116                isPlaceholder = true;
 117            }
 118            else
 119            {
 0120                SetNormalStyle();
 0121                isPlaceholder = false;
 122            }
 123
 0124            isFocused = false;
 0125        }
 126
 127        private bool InputIsNullOrEmpty() =>
 0128            string.IsNullOrEmpty(textField.text);
 129
 130        private void OnValueChanged(ChangeEvent<string> newValue)
 131        {
 0132            if (!isFocused)
 133            {
 0134                OnFocusOut(null);
 135            }
 0136        }
 137
 138        public void Dispose()
 139        {
 0140            textField.UnregisterCallback<FocusInEvent>(OnFocusIn);
 0141            textField.UnregisterCallback<FocusOutEvent>(OnFocusOut);
 0142            textField.UnregisterValueChangedCallback(OnValueChanged);
 0143        }
 144    }
 145}