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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(
  32. PlasticLocalization.Name.CheckinComment),
  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. // The number here (230) controls how much the right side buttons are pushed off the
  81. // screen when window is at min width
  82. float contentWidth = width - 230f;
  83. Rect result = GUILayoutUtility.GetRect(
  84. contentWidth,
  85. UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT);
  86. result.width = contentWidth;
  87. result.height = UnityConstants.PLASTIC_WINDOW_COMMENT_SECTION_HEIGHT;
  88. result.xMin = 50f;
  89. return result;
  90. }
  91. }
  92. }