2016-08-18 08:16:40 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-08-22 09:21:22 +08:00
|
|
|
|
using System.Diagnostics;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Principal;
|
2016-11-29 09:18:38 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
using System.Windows.Media;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using System.Windows.Media.Imaging;
|
2016-11-29 09:18:38 +08:00
|
|
|
|
using System.Xml.Linq;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using Windows.ApplicationModel;
|
2016-08-20 06:24:21 +08:00
|
|
|
|
using Windows.Management.Deployment;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using AppxPackaing;
|
|
|
|
|
using Shell;
|
2016-08-20 08:17:28 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
using IStream = AppxPackaing.IStream;
|
2016-08-20 06:24:21 +08:00
|
|
|
|
using Rect = System.Windows.Rect;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
|
namespace Wox.Plugin.Program.Programs
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public class UWP
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string FullName { get; }
|
|
|
|
|
public string FamilyName { get; }
|
|
|
|
|
public string Location { get; set; }
|
|
|
|
|
|
|
|
|
|
public Application[] Apps { get; set; }
|
|
|
|
|
public Package Package { get; }
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
public PackageVersion Version { get; set; }
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public UWP(Package package)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
Package = package;
|
2016-11-29 09:18:38 +08:00
|
|
|
|
Location = Package.InstalledLocation.Path;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
Name = Package.Id.Name;
|
|
|
|
|
FullName = Package.Id.FullName;
|
|
|
|
|
FamilyName = Package.Id.FamilyName;
|
2016-11-29 09:18:38 +08:00
|
|
|
|
InitializeAppInfo();
|
|
|
|
|
Apps = Apps.Where(a =>
|
|
|
|
|
{
|
|
|
|
|
var valid =
|
|
|
|
|
!string.IsNullOrEmpty(a.UserModelId) &&
|
|
|
|
|
!string.IsNullOrEmpty(a.DisplayName);
|
|
|
|
|
return valid;
|
|
|
|
|
}).ToArray();
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private void InitializeAppInfo()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var path = Path.Combine(Location, "AppxManifest.xml");
|
2016-11-29 09:18:38 +08:00
|
|
|
|
|
|
|
|
|
var namespaces = XmlNamespaces(path);
|
|
|
|
|
InitPackageVersion(namespaces);
|
|
|
|
|
|
|
|
|
|
var appxFactory = new AppxFactory();
|
2016-08-19 10:00:52 +08:00
|
|
|
|
IStream stream;
|
|
|
|
|
const uint noAttribute = 0x80;
|
|
|
|
|
const Stgm exclusiveRead = Stgm.Read | Stgm.ShareExclusive;
|
|
|
|
|
var result = SHCreateStreamOnFileEx(path, exclusiveRead, noAttribute, false, null, out stream);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
if (result == Hresult.Ok)
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var reader = appxFactory.CreateManifestReader(stream);
|
|
|
|
|
var manifestApps = reader.GetApplications();
|
|
|
|
|
var apps = new List<Application>();
|
|
|
|
|
while (manifestApps.GetHasCurrent() != 0)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var manifestApp = manifestApps.GetCurrent();
|
|
|
|
|
var appListEntry = manifestApp.GetStringValue("AppListEntry");
|
2016-09-06 04:58:40 +08:00
|
|
|
|
if (appListEntry != "none")
|
2016-08-21 02:19:33 +08:00
|
|
|
|
{
|
2016-09-06 04:58:40 +08:00
|
|
|
|
var app = new Application
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
UserModelId = manifestApp.GetAppUserModelId(),
|
|
|
|
|
DisplayName = manifestApp.GetStringValue("DisplayName"),
|
|
|
|
|
Description = manifestApp.GetStringValue("Description"),
|
|
|
|
|
BackgroundColor = manifestApp.GetStringValue("BackgroundColor"),
|
|
|
|
|
Location = Location
|
2016-09-06 04:58:40 +08:00
|
|
|
|
};
|
2016-11-29 09:18:38 +08:00
|
|
|
|
app.DisplayName = ResourceFromPri(FullName, app.DisplayName);
|
|
|
|
|
app.Description = ResourceFromPri(FullName, app.Description);
|
|
|
|
|
// todo move into Application class, so it can specific app info when logo error
|
|
|
|
|
app.LogoUri = LogoUriFromManifest(manifestApp);
|
|
|
|
|
app.LogoPath = LogoPathFromUri(app.LogoUri);
|
|
|
|
|
apps.Add(app);
|
2016-08-21 02:19:33 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
manifestApps.MoveNext();
|
|
|
|
|
}
|
|
|
|
|
Apps = apps.Where(a => a.AppListEntry != "none").ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// http://www.hanselman.com/blog/GetNamespacesFromAnXMLDocumentWithXPathDocumentAndLINQToXML.aspx
|
|
|
|
|
private static string[] XmlNamespaces(string path)
|
|
|
|
|
{
|
|
|
|
|
XDocument z = XDocument.Load(path);
|
|
|
|
|
if (z.Root != null)
|
|
|
|
|
{
|
|
|
|
|
var namespaces = z.Root.Attributes().
|
|
|
|
|
Where(a => a.IsNamespaceDeclaration).
|
|
|
|
|
GroupBy(
|
|
|
|
|
a => a.Name.Namespace == XNamespace.None ? string.Empty : a.Name.LocalName,
|
|
|
|
|
a => XNamespace.Get(a.Value)
|
|
|
|
|
).Select(
|
|
|
|
|
g => g.First().ToString()
|
|
|
|
|
).ToArray();
|
|
|
|
|
return namespaces;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"can't find namespaces for <{path}>");
|
|
|
|
|
return new string[] {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitPackageVersion(string[] namespaces)
|
|
|
|
|
{
|
|
|
|
|
var versionFromNamespace = new Dictionary<string, PackageVersion>
|
|
|
|
|
{
|
|
|
|
|
{"http://schemas.microsoft.com/appx/manifest/foundation/windows10", PackageVersion.Windows10},
|
|
|
|
|
{"http://schemas.microsoft.com/appx/2013/manifest", PackageVersion.Windows81},
|
|
|
|
|
{"http://schemas.microsoft.com/appx/2010/manifest", PackageVersion.Windows8},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var n in versionFromNamespace.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (namespaces.Contains(n))
|
|
|
|
|
{
|
|
|
|
|
Version = versionFromNamespace[n];
|
|
|
|
|
return;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Error($"Unknown Appmanifest version: {FullName}");
|
|
|
|
|
Version = PackageVersion.Unknown;
|
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private string LogoUriFromManifest(IAppxManifestApplication app)
|
|
|
|
|
{
|
|
|
|
|
var logoKeyFromVersion = new Dictionary<PackageVersion, string>
|
|
|
|
|
{
|
|
|
|
|
{PackageVersion.Windows10, "Square44x44Logo"},
|
|
|
|
|
{PackageVersion.Windows81, "Square30x30Logo"},
|
|
|
|
|
{PackageVersion.Windows8, "SmallLogo"},
|
|
|
|
|
};
|
|
|
|
|
if (logoKeyFromVersion.ContainsKey(Version))
|
|
|
|
|
{
|
|
|
|
|
var key = logoKeyFromVersion[Version];
|
|
|
|
|
var logoUri = app.GetStringValue(key);
|
|
|
|
|
return logoUri;
|
2016-09-06 04:58:40 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
return string.Empty;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private string LogoPathFromUri(string uri)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
// all https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
|
|
|
|
|
// windows 10 https://msdn.microsoft.com/en-us/library/windows/apps/dn934817.aspx
|
|
|
|
|
// windows 8.1 https://msdn.microsoft.com/en-us/library/windows/apps/hh965372.aspx#target_size
|
|
|
|
|
// windows 8 https://msdn.microsoft.com/en-us/library/windows/apps/br211475.aspx
|
|
|
|
|
|
|
|
|
|
string path;
|
|
|
|
|
if (uri.Contains("\\"))
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
path = Path.Combine(Location, uri);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
else
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
// for C:\Windows\MiracastView etc
|
|
|
|
|
path = Path.Combine(Location, "Assets", uri);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
if (extension != null)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var end = path.Length - extension.Length;
|
|
|
|
|
var prefix = path.Substring(0, end);
|
|
|
|
|
var paths = new List<string> {path};
|
|
|
|
|
|
|
|
|
|
// todo hidpi icon
|
|
|
|
|
if (Version == PackageVersion.Windows10)
|
2016-09-06 03:33:31 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-200{extension}");
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
else if (Version == PackageVersion.Windows81)
|
2016-09-06 03:33:31 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-120{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-140{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-160{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-180{extension}");
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
else if (Version == PackageVersion.Windows8)
|
2016-09-06 03:33:31 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var selected = paths.FirstOrDefault(File.Exists);
|
|
|
|
|
if (!string.IsNullOrEmpty(selected))
|
|
|
|
|
{
|
|
|
|
|
return selected;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"<{FullName}> can't find logo uri: <{uri}>");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"<{FullName}> cantains uri doesn't have extension: <{uri}>");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
|
|
|
|
|
private string ResourceFromPri(string packageFullName, string resourceReference)
|
2016-09-06 04:58:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
const string prefix = "ms-resource:";
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(resourceReference) && resourceReference.StartsWith(prefix))
|
2016-09-06 04:58:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
// magic comes from @talynone
|
|
|
|
|
// https://github.com/talynone/Wox.Plugin.WindowsUniversalAppLauncher/blob/master/StoreAppLauncher/Helpers/NativeApiHelper.cs#L139-L153
|
|
|
|
|
string key = resourceReference.Substring(prefix.Length);
|
|
|
|
|
string parsed;
|
|
|
|
|
if (key.StartsWith("//"))
|
|
|
|
|
{
|
|
|
|
|
parsed = prefix + key;
|
|
|
|
|
}
|
|
|
|
|
else if (key.StartsWith("/"))
|
2016-09-06 04:58:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
parsed = prefix + "//" + key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parsed = $"{prefix}//{Name}/resources/{key}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outBuffer = new StringBuilder(128);
|
|
|
|
|
string source = $"@{{{packageFullName}? {parsed}}}";
|
|
|
|
|
var capacity = (uint) outBuffer.Capacity;
|
|
|
|
|
var hResult = SHLoadIndirectString(source, outBuffer, capacity, IntPtr.Zero);
|
|
|
|
|
if (hResult == Hresult.Ok)
|
|
|
|
|
{
|
|
|
|
|
var loaded = outBuffer.ToString();
|
|
|
|
|
if (!string.IsNullOrEmpty(loaded))
|
|
|
|
|
{
|
|
|
|
|
return loaded;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-09-06 05:41:09 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var error = $"Load {source} failed, null or empty result";
|
2016-11-29 09:26:47 +08:00
|
|
|
|
Log.Error(error);
|
2016-11-29 09:18:38 +08:00
|
|
|
|
return string.Empty;
|
2016-09-06 05:41:09 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// known hresult 2147942522:
|
|
|
|
|
// 'Microsoft Corporation' violates pattern constraint of '\bms-resource:.{1,256}'.
|
|
|
|
|
// for
|
|
|
|
|
// Microsoft.MicrosoftOfficeHub_17.7608.23501.0_x64__8wekyb3d8bbwe: ms-resource://Microsoft.MicrosoftOfficeHub/officehubintl/AppManifest_GetOffice_Description
|
|
|
|
|
// Microsoft.BingFoodAndDrink_3.0.4.336_x64__8wekyb3d8bbwe: ms-resource:AppDescription
|
|
|
|
|
var message = $"Load {source} failed, HResult error code: {hResult}";
|
|
|
|
|
Log.Error(message);
|
|
|
|
|
var exception = Marshal.GetExceptionForHR((int) hResult);
|
|
|
|
|
Log.Exception(exception);
|
|
|
|
|
return string.Empty;
|
2016-09-06 04:58:40 +08:00
|
|
|
|
}
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return resourceReference;
|
|
|
|
|
}
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 01:25:55 +08:00
|
|
|
|
public static Application[] All()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
|
|
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
|
var windows10 = new Version(10, 0);
|
|
|
|
|
var support = Environment.OSVersion.Version.Major >= windows10.Major;
|
|
|
|
|
if (support)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var watch = new System.Diagnostics.Stopwatch();
|
|
|
|
|
watch.Start();
|
|
|
|
|
|
|
|
|
|
var applications = CurrentUserPackages().AsParallel().SelectMany(p => new UWP(p).Apps).ToArray();
|
|
|
|
|
|
|
|
|
|
watch.Stop();
|
|
|
|
|
Log.Info("UWP ALL" + watch.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
return applications;
|
|
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
return new Application[] {};
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
|
|
|
|
|
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Package> CurrentUserPackages()
|
|
|
|
|
{
|
2016-08-21 02:19:33 +08:00
|
|
|
|
var user = WindowsIdentity.GetCurrent().User;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
var userSecurityId = user.Value;
|
|
|
|
|
var packageManager = new PackageManager();
|
|
|
|
|
var packages = packageManager.FindPackagesForUser(userSecurityId);
|
2016-11-29 09:18:38 +08:00
|
|
|
|
packages =
|
|
|
|
|
packages.Where(
|
|
|
|
|
p => !p.IsFramework && !p.IsDevelopmentMode && !string.IsNullOrEmpty(p.InstalledLocation.Path));
|
2016-08-18 08:16:40 +08:00
|
|
|
|
return packages;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
return new Package[] {};
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return FamilyName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2016-08-18 08:56:26 +08:00
|
|
|
|
var uwp = obj as UWP;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
if (uwp != null)
|
|
|
|
|
{
|
|
|
|
|
return FamilyName.Equals(uwp.FamilyName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return FamilyName.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
public class Application : IProgram
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
public string AppListEntry { get; set; }
|
2016-08-18 08:16:40 +08:00
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public string UserModelId { get; set; }
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public string BackgroundColor { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
public string LogoUri { get; set; }
|
|
|
|
|
public string LogoPath { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
public string Location { get; set; }
|
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
private int Score(string query)
|
|
|
|
|
{
|
|
|
|
|
var score1 = StringMatcher.Score(DisplayName, query);
|
|
|
|
|
var score2 = StringMatcher.ScoreForPinyin(DisplayName, query);
|
|
|
|
|
var score3 = StringMatcher.Score(Description, query);
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var score = new[] {score1, score2, score3}.Max();
|
2016-08-22 09:21:22 +08:00
|
|
|
|
return score;
|
|
|
|
|
}
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
public Result Result(string query, IPublicAPI api)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-22 09:21:22 +08:00
|
|
|
|
var result = new Result
|
|
|
|
|
{
|
|
|
|
|
SubTitle = Location,
|
|
|
|
|
Icon = Logo,
|
|
|
|
|
Score = Score(query),
|
|
|
|
|
ContextData = this,
|
|
|
|
|
Action = e =>
|
|
|
|
|
{
|
|
|
|
|
Launch(api);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (Description.Length >= DisplayName.Length &&
|
|
|
|
|
Description.Substring(0, DisplayName.Length) == DisplayName)
|
|
|
|
|
{
|
|
|
|
|
result.Title = Description;
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(Description))
|
|
|
|
|
{
|
|
|
|
|
result.Title = $"{DisplayName}: {Description}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Title = DisplayName;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> ContextMenus(IPublicAPI api)
|
|
|
|
|
{
|
|
|
|
|
var contextMenus = new List<Result>
|
|
|
|
|
{
|
|
|
|
|
new Result
|
|
|
|
|
{
|
|
|
|
|
Title = api.GetTranslation("wox_plugin_program_open_containing_folder"),
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
|
|
|
|
var hide = Main.StartProcess(new ProcessStartInfo(Location));
|
|
|
|
|
return hide;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = "Images/folder.png"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return contextMenus;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private async void Launch(IPublicAPI api)
|
2016-08-22 09:21:22 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var appManager = new ApplicationActivationManager();
|
|
|
|
|
uint unusedPid;
|
|
|
|
|
const string noArgs = "";
|
|
|
|
|
const ACTIVATEOPTIONS noFlags = ACTIVATEOPTIONS.AO_NONE;
|
|
|
|
|
await Task.Run(() =>
|
2016-08-22 09:21:22 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
appManager.ActivateApplication(UserModelId, noArgs, noFlags, out unusedPid);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
var name = "Plugin: Program";
|
|
|
|
|
var message = $"Can't start UWP: {DisplayName}";
|
|
|
|
|
api.ShowMsg(name, message, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public ImageSource Logo()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var logo = ImageFromPath(LogoPath);
|
|
|
|
|
var plated = PlatedImage(logo);
|
2016-08-18 08:56:26 +08:00
|
|
|
|
|
2016-08-19 10:00:52 +08:00
|
|
|
|
// todo magic! temp fix for cross thread object
|
|
|
|
|
plated.Freeze();
|
|
|
|
|
return plated;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 03:33:31 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private BitmapImage ImageFromPath(string path)
|
2016-08-19 10:00:52 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
if (File.Exists(path))
|
2016-08-20 06:15:40 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var image = new BitmapImage(new Uri(path));
|
|
|
|
|
return image;
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
Log.Error($"Can't get logo for <{UserModelId}> with path <{path}>");
|
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private ImageSource PlatedImage(BitmapImage image)
|
2016-09-06 03:33:31 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent")
|
2016-09-06 03:33:31 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var width = image.Width;
|
|
|
|
|
var height = image.Height;
|
|
|
|
|
var x = 0;
|
|
|
|
|
var y = 0;
|
|
|
|
|
|
|
|
|
|
var group = new DrawingGroup();
|
2016-09-06 03:33:31 +08:00
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var converted = ColorConverter.ConvertFromString(BackgroundColor);
|
|
|
|
|
if (converted != null)
|
2016-09-06 00:45:29 +08:00
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
var color = (Color) converted;
|
|
|
|
|
var brush = new SolidColorBrush(color);
|
|
|
|
|
var pen = new Pen(brush, 1);
|
|
|
|
|
var backgroundArea = new Rect(0, 0, width, width);
|
|
|
|
|
var rectabgle = new RectangleGeometry(backgroundArea);
|
|
|
|
|
var rectDrawing = new GeometryDrawing(brush, pen, rectabgle);
|
|
|
|
|
group.Children.Add(rectDrawing);
|
|
|
|
|
|
|
|
|
|
var imageArea = new Rect(x, y, image.Width, image.Height);
|
|
|
|
|
var imageDrawing = new ImageDrawing(image, imageArea);
|
|
|
|
|
group.Children.Add(imageDrawing);
|
|
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/6676072/get-system-drawing-bitmap-of-a-wpf-area-using-visualbrush
|
|
|
|
|
var visual = new DrawingVisual();
|
|
|
|
|
var context = visual.RenderOpen();
|
|
|
|
|
context.DrawDrawing(group);
|
|
|
|
|
context.Close();
|
|
|
|
|
const int dpiScale100 = 96;
|
|
|
|
|
var bitmap = new RenderTargetBitmap(
|
|
|
|
|
Convert.ToInt32(width), Convert.ToInt32(height),
|
|
|
|
|
dpiScale100, dpiScale100,
|
|
|
|
|
PixelFormats.Pbgra32
|
|
|
|
|
);
|
|
|
|
|
bitmap.Render(visual);
|
|
|
|
|
return bitmap;
|
2016-09-06 00:45:29 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
Log.Error($"Can't convert background string <{BackgroundColor}> to color");
|
2016-09-06 00:45:29 +08:00
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
|
|
|
|
}
|
2016-08-20 06:15:40 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
// todo use windows theme as background
|
2016-09-06 00:45:29 +08:00
|
|
|
|
return image;
|
2016-08-20 06:15:40 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 08:16:40 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{DisplayName}: {Description}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:18:38 +08:00
|
|
|
|
public enum PackageVersion
|
|
|
|
|
{
|
|
|
|
|
Windows10,
|
|
|
|
|
Windows81,
|
|
|
|
|
Windows8,
|
|
|
|
|
Unknown
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 08:16:40 +08:00
|
|
|
|
[Flags]
|
|
|
|
|
private enum Stgm : uint
|
|
|
|
|
{
|
|
|
|
|
Read = 0x0,
|
2016-11-29 09:18:38 +08:00
|
|
|
|
ShareExclusive = 0x10,
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum Hresult : uint
|
|
|
|
|
{
|
2016-11-29 09:18:38 +08:00
|
|
|
|
Ok = 0x0000,
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
2016-11-29 09:18:38 +08:00
|
|
|
|
private static extern Hresult SHCreateStreamOnFileEx(string fileName, Stgm grfMode, uint attributes, bool create,
|
|
|
|
|
IStream reserved, out IStream stream);
|
|
|
|
|
|
|
|
|
|
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
|
|
|
|
private static extern Hresult SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, uint cchOutBuf,
|
|
|
|
|
IntPtr ppvReserved);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-11-29 09:18:38 +08:00
|
|
|
|
}
|