Skip to content

Commit

Permalink
Merge pull request #2900 from Wox-launcher/bao
Browse files Browse the repository at this point in the history
query and startup performance
  • Loading branch information
bao-qian committed Apr 30, 2020
2 parents 324dc8e + be07026 commit 2f8dc3e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
21 changes: 6 additions & 15 deletions Plugins/Wox.Plugin.Program/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void preloadPrograms()
});
Logger.WoxInfo($"Number of preload win32 programs <{_win32s.Length}>");
Logger.WoxInfo($"Number of preload uwps <{_uwps.Length}>");
}
}

public void Save()
{
Expand All @@ -55,11 +55,8 @@ public List<Result> Query(Query query)
{
Win32[] win32;
UWP.Application[] uwps;
lock (IndexLock)
{ // just take the reference inside the lock to eliminate query time issues.
win32 = _win32s;
uwps = _uwps;
}
win32 = _win32s;
uwps = _uwps;

var results1 = win32.AsParallel()
.Where(p => p.Enabled)
Expand Down Expand Up @@ -109,10 +106,7 @@ public void loadSettings()
public static void IndexWin32Programs()
{
var win32S = Win32.All(_settings);
lock (IndexLock)
{
_win32s = win32S;
}
_win32s = win32S;
}

public static void IndexUWPPrograms()
Expand All @@ -122,10 +116,7 @@ public static void IndexUWPPrograms()

var applications = support ? UWP.All() : new UWP.Application[] { };
//var applications = new UWP.Application[] { };
lock (IndexLock)
{
_uwps = applications;
}
_uwps = applications;
}

public static void IndexPrograms()
Expand Down Expand Up @@ -181,7 +172,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
return menuOptions;
}


public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)
{
try
Expand Down
56 changes: 41 additions & 15 deletions Wox/Converters/HighlightTextConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,72 @@
using System.Windows.Documents;
using System.Windows.Media;
using Wox.Core.Resource;
using Wox.Infrastructure.UserSettings;

namespace Wox.Converters
{
internal class HightLightStyle
{
public Brush Color { get; set; }
public FontStyle FontStyle { get; set; }
public FontWeight FontWeight { get; set; }
public FontStretch FontStretch { get; set; }

internal HightLightStyle(bool selected)
{
var app = Application.Current as App;
Settings settings = app.Settings;
ResourceDictionary resources = app.Resources;

Color = (Brush)(selected ?
resources.Contains("ItemSelectedHighlightColor") ? resources["ItemSelectedHighlightColor"] : resources["BaseItemSelectedHighlightColor"] :
resources.Contains("ItemHighlightColor") ? resources["ItemHighlightColor"] : resources["BaseItemHighlightColor"]);
FontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(settings.ResultHighlightFontStyle);
FontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(settings.ResultHighlightFontWeight);
FontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(settings.ResultHighlightFontStretch);
}

}
public class HighlightTextConverter : IMultiValueConverter
{
private static Lazy<HightLightStyle> _highLightStyle = new Lazy<HightLightStyle>(() => new HightLightStyle(false));
private static Lazy<HightLightStyle> _highLightSelectedStyle = new Lazy<HightLightStyle>(() => new HightLightStyle(true));

public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
{
var text = value[0] as string;
var highlightData = value[1] as List<int>;
var selected = value[2] as bool? == true;

var textBlock = new Span();

if (highlightData == null || !highlightData.Any())
{
// No highlight data, just return the text
return new Run(text);
}

var settings = (Application.Current as App).Settings;
var resources = ThemeManager.Instance.GetResourceDictionary();

var highlightColor = (Brush) (selected?
resources.Contains("ItemSelectedHighlightColor")? resources["ItemSelectedHighlightColor"]: resources["BaseItemSelectedHighlightColor"]:
resources.Contains("ItemHighlightColor")? resources["ItemHighlightColor"]: resources["BaseItemHighlightColor"]);
var highlightStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(settings.ResultHighlightFontStyle);
var highlightWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(settings.ResultHighlightFontWeight);
var highlightStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(settings.ResultHighlightFontStretch);

HightLightStyle style;
if (selected)
{
style = _highLightSelectedStyle.Value;
}
else
{
style = _highLightStyle.Value;
}

var textBlock = new Span();
for (var i = 0; i < text.Length; i++)
{
var currentCharacter = text.Substring(i, 1);
if (this.ShouldHighlight(highlightData, i))
{
textBlock.Inlines.Add((new Run(currentCharacter)
{
Foreground = highlightColor,
FontWeight = highlightWeight,
FontStyle = highlightStyle,
FontStretch = highlightStretch
Foreground = style.Color,
FontWeight = style.FontWeight,
FontStyle = style.FontStyle,
FontStretch = style.FontStretch
}));
}
else
Expand Down
33 changes: 18 additions & 15 deletions Wox/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,23 +472,26 @@ private void QueryResults()
};
CountdownEvent countdown = new CountdownEvent(plugins.Count);
Parallel.ForEach(plugins, plugin =>
foreach (var plugin in plugins)
{
if (token.IsCancellationRequested)
Task.Run(() =>
{
Logger.WoxTrace($"canceled {token.GetHashCode()} {Thread.CurrentThread.ManagedThreadId} {queryText} {plugin.Metadata.Name}");
countdown.Signal();
return;
}
var results = PluginManager.QueryForPlugin(plugin, query);
if (token.IsCancellationRequested)
{
Logger.WoxTrace($"canceled {token.GetHashCode()} {Thread.CurrentThread.ManagedThreadId} {queryText} {plugin.Metadata.Name}");
countdown.Signal();
return;
}
_resultsQueue.Add(new ResultsForUpdate(results, plugin.Metadata, query, token, countdown));
});
if (token.IsCancellationRequested)
{
Logger.WoxTrace($"canceled {token.GetHashCode()} {Thread.CurrentThread.ManagedThreadId} {queryText} {plugin.Metadata.Name}");
countdown.Signal();
return;
}
var results = PluginManager.QueryForPlugin(plugin, query);
if (token.IsCancellationRequested)
{
Logger.WoxTrace($"canceled {token.GetHashCode()} {Thread.CurrentThread.ManagedThreadId} {queryText} {plugin.Metadata.Name}");
countdown.Signal();
return;
}
_resultsQueue.Add(new ResultsForUpdate(results, plugin.Metadata, query, token, countdown));
}, token);
}
Task.Run(() =>
{
Expand Down

0 comments on commit 2f8dc3e

Please sign in to comment.