mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
- VV AddComponent window no longer takes 300ms every time you press a key. - Significantly optimise ItemList internally.
106 lines
3.0 KiB
C#
106 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Threading;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.CustomControls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Collections;
|
|
using Robust.Shared.Localization;
|
|
using Robust.Shared.Log;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Robust.Client.ViewVariables
|
|
{
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class ViewVariablesAddWindow : DefaultWindow
|
|
{
|
|
private string? _lastSearch;
|
|
private string[] _entries = Array.Empty<string>();
|
|
|
|
public event Action<AddButtonPressedEventArgs>? AddButtonPressed;
|
|
|
|
public ViewVariablesAddWindow(IEnumerable<string> entries, string title)
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
|
|
Title = Loc.GetString(title);
|
|
|
|
EntryItemList.OnItemSelected += _ => RefreshAddButton();
|
|
EntryItemList.OnItemDeselected += _ => RefreshAddButton();
|
|
SearchLineEdit.OnTextChanged += OnSearchTextChanged;
|
|
AddButton.OnPressed += OnAddButtonPressed;
|
|
|
|
Populate(entries);
|
|
|
|
SetSize = new Vector2(200, 300);
|
|
}
|
|
|
|
private void RefreshAddButton()
|
|
{
|
|
AddButton.Disabled = !EntryItemList.GetSelected().Any();
|
|
}
|
|
|
|
public void Populate(IEnumerable<string> components)
|
|
{
|
|
_entries = components.ToArray();
|
|
Array.Sort(_entries);
|
|
Populate(_lastSearch);
|
|
}
|
|
|
|
private void Populate(string? search = null)
|
|
{
|
|
_lastSearch = search;
|
|
EntryItemList.ClearSelected();
|
|
EntryItemList.Clear();
|
|
|
|
AddButton.Disabled = true;
|
|
var items = new ValueList<string>();
|
|
|
|
foreach (var component in _entries)
|
|
{
|
|
if(!string.IsNullOrEmpty(search) && !component.Contains(search, StringComparison.InvariantCultureIgnoreCase))
|
|
continue;
|
|
|
|
items.Add(component);
|
|
}
|
|
|
|
EntryItemList.AddItems(items);
|
|
}
|
|
|
|
private void OnSearchTextChanged(LineEdit.LineEditEventArgs obj)
|
|
{
|
|
Populate(obj.Text);
|
|
}
|
|
|
|
private void OnAddButtonPressed(BaseButton.ButtonEventArgs obj)
|
|
{
|
|
var selected = EntryItemList.GetSelected().ToArray();
|
|
|
|
// Nothing to do here!
|
|
if (selected.Length == 0)
|
|
return;
|
|
|
|
var comp = selected[0];
|
|
|
|
// This shouldn't really happen.
|
|
if (comp.Text == null)
|
|
return;
|
|
|
|
AddButtonPressed?.Invoke(new AddButtonPressedEventArgs(comp.Text));
|
|
}
|
|
|
|
public sealed class AddButtonPressedEventArgs : EventArgs
|
|
{
|
|
public string Entry { get; }
|
|
|
|
public AddButtonPressedEventArgs(string entry)
|
|
{
|
|
Entry = entry;
|
|
}
|
|
}
|
|
}
|
|
}
|