BetterTextField.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using JetBrains.Annotations;
  3. using Unity.Cloud.Collaborate.Assets;
  4. using Unity.Cloud.Collaborate.UserInterface;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. namespace Unity.Cloud.Collaborate.Components
  9. {
  10. // Hopefully these features will eventually be in the default TextField eventually.
  11. internal class BetterTextField : TextField
  12. {
  13. /// <summary>
  14. /// USS class name of elements of this type.
  15. /// </summary>
  16. public const string UssClassName = "unity-better-text-field";
  17. /// <summary>
  18. /// USS class name of placeholder elements of this type.
  19. /// </summary>
  20. public const string PlaceholderUssClassName = UssClassName + "__placeholder";
  21. static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(BetterTextField)}.uss";
  22. readonly Label m_PlaceholderLabel;
  23. /// <summary>
  24. /// Notify external subscribers that value of text property changed.
  25. /// </summary>
  26. public Action<string> OnValueChangedHandler;
  27. public BetterTextField()
  28. {
  29. AddToClassList(UssClassName);
  30. styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
  31. // Add and configure placeholder
  32. m_PlaceholderLabel = new Label { pickingMode = PickingMode.Ignore };
  33. m_PlaceholderLabel.AddToClassList(PlaceholderUssClassName);
  34. Add(m_PlaceholderLabel);
  35. RegisterCallback<FocusInEvent>(e => HidePlaceholder());
  36. RegisterCallback<FocusOutEvent>(e =>
  37. {
  38. if (string.IsNullOrEmpty(text))
  39. {
  40. ShowPlaceholder();
  41. }
  42. });
  43. this.RegisterValueChangedCallback(e => OnValueChangedHandler?.Invoke(e.newValue));
  44. }
  45. void UpdatePlaceholderVisibility()
  46. {
  47. if (string.IsNullOrEmpty(value))
  48. {
  49. // Value can be set before the focus control is initialised.
  50. if (focusController?.focusedElement != this)
  51. {
  52. ShowPlaceholder();
  53. }
  54. }
  55. else
  56. {
  57. HidePlaceholder();
  58. }
  59. }
  60. void HidePlaceholder()
  61. {
  62. m_PlaceholderLabel?.AddToClassList(UiConstants.ussHidden);
  63. }
  64. void ShowPlaceholder()
  65. {
  66. m_PlaceholderLabel?.RemoveFromClassList(UiConstants.ussHidden);
  67. }
  68. public override string value
  69. {
  70. get => base.value;
  71. set
  72. {
  73. // Catch case of value being set programatically.
  74. base.value = value;
  75. UpdatePlaceholderVisibility();
  76. }
  77. }
  78. public string Placeholder
  79. {
  80. get => m_PlaceholderLabel.text;
  81. set => m_PlaceholderLabel.text = value;
  82. }
  83. public override void SetValueWithoutNotify(string newValue)
  84. {
  85. base.SetValueWithoutNotify(newValue);
  86. UpdatePlaceholderVisibility();
  87. }
  88. [UsedImplicitly]
  89. public new class UxmlFactory : UxmlFactory<BetterTextField, UxmlTraits> { }
  90. public new class UxmlTraits : TextField.UxmlTraits
  91. {
  92. readonly UxmlStringAttributeDescription m_Hint = new UxmlStringAttributeDescription { name = "placeholder" };
  93. public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
  94. {
  95. base.Init(ve, bag, cc);
  96. var field = (BetterTextField)ve;
  97. field.Placeholder = m_Hint.GetValueFromBag(bag, cc);
  98. }
  99. }
  100. }
  101. }