Merge branch 'dev' into fix_errormsg_delay

This commit is contained in:
Jeremy Wu 2021-01-21 13:37:45 +11:00
commit a939087367
19 changed files with 565 additions and 231 deletions

View file

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

View file

@ -50,14 +50,18 @@ namespace Flow.Launcher.Infrastructure.Logger
return valid;
}
[MethodImpl(MethodImplOptions.Synchronized)]
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);
ExceptionInternal(classNameWithMethod, message, exception);
#endif
}
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,

View file

@ -14,10 +14,10 @@
</PropertyGroup>
<PropertyGroup>
<Version>1.3.1</Version>
<PackageVersion>1.3.1</PackageVersion>
<AssemblyVersion>1.3.1</AssemblyVersion>
<FileVersion>1.3.1</FileVersion>
<Version>1.4.0</Version>
<PackageVersion>1.4.0</PackageVersion>
<AssemblyVersion>1.4.0</AssemblyVersion>
<FileVersion>1.4.0</FileVersion>
<PackageId>Flow.Launcher.Plugin</PackageId>
<Authors>Flow-Launcher</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@ -64,4 +64,4 @@
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
</ItemGroup>
</Project>
</Project>

View file

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

View file

@ -0,0 +1,63 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Themes/Base.xaml" />
</ResourceDictionary.MergedDictionaries>
<system:Boolean x:Key="ThemeBlurEnabled">True</system:Boolean>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="#FFFFFFFF" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style x:Key="QuerySuggestionBoxStyle" BasedOn="{StaticResource BaseQuerySuggestionBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="LightGray" />
</Style>
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Black" Opacity="0.6"/>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="WindowStyle" BasedOn="{StaticResource BaseWindowStyle}" TargetType="{x:Type Window}">
<Setter Property="Width" Value="750" /> <!-- This is used to set the blur width only -->
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Black" Opacity="0.6"/>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="PendingLineStyle" BasedOn="{StaticResource BasePendingLineStyle}" TargetType="{x:Type Line}">
</Style>
<!-- Item Style -->
<Style x:Key="ItemTitleStyle" BasedOn="{StaticResource BaseItemTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="0, -10"/>
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>
<Style x:Key="ItemSubTitleStyle" BasedOn="{StaticResource BaseItemSubTitleStyle}" TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>
<Style x:Key="ItemTitleSelectedStyle" BasedOn="{StaticResource BaseItemTitleSelectedStyle}" TargetType="{x:Type TextBlock}" >
<Setter Property="Margin" Value="0, -10"/>
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>
<Style x:Key="ItemSubTitleSelectedStyle" BasedOn="{StaticResource BaseItemSubTitleSelectedStyle}" TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#356ef3</SolidColorBrush>
<!-- button style in the middle of the scrollbar -->
<Style x:Key="ThumbStyle" BasedOn="{StaticResource BaseThumbStyle}" TargetType="{x:Type Thumb}">
</Style>
<Style x:Key="ScrollBarStyle" BasedOn="{StaticResource BaseScrollBarStyle}" TargetType="{x:Type ScrollBar}">
<Setter Property="Background" Value="#a0a0a0"/>
</Style>
</ResourceDictionary>

View file

@ -17,7 +17,7 @@
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" Opacity="0.1"/>
<SolidColorBrush Color="White" Opacity="0.5"/>
</Setter.Value>
</Setter>
</Style>
@ -26,7 +26,7 @@
<Setter Property="Width" Value="750" /> <!-- This is used to set the blur width only -->
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" Opacity="0.1"/>
<SolidColorBrush Color="White" Opacity="0.5"/>
</Setter.Value>
</Setter>
</Style>

View file

@ -4,21 +4,19 @@
<ResourceDictionary Source="pack://application:,,,/Themes/Base.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#EDEDED" />
<Setter Property="Background" Value="LightGray" />
<Setter Property="Foreground" Value="#222222" />
<Setter Property="FontSize" Value="38" />
</Style>
<Style x:Key="QuerySuggestionBoxStyle" BasedOn="{StaticResource BaseQuerySuggestionBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#EDEDED" />
<Setter Property="Foreground" Value="#222222" />
<Setter Property="FontSize" Value="38" />
<Setter Property="Background" Value="LightGray" />
<Setter Property="Foreground" Value="#6E6E6E" />
</Style>
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="#B0B0B0" />
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Background" Value="#EDEDED"></Setter>
<Setter Property="Background" Value="LightGray"></Setter>
</Style>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource BaseWindowStyle}" >
</Style>
@ -31,15 +29,15 @@
<Setter Property="Foreground" Value="#A6A6A6" />
</Style>
<Style x:Key="ItemSubTitleStyle" BasedOn="{StaticResource BaseItemSubTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#A6A6A6" />
<Setter Property="Foreground" Value="#6B6B6B" />
</Style>
<Style x:Key="ItemTitleSelectedStyle" BasedOn="{StaticResource BaseItemTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FFFFF8" />
<Setter Property="Foreground" Value="White" />
</Style>
<Style x:Key="ItemSubTitleSelectedStyle" BasedOn="{StaticResource BaseItemSubTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#ffffff" />
<Setter Property="Foreground" Value="#EDEDED" />
</Style>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#00AAF6</SolidColorBrush>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#787878</SolidColorBrush>
<Style x:Key="ThumbStyle" BasedOn="{StaticResource BaseThumbStyle}" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>

