HexManiacAdvance/src/HexManiac.Core/ViewModels/AutoCompleteSelectionItem.cs
Benjamin Popp 14aa655bd6 Improve paste performance
During paste operations, we don't actually need autocomplete. It's important to keep the options in mind, but we can delay evaluation until they're actually used.

Change autocomplete related methods to return lazy IEnumerable objects instead of returning a pre-populated read-only list. Add a LazyList<T> class that can handle the once-time evaluation logic. If no element of the list is ever checked, then the auto-complete code never runs.

Change ViewPort's UpdateToolsFromSelection tha happens when the selection changes. Only evaluate it after the edit operation is complete.
2021-04-07 14:48:23 -05:00

49 lines
2.0 KiB
C#

using HavenSoft.HexManiac.Core.Models.Runs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace HavenSoft.HexManiac.Core.ViewModels {
public class AutoCompleteSelectionItem : IEquatable<AutoCompleteSelectionItem>, INotifyPropertyChanged {
public string DisplayText { get; }
public string CompletionText { get; }
public bool IsSelected { get; }
public bool IsFormatComplete { get; set; } = true;
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { add { } remove { } }
public AutoCompleteSelectionItem(string text, bool selection) => (CompletionText, DisplayText, IsSelected) = (text, text, selection);
public AutoCompleteSelectionItem(string display, string completion, bool selection) => (DisplayText, CompletionText, IsSelected) = (display, completion, selection);
public static int SelectedIndex(IEnumerable<AutoCompleteSelectionItem> options) {
int index = 0;
foreach (var option in options) {
if (option.IsSelected) return index;
index += 1;
}
return -1;
}
public static IEnumerable<AutoCompleteSelectionItem> Generate(IEnumerable<string> options, int selectionIndex) {
int i = 0;
foreach (var option in options ?? new string[0]) {
yield return new AutoCompleteSelectionItem(option, i == selectionIndex);
i++;
}
}
public static IEnumerable<AutoCompleteSelectionItem> Generate(IEnumerable<AutocompleteItem> options, int selectionIndex) {
int i = 0;
foreach (var option in options) {
yield return new AutoCompleteSelectionItem(option.Text, option.LineText, i == selectionIndex);
i++;
}
}
public bool Equals(AutoCompleteSelectionItem other) {
if (other == null) return false;
return IsSelected == other.IsSelected && CompletionText == other.CompletionText;
}
}
}