11// UITK TreeView is not supported in earlier versions
22// Therefore the UITK version of the InputActionAsset Editor is not available on earlier Editor versions either.
33#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
4+ using System ;
45using System . Threading . Tasks ;
56using UnityEditor ;
67using UnityEngine . UIElements ;
@@ -16,6 +17,10 @@ internal class InputActionsTreeViewItem : VisualElement
1617
1718 private const string kRenameTextField = "rename-text-field" ;
1819 public event EventCallback < string > EditTextFinished ;
20+ public Action < ContextualMenuPopulateEvent > OnContextualMenuPopulateEvent ;
21+
22+ // for testing purposes to know if the item is focused to accept input
23+ internal bool IsFocused { get ; private set ; } = false ;
1924
2025 private bool m_IsEditing ;
2126 private static InputActionsTreeViewItem s_EditingItem = null ;
@@ -33,17 +38,32 @@ public InputActionsTreeViewItem()
3338 focusable = true ;
3439 delegatesFocus = false ;
3540
36- renameTextfield . selectAllOnFocus = true ;
3741 renameTextfield . selectAllOnMouseUp = false ;
3842
39-
40- RegisterCallback < MouseDownEvent > ( OnMouseDownEventForRename ) ;
41- renameTextfield . RegisterCallback < FocusOutEvent > ( e => OnEditTextFinished ( ) ) ;
43+ RegisterInputField ( ) ;
44+ _ = new ContextualMenuManipulator ( menuBuilder =>
45+ {
46+ OnContextualMenuPopulateEvent ? . Invoke ( menuBuilder ) ;
47+ } )
48+ { target = this } ;
4249 }
4350
4451 public Label label => this . Q < Label > ( ) ;
4552 private TextField renameTextfield => this . Q < TextField > ( kRenameTextField ) ;
4653
54+ public void RegisterInputField ( )
55+ {
56+ renameTextfield . SetEnabled ( true ) ;
57+ renameTextfield . selectAllOnFocus = true ;
58+ RegisterCallback < MouseDownEvent > ( OnMouseDownEventForRename ) ;
59+ renameTextfield . RegisterCallback < FocusInEvent > ( e => IsFocused = true ) ;
60+ renameTextfield . RegisterCallback < FocusOutEvent > ( e =>
61+ {
62+ OnEditTextFinished ( ) ;
63+ IsFocused = false ;
64+ } ) ;
65+ }
66+
4767 public void UnregisterInputField ( )
4868 {
4969 renameTextfield . SetEnabled ( false ) ;
@@ -52,28 +72,38 @@ public void UnregisterInputField()
5272 renameTextfield . UnregisterCallback < FocusOutEvent > ( e => OnEditTextFinished ( ) ) ;
5373 }
5474
55- private float lastSingleClick ;
75+ private double lastSingleClick ;
5676 private static InputActionsTreeViewItem selected ;
5777
5878 private void OnMouseDownEventForRename ( MouseDownEvent e )
5979 {
6080 if ( e . clickCount != 1 || e . button != ( int ) MouseButton . LeftMouse || e . target == null )
6181 return ;
62-
63- if ( selected == this && Time . time - lastSingleClick < 3f )
82+ var now = EditorApplication . timeSinceStartup ;
83+ if ( selected == this && now - lastSingleClick < 3 )
6484 {
6585 FocusOnRenameTextField ( ) ;
6686 e . StopImmediatePropagation ( ) ;
6787 lastSingleClick = 0 ;
88+ return ;
6889 }
69- lastSingleClick = Time . time ;
90+ lastSingleClick = now ;
7091 selected = this ;
7192 }
7293
7394 public void Reset ( )
7495 {
96+ if ( m_IsEditing )
97+ {
98+ lastSingleClick = 0 ;
99+ delegatesFocus = false ;
100+
101+ renameTextfield . AddToClassList ( InputActionsEditorConstants . HiddenStyleClassName ) ;
102+ label . RemoveFromClassList ( InputActionsEditorConstants . HiddenStyleClassName ) ;
103+ s_EditingItem = null ;
104+ m_IsEditing = false ;
105+ }
75106 EditTextFinished = null ;
76- m_IsEditing = false ;
77107 }
78108
79109 public void FocusOnRenameTextField ( )
@@ -88,7 +118,7 @@ public void FocusOnRenameTextField()
88118
89119 //a bit hacky - e.StopImmediatePropagation() for events does not work like expected on ListViewItems or TreeViewItems because
90120 //the listView/treeView reclaims the focus - this is a workaround with less overhead than rewriting the events
91- DelayCall ( ) ;
121+ schedule . Execute ( ( ) => renameTextfield . Q < TextField > ( ) . Focus ( ) ) . StartingIn ( 120 ) ;
92122 renameTextfield . SelectAll ( ) ;
93123
94124 s_EditingItem = this ;
@@ -100,12 +130,6 @@ public static void CancelRename()
100130 s_EditingItem ? . OnEditTextFinished ( ) ;
101131 }
102132
103- async void DelayCall ( )
104- {
105- await Task . Delay ( 120 ) ;
106- renameTextfield . Q < TextField > ( ) . Focus ( ) ;
107- }
108-
109133 private void OnEditTextFinished ( )
110134 {
111135 if ( ! m_IsEditing )
@@ -124,6 +148,7 @@ private void OnEditTextFinished()
124148 renameTextfield . schedule . Execute ( ( ) => renameTextfield . SetValueWithoutNotify ( text ) ) ;
125149 return ;
126150 }
151+
127152 EditTextFinished ? . Invoke ( text ) ;
128153 }
129154 }
0 commit comments