View file

@ -6,19 +6,19 @@
</ResourceDictionary.MergedDictionaries>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#EBEBEB"/>
<Setter Property="Background" Value="White "/>
<Setter Property="Foreground" Value="#000000" />
</Style>
<Style x:Key="QuerySuggestionBoxStyle" BasedOn="{StaticResource BaseQuerySuggestionBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#EBEBEB"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="#000000" />
</Style>
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="Background" Value="#ffffff"></Setter>
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="Background" Value="White"></Setter>
</Style>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource BaseWindowStyle}" >
</Style>
@ -38,7 +38,7 @@
<Style x:Key="ItemSubTitleSelectedStyle" BasedOn="{StaticResource BaseItemSubTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#F6F6FF" />
</Style>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#3875D7</SolidColorBrush>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#909090</SolidColorBrush>
<!-- button style in the middle of the scrollbar -->
<Style x:Key="ThumbStyle" BasedOn="{StaticResource BaseThumbStyle}" TargetType="{x:Type Thumb}">

View file

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Themes/Base.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#2e3440" />
<Setter Property="Foreground" Value="#eceff4" />
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="QuerySuggestionBoxStyle" BasedOn="{StaticResource BaseQuerySuggestionBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#2e3440" />
<Setter Property="Foreground" Value="#eceff4" />
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="#4c566a" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Background" Value="#2e3440"></Setter>
</Style>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource BaseWindowStyle}" >
</Style>
<Style x:Key="PendingLineStyle" BasedOn="{StaticResource BasePendingLineStyle}" TargetType="{x:Type Line}" />
<Style x:Key="ItemTitleStyle" BasedOn="{StaticResource BaseItemTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#e5e9f0" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style x:Key="ItemNumberStyle" BasedOn="{StaticResource BaseItemNumberStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#A6A6A6" />
</Style>
<Style x:Key="ItemSubTitleStyle" BasedOn="{StaticResource BaseItemSubTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#d8dee9" />
</Style>
<Style x:Key="ItemTitleSelectedStyle" BasedOn="{StaticResource BaseItemTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#2e3440" />
</Style>
<Style x:Key="ItemSubTitleSelectedStyle" BasedOn="{StaticResource BaseItemSubTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#4c566a" />
</Style>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#5e81ac</SolidColorBrush>
<Style x:Key="ThumbStyle" BasedOn="{StaticResource BaseThumbStyle}" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border CornerRadius="2" DockPanel.Dock="Right" Background="#4c566a" BorderBrush="Transparent" BorderThickness="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ScrollBarStyle" BasedOn="{StaticResource BaseScrollBarStyle}" TargetType="{x:Type ScrollBar}" >
<Setter Property="Width" Value="3"/>
</Style>
</ResourceDictionary>

View file

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Themes/Base.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#4c566a" />
<Setter Property="Foreground" Value="#eceff4" />
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="QuerySuggestionBoxStyle" BasedOn="{StaticResource BaseQuerySuggestionBoxStyle}" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#4c566a" />
<Setter Property="Foreground" Value="#eceff4" />
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="WindowBorderStyle" BasedOn="{StaticResource BaseWindowBorderStyle}" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="#2e3440" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Background" Value="#4c566a"></Setter>
</Style>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource BaseWindowStyle}" >
</Style>
<Style x:Key="PendingLineStyle" BasedOn="{StaticResource BasePendingLineStyle}" TargetType="{x:Type Line}" />
<Style x:Key="ItemTitleStyle" BasedOn="{StaticResource BaseItemTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#e5e9f0" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style x:Key="ItemNumberStyle" BasedOn="{StaticResource BaseItemNumberStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#A6A6A6" />
</Style>
<Style x:Key="ItemSubTitleStyle" BasedOn="{StaticResource BaseItemSubTitleStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#d8dee9" />
</Style>
<Style x:Key="ItemTitleSelectedStyle" BasedOn="{StaticResource BaseItemTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#2e3440" />
</Style>
<Style x:Key="ItemSubTitleSelectedStyle" BasedOn="{StaticResource BaseItemSubTitleSelectedStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#4c566a" />
</Style>
<SolidColorBrush x:Key="ItemSelectedBackgroundColor">#5e81ac</SolidColorBrush>
<Style x:Key="ThumbStyle" BasedOn="{StaticResource BaseThumbStyle}" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border CornerRadius="2" DockPanel.Dock="Right" Background="#2e3440" BorderBrush="Transparent" BorderThickness="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ScrollBarStyle" BasedOn="{StaticResource BaseScrollBarStyle}" TargetType="{x:Type ScrollBar}" >
<Setter Property="Width" Value="3"/>
</Style>
</ResourceDictionary>

