Search Unity

eternal focus

Discussion in 'UIElements' started by Devi-User, Nov 26, 2018.

  1. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    I want to use a textfield that will always have focus.
    I tried to solve this problem as follows:
    - I created a manipulator
    - I signed up for the Blur event
    - I call target.Focus every time this event is triggered.
    However, there is one big problem I got as a result. Every time the target loses focus and the target.Focus () method is called, all the text in the field is selected. I do not want this effect, I want the cursor and the selection to remain the same.
    I tried several different ways to solve this problem. For example, subscribe to FocusOutEvent, which seems to me to work before losing focus, and remember the values of TextField.cursorIndex, TextField.selectionIndex. Then, after I call target.Focus (), I also call textfield.SelectRange (oldCursorIndex, oldSelectionIndex).
    However, this does not work. I tried using other index values and this is also a failure.
    Can you help me with something?
    I want the texfield focus to not disappear when other objects are activated and that this trick does not lead to the selection of the entire text.

    P.S. I do not understand how the post was in another thread of the forum. Sorry.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    220
    No worries. As I replied in the other duplicate post:

    I think you'll be better off trying to prevent the focus change in the first place. Otherwise you'll be fighting against the API. Imagine at some point later you add 2 of these eternally focused elements in your window. While it would be fun to see them fighting for that sweet focus for a bit, it would get old fast.

    So, who is stealing your focus and why?
     
  3. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    Other elements take focus that I can click on (or background).
    In short, I'm trying to make a searchable popup. This is very similar to the AddComponent menu in Unity.
    I understand that the elements that I click on should receive focus, but still I would like the user to continue entering data in the search field even if I clicked on an element or background before.
    I use picking-mode = "Ignore" in all parent elements, but this does not save focus.

    In the screenshot, I show how my text field loses focus, despite the fact that all elements of the background have pickingMode = ignore
     

    Attached Files:

    Last edited: Nov 27, 2018
  4. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    44
    To prevent other element from getting focused, you can make them all non focusable by setting `element.focusable = false` and register a callback on the MouseDownEvent on each of them that would call `evt.PreventDefault()` to prevent the click default action (switching the focus) from executing. However, the focused element is the element that is the target of the keyboard events. If you make the text field always keep the focus, you will lose the ability to navigate the list with arrow keys, for example. Also, user typically do not expect their UI to work this way.

    Something else you can do is to register a callback on the root VisualElement that would catch the keyboard events and update the text field.
     
  5. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    Thank you for your answers!

    'focusable' not in the current alpha 2019 :(
    upload_2018-11-28_23-4-19.png

    AddComponent does not follow this rule. If the text field is under the focus, you can work with the hierarchy by pressing the left / right keys. We can also use the up / down arrows even if we enter text in the field. We can also press the button to cancel input, and then move the pointer away from it, without losing textfield's focus.
    c611c1ac-315b-41e8-bfb1-445ad05376e2.gif ab7246c0-17d8-47b5-8974-0117541886d4.gif
     
    Last edited: Nov 28, 2018
  6. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    In fact, what I am most concerned about in this matter is that TextField.SelectRange is not working in FocusEvent. It definitely updates the selectIndex and cursorIndex values, but does not affect the actual selection.

    In this case, I can call SelectRange using EditorApplication.delayCall and it will work, but it will cause an unpleasant blink.
    ad5c7a44-0ad9-4b2a-89df-c21ac5aad26d.gif
     
    Last edited: Nov 28, 2018