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