Newer
Older
Simple-Multiplayer-Unity3D / Multiplayer Project / Library / PackageCache / com.unity.collab-proxy@2.7.1 / Editor / Views / PendingChanges / DrawCommentTextArea.cs
  1. using System.Reflection;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using PlasticGui;
  5. using Unity.PlasticSCM.Editor.UI;
  6. namespace Unity.PlasticSCM.Editor.Views.PendingChanges
  7. {
  8. internal static class DrawCommentTextArea
  9. {
  10. internal static void For(
  11. PendingChangesTab pendingChangesTab,
  12. float width,
  13. bool isOperationRunning)
  14. {
  15. using (new GuiEnabled(!isOperationRunning))
  16. {
  17. EditorGUILayout.BeginHorizontal();
  18. Rect textAreaRect = BuildTextAreaRect(
  19. pendingChangesTab.CommentText,
  20. width);
  21. EditorGUI.BeginChangeCheck();
  22. pendingChangesTab.UpdateComment(
  23. DoTextArea(
  24. pendingChangesTab.CommentText ?? string.Empty,
  25. pendingChangesTab.ForceToShowComment,
  26. textAreaRect));
  27. if (EditorGUI.EndChangeCheck())
  28. OnTextAreaChanged(pendingChangesTab);
  29. if (string.IsNullOrEmpty(pendingChangesTab.CommentText))
  30. {
  31. DoPlaceholderIfNeeded(
  32. PlasticLocalization.Name.CheckinComment.GetString(),
  33. textAreaRect);
  34. }
  35. EditorGUILayout.EndHorizontal();
  36. }
  37. }
  38. static void OnTextAreaChanged(PendingChangesTab pendingChangesTab)
  39. {
  40. pendingChangesTab.ClearIsCommentWarningNeeded();
  41. }
  42. static string DoTextArea(
  43. string text,
  44. bool forceToShowText,
  45. Rect textAreaRect)
  46. {
  47. // while the text area has the focus, the changes to
  48. // the source string will not be picked up by the text editor.
  49. // so, when we want to change the text programmatically
  50. // we have to remove the focus, set the text and then reset the focus.
  51. TextEditor textEditor = typeof(EditorGUI)
  52. .GetField("activeEditor", BindingFlags.Static | BindingFlags.NonPublic)
  53. .GetValue(null) as TextEditor;
  54. bool shouldBeFocusFixed = forceToShowText && textEditor != null;
  55. if (shouldBeFocusFixed)
  56. EditorGUIUtility.keyboardControl = 0;
  57. var modifiedTextAreaStyle = new GUIStyle(EditorStyles.textArea);
  58. modifiedTextAreaStyle.padding.left = 7;
  59. modifiedTextAreaStyle.padding.top = 5;
  60. modifiedTextAreaStyle.stretchWidth = false;
  61. modifiedTextAreaStyle.stretchHeight = false;
  62. text = EditorGUI.TextArea(textAreaRect, text, modifiedTextAreaStyle);
  63. if (shouldBeFocusFixed)
  64. EditorGUIUtility.keyboardControl = textEditor.controlID;
  65. return text;
  66. }
  67. static void DoPlaceholderIfNeeded(string placeholder, Rect textAreaRect)
  68. {
  69. int textAreaControlId = GUIUtility.GetControlID(FocusType.Passive) - 1;
  70. if (EditorGUIUtility.keyboardControl == textAreaControlId)
  71. return;
  72. Rect hintRect = textAreaRect;
  73. hintRect.height = EditorStyles.textArea.lineHeight;
  74. GUI.Label(hintRect, placeholder, UnityStyles.PendingChangesTab.CommentPlaceHolder);
  75. }
  76. static Rect BuildTextAreaRect(string text, float width)
  77. {
  78. GUIStyle commentTextAreaStyle = UnityStyles.PendingChangesTab.CommentTextArea;
  79. commentTextAreaStyle.stretchWidth = false;
  80. Rect result = GUILayoutUtility.GetRect(
  81. width,
  82. UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT);
  83. result.width = width;
  84. result.height = UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT;
  85. result.xMin = 50f;
  86. return result;
  87. }
  88. }
  89. }