Skip to content

Commit

Permalink
[HaCreator] Fix searchbox -- prior task is not cancelled as the user …
Browse files Browse the repository at this point in the history
…types a new character

-- performance is dramatically faster now~!
  • Loading branch information
lastbattle committed Sep 14, 2024
1 parent f0940c5 commit 6ca91ab
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 514 deletions.
118 changes: 10 additions & 108 deletions HaCreator/CustomControls/MapBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using HaSharedLibrary.Wz;
using MapleLib.WzLib.WzStructure;
using System.Data.SQLite;
using HaCreator.GUI.InstanceEditor;

namespace HaCreator.CustomControls
{
Expand All @@ -32,13 +33,22 @@ public partial class MapBrowser : UserControl
private bool _bTownOnlyFilter = false;
private bool _bIsHistoryMapBrowser = false;

private LoadSearchHelper _search;
public LoadSearchHelper Search
{
get { return _search; }
private set { }
}

/// <summary>
/// Constructor
/// </summary>
public MapBrowser()
{
InitializeComponent();

this._search = new LoadSearchHelper(mapNamesBox, maps);

this.minimapBox.SizeMode = PictureBoxSizeMode.Zoom;
}

Expand Down Expand Up @@ -232,114 +242,6 @@ public void ClearLoadedMapHistory() {
#endregion

#region UI
private string _previousSeachText = string.Empty;
private CancellationTokenSource _existingSearchTaskToken = null;
/// <summary>
/// On search box text changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e">May be null</param>
public void searchBox_TextChanged(object sender, EventArgs e)
{
TextBox searchBox = (TextBox)sender;
string searchText = searchBox.Text.ToLower();

if (_previousSeachText == searchText)
return;
_previousSeachText = searchText; // set

// start searching
searchMapsInternal(searchText);
}

/// <summary>
/// Search and filters map according to the user's query
/// </summary>
/// <param name="searchText"></param>
public void searchMapsInternal(string searchText) {
if (!_bMapsLoaded)
return;

// Cancel existing task if any
if (_existingSearchTaskToken != null && !_existingSearchTaskToken.IsCancellationRequested) {
_existingSearchTaskToken.Cancel();
}

// Clear
mapNamesBox.Items.Clear();
if (searchText == string.Empty) {
var filteredItems = maps.Where(kvp => {
MapInfo mapInfo = null;
if (mapsMapInfo.ContainsKey(kvp)) {
mapInfo = mapsMapInfo[kvp].Item2;
if (this._bTownOnlyFilter) {
if (!mapInfo.town)
return false;
}
}
return true;
}).Select(kvp => kvp) // or kvp.Value or any transformation you need
.Cast<object>()
.ToArray();


mapNamesBox.Items.AddRange(filteredItems);

mapNamesBox_SelectedIndexChanged(null, null);
}
else {

Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;

// new task
_existingSearchTaskToken = new CancellationTokenSource();
var cancellationToken = _existingSearchTaskToken.Token;

Task t = Task.Run(() => {
Thread.Sleep(500); // average key typing speed
List<string> mapsFiltered = new List<string>();
foreach (string map in maps) {
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
MapInfo mapInfo = null;
if (mapsMapInfo.ContainsKey(map)) {
mapInfo = mapsMapInfo[map].Item2;
}
// Filter by string first
if (map.ToLower().Contains(searchText)) {
// Filter again by 'town' if mapInfo is not null.
if (mapInfo != null) {
if (this._bTownOnlyFilter) {
if (!mapInfo.town)
continue;
}
}
mapsFiltered.Add(map);
}
}
currentDispatcher.BeginInvoke(new Action(() => {
foreach (string map in mapsFiltered) {
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
mapNamesBox.Items.Add(map);
}
if (mapNamesBox.Items.Count > 0) {
mapNamesBox.SelectedIndex = 0; // set default selection to reduce clicks
}
}));
}, cancellationToken);

}
}

/// <summary>
/// On map selection changed
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion HaCreator/GUI/InstanceEditor/LoadItemSelector.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 3 additions & 99 deletions HaCreator/GUI/InstanceEditor/LoadItemSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public LoadItemSelector(int filterItemId)
{
InitializeComponent();

LoadSearchHelper searchBox = new LoadSearchHelper(listBox_itemList, itemNames);
this.searchBox.TextChanged += searchBox.TextChanged;

this.FormClosing += LoadQuestSelector_FormClosing;

this._filterItemId = filterItemId;
Expand Down Expand Up @@ -159,105 +162,6 @@ private void LoadQuestSelector_FormClosing(object sender, FormClosingEventArgs e
}
#endregion

#region Search box
private string _previousSeachText = string.Empty;
private CancellationTokenSource _existingSearchTaskToken = null;

/// <summary>
/// Searchbox text changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void searchBox_TextChanged(object sender, EventArgs e)
{
TextBox searchBox = (TextBox)sender;
string searchText = searchBox.Text.ToLower();

if (_previousSeachText == searchText)
return;
_previousSeachText = searchText; // set

// start searching
searchItemInternal(searchText);
}

/// <summary>
/// Search and filters map according to the user's query
/// </summary>
/// <param name="searchText"></param>
public void searchItemInternal(string searchText)
{
if (!_bItemsLoaded)
return;

// Cancel existing task if any
if (_existingSearchTaskToken != null && !_existingSearchTaskToken.IsCancellationRequested)
{
_existingSearchTaskToken.Cancel();
}

// Clear
listBox_itemList.Items.Clear();
if (searchText == string.Empty)
{
var filteredItems = itemNames.Where(kvp =>
{
return true;
}).Select(kvp => kvp) // or kvp.Value or any transformation you need
.Cast<object>()
.ToArray();

listBox_itemList.Items.AddRange(filteredItems);

listBox_itemList_SelectedIndexChanged(null, null);
}
else
{

Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;

// new task
_existingSearchTaskToken = new CancellationTokenSource();
var cancellationToken = _existingSearchTaskToken.Token;

Task t = Task.Run(() =>
{
Thread.Sleep(500); // average key typing speed
List<string> itemsFiltered = new List<string>();
foreach (string map in itemNames)
{
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
// Filter by string first
if (map.ToLower().Contains(searchText))
{
itemsFiltered.Add(map);
}
}
currentDispatcher.BeginInvoke(new Action(() =>
{
foreach (string map in itemsFiltered)
{
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
listBox_itemList.Items.Add(map);
}
if (listBox_itemList.Items.Count > 0)
{
listBox_itemList.SelectedIndex = 0; // set default selection to reduce clicks
}
}));
}, cancellationToken);

}
}
#endregion

/// <summary>
/// On list box selection changed
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion HaCreator/GUI/InstanceEditor/LoadJobSelector.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 3 additions & 99 deletions HaCreator/GUI/InstanceEditor/LoadJobSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public LoadJobSelector()
{
InitializeComponent();

LoadSearchHelper searchBox = new LoadSearchHelper(listBox_npcList, jobNames);
this.searchBox.TextChanged += searchBox.TextChanged;

this.FormClosing += LoadQuestSelector_FormClosing;

// load items
Expand Down Expand Up @@ -131,105 +134,6 @@ private void LoadQuestSelector_FormClosing(object sender, FormClosingEventArgs e
}
#endregion

#region Search box
private string _previousSeachText = string.Empty;
private CancellationTokenSource _existingSearchTaskToken = null;

/// <summary>
/// Searchbox text changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void searchBox_TextChanged(object sender, EventArgs e)
{
TextBox searchBox = (TextBox)sender;
string searchText = searchBox.Text.ToLower();

if (_previousSeachText == searchText)
return;
_previousSeachText = searchText; // set

// start searching
searchItemInternal(searchText);
}

/// <summary>
/// Search and filters map according to the user's query
/// </summary>
/// <param name="searchText"></param>
public void searchItemInternal(string searchText)
{
if (!_bJobsLoaded)
return;

// Cancel existing task if any
if (_existingSearchTaskToken != null && !_existingSearchTaskToken.IsCancellationRequested)
{
_existingSearchTaskToken.Cancel();
}

// Clear
listBox_npcList.Items.Clear();
if (searchText == string.Empty)
{
var filteredItems = jobNames.Where(kvp =>
{
return true;
}).Select(kvp => kvp) // or kvp.Value or any transformation you need
.Cast<object>()
.ToArray();

listBox_npcList.Items.AddRange(filteredItems);

listBox_itemList_SelectedIndexChanged(null, null);
}
else
{

Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;

// new task
_existingSearchTaskToken = new CancellationTokenSource();
var cancellationToken = _existingSearchTaskToken.Token;

Task t = Task.Run(() =>
{
Thread.Sleep(500); // average key typing speed
List<string> itemsFiltered = new List<string>();
foreach (string map in jobNames)
{
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
// Filter by string first
if (map.ToLower().Contains(searchText))
{
itemsFiltered.Add(map);
}
}
currentDispatcher.BeginInvoke(new Action(() =>
{
foreach (string map in itemsFiltered)
{
if (_existingSearchTaskToken.IsCancellationRequested)
return; // stop immediately
listBox_npcList.Items.Add(map);
}
if (listBox_npcList.Items.Count > 0)
{
listBox_npcList.SelectedIndex = 0; // set default selection to reduce clicks
}
}));
}, cancellationToken);

}
}
#endregion

/// <summary>
/// On list box selection changed
/// </summary>
Expand Down
Loading

0 comments on commit 6ca91ab

Please sign in to comment.