Skip to content

Commit

Permalink
Improve rename popup (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
rthery authored May 7, 2020
1 parent a1cf78d commit 4c6d22a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Scripts/Editor/RenamePopup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using UnityEditor;
using UnityEditor;
using UnityEngine;

namespace XNodeEditor {
/// <summary> Utility for renaming assets </summary>
public class RenamePopup : EditorWindow {
private const string inputControlName = "nameInput";

public static RenamePopup current { get; private set; }
public Object target;
public string input;
Expand All @@ -19,7 +21,6 @@ public static RenamePopup Show(Object target, float width = 200) {
window.input = target.name;
window.minSize = new Vector2(100, 44);
window.position = new Rect(0, 0, width, 44);
GUI.FocusControl("ClearAllFocus");
window.UpdatePositionToMouse();
return window;
}
Expand All @@ -43,7 +44,9 @@ private void OnGUI() {
UpdatePositionToMouse();
firstFrame = false;
}
GUI.SetNextControlName(inputControlName);
input = EditorGUILayout.TextField(input);
EditorGUI.FocusTextInControl(inputControlName);
Event e = Event.current;
// If input is empty, revert name to default instead
if (input == null || input.Trim() == "") {
Expand All @@ -67,6 +70,14 @@ private void OnGUI() {
target.TriggerOnValidate();
}
}

if (e.isKey && e.keyCode == KeyCode.Escape) {
Close();
}
}

private void OnDestroy() {
EditorGUIUtility.editingTextField = false;
}
}
}

0 comments on commit 4c6d22a

Please sign in to comment.