Search Unity

Editor GUI TextField loses focus

Discussion in 'Immediate Mode GUI (IMGUI)' started by KemojoPtr, Mar 14, 2017.

  1. KemojoPtr

    KemojoPtr

    Joined:
    Oct 29, 2015
    Posts:
    3
    I have a problem in two of my custom editor windows.
    When the user types something into a text field which causes the content above the text field to change, the focus is immediately lost from the text field.

    Here is an example:

    I have a custom log window that allows me to filter my logs by content and type.
    Ignore the ugly grey backgrounds.


    As you can see at the bottom there is a filter. When I type in the filter the logs above change, which causes the filter to lose keyboard focus. This results in me having to refocus the text field after every typed character.

    Ideally I would not like to rewrite my window so that the filter is above the changing content. Likewise I don't want to rely on a button press to filter my content.

    I've tried in hopes that giving the text field a control name would allow the editor to retain focus. But no change.
    Code (CSharp):
    1. GUI.SetNextControlName("FilterTextField");
    2. filterText = EditorGUILayout.TextField(filterText, EditorStyles.toolbarTextField, GUILayout.ExpandWidth(true));
    I've also tried detecting a change myself and forcefully resetting the focus, which also didn't work
    Code (CSharp):
    1. GUI.SetNextControlName("FilterTextField");
    2. EditorGUI.BeginChangeCheck();
    3. filterText = EditorGUILayout.TextField(filterText, EditorStyles.toolbarTextField, GUILayout.ExpandWidth(true));
    4. if (EditorGUI.EndChangeCheck())
    5. {
    6.     EditorGUI.FocusTextInControl("FilterTextField");
    7. }
    Does anyone know if what I'm doing is even possible? Or do I have to change my windows to work around this limitation.
     

    Attached Files:

  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    No problem with my search field, this is the code :

    Code (CSharp):
    1.  
    2.         /// <summary>
    3.         /// Make a search field GUI like Project, Hierarchy or Scene views
    4.         /// </summary>
    5.         /// <param name="_maxWidth">Maximum width of field</param>
    6.         /// <param name="_searchFilter">Which search string is set to field</param>
    7.         /// <returns></returns>
    8.         public static bool SearchFieldGUI(float _maxWidth, ref string _searchFilter)
    9.         {
    10.             GUIStyle toolbarSearchFieldStyle = new GUIStyle("ToolbarSeachTextField");
    11.             GUIStyle toolbarSearchFieldCancelButtonStyle = new GUIStyle("ToolbarSeachCancelButton");
    12.             GUIStyle toolbarSearchFieldCancelButtonEmptyStyle = new GUIStyle("ToolbarSeachCancelButtonEmpty");
    13.  
    14.             Event current = Event.current;
    15.  
    16.             Rect rect = GUILayoutUtility.GetRect(_maxWidth * 0.2f, _maxWidth, 16f, 16f, toolbarSearchFieldStyle);
    17.             rect.width -= 16f;
    18.  
    19.             string str = EditorGUI.TextField(rect, _searchFilter, toolbarSearchFieldStyle);
    20.             int controlId = EditorUtils.GetLastControlId();
    21.  
    22.             rect.x += rect.width;
    23.             rect.width = 16f;
    24.             // Clear search filter
    25.             if (GUI.Button(rect, GUIContent.none, _searchFilter == string.Empty ? toolbarSearchFieldCancelButtonEmptyStyle : toolbarSearchFieldCancelButtonStyle) || (current.type == EventType.KeyDown && current.keyCode == KeyCode.Escape))
    26.             {
    27.                 _searchFilter = string.Empty;
    28.                 GUIUtility.keyboardControl = 0;
    29.                 return true;
    30.             }
    31.             // Typing search filter
    32.             else if (str != _searchFilter)
    33.             {
    34.                 _searchFilter = str;
    35.                 return true;
    36.             }
    37.             // Unfocus search field
    38.             else if (current.type == EventType.MouseUp)
    39.             {
    40.                 if (controlId != GUIUtility.hotControl && controlId == GUIUtility.keyboardControl)
    41.                 {
    42.                     GUIUtility.keyboardControl = 0;
    43.                     EditorGUIUtility.editingTextField = false;
    44.                     current.Use();
    45.                 }
    46.             }
    47.  
    48.             return false;
    49.         }
     
  3. KemojoPtr

    KemojoPtr

    Joined:
    Oct 29, 2015
    Posts:
    3
    Unfortunately that doesn't fix my issue. Your search bar definitely looks better then mine though.
    In the Unity i'm using (5.5.1) EditorUtils.GetLastControlId() could not resolve, and the closest replacement I could find was GUIUtility.GetControlID(FocusType.Keyboard). Not sure if that makes a difference.
     
  4. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Oops sorry miss this one, here is the code :
    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Return last control ID setted in GUI
    4. /// </summary>
    5. /// <returns>Last control ID setted</returns>
    6. public static int GetLastControlId()
    7. {
    8.     FieldInfo getLastControlId = typeof (EditorGUIUtility).GetField("s_LastControlID", BindingFlags.Static | BindingFlags.NonPublic);
    9.     if(getLastControlId != null)
    10.         return (int)getLastControlId.GetValue(null);
    11.     return 0;
    12. }
     
    CoughE likes this.