Batch process ui change with interval 50ms

This commit is contained in:
弘韬 张 2020-11-11 18:36:01 +08:00
parent 0e0f24f635
commit 52887aacf0
3 changed files with 93 additions and 62 deletions

View file

@ -106,13 +106,25 @@ namespace Flow.Launcher.ViewModel
{
_resultsUpdateQueue = new BlockingCollection<ResultsForUpdate>();
Task.Run(() =>
Task.Run(async () =>
{
while (true)
{
var resultToUpdate = _resultsUpdateQueue.Take();
if (!resultToUpdate.Token.IsCancellationRequested)
UpdateResultView(resultToUpdate.Results, resultToUpdate.Metadata, resultToUpdate.Query);
List<ResultsForUpdate> queue = new List<ResultsForUpdate>() { _resultsUpdateQueue.Take() };
await Task.Delay(50);
while (_resultsUpdateQueue.TryTake(out var resultsForUpdate))
{
queue.Add(resultsForUpdate);
}
//foreach (var update in queue)
//{
// UpdateResultView(update.Results, update.Metadata, update.Query);
//}
UpdateResultView(queue.Where(r => !r.Token.IsCancellationRequested));
}
});
}
@ -412,7 +424,7 @@ namespace Flow.Launcher.ViewModel
if (query != null)
{
// handle the exclusiveness of plugin using action keyword
RemoveOldQueryResults(query);
// RemoveOldQueryResults(query);
_lastQuery = query;
Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
@ -443,7 +455,7 @@ namespace Flow.Launcher.ViewModel
{
// nothing to do here
}
// this should happen once after all queries are done so progress bar should continue
// until the end of all querying
@ -459,6 +471,7 @@ namespace Flow.Launcher.ViewModel
{
Results.Clear();
Results.Visbility = Visibility.Collapsed;
}
}
@ -683,6 +696,23 @@ namespace Flow.Launcher.ViewModel
}
}
public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
{
foreach (var result in resultsForUpdates.SelectMany(u => u.Results))
{
if (_topMostRecord.IsTopMost(result))
{
result.Score = int.MaxValue;
}
else
{
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5;
}
}
Results.AddResults(resultsForUpdates);
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
@ -705,10 +735,6 @@ namespace Flow.Launcher.ViewModel
Results.AddResults(list, metadata.ID);
}
if (Results.Visbility != Visibility.Visible && list.Count > 0)
{
Results.Visbility = Visibility.Visible;
}
}
#endregion

View file

@ -6,7 +6,7 @@ using System.Threading;
namespace Flow.Launcher.ViewModel
{
class ResultsForUpdate
public class ResultsForUpdate
{
public List<Result> Results { get; }

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
@ -115,19 +116,20 @@ namespace Flow.Launcher.ViewModel
public void Clear()
{
Results.Clear();
Results.RemoveAll();
}
public void RemoveResultsExcept(PluginMetadata metadata)
{
Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
//Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
}
public void RemoveResultsFor(PluginMetadata metadata)
{
Results.RemoveAll(r => r.Result.PluginID == metadata.ID);
//Results.RemoveAll(r => r.Result.PluginID == metadata.ID);
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
@ -138,16 +140,44 @@ namespace Flow.Launcher.ViewModel
// update UI in one run, so it can avoid UI flickering
Results.Update(newResults);
if (Results.Count > 0)
if (Visbility != Visibility.Visible && Results.Count > 0)
{
Margin = new Thickness { Top = 8 };
SelectedIndex = 0;
Visbility = Visibility.Visible;
}
else
{
Margin = new Thickness { Top = 0 };
Visbility = Visibility.Collapsed;
}
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
{
var newResults = NewResults(resultsForUpdates);
lock (_collectionLock)
{
Results.Update(newResults);
}
switch (Visbility)
{
case Visibility.Collapsed when Results.Count > 0:
Margin = new Thickness { Top = 8 };
SelectedIndex = 0;
Visbility = Visibility.Visible;
break;
case Visibility.Visible when Results.Count == 0:
Margin = new Thickness { Top = 0 };
Visbility = Visibility.Collapsed;
break;
}
}
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
{
@ -165,8 +195,25 @@ namespace Flow.Launcher.ViewModel
.Take(MaxResults * 2)
.ToList();
}
private List<ResultViewModel> NewResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
{
if (!resultsForUpdates.Any())
return Results.ToList();
var results = Results as IEnumerable<ResultViewModel>;
return results.Where(r => !resultsForUpdates.Any(u => u.Metadata.ID == r.Result.PluginID))
.Concat(resultsForUpdates
.SelectMany(u => u.Results)
.Select(r => new ResultViewModel(r, _settings)))
.OrderByDescending(rv => rv.Result.Score)
.Take(MaxResults * 2)
.ToList();
}
#endregion
#region FormattedText Dependency Property
public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
"FormattedText",
@ -198,20 +245,12 @@ namespace Flow.Launcher.ViewModel
}
#endregion
public class ResultCollection : ObservableCollection<ResultViewModel>
public class ResultCollection : ObservableCollection<ResultViewModel>, INotifyCollectionChanged
{
public void RemoveAll(Predicate<ResultViewModel> predicate)
public event NotifyCollectionChangedEventHandler CollectionChanged;
public void RemoveAll()
{
CheckReentrancy();
for (int i = Count - 1; i >= 0; i--)
{
if (predicate(this[i]))
{
RemoveAt(i);
}
}
ClearItems();
}
/// <summary>
@ -226,44 +265,10 @@ namespace Flow.Launcher.ViewModel
{
Add(item);
}
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
return;
int newCount = newItems.Count;
int oldCount = Items.Count;
int location = newCount > oldCount ? oldCount : newCount;
for (int i = 0; i < location; i++)
{
ResultViewModel oldResult = this[i];
ResultViewModel newResult = newItems[i];
if (!oldResult.Equals(newResult))
{ // result is not the same update it in the current index
this[i] = newResult;
}
else if (oldResult.Result.Score != newResult.Result.Score)
{
this[i].Result.Score = newResult.Result.Score;
}
}
if (newCount >= oldCount)
{
for (int i = oldCount; i < newCount; i++)
{
Add(newItems[i]);
}
}
else
{
for (int i = oldCount - 1; i >= newCount; i--)
{
RemoveAt(i);
}
}
}
}
}