View file

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -19,9 +17,8 @@ using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Storage;
using System.Windows.Media;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
using System.Threading.Tasks.Dataflow;
namespace Flow.Launcher.ViewModel
{
@ -48,6 +45,8 @@ namespace Flow.Launcher.ViewModel
private bool _saved;
private readonly Internationalization _translator = InternationalizationManager.Instance;
private BufferBlock<ResultsForUpdate> _resultsUpdateQueue;
private Task _resultsViewUpdateTask;
#endregion
@ -75,8 +74,11 @@ namespace Flow.Launcher.ViewModel
_selectedResults = Results;
InitializeKeyCommands();
RegisterViewUpdate();
RegisterResultsUpdatedEvent();
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
SetOpenResultModifiers();
@ -89,15 +91,51 @@ namespace Flow.Launcher.ViewModel
var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) =>
{
Task.Run(() =>
{
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
UpdateResultView(e.Results, pair.Metadata, e.Query);
}, _updateToken);
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
if (e.Query.Search == _lastQuery.Search)
_resultsUpdateQueue.Post(new ResultsForUpdate(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()
{
@ -195,12 +233,13 @@ namespace Flow.Launcher.ViewModel
public ResultsViewModel Results { get; private set; }
public ResultsViewModel ContextMenu { get; private set; }
public ResultsViewModel History { get; private set; }
private string _lastQueryText;
private string _queryText;
public string QueryText
{
get { return _queryText; }
get => _queryText;
set
{
_queryText = value;
@ -315,9 +354,20 @@ namespace Flow.Launcher.ViewModel
{
var filtered = results.Where
(
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet()
|| StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
).ToList();
r =>
{
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);
}
else
@ -371,112 +421,128 @@ namespace Flow.Launcher.ViewModel
private void QueryResults()
{
if (!string.IsNullOrEmpty(QueryText))
_updateSource?.Cancel();
if (string.IsNullOrWhiteSpace(QueryText))
{
_updateSource?.Cancel();
var currentUpdateSource = new CancellationTokenSource();
_updateSource = currentUpdateSource;
var currentCancellationToken = _updateSource.Token;
_updateToken = currentCancellationToken;
Results.Clear();
Results.Visbility = Visibility.Collapsed;
return;
}
ProgressBarVisibility = Visibility.Hidden;
_isQueryRunning = true;
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
if (query != null)
_updateSource?.Dispose();
var currentUpdateSource = new CancellationTokenSource();
_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
RemoveOldQueryResults(query);
if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
{
// 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
if (currentUpdateSource == _updateSource && _isQueryRunning)
if (!currentCancellationToken.IsCancellationRequested && _isQueryRunning)
{
ProgressBarVisibility = Visibility.Visible;
}
}, currentCancellationToken);
var plugins = PluginManager.ValidPluginsForQuery(query);
Task.Run(async () =>
Task[] tasks = new Task[plugins.Count];
try
{
// so looping will stop once it was cancelled
Task[] tasks = new Task[plugins.Count];
try
for (var i = 0; i < plugins.Count; i++)
{
for (var i = 0; i < plugins.Count; i++)
if (!plugins[i].Metadata.Disabled)
{
if (!plugins[i].Metadata.Disabled)
{
tasks[i] = QueryTask(plugins[i], query, currentCancellationToken);
}
else
{
tasks[i] = Task.CompletedTask; // Avoid Null
}
tasks[i] = QueryTask(plugins[i]);
}
else
{
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
// until the end of all querying
_isQueryRunning = false;
// 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
}
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)
{
// update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
}
// Local function
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;
}
_resultsUpdateQueue.Post(new ResultsForUpdate(results, plugin.Metadata, query,
currentCancellationToken));
}
}, currentCancellationToken)
.ContinueWith(t => Log.Exception("|MainViewModel|Plugins Query Exceptions", t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
}
private void RemoveOldQueryResults(Query query)
{
string lastKeyword = _lastQuery.ActionKeyword;
string keyword = query.ActionKeyword;
if (string.IsNullOrEmpty(lastKeyword))
{
if (!string.IsNullOrEmpty(keyword))
{
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
Results.KeepResultsFor(PluginManager.NonGlobalPlugins[keyword].Metadata);
}
}
else
{
if (string.IsNullOrEmpty(keyword))
{
Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
Results.KeepResultsExcept(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
}
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;
}
private bool HistorySelected()
{
var selected = SelectedResults == History;
@ -683,30 +748,47 @@ namespace Flow.Launcher.ViewModel
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </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;
}
else
{
var priorityScore = metadata.Priority * 150;
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore;
if (_topMostRecord.IsTopMost(result))
{
result.Score = int.MaxValue;
}
else
{
var priorityScore = metaResults.Metadata.Priority * 150;
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore;
}
}
}
if (originQuery.RawQuery == _lastQuery.RawQuery)
{
Results.AddResults(list, metadata.ID);
}
if (Results.Visbility != Visibility.Visible && list.Count > 0)
{
Results.Visbility = Visibility.Visible;
}
Results.AddResults(resultsForUpdates, token);
}
#endregion

View file

@ -1,9 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
@ -106,14 +104,10 @@ namespace Flow.Launcher.ViewModel
}
if (ImageLoader.CacheContainImage(imagePath))
{
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
}
else
{
return await Task.Run(() => ImageLoader.Load(imagePath));
}
return await Task.Run(() => ImageLoader.Load(imagePath));
}
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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@ -17,7 +20,6 @@ namespace Flow.Launcher.ViewModel
public ResultCollection Results { get; }
private readonly object _addResultsLock = new object();
private readonly object _collectionLock = new object();
private readonly Settings _settings;
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
@ -116,17 +118,20 @@ namespace Flow.Launcher.ViewModel
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>
@ -134,70 +139,99 @@ namespace Flow.Launcher.ViewModel
/// </summary>
public void AddResults(List<Result> newRawResults, string resultId)
{
lock (_addResultsLock)
lock (_collectionLock)
{
var newResults = NewResults(newRawResults, resultId);
// update UI in one run, so it can avoid UI flickering
Results.Update(newResults);
if (Results.Count > 0)
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ff71969-f183-4744-909d-50f7cd414954/binding-a-tabcontrols-selectedindex-not-working?forum=wpf
// fix selected index flow
var updateTask = Task.Run(() =>
{
// 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 };
SelectedIndex = 0;
}
else
{
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)
{
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 oldResults = results.Where(r => r.Result.PluginID == resultId).ToList();
// Find the same results in A (old results) and B (new newResults)
var sameResults = oldResults
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result)))
.ToList();
return results.Where(r => r.Result.PluginID != resultId)
.Concat(results.Intersect(newResults).Union(newResults))
.OrderByDescending(r => r.Result.Score)
.ToList();
}
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(sameResults))
{
results.Remove(result);
}
private List<ResultViewModel> NewResults(IEnumerable<ResultsForUpdate> resultsForUpdates)
{
if (!resultsForUpdates.Any())
return Results.ToList();
// update result with B's score and index position
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];
var results = Results as IEnumerable<ResultViewModel>;
oldResult.Result.Score = newScore;
oldResult.Result.OriginQuery = newResult.Result.OriginQuery;
results.RemoveAt(oldIndex);
int newIndex = InsertIndexOf(newScore, results);
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;
return results.Where(r => r != null && !resultsForUpdates.Any(u => u.Metadata.ID == r.Result.PluginID))
.Concat(
resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings)))
.OrderByDescending(rv => rv.Result.Score)
.ToList();
}
#endregion
@ -234,58 +268,71 @@ namespace Flow.Launcher.ViewModel
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();
for (int i = Count - 1; i >= 0; i--)
if (!_suppressNotifying)
{
if (predicate(this[i]))
{
RemoveAt(i);
}
base.OnCollectionChanged(e);
}
}
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>
/// Update the results collection with new results, try to keep identical results
/// </summary>
/// <param name="newItems"></param>
public void Update(List<ResultViewModel> newItems)
public void Update(List<ResultViewModel> newItems, CancellationToken token = default)
{
int newCount = newItems.Count;
int oldCount = Items.Count;
int location = newCount > oldCount ? oldCount : newCount;
_token = token;
if (Count == 0 && newItems.Count == 0 || _token.IsCancellationRequested)
return;
for (int i = 0; i < location; i++)
if (editTime < 10 || newItems.Count < 30)
{
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]);
}
if (Count != 0) ClearItems();
AddRange(newItems);
editTime++;
return;
}
else
{
for (int i = oldCount - 1; i >= newCount; i--)
{
RemoveAt(i);
}
Clear();
BulkAddRange(newItems);
editTime++;
}
}
}

View file

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

View file

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

View file

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

View file

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

View file

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