Code refactoring #198

This commit is contained in:
bao-qian 2016-08-19 03:00:52 +01:00
parent 30cbcbb3c4
commit 0298fa602a
3 changed files with 53 additions and 68 deletions

View File

@ -15,7 +15,7 @@ namespace Wox.Plugin.Program
public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable
{ {
private static List<Program> _programs = new List<Program>(); private static List<Program> _programs = new List<Program>();
private static List<UWP> _uwpApps = new List<UWP>(); private static List<UWP> _uwps = new List<UWP>();
private PluginInitContext _context; private PluginInitContext _context;
@ -51,7 +51,7 @@ namespace Wox.Plugin.Program
.Where(p => Score(p, query.Search) > 0) .Where(p => Score(p, query.Search) > 0)
.Select(ResultFromProgram); .Select(ResultFromProgram);
var results2 = _uwpApps.AsParallel() var results2 = _uwps.AsParallel()
.Where(u => Score(u, query.Search) > 0) .Where(u => Score(u, query.Search) > 0)
.Select(ResultFromUWPApp); .Select(ResultFromUWPApp);
var result = results1.Concat(results2).ToList(); var result = results1.Concat(results2).ToList();
@ -144,7 +144,7 @@ namespace Wox.Plugin.Program
var support = Environment.OSVersion.Version.Major >= windows10.Major; var support = Environment.OSVersion.Version.Major >= windows10.Major;
if (support) if (support)
{ {
_uwpApps = UWP.All(); _uwps = UWP.All();
} }
} }

View File

