mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Use DynamicData as resultcollection
This commit is contained in:
parent
ce6321cf8b
commit
3fe58d7ec5
3 changed files with 67 additions and 161 deletions
|
|
@ -12,12 +12,12 @@
|
||||||
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
|
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
|
||||||
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
|
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
|
||||||
SelectionMode="AlwaysSelected"
|
SelectionMode="AlwaysSelected"
|
||||||
AutoScrollToSelectedItem="False"
|
AutoScrollToSelectedItem="True"
|
||||||
IsVisible="{Binding Visibility}"
|
IsVisible="{Binding Visibility}"
|
||||||
MaxHeight="{Binding MaxHeight}">
|
MaxHeight="{Binding MaxHeight}">
|
||||||
<ListBox.ItemTemplate>
|
<ListBox.ItemTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<Button HorizontalAlignment="Stretch" Height="40">
|
<Button HorizontalAlignment="Stretch" Height="40" x:DataType="vm:ResultViewModel">
|
||||||
<Button.Template>
|
<Button.Template>
|
||||||
<ControlTemplate>
|
<ControlTemplate>
|
||||||
<ContentPresenter Content="{TemplateBinding Button.Content}" />
|
<ContentPresenter Content="{TemplateBinding Button.Content}" />
|
||||||
|
|
@ -78,7 +78,7 @@
|
||||||
Margin="0,0,0,0"
|
Margin="0,0,0,0"
|
||||||
HorizontalAlignment="Center"
|
HorizontalAlignment="Center"
|
||||||
IsHitTestVisible="False"
|
IsHitTestVisible="False"
|
||||||
Source="{Binding Image^, FallbackValue={Binding DefaultImage}, TargetNullValue={x:Null}}"
|
Source="{Binding Image^, FallbackValue={Binding DefaultImage}}"
|
||||||
Stretch="Uniform"
|
Stretch="Uniform"
|
||||||
IsVisible="{Binding ShowIcon}">
|
IsVisible="{Binding ShowIcon}">
|
||||||
</Image>
|
</Image>
|
||||||
|
|
|
||||||
|
|
@ -239,10 +239,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (Result.ProgressBar == null)
|
return Result.ProgressBar ?? 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
return Result.ProgressBar.Value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,22 @@
|
||||||
using Flow.Launcher.Infrastructure.UserSettings;
|
using System;
|
||||||
|
using Flow.Launcher.Infrastructure.UserSettings;
|
||||||
using Flow.Launcher.Plugin;
|
using Flow.Launcher.Plugin;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reactive.Disposables;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
using System.Windows.Documents;
|
using System.Windows.Documents;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using Avalonia.ReactiveUI;
|
||||||
|
using DynamicData;
|
||||||
|
using DynamicData.Binding;
|
||||||
|
|
||||||
namespace Flow.Launcher.ViewModel
|
namespace Flow.Launcher.ViewModel
|
||||||
{
|
{
|
||||||
|
|
@ -16,16 +24,40 @@ namespace Flow.Launcher.ViewModel
|
||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
public ResultCollection Results { get; }
|
private ReadOnlyObservableCollection<ResultViewModel> _results;
|
||||||
|
public ReadOnlyObservableCollection<ResultViewModel> Results => _results;
|
||||||
|
|
||||||
|
private SourceList<Result> _resultsCache = new();
|
||||||
|
|
||||||
private readonly object _collectionLock = new object();
|
private readonly object _collectionLock = new object();
|
||||||
private readonly Settings _settings;
|
private readonly Settings _settings;
|
||||||
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
|
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
|
||||||
|
|
||||||
|
public void MoveIndex(int diff)
|
||||||
|
{
|
||||||
|
if (Results.Count == 0)
|
||||||
|
{
|
||||||
|
SelectedIndex = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SelectedIndex = Mod(SelectedIndex + diff, Results.Count);
|
||||||
|
return;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
int Mod(int x, int m) => (x % m + m) % m;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IDisposable _resultsSubscription;
|
||||||
|
|
||||||
public ResultsViewModel()
|
public ResultsViewModel()
|
||||||
{
|
{
|
||||||
Results = new ResultCollection();
|
_resultsSubscription = _resultsCache.Connect()
|
||||||
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
|
.Sort(SortExpressionComparer<Result>.Descending(r => r.Score))
|
||||||
|
.Transform(r => new ResultViewModel(r, _settings))
|
||||||
|
.Bind(out _results)
|
||||||
|
.Do(_ => MoveIndex(-SelectedIndex))
|
||||||
|
.Subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResultsViewModel(Settings settings) : this()
|
public ResultsViewModel(Settings settings) : this()
|
||||||
|
|
@ -59,21 +91,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
|
||||||
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
|
|
||||||
{
|
|
||||||
int index = 0;
|
|
||||||
for (; index < list.Count; index++)
|
|
||||||
{
|
|
||||||
var result = list[index];
|
|
||||||
if (newScore > result.Result.Score)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int NewIndex(int i)
|
private int NewIndex(int i)
|
||||||
{
|
{
|
||||||
var n = Results.Count;
|
var n = Results.Count;
|
||||||
|
|
@ -120,20 +137,23 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
lock (_collectionLock)
|
_resultsCache.Clear();
|
||||||
Results.RemoveAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void KeepResultsFor(PluginMetadata metadata)
|
public void KeepResultsFor(PluginMetadata metadata)
|
||||||
{
|
{
|
||||||
lock (_collectionLock)
|
_resultsCache.Edit(list =>
|
||||||
Results.Update(Results.Where(r => r.Result.PluginID == metadata.ID).ToList());
|
{
|
||||||
|
list.Remove(list.Where(x => x.PluginID != metadata.ID));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void KeepResultsExcept(PluginMetadata metadata)
|
public void KeepResultsExcept(PluginMetadata metadata)
|
||||||
{
|
{
|
||||||
lock (_collectionLock)
|
_resultsCache.Edit(list =>
|
||||||
Results.Update(Results.Where(r => r.Result.PluginID != metadata.ID).ToList());
|
{
|
||||||
|
list.Remove(list.Where(x => x.PluginID == metadata.ID));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -141,9 +161,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddResults(List<Result> newRawResults, string resultId)
|
public void AddResults(List<Result> newRawResults, string resultId)
|
||||||
{
|
{
|
||||||
var newResults = NewResults(newRawResults, resultId);
|
NewResults(newRawResults, resultId);
|
||||||
|
|
||||||
UpdateResults(newResults);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -151,59 +169,30 @@ namespace Flow.Launcher.ViewModel
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token)
|
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token)
|
||||||
{
|
{
|
||||||
var newResults = NewResults(resultsForUpdates);
|
NewResults(resultsForUpdates);
|
||||||
|
|
||||||
if (token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
|
|
||||||
UpdateResults(newResults, token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateResults(List<ResultViewModel> newResults, CancellationToken token = default)
|
|
||||||
|
private void NewResults(IReadOnlyCollection<Result> newRawResults, string resultId)
|
||||||
{
|
{
|
||||||
lock (_collectionLock)
|
_resultsCache.Edit(list =>
|
||||||
{
|
{
|
||||||
// update UI in one run, so it can avoid UI flickering
|
list.Remove(list.Where(x => x.PluginID == resultId).ToList());
|
||||||
Results.Update(newResults, token);
|
list.AddRange(newRawResults);
|
||||||
if (Results.Any())
|
});
|
||||||
SelectedItem = Results[0];
|
}
|
||||||
}
|
|
||||||
|
|
||||||
switch (Visibility)
|
private void NewResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
|
||||||
|
{
|
||||||
|
_resultsCache.Edit(list =>
|
||||||
{
|
{
|
||||||
case false when Results.Count > 0:
|
list.Remove(list.Where(x => resultsForUpdates.Any(r => r.ID == x.PluginID)).ToList());
|
||||||
SelectedIndex = 0;
|
|
||||||
Visibility = true;
|
|
||||||
break;
|
|
||||||
case true when Results.Count == 0:
|
|
||||||
Visibility = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
|
foreach (var resultsForUpdate in resultsForUpdates)
|
||||||
{
|
{
|
||||||
if (newRawResults.Count == 0)
|
list.AddRange(resultsForUpdate.Results);
|
||||||
return Results;
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings));
|
|
||||||
|
|
||||||
return Results.Where(r => r.Result.PluginID != resultId)
|
|
||||||
.Concat(newResults)
|
|
||||||
.OrderByDescending(r => r.Result.Score)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ResultViewModel> NewResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
|
|
||||||
{
|
|
||||||
if (!resultsForUpdates.Any())
|
|
||||||
return Results;
|
|
||||||
|
|
||||||
return Results.Where(r => r != null && !resultsForUpdates.Any(u => u.ID == r.Result.PluginID))
|
|
||||||
.Concat(resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings)))
|
|
||||||
.OrderByDescending(rv => rv.Result.Score)
|
|
||||||
.ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -240,85 +229,5 @@ namespace Flow.Launcher.ViewModel
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
|
|
||||||
{
|
|
||||||
private long editTime = 0;
|
|
||||||
|
|
||||||
private CancellationToken _token;
|
|
||||||
|
|
||||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
||||||
|
|
||||||
|
|
||||||
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
CollectionChanged?.Invoke(this, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void BulkAddAll(List<ResultViewModel> resultViews)
|
|
||||||
{
|
|
||||||
AddRange(resultViews);
|
|
||||||
|
|
||||||
// can return because the list will be cleared next time updated, which include a reset event
|
|
||||||
if (_token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// manually update event
|
|
||||||
// wpf use DirectX / double buffered already, so just reset all won't cause ui flickering
|
|
||||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddAll(List<ResultViewModel> Items)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < Items.Count; i++)
|
|
||||||
{
|
|
||||||
var item = Items[i];
|
|
||||||
if (_token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
Add(item);
|
|
||||||
OnCollectionChanged(
|
|
||||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveAll(int Capacity = 512)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
if (this.Capacity > 8000 && Capacity < this.Capacity)
|
|
||||||
this.Capacity = Capacity;
|
|
||||||
|
|
||||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Update the results collection with new results, try to keep identical results
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="newItems"></param>
|
|
||||||
public void Update(List<ResultViewModel> newItems, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
_token = token;
|
|
||||||
if (Count == 0 && newItems.Count == 0 || _token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (editTime < 10 || newItems.Count < 30)
|
|
||||||
{
|
|
||||||
if (Count != 0) RemoveAll(newItems.Count);
|
|
||||||
AddAll(newItems);
|
|
||||||
editTime++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
BulkAddAll(newItems);
|
|
||||||
if (Capacity > 8000 && newItems.Count < 3000)
|
|
||||||
{
|
|
||||||
Capacity = newItems.Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
editTime++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue