Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] ListView Lost focus every time enter value #97

Open
aprius opened this issue Aug 15, 2024 · 5 comments
Open

[Bug] ListView Lost focus every time enter value #97

aprius opened this issue Aug 15, 2024 · 5 comments

Comments

@aprius
Copy link
Contributor

aprius commented Aug 15, 2024

Alchemy v2.0.1
Unity 6000.0.f14

public List<string> samples;
bandicam.2024-08-15.10-53-35-835.mp4

Every time I fill in a value, the focus is lost and I have to click again to continue filling in.

@aprius
Copy link
Contributor Author

aprius commented Aug 15, 2024

@AnnulusGames The cause seems to be the PropertyListView class.

@KiyoK7227
Copy link

This bug is troubling me too.
I’ve been writing things in Notepad and copying and pasting them, but it's not ideal.
I hope this will be fixed.

@aprius
Copy link
Contributor Author

aprius commented Aug 22, 2024

It seems Unity changed something with ListView but I haven't found anything from their changelog so I tried to fix it by every time I enter a value I will focus back on the last element I was editing. Another problem while I fixed it this stupid way is that the focus doesn't move the cursor to the end but instead it selects the entire content and so again I have to add a RightArrow button press action to focus at the end so I can continue entering content. I don't think this is a good way but I haven't found a solution yet or wait for a fix from Unity if there is one

@aprius
Copy link
Contributor Author

aprius commented Aug 22, 2024

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
using Alchemy.Inspector;

namespace Alchemy.Editor.Elements
{
    /// <summary>
    /// Visual Element that draws SerializedProperty of Array or List
    /// </summary>
    public sealed class PropertyListView : BindableElement
    {
        public PropertyListView(SerializedProperty property)
        {
            Assert.IsTrue(property.isArray);

            var parentObj = property.GetDeclaredObject();
            var events = property.GetAttribute<OnListViewChangedAttribute>(true);
            VisualElement lastFocusedElement = null;

            listView = GUIHelper.CreateListViewFromFieldInfo(parentObj, property.GetFieldInfo());
            listView.headerTitle = ObjectNames.NicifyVariableName(property.displayName);
            listView.bindItem = (element, index) =>
            {
                var arrayElement = property.GetArrayElementAtIndex(index);
                var e = new AlchemyPropertyField(arrayElement, property.GetPropertyType(true), true);
                element.Add(e);
                element.Bind(arrayElement.serializedObject);
                var field = lastFocusedElement?.Q<TextField>();
                if (field != null)
                {
                    field.Focus();
                    field.SelectRange(field.value.Length, field.value.Length);
                    using var evt = KeyboardEventBase<KeyDownEvent>.GetPooled('\0', KeyCode.RightArrow, EventModifiers.FunctionKey);
                    field.SendEvent(evt);
                }
                
                if (events != null)
                {
                    e.TrackPropertyValue(arrayElement, x =>
                    {
                        ReflectionHelper.Invoke(parentObj, events.OnItemChanged,
                            new object[] { index, x.GetValue<object>() });
                    });
                }
            };
            listView.unbindItem = (element, index) =>
            {
                lastFocusedElement = element;
                element.Clear();
                element.Unbind();
            };

            var label = listView.Q<Label>();
            if (label != null) label.style.unityFontStyleAndWeight = FontStyle.Bold;

            listView.BindProperty(property);
            Add(listView);
        }

        readonly ListView listView;

        public string Label
        {
            get => listView.headerTitle;
            set => listView.headerTitle = value;
        }
    }
}

@Niek352
Copy link

Niek352 commented Nov 22, 2024

Unity 2022.3.51f doesn't have this anymore. Probably fixed in later versions Unity6 too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants