Merge remote-tracking branch 'upstream/dev' into ExplorerPathAsync

This commit is contained in:
弘韬 张 2021-01-20 13:25:13 +08:00
commit ba236dad63
15 changed files with 441 additions and 248 deletions

View file

@ -73,8 +73,7 @@ namespace Flow.Launcher.Infrastructure.Image
public bool ContainsKey(string key) public bool ContainsKey(string key)
{ {
var contains = Data.ContainsKey(key) && Data[key] != null; return Data.ContainsKey(key) && Data[key].imageSource != null;
return contains;
} }
public int CacheSize() public int CacheSize()

View file

@ -55,9 +55,13 @@ namespace Flow.Launcher.Infrastructure.Logger
[MethodImpl(MethodImplOptions.Synchronized)] [MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "") public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
{ {
#if DEBUG
throw exception;
#else
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName); var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
ExceptionInternal(classNameWithMethod, message, exception); ExceptionInternal(classNameWithMethod, message, exception);
#endif
} }
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,

View file

@ -14,10 +14,10 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<Version>1.3.1</Version> <Version>1.4.0</Version>
<PackageVersion>1.3.1</PackageVersion> <PackageVersion>1.4.0</PackageVersion>
<AssemblyVersion>1.3.1</AssemblyVersion> <AssemblyVersion>1.4.0</AssemblyVersion>
<FileVersion>1.3.1</FileVersion> <FileVersion>1.4.0</FileVersion>
<PackageId>Flow.Launcher.Plugin</PackageId> <PackageId>Flow.Launcher.Plugin</PackageId>
<Authors>Flow-Launcher</Authors> <Authors>Flow-Launcher</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>

View file

@ -9,7 +9,7 @@
d:DataContext="{d:DesignInstance vm:ResultsViewModel}" d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
MaxHeight="{Binding MaxHeight}" MaxHeight="{Binding MaxHeight}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
HorizontalContentAlignment="Stretch" ItemsSource="{Binding Results}" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Results}"
Margin="{Binding Margin}" Margin="{Binding Margin}"
Visibility="{Binding Visbility}" Visibility="{Binding Visbility}"

View file

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -19,9 +17,8 @@ using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Storage; using Flow.Launcher.Storage;
using System.Windows.Media;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using System.Threading.Tasks.Dataflow;
namespace Flow.Launcher.ViewModel namespace Flow.Launcher.ViewModel
{ {
@ -48,6 +45,8 @@ namespace Flow.Launcher.ViewModel
private bool _saved; private bool _saved;
private readonly Internationalization _translator = InternationalizationManager.Instance; private readonly Internationalization _translator = InternationalizationManager.Instance;
private BufferBlock<ResultsForUpdate> _resultsUpdateQueue;
private Task _resultsViewUpdateTask;
#endregion #endregion
@ -75,8 +74,11 @@ namespace Flow.Launcher.ViewModel
_selectedResults = Results; _selectedResults = Results;
InitializeKeyCommands(); InitializeKeyCommands();
RegisterViewUpdate();
RegisterResultsUpdatedEvent(); RegisterResultsUpdatedEvent();
SetHotkey(_settings.Hotkey, OnHotkey); SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey(); SetCustomPluginHotkey();
SetOpenResultModifiers(); SetOpenResultModifiers();
@ -89,15 +91,51 @@ namespace Flow.Launcher.ViewModel
var plugin = (IResultUpdated) pair.Plugin; var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) => plugin.ResultsUpdated += (s, e) =>
{ {
Task.Run(() => PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
{ if (e.Query.Search == _lastQuery.Search)
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query); _resultsUpdateQueue.Post(new ResultsForUpdate(e.Results, pair.Metadata, e.Query, _updateToken));
UpdateResultView(e.Results, pair.Metadata, e.Query);
}, _updateToken);
}; };
} }
} }
private void RegisterViewUpdate()
{
_resultsUpdateQueue = new BufferBlock<ResultsForUpdate>();
_resultsViewUpdateTask =
Task.Run(updateAction).ContinueWith(continueAction, TaskContinuationOptions.OnlyOnFaulted);
async Task updateAction()
{
var queue = new Dictionary<string, ResultsForUpdate>();
while (await _resultsUpdateQueue.OutputAvailableAsync())
{
queue.Clear();
await Task.Delay(20);
while (_resultsUpdateQueue.TryReceive(out var item))
{
if (!item.Token.IsCancellationRequested)
queue[item.ID] = item;
}
UpdateResultView(queue.Values);
}
}
;
void continueAction(Task t)
{
#if DEBUG
throw t.Exception;
#else
Log.Error($"Error happen in task dealing with viewupdate for results. {t.Exception}");
_resultsViewUpdateTask =
Task.Run(updateAction).ContinueWith(continueAction, TaskContinuationOptions.OnlyOnFaulted);
#endif
}
}
private void InitializeKeyCommands() private void InitializeKeyCommands()
{ {
@ -195,12 +233,13 @@ namespace Flow.Launcher.ViewModel
public ResultsViewModel Results { get; private set; } public ResultsViewModel Results { get; private set; }
public ResultsViewModel ContextMenu { get; private set; } public ResultsViewModel ContextMenu { get; private set; }
public ResultsViewModel History { get; private set; } public ResultsViewModel History { get; private set; }
private string _lastQueryText;
private string _queryText; private string _queryText;
public string QueryText public string QueryText
{ {
get { return _queryText; } get => _queryText;
set set
{ {
_queryText = value; _queryText = value;
@ -315,9 +354,20 @@ namespace Flow.Launcher.ViewModel
{ {
var filtered = results.Where var filtered = results.Where
( (
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() r =>
|| StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() {
).ToList(); var match = StringMatcher.FuzzySearch(query, r.Title);
if (!match.IsSearchPrecisionScoreMet())
{
match = StringMatcher.FuzzySearch(query, r.SubTitle);
}
if (!match.IsSearchPrecisionScoreMet()) return false;
r.Score = match.Score;
return true;
}).ToList();
ContextMenu.AddResults(filtered, id); ContextMenu.AddResults(filtered, id);
} }
else else
@ -371,112 +421,128 @@ namespace Flow.Launcher.ViewModel
private void QueryResults() private void QueryResults()
{ {
if (!string.IsNullOrEmpty(QueryText)) _updateSource?.Cancel();
if (string.IsNullOrWhiteSpace(QueryText))
{ {
_updateSource?.Cancel(); Results.Clear();
var currentUpdateSource = new CancellationTokenSource(); Results.Visbility = Visibility.Collapsed;
_updateSource = currentUpdateSource; return;
var currentCancellationToken = _updateSource.Token; }
_updateToken = currentCancellationToken;
ProgressBarVisibility = Visibility.Hidden; _updateSource?.Dispose();
_isQueryRunning = true;
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins); var currentUpdateSource = new CancellationTokenSource();
if (query != null) _updateSource = currentUpdateSource;
var currentCancellationToken = _updateSource.Token;
_updateToken = currentCancellationToken;
ProgressBarVisibility = Visibility.Hidden;
_isQueryRunning = true;
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
// handle the exclusiveness of plugin using action keyword
RemoveOldQueryResults(query);
_lastQuery = query;
var plugins = PluginManager.ValidPluginsForQuery(query);
Task.Run(async () =>
{ {
// handle the exclusiveness of plugin using action keyword if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
RemoveOldQueryResults(query); {
// Wait 45 millisecond for query change in global query
// if query changes, return so that it won't be calculated
await Task.Delay(45, currentCancellationToken);
if (currentCancellationToken.IsCancellationRequested)
return;
}
_lastQuery = query; _ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
{ {
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
if (currentUpdateSource == _updateSource && _isQueryRunning) if (!currentCancellationToken.IsCancellationRequested && _isQueryRunning)
{ {
ProgressBarVisibility = Visibility.Visible; ProgressBarVisibility = Visibility.Visible;
} }
}, currentCancellationToken); }, currentCancellationToken);
var plugins = PluginManager.ValidPluginsForQuery(query); Task[] tasks = new Task[plugins.Count];
Task.Run(async () => try
{ {
// so looping will stop once it was cancelled for (var i = 0; i < plugins.Count; i++)
Task[] tasks = new Task[plugins.Count];
try
{ {
for (var i = 0; i < plugins.Count; i++) if (!plugins[i].Metadata.Disabled)
{ {
if (!plugins[i].Metadata.Disabled) tasks[i] = QueryTask(plugins[i]);
{ }
tasks[i] = QueryTask(plugins[i], query, currentCancellationToken); else
} {
else tasks[i] = Task.CompletedTask; // Avoid Null
{
tasks[i] = Task.CompletedTask; // Avoid Null
}
} }
// Check the code, WhenAll will translate all type of IEnumerable or Collection to Array, so make an array at first
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{
// nothing to do here
} }
// this should happen once after all queries are done so progress bar should continue // Check the code, WhenAll will translate all type of IEnumerable or Collection to Array, so make an array at first
// until the end of all querying await Task.WhenAll(tasks);
_isQueryRunning = false; }
catch (OperationCanceledException)
{
// nothing to do here
}
if (currentCancellationToken.IsCancellationRequested)
return;
// this should happen once after all queries are done so progress bar should continue
// until the end of all querying
_isQueryRunning = false;
if (!currentCancellationToken.IsCancellationRequested)
{
// update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
}
// Local function
async Task QueryTask(PluginPair plugin)
{
// Since it is wrapped within a Task.Run, the synchronous context is null
// Task.Yield will force it to run in ThreadPool
await Task.Yield();
var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken);
if (!currentCancellationToken.IsCancellationRequested) if (!currentCancellationToken.IsCancellationRequested)
{ _resultsUpdateQueue.Post(new ResultsForUpdate(results, plugin.Metadata, query,
// update to hidden if this is still the current query currentCancellationToken));
ProgressBarVisibility = Visibility.Hidden; }
} }, currentCancellationToken)
.ContinueWith(t => Log.Exception("|MainViewModel|Plugins Query Exceptions", t.Exception),
// Local function TaskContinuationOptions.OnlyOnFaulted);
async Task QueryTask(PluginPair plugin, Query query, CancellationToken token)
{
// Since it is wrapped within a Task.Run, the synchronous context is null
// Task.Yield will force it to run in ThreadPool
await Task.Yield();
var results = await PluginManager.QueryForPlugin(plugin, query, token);
if (!currentCancellationToken.IsCancellationRequested)
UpdateResultView(results, plugin.Metadata, query);
}
}, currentCancellationToken).ContinueWith(
t => Log.Exception("|MainViewModel|Plugins Query Exceptions", t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
}
}
else
{
Results.Clear();
Results.Visbility = Visibility.Collapsed;
}
} }
private void RemoveOldQueryResults(Query query) private void RemoveOldQueryResults(Query query)
{ {
string lastKeyword = _lastQuery.ActionKeyword; string lastKeyword = _lastQuery.ActionKeyword;
string keyword = query.ActionKeyword; string keyword = query.ActionKeyword;
if (string.IsNullOrEmpty(lastKeyword)) if (string.IsNullOrEmpty(lastKeyword))
{ {
if (!string.IsNullOrEmpty(keyword)) if (!string.IsNullOrEmpty(keyword))
{ {
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); Results.KeepResultsFor(PluginManager.NonGlobalPlugins[keyword].Metadata);
} }
} }
else else
{ {
if (string.IsNullOrEmpty(keyword)) if (string.IsNullOrEmpty(keyword))
{ {
Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata); Results.KeepResultsExcept(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
} }
else if (lastKeyword != keyword) else if (lastKeyword != keyword)
{ {
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); Results.KeepResultsFor(PluginManager.NonGlobalPlugins[keyword].Metadata);
} }
} }
} }
@ -554,7 +620,6 @@ namespace Flow.Launcher.ViewModel
return selected; return selected;
} }
private bool HistorySelected() private bool HistorySelected()
{ {
var selected = SelectedResults == History; var selected = SelectedResults == History;
@ -683,30 +748,47 @@ namespace Flow.Launcher.ViewModel
/// <summary> /// <summary>
/// To avoid deadlock, this method should not called from main thread /// To avoid deadlock, this method should not called from main thread
/// </summary> /// </summary>
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery) public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
{ {
foreach (var result in list) if (!resultsForUpdates.Any())
return;
CancellationToken token;
try
{ {
if (_topMostRecord.IsTopMost(result)) // Don't know why sometimes even resultsForUpdates is empty, the method won't return;
token = resultsForUpdates.Select(r => r.Token).Distinct().SingleOrDefault();
}
#if DEBUG
catch
{
throw new ArgumentException("Unacceptable token");
}
#else
catch
{
token = default;
}
#endif
foreach (var metaResults in resultsForUpdates)
{
foreach (var result in metaResults.Results)
{ {
result.Score = int.MaxValue; if (_topMostRecord.IsTopMost(result))
} {
else result.Score = int.MaxValue;
{ }
var priorityScore = metadata.Priority * 150; else
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore; {
var priorityScore = metaResults.Metadata.Priority * 150;
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore;
}
} }
} }
if (originQuery.RawQuery == _lastQuery.RawQuery) Results.AddResults(resultsForUpdates, token);
{
Results.AddResults(list, metadata.ID);
}
if (Results.Visbility != Visibility.Visible && list.Count > 0)
{
Results.Visbility = Visibility.Visible;
}
} }
#endregion #endregion

View file

@ -1,9 +1,7 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Image; using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
@ -106,14 +104,10 @@ namespace Flow.Launcher.ViewModel
} }
if (ImageLoader.CacheContainImage(imagePath)) if (ImageLoader.CacheContainImage(imagePath))
{
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate // will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath); return ImageLoader.Load(imagePath);
}
else return await Task.Run(() => ImageLoader.Load(imagePath));
{
return await Task.Run(() => ImageLoader.Load(imagePath));
}
} }
public Result Result { get; } public Result Result { get; }

View file

@ -0,0 +1,35 @@
using Flow.Launcher.Plugin;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Flow.Launcher.ViewModel
{
public class ResultsForUpdate
{
public List<Result> Results { get; }
public PluginMetadata Metadata { get; }
public string ID { get; }
public Query Query { get; }
public CancellationToken Token { get; }
public ResultsForUpdate(List<Result> results, string resultID, CancellationToken token)
{
Results = results;
ID = resultID;
Token = token;
}
public ResultsForUpdate(List<Result> results, PluginMetadata metadata, Query query, CancellationToken token)
{
Results = results;
Metadata = metadata;
Query = query;
Token = token;
ID = metadata.ID;
}
}
}

View file

@ -1,7 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -17,7 +20,6 @@ namespace Flow.Launcher.ViewModel
public ResultCollection Results { get; } public ResultCollection Results { get; }
private readonly object _addResultsLock = new object();
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;
@ -116,17 +118,20 @@ namespace Flow.Launcher.ViewModel
public void Clear() public void Clear()
{ {
Results.Clear(); lock (_collectionLock)
Results.RemoveAll();
} }
public void RemoveResultsExcept(PluginMetadata metadata) public void KeepResultsFor(PluginMetadata metadata)
{ {
Results.RemoveAll(r => r.Result.PluginID != metadata.ID); lock (_collectionLock)
Results.Update(Results.Where(r => r.Result.PluginID == metadata.ID).ToList());
} }
public void RemoveResultsFor(PluginMetadata metadata) public void KeepResultsExcept(PluginMetadata metadata)
{ {
Results.RemoveAll(r => r.Result.PluginID == metadata.ID); lock (_collectionLock)
Results.Update(Results.Where(r => r.Result.PluginID != metadata.ID).ToList());
} }
/// <summary> /// <summary>
@ -134,70 +139,99 @@ namespace Flow.Launcher.ViewModel
/// </summary> /// </summary>
public void AddResults(List<Result> newRawResults, string resultId) public void AddResults(List<Result> newRawResults, string resultId)
{ {
lock (_addResultsLock) lock (_collectionLock)
{ {
var newResults = NewResults(newRawResults, resultId); var newResults = NewResults(newRawResults, resultId);
// update UI in one run, so it can avoid UI flickering // https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ff71969-f183-4744-909d-50f7cd414954/binding-a-tabcontrols-selectedindex-not-working?forum=wpf
Results.Update(newResults); // fix selected index flow
var updateTask = Task.Run(() =>
if (Results.Count > 0)
{ {
// update UI in one run, so it can avoid UI flickering
Results.Update(newResults);
if (Results.Any())
SelectedItem = Results[0];
});
if (!updateTask.Wait(300))
{
updateTask.Dispose();
throw new TimeoutException("Update result use too much time.");
}
}
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, CancellationToken token)
{
var newResults = NewResults(resultsForUpdates);
if (token.IsCancellationRequested)
return;
lock (_collectionLock)
{
// update UI in one run, so it can avoid UI flickering
Results.Update(newResults, token);
if (Results.Any())
SelectedItem = Results[0];
}
switch (Visbility)
{
case Visibility.Collapsed when Results.Count > 0:
Margin = new Thickness { Top = 8 }; Margin = new Thickness { Top = 8 };
SelectedIndex = 0; SelectedIndex = 0;
} Visbility = Visibility.Visible;
else break;
{ case Visibility.Visible when Results.Count == 0:
Margin = new Thickness { Top = 0 }; Margin = new Thickness { Top = 0 };
} Visbility = Visibility.Collapsed;
break;
} }
} }
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId) private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
{ {
var results = Results.ToList(); if (newRawResults.Count == 0)
return Results.ToList();
var results = Results as IEnumerable<ResultViewModel>;
var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings)).ToList(); var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings)).ToList();
var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList();
// Find the same results in A (old results) and B (new newResults) return results.Where(r => r.Result.PluginID != resultId)
var sameResults = oldResults .Concat(results.Intersect(newResults).Union(newResults))
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result))) .OrderByDescending(r => r.Result.Score)
.ToList(); .ToList();
}
// remove result of relative complement of B in A private List<ResultViewModel> NewResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
foreach (var result in oldResults.Except(sameResults)) {
{ if (!resultsForUpdates.Any())
results.Remove(result); return Results.ToList();
}
// update result with B's score and index position var results = Results as IEnumerable<ResultViewModel>;
foreach (var sameResult in sameResults)
{
int oldIndex = results.IndexOf(sameResult);
int oldScore = results[oldIndex].Result.Score;
var newResult = newResults[newResults.IndexOf(sameResult)];
int newScore = newResult.Result.Score;
if (newScore != oldScore)
{
var oldResult = results[oldIndex];
oldResult.Result.Score = newScore; return results.Where(r => r != null && !resultsForUpdates.Any(u => u.Metadata.ID == r.Result.PluginID))
oldResult.Result.OriginQuery = newResult.Result.OriginQuery; .Concat(
resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings)))
results.RemoveAt(oldIndex); .OrderByDescending(rv => rv.Result.Score)
int newIndex = InsertIndexOf(newScore, results); .ToList();
results.Insert(newIndex, oldResult);
}
}
// insert result in relative complement of A in B
foreach (var result in newResults.Except(sameResults))
{
int newIndex = InsertIndexOf(result.Result.Score, results);
results.Insert(newIndex, result);
}
return results;
} }
#endregion #endregion
@ -234,58 +268,71 @@ namespace Flow.Launcher.ViewModel
public class ResultCollection : ObservableCollection<ResultViewModel> public class ResultCollection : ObservableCollection<ResultViewModel>
{ {
private long editTime = 0;
public void RemoveAll(Predicate<ResultViewModel> predicate) private bool _suppressNotifying = false;
private CancellationToken _token;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{ {
CheckReentrancy(); if (!_suppressNotifying)
for (int i = Count - 1; i >= 0; i--)
{ {
if (predicate(this[i])) base.OnCollectionChanged(e);
{
RemoveAt(i);
}
} }
} }
public void BulkAddRange(IEnumerable<ResultViewModel> resultViews)
{
// suppress notifying before adding all element
_suppressNotifying = true;
foreach (var item in resultViews)
{
Add(item);
}
_suppressNotifying = false;
// manually update event
// wpf use directx / double buffered already, so just reset all won't cause ui flickering
if (_token.IsCancellationRequested)
return;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public void AddRange(IEnumerable<ResultViewModel> Items)
{
foreach (var item in Items)
{
if (_token.IsCancellationRequested)
return;
Add(item);
}
}
public void RemoveAll()
{
ClearItems();
}
/// <summary> /// <summary>
/// Update the results collection with new results, try to keep identical results /// Update the results collection with new results, try to keep identical results
/// </summary> /// </summary>
/// <param name="newItems"></param> /// <param name="newItems"></param>
public void Update(List<ResultViewModel> newItems) public void Update(List<ResultViewModel> newItems, CancellationToken token = default)
{ {
int newCount = newItems.Count; _token = token;
int oldCount = Items.Count; if (Count == 0 && newItems.Count == 0 || _token.IsCancellationRequested)
int location = newCount > oldCount ? oldCount : newCount; return;
for (int i = 0; i < location; i++) if (editTime < 10 || newItems.Count < 30)
{ {
ResultViewModel oldResult = this[i]; if (Count != 0) ClearItems();
ResultViewModel newResult = newItems[i]; AddRange(newItems);
if (!oldResult.Equals(newResult)) editTime++;
{ // result is not the same update it in the current index return;
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 else
{ {
for (int i = oldCount - 1; i >= newCount; i--) Clear();
{ BulkAddRange(newItems);
RemoveAt(i); editTime++;
}
} }
} }
} }

View file

@ -7,7 +7,7 @@
"Name": "Explorer", "Name": "Explorer",
"Description": "Search and manage files and folders. Explorer utilises Windows Index Search", "Description": "Search and manage files and folders. Explorer utilises Windows Index Search",
"Author": "Jeremy Wu", "Author": "Jeremy Wu",
"Version": "1.2.6", "Version": "1.3.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.Explorer.dll", "ExecuteFileName": "Flow.Launcher.Plugin.Explorer.dll",

View file

@ -37,8 +37,17 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = viewModel.Settings; Settings = viewModel.Settings;
contextMenu = new ContextMenu(Context); contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings); pluginManager = new PluginsManager(Context, Settings);
await pluginManager.UpdateManifest(); var updateManifestTask = pluginManager.UpdateManifest();
lastUpdateTime = DateTime.Now; if (await Task.WhenAny(updateManifestTask, Task.Delay(500)) == updateManifestTask)
{
lastUpdateTime = DateTime.Now;
}
else
{
context.API.ShowMsg("Plugin Manifest Download Fail.",
@"Please check internet transmission with Github.com.
You may not be able to Install and Update Plugin.", pluginManager.icoPath);
}
} }
public List<Result> LoadContextMenus(Result selectedResult) public List<Result> LoadContextMenus(Result selectedResult)
@ -61,7 +70,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
return search switch return search switch
{ {
var s when s.StartsWith(Settings.HotKeyInstall) => pluginManager.RequestInstallOrUpdate(s), var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token),
var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s), var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s),
var s when s.StartsWith(Settings.HotkeyUpdate) => pluginManager.RequestUpdate(s), var s when s.StartsWith(Settings.HotkeyUpdate) => pluginManager.RequestUpdate(s),
_ => pluginManager.GetDefaultHotKeys().Where(hotkey => _ => pluginManager.GetDefaultHotKeys().Where(hotkey =>

View file

@ -7,6 +7,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -36,7 +37,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
} }
} }
private readonly string icoPath = "Images\\pluginsmanager.png"; internal readonly string icoPath = "Images\\pluginsmanager.png";
internal PluginsManager(PluginInitContext context, Settings settings) internal PluginsManager(PluginInitContext context, Settings settings)
{ {
@ -64,27 +65,27 @@ namespace Flow.Launcher.Plugin.PluginsManager
return false; return false;
} }
}, },
new Result() new Result()
{
Title = Settings.HotkeyUninstall,
IcoPath = icoPath,
Action = _ =>
{ {
Title = Settings.HotkeyUninstall, Context.API.ChangeQuery("pm uninstall ");
IcoPath = icoPath, return false;
Action = _ =>
{
Context.API.ChangeQuery("pm uninstall ");
return false;
}
},
new Result()
{
Title = Settings.HotkeyUpdate,
IcoPath = icoPath,
Action = _ =>
{
Context.API.ChangeQuery("pm update ");
return false;
}
} }
}; },
new Result()
{
Title = Settings.HotkeyUpdate,
IcoPath = icoPath,
Action = _ =>
{
Context.API.ChangeQuery("pm update ");
return false;
}
}
};
} }
internal async Task InstallOrUpdate(UserPlugin plugin) internal async Task InstallOrUpdate(UserPlugin plugin)
@ -137,7 +138,8 @@ namespace Flow.Launcher.Plugin.PluginsManager
catch (Exception e) catch (Exception e)
{ {
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"), Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"), plugin.Name)); string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
plugin.Name));
Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "InstallOrUpdate"); Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "InstallOrUpdate");
@ -164,7 +166,8 @@ namespace Flow.Launcher.Plugin.PluginsManager
from existingPlugin in Context.API.GetAllPlugins() from existingPlugin in Context.API.GetAllPlugins()
join pluginFromManifest in pluginsManifest.UserPlugins join pluginFromManifest in pluginsManifest.UserPlugins
on existingPlugin.Metadata.ID equals pluginFromManifest.ID on existingPlugin.Metadata.ID equals pluginFromManifest.ID
where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) < 0 // if current version precedes manifest version where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
0 // if current version precedes manifest version
select select
new new
{ {
@ -214,22 +217,29 @@ namespace Flow.Launcher.Plugin.PluginsManager
Task.Run(async delegate Task.Run(async delegate
{ {
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), Context.API.ShowMsg(
Context.API.GetTranslation("plugin_pluginsmanager_please_wait")); Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false); await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath)
.ConfigureAwait(false);
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), Context.API.ShowMsg(
Context.API.GetTranslation("plugin_pluginsmanager_download_success")); Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
Install(x.PluginNewUserPlugin, downloadToFilePath); Install(x.PluginNewUserPlugin, downloadToFilePath);
Context.API.RestartApp(); Context.API.RestartApp();
}).ContinueWith(t => }).ContinueWith(t =>
{ {
Log.Exception("PluginsManager", $"Update failed for {x.Name}", t.Exception.InnerException, "RequestUpdate"); Log.Exception("PluginsManager", $"Update failed for {x.Name}",
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"), t.Exception.InnerException, "RequestUpdate");
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"), x.Name)); Context.API.ShowMsg(
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
string.Format(
Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
x.Name));
}, TaskContinuationOptions.OnlyOnFaulted); }, TaskContinuationOptions.OnlyOnFaulted);
return true; return true;
@ -264,8 +274,21 @@ namespace Flow.Launcher.Plugin.PluginsManager
.ToList(); .ToList();
} }
internal List<Result> RequestInstallOrUpdate(string searchName) private Task _downloadManifestTask = Task.CompletedTask;
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
{ {
if (!pluginsManifest.UserPlugins.Any() &&
_downloadManifestTask.Status != TaskStatus.Running)
{
_downloadManifestTask = pluginsManifest.DownloadManifest();
}
await _downloadManifestTask;
if (token.IsCancellationRequested)
return null;
var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim(); var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim();
var results = var results =

View file

@ -6,7 +6,7 @@
"Name": "Plugins Manager", "Name": "Plugins Manager",
"Description": "Management of installing, uninstalling or updating Flow Launcher plugins", "Description": "Management of installing, uninstalling or updating Flow Launcher plugins",
"Author": "Jeremy Wu", "Author": "Jeremy Wu",
"Version": "1.5.0", "Version": "1.6.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.PluginsManager.dll", "ExecuteFileName": "Flow.Launcher.Plugin.PluginsManager.dll",

View file

@ -4,7 +4,7 @@
"Name": "Program", "Name": "Program",
"Description": "Search programs in Flow.Launcher", "Description": "Search programs in Flow.Launcher",
"Author": "qianlifeng", "Author": "qianlifeng",
"Version": "1.2.3", "Version": "1.3.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.Program.dll", "ExecuteFileName": "Flow.Launcher.Plugin.Program.dll",

View file

@ -4,7 +4,7 @@
"Name": "System Commands", "Name": "System Commands",
"Description": "Provide System related commands. e.g. shutdown,lock,setting etc.", "Description": "Provide System related commands. e.g. shutdown,lock,setting etc.",
"Author": "qianlifeng", "Author": "qianlifeng",
"Version": "1.1.2", "Version": "1.2.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.Sys.dll", "ExecuteFileName": "Flow.Launcher.Plugin.Sys.dll",

View file

@ -25,7 +25,7 @@
"Name": "Web Searches", "Name": "Web Searches",
"Description": "Provide the web search ability", "Description": "Provide the web search ability",
"Author": "qianlifeng", "Author": "qianlifeng",
"Version": "1.2.1", "Version": "1.3.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll", "ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",