@ -4,7 +4,6 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security.Principal; using System.Security.Principal;
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;
@ -17,7 +16,6 @@ using AppxPackaing;
using Shell; using Shell;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using IStream = AppxPackaing.IStream; using IStream = AppxPackaing.IStream;
using Path = System.IO.Path;
using Size = Windows.Foundation.Size; using Size = Windows.Foundation.Size;
namespace Wox.Plugin.Program.ProgramSources namespace Wox.Plugin.Program.ProgramSources
@ -31,7 +29,6 @@ namespace Wox.Plugin.Program.ProgramSources
public string DisplayName { get; set; } public string DisplayName { get; set; }
public string Description { get; set; } public string Description { get; set; }
public string Logo { get; set; } //todo
public string PublisherDisplayName { get; set; } public string PublisherDisplayName { get; set; }
public string Location { get; set; } public string Location { get; set; }
@ -41,8 +38,6 @@ namespace Wox.Plugin.Program.ProgramSources
public int Score { get; set; } public int Score { get; set; }
public UWP(Package package) public UWP(Package package)
{ {
Package = package; Package = package;
@ -64,25 +59,18 @@ namespace Wox.Plugin.Program.ProgramSources
private void InitializeAppInfo() private void InitializeAppInfo()
{ {
var manifestPath = Path.Combine(Location, "AppxManifest.xml"); var path = Path.Combine(Location, "AppxManifest.xml");
var appxFactory = new AppxFactory(); var appx = new AppxFactory();
IStream manifestStream; IStream stream;
var result = SHCreateStreamOnFileEx( const uint noAttribute = 0x80;
manifestPath, const Stgm exclusiveRead = Stgm.Read | Stgm.ShareExclusive;
Stgm.Read | Stgm.ShareExclusive, var result = SHCreateStreamOnFileEx(path, exclusiveRead, noAttribute, false, null, out stream);
0,
false,
null,
out manifestStream
);
if (result == Hresult.Ok) if (result == Hresult.Ok)
{ {
var reader = appxFactory.CreateManifestReader(manifestStream); var reader = appx.CreateManifestReader(stream);
var properties = reader.GetProperties(); var properties = reader.GetProperties();
Logo = properties.GetStringValue("Logo");
Logo = Path.Combine(Location, Logo);
PublisherDisplayName = properties.GetStringValue("PublisherDisplayName"); PublisherDisplayName = properties.GetStringValue("PublisherDisplayName");
DisplayName = properties.GetStringValue("DisplayName"); DisplayName = properties.GetStringValue("DisplayName");
Description = properties.GetStringValue("Description"); Description = properties.GetStringValue("Description");
@ -127,7 +115,8 @@ namespace Wox.Plugin.Program.ProgramSources
{ {
DisplayName = a.DisplayInfo.DisplayName, DisplayName = a.DisplayInfo.DisplayName,
Description = a.DisplayInfo.Description, Description = a.DisplayInfo.Description,
LogoStream = a.DisplayInfo.GetLogo(new Size(10, 10)) // todo: which size is valid?
LogoStream = a.DisplayInfo.GetLogo(new Size(44, 44))
}).ToArray(); }).ToArray();
} }
@ -228,47 +217,48 @@ namespace Wox.Plugin.Program.ProgramSources
public ImageSource Logo() public ImageSource Logo()
{ {
BitmapImage image; var logo = !string.IsNullOrEmpty(LogoPath) ? ImageFromPath(LogoPath) : ImageFromStream(LogoStream);
if (!string.IsNullOrEmpty(LogoPath))
{
// https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
var extension = LogoPath.Substring(LogoPath.Length - 4);
var filename = LogoPath.Substring(0, LogoPath.Length - 4);
// todo: remove hard cod scale
var path1 = $"{filename}.scale-200{extension}";
var path2 = $"{filename}.scale-100{extension}";
var uri = File.Exists(path1) ? new Uri(path1) : new Uri(path2);
image = new BitmapImage(uri);
}
else
{
IRandomAccessStreamWithContentType stream;
try
{
stream = LogoStream.OpenReadAsync().AsTask().Result;
}
catch (Exception e)
{
var message = $"{e.Message} @ {DisplayName}";
Log.Error(message);
throw;
}
image = new BitmapImage(); var validBaground = !string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent";
image.BeginInit(); var plated = validBaground ? PlatedImage(logo) : logo;
image.StreamSource = stream.AsStream();
image.EndInit(); // todo magic! temp fix for cross thread object
plated.Freeze();
return plated;
}
private BitmapImage ImageFromPath(string path)
{
// https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
var extension = path.Substring(path.Length - 4);
var filename = path.Substring(0, path.Length - 4);
// todo: remove hard cod scale
var path1 = $"{filename}.scale-200{extension}";
var path2 = $"{filename}.scale-100{extension}";
var uri = File.Exists(path1) ? new Uri(path1) : new Uri(path2);
var image = new BitmapImage(uri);
return image;
}
private BitmapImage ImageFromStream(RandomAccessStreamReference reference)
{
IRandomAccessStreamWithContentType stream;
try
{
stream = reference.OpenReadAsync().AsTask().Result;
}
catch (Exception e)
{
var message = $"{e.Message} @ {DisplayName}";
Log.Error(message);
throw;
} }
if (!string.IsNullOrEmpty(BackgroundColor) || BackgroundColor == "transparent") var image = new BitmapImage();
{ image.BeginInit();
return PlatedImage(image); image.StreamSource = stream.AsStream();
} image.EndInit();
else return image;
{
image.Freeze();
return image;
}
} }
private ImageSource PlatedImage(BitmapImage image) private ImageSource PlatedImage(BitmapImage image)
@ -300,22 +290,18 @@ namespace Wox.Plugin.Program.ProgramSources
var context = visual.RenderOpen(); var context = visual.RenderOpen();
context.DrawDrawing(group); context.DrawDrawing(group);
context.Close(); context.Close();
var scale100DPI = 96; const int dpiScale100 = 96;
var bitmap = new RenderTargetBitmap( var bitmap = new RenderTargetBitmap(
Convert.ToInt32(width), Convert.ToInt32(height), Convert.ToInt32(width), Convert.ToInt32(height),
scale100DPI, scale100DPI, dpiScale100, dpiScale100,
PixelFormats.Pbgra32 PixelFormats.Pbgra32
); );
bitmap.Render(visual); bitmap.Render(visual);
// todo magic! temp fix for cross thread object
bitmap.Freeze();
return bitmap; return bitmap;
} }
else else
{ {
var bitmap = new BitmapImage(); var bitmap = new BitmapImage();
bitmap.Freeze();
return bitmap; return bitmap;
} }
} }

View File

@ -24,7 +24,6 @@ namespace Wox.ViewModel
{ {
if (string.IsNullOrEmpty(Result.IcoPath)) if (string.IsNullOrEmpty(Result.IcoPath))
{ {
ImageSource icon = null;
try try
{ {
return Result.Icon(); return Result.Icon();