No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DrawCommentTextArea.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.CommentText = DoTextArea(
  23. pendingChangesTab.CommentText ?? string.Empty,
  24. pendingChangesTab.ForceToShowComment,
  25. textAreaRect);
  26. pendingChangesTab.ForceToShowComment = false;
  27. if (EditorGUI.EndChangeCheck())
  28. OnTextAreaChanged(pendingChangesTab);
  29. if (string.IsNullOrEmpty(pendingChangesTab.CommentText))
  30. {
  31. DoPlaceholderIfNeeded(PlasticLocalization.GetString(PlasticLocalization.Name.CheckinOnlyComment),
  32. textAreaRect);
  33. }
  34. EditorGUILayout.EndHorizontal();
  35. }
  36. }
  37. static void OnTextAreaChanged(PendingChangesTab pendingChangesTab)
  38. {
  39. pendingChangesTab.ClearIsCommentWarningNeeded();
  40. }
  41. static string DoTextArea(
  42. string text,
  43. bool forceToShowText,
  44. Rect textAreaRect)
  45. {
  46. // while the text area has the focus, the changes to
  47. // the source string will not be picked up by the text editor.
  48. // so, when we want to change the text programmatically
  49. // we have to remove the focus, set the text and then reset the focus.
  50. TextEditor textEditor = typeof(EditorGUI)
  51. .GetField("activeEditor", BindingFlags.Static | BindingFlags.NonPublic)
  52. .GetValue(null) as TextEditor;
  53. bool shouldBeFocusFixed = forceToShowText && textEditor != null;
  54. if (shouldBeFocusFixed)
  55. EditorGUIUtility.keyboardControl = 0;
  56. var modifiedTextAreaStyle = new GUIStyle(EditorStyles.textArea);
  57. modifiedTextAreaStyle.padding.left = 7;
  58. modifiedTextAreaStyle.padding.top = 5;
  59. modifiedTextAreaStyle.stretchWidth = false;
  60. modifiedTextAreaStyle.stretchHeight = false;
  61. text = EditorGUI.TextArea(textAreaRect, text, modifiedTextAreaStyle);
  62. if (shouldBeFocusFixed)
  63. EditorGUIUtility.keyboardControl = textEditor.controlID;
  64. return text;
  65. }
  66. static void DoPlaceholderIfNeeded(string placeholder, Rect textAreaRect)
  67. {
  68. int textAreaControlId = GUIUtility.GetControlID(FocusType.Passive) - 1;
  69. if (EditorGUIUtility.keyboardControl == textAreaControlId)
  70. return;
  71. Rect hintRect = textAreaRect;
  72. hintRect.height = EditorStyles.textArea.lineHeight;
  73. GUI.Label(hintRect, placeholder, UnityStyles.PendingChangesTab.CommentPlaceHolder);
  74. }
  75. static Rect BuildTextAreaRect(string text, float width)
  76. {
  77. GUIStyle commentTextAreaStyle = UnityStyles.PendingChangesTab.CommentTextArea;
  78. commentTextAreaStyle.stretchWidth = false;
  79. // The number here (230) controls how much the right side buttons are pushed off the
  80. // screen when window is at min width
  81. float contentWidth = width - 230f;
  82. Rect result = GUILayoutUtility.GetRect(
  83. contentWidth,
  84. UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT);
  85. result.width = contentWidth;
  86. result.height = UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT;
  87. result.xMin = 50f;
  88. return result;
  89. }
  90. }
  91. }