perf: 改进Gui指定材料下拉框过滤逻辑

This commit is contained in:
et
2022-09-04 16:50:12 +08:00
committed by ET
parent c27b54a831
commit acfba02da2
6 changed files with 134 additions and 17 deletions

View File

@@ -1062,7 +1062,7 @@ namespace MeoAsstGui
task_params["stone"] = max_stone;
task_params["times"] = max_times;
task_params["report_to_penguin"] = true;
if (drops_item_quantity != 0)
if (drops_item_quantity != 0 && !string.IsNullOrWhiteSpace(drops_item_id))
{
task_params["drops"] = new JObject();
task_params["drops"][drops_item_id] = drops_item_quantity;

View File

@@ -18,5 +18,7 @@ namespace MeoAsstGui
/// </summary>
public class CombData : GenericCombData<string>
{
/// <inheritdoc/>
public override string ToString() => Display;
}
}

View File

@@ -0,0 +1,111 @@
using System.Windows.Controls;
using System.Windows.Input;
namespace MeoAsstGui.Helper
{
/// <summary>
/// <seealso cref="ComboBox"/> Extensions
/// </summary>
public static class ComboBoxExtensions
{
private const string InputTag = "TextInput";
private const string SelectionTag = "Selection";
/// <summary>
/// Make <seealso cref="ComboBox"/> searchable
/// </summary>
/// <param name="targetComboBox">Target <seealso cref="ComboBox"/></param>
public static void MakeComboBoxSearchable(this ComboBox targetComboBox)
{
var targetTextBox = targetComboBox?.Template.FindName("PART_EditableTextBox", targetComboBox) as TextBox;
if (targetTextBox == null)
{
return;
}
targetComboBox.Items.IsLiveFiltering = true;
targetComboBox.StaysOpenOnEdit = true;
targetComboBox.IsEditable = true;
targetComboBox.IsTextSearchEnabled = false;
// enter input mode by default
targetComboBox.Tag = InputTag;
targetTextBox.PreviewKeyDown += (se, ev) =>
{
if (ev.Key == Key.Enter || ev.Key == Key.Return || ev.Key == Key.Tab)
{
return;
}
// switch to input mode
targetComboBox.Tag = InputTag;
// reset selection
if (targetComboBox.SelectedItem != null)
{
targetComboBox.SelectedItem = null;
targetComboBox.Text = string.Empty;
}
};
targetTextBox.TextChanged += (o, args) =>
{
if (targetComboBox.Tag is SelectionTag)
{
// text changed in selection mode, switch to input mode
targetComboBox.Tag = InputTag;
}
else
{
// input mode
var searchTerm = targetTextBox.Text;
// reset selection
if (targetComboBox.SelectionBoxItem != null)
{
targetComboBox.SelectedItem = null;
targetTextBox.Text = searchTerm;
targetTextBox.SelectionStart = targetTextBox.Text.Length;
}
// filter items
if (string.IsNullOrEmpty(searchTerm))
{
targetComboBox.Items.Filter = item => true;
targetComboBox.SelectedItem = default;
}
else
{
targetComboBox.Items.Filter = item => item.ToString().Contains(searchTerm);
}
targetTextBox.SelectionStart = targetTextBox.Text.Length;
}
targetComboBox.IsDropDownOpen = true;
};
targetComboBox.SelectionChanged += (o, args) =>
{
// selection changed, switch to selection mode
if (targetComboBox.SelectedItem != null)
{
targetComboBox.Tag = SelectionTag;
}
};
targetComboBox.LostFocus += (o, args) =>
{
// reset text to the corresponding display text of selected item
var text = targetComboBox.SelectedItem?.ToString();
if (targetComboBox.Text != text)
{
targetComboBox.Tag = SelectionTag;
targetComboBox.Text = text;
targetComboBox.Items.Filter = item => true;
}
};
}
}
}

View File

@@ -108,6 +108,7 @@
<Compile Include="Helper\AsstProxy.cs" />
<Compile Include="Helper\AutoScroll.cs" />
<Compile Include="Helper\CombData.cs" />
<Compile Include="Helper\ComboBoxExtensions.cs" />
<Compile Include="Helper\GenericCombData{TValueType}.cs" />
<Compile Include="Helper\Localization.cs" />
<Compile Include="Helper\LogColor.cs" />

View File

@@ -110,7 +110,8 @@
DisplayMemberPath="Display"
IsDropDownOpen="{Binding IsDropDown}"
IsEditable="True"
IsTextSearchEnabled="True"
IsTextSearchEnabled="False"
Loaded="{s:Action DropsList_Loaded}"
ItemsSource="{Binding DropsList}"
SelectedValue="{Binding DropsItemId}"
SelectedValuePath="Value"

View File

@@ -22,6 +22,8 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using MeoAsstGui.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stylet;
@@ -1487,6 +1489,8 @@ namespace MeoAsstGui
}
}
#region Drops
private bool _isSpecifiedDrops = Convert.ToBoolean(ViewStatusStorage.Get("MainFunction.Drops.Enable", bool.FalseString));
/// <summary>
@@ -1552,7 +1556,7 @@ namespace MeoAsstGui
/// </summary>
public ObservableCollection<CombData> DropsList { get; set; }
private string _dropsItemId = ViewStatusStorage.Get("MainFunction.Drops.ItemId", "0");
private string _dropsItemId = ViewStatusStorage.Get("MainFunction.Drops.ItemId", string.Empty);
/// <summary>
/// Gets or sets the item ID of drops.
@@ -1562,11 +1566,6 @@ namespace MeoAsstGui
get => _dropsItemId;
set
{
if (value == null)
{
return;
}
SetAndNotify(ref _dropsItemId, value);
ViewStatusStorage.Set("MainFunction.Drops.ItemId", DropsItemId);
}
@@ -1599,15 +1598,6 @@ namespace MeoAsstGui
}
_preSetDropsItemTicks = DateTime.Now.Ticks;
DropsList.Clear();
foreach (CombData drop in AllDrops)
{
var enumStr = drop.Display;
if (enumStr.Contains(value))
{
DropsList.Add(drop);
}
}
SetAndNotify(ref _dropsItem, value);
}
@@ -1638,5 +1628,17 @@ namespace MeoAsstGui
ViewStatusStorage.Set("MainFunction.Drops.Quantity", DropsQuantity);
}
}
/// <summary>
/// DropsList ComboBox loaded
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event args</param>
public void DropsList_Loaded(object sender, EventArgs e)
{
(sender as ComboBox)?.MakeComboBoxSearchable();
}
#endregion Drops
}
}