From 0298fa602a6234a64b4f236088fb223eae532d2c Mon Sep 17 00:00:00 2001 From: bao-qian Date: Fri, 19 Aug 2016 03:00:52 +0100 Subject: [PATCH] Code refactoring #198 --- Plugins/Wox.Plugin.Program/Main.cs | 6 +- .../Wox.Plugin.Program/ProgramSources/UWP.cs | 114 ++++++++---------- Wox/ViewModel/ResultViewModel.cs | 1 - 3 files changed, 53 insertions(+), 68 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index 5cfc7d35de..08fb1db094 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -15,7 +15,7 @@ namespace Wox.Plugin.Program public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable { private static List _programs = new List(); - private static List _uwpApps = new List(); + private static List _uwps = new List(); private PluginInitContext _context; @@ -51,7 +51,7 @@ namespace Wox.Plugin.Program .Where(p => Score(p, query.Search) > 0) .Select(ResultFromProgram); - var results2 = _uwpApps.AsParallel() + var results2 = _uwps.AsParallel() .Where(u => Score(u, query.Search) > 0) .Select(ResultFromUWPApp); var result = results1.Concat(results2).ToList(); @@ -144,7 +144,7 @@ namespace Wox.Plugin.Program var support = Environment.OSVersion.Version.Major >= windows10.Major; if (support) { - _uwpApps = UWP.All(); + _uwps = UWP.All(); } } diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/UWP.cs b/Plugins/Wox.Plugin.Program/ProgramSources/UWP.cs index 815066a519..adf9c0a359 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/UWP.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/UWP.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Security.Principal; -using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; @@ -17,7 +16,6 @@ using AppxPackaing; using Shell; using Wox.Infrastructure.Logger; using IStream = AppxPackaing.IStream; -using Path = System.IO.Path; using Size = Windows.Foundation.Size; namespace Wox.Plugin.Program.ProgramSources @@ -31,7 +29,6 @@ namespace Wox.Plugin.Program.ProgramSources public string DisplayName { get; set; } public string Description { get; set; } - public string Logo { get; set; } //todo public string PublisherDisplayName { get; set; } public string Location { get; set; } @@ -41,8 +38,6 @@ namespace Wox.Plugin.Program.ProgramSources public int Score { get; set; } - - public UWP(Package package) { Package = package; @@ -64,25 +59,18 @@ namespace Wox.Plugin.Program.ProgramSources private void InitializeAppInfo() { - var manifestPath = Path.Combine(Location, "AppxManifest.xml"); - var appxFactory = new AppxFactory(); - IStream manifestStream; - var result = SHCreateStreamOnFileEx( - manifestPath, - Stgm.Read | Stgm.ShareExclusive, - 0, - false, - null, - out manifestStream - ); + var path = Path.Combine(Location, "AppxManifest.xml"); + var appx = new AppxFactory(); + IStream stream; + const uint noAttribute = 0x80; + const Stgm exclusiveRead = Stgm.Read | Stgm.ShareExclusive; + var result = SHCreateStreamOnFileEx(path, exclusiveRead, noAttribute, false, null, out stream); if (result == Hresult.Ok) { - var reader = appxFactory.CreateManifestReader(manifestStream); + var reader = appx.CreateManifestReader(stream); var properties = reader.GetProperties(); - Logo = properties.GetStringValue("Logo"); - Logo = Path.Combine(Location, Logo); PublisherDisplayName = properties.GetStringValue("PublisherDisplayName"); DisplayName = properties.GetStringValue("DisplayName"); Description = properties.GetStringValue("Description"); @@ -127,7 +115,8 @@ namespace Wox.Plugin.Program.ProgramSources { DisplayName = a.DisplayInfo.DisplayName, 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(); } @@ -228,47 +217,48 @@ namespace Wox.Plugin.Program.ProgramSources public ImageSource Logo() { - BitmapImage image; - 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; - } + var logo = !string.IsNullOrEmpty(LogoPath) ? ImageFromPath(LogoPath) : ImageFromStream(LogoStream); - image = new BitmapImage(); - image.BeginInit(); - image.StreamSource = stream.AsStream(); - image.EndInit(); + var validBaground = !string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent"; + var plated = validBaground ? PlatedImage(logo) : logo; + + // 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") - { - return PlatedImage(image); - } - else - { - image.Freeze(); - return image; - } + var image = new BitmapImage(); + image.BeginInit(); + image.StreamSource = stream.AsStream(); + image.EndInit(); + return image; } private ImageSource PlatedImage(BitmapImage image) @@ -300,22 +290,18 @@ namespace Wox.Plugin.Program.ProgramSources var context = visual.RenderOpen(); context.DrawDrawing(group); context.Close(); - var scale100DPI = 96; + const int dpiScale100 = 96; var bitmap = new RenderTargetBitmap( Convert.ToInt32(width), Convert.ToInt32(height), - scale100DPI, scale100DPI, + dpiScale100, dpiScale100, PixelFormats.Pbgra32 ); bitmap.Render(visual); - - // todo magic! temp fix for cross thread object - bitmap.Freeze(); return bitmap; } else { var bitmap = new BitmapImage(); - bitmap.Freeze(); return bitmap; } } diff --git a/Wox/ViewModel/ResultViewModel.cs b/Wox/ViewModel/ResultViewModel.cs index edea2d250d..aa9b597adc 100644 --- a/Wox/ViewModel/ResultViewModel.cs +++ b/Wox/ViewModel/ResultViewModel.cs @@ -24,7 +24,6 @@ namespace Wox.ViewModel { if (string.IsNullOrEmpty(Result.IcoPath)) { - ImageSource icon = null; try { return Result.Icon();