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-08-18 08:56:26 +08:00
|
|
|
|
using System.Windows.Media;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using Windows.ApplicationModel;
|
|
|
|
|
using Windows.ApplicationModel.Core;
|
2016-08-20 06:24:21 +08:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
using Windows.Management.Deployment;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
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 DisplayName { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public string PublisherDisplayName { get; set; }
|
|
|
|
|
public string Location { get; set; }
|
|
|
|
|
|
|
|
|
|
public Application[] Apps { get; set; }
|
|
|
|
|
public Package Package { get; }
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public UWP(Package package)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
Package = package;
|
|
|
|
|
Name = Package.Id.Name;
|
|
|
|
|
FullName = Package.Id.FullName;
|
|
|
|
|
FamilyName = Package.Id.FamilyName;
|
|
|
|
|
Location = Package.InstalledLocation.Path;
|
2016-09-06 04:58:40 +08:00
|
|
|
|
Apps = MergedApps();
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 04:58:40 +08:00
|
|
|
|
private Application[] AppInfos()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
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);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
if (result == Hresult.Ok)
|
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var reader = appx.CreateManifestReader(stream);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
var properties = reader.GetProperties();
|
|
|
|
|
PublisherDisplayName = properties.GetStringValue("PublisherDisplayName");
|
|
|
|
|
DisplayName = properties.GetStringValue("DisplayName");
|
|
|
|
|
Description = properties.GetStringValue("Description");
|
|
|
|
|
|
|
|
|
|
var apps = reader.GetApplications();
|
2016-09-06 04:58:40 +08:00
|
|
|
|
var parsedApps = new List<Application>();
|
|
|
|
|
while (apps.GetHasCurrent() != 0)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-09-06 03:33:31 +08:00
|
|
|
|
var current = apps.GetCurrent();
|
|
|
|
|
var appListEntry = current.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
|
|
|
|
|
{
|
|
|
|
|
UserModelId = current.GetAppUserModelId(),
|
|
|
|
|
BackgroundColor = current.GetStringValue("BackgroundColor") ?? string.Empty,
|
|
|
|
|
Location = Location,
|
2016-09-06 05:30:29 +08:00
|
|
|
|
LogoPath = Application.LogoFromManifest(current, Location),
|
|
|
|
|
Valid = true // useless for now
|
2016-09-06 04:58:40 +08:00
|
|
|
|
};
|
2016-09-06 03:33:31 +08:00
|
|
|
|
|
2016-09-06 05:30:29 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(app.UserModelId))
|
|
|
|
|
{
|
|
|
|
|
parsedApps.Add(app);
|
|
|
|
|
}
|
2016-08-21 02:19:33 +08:00
|
|
|
|
}
|
2016-08-18 08:16:40 +08:00
|
|
|
|
apps.MoveNext();
|
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
|
|
|
|
return parsedApps.ToArray();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Application[] { };
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 04:58:40 +08:00
|
|
|
|
private Application[] AppDisplayInfos()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
IReadOnlyList<AppListEntry> apps;
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-09-06 04:58:40 +08:00
|
|
|
|
apps = Package.GetAppListEntriesAsync().AsTask().Result;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
var message = $"{e.Message} @ {Name}";
|
|
|
|
|
Console.WriteLine(message);
|
2016-09-06 04:58:40 +08:00
|
|
|
|
return new Application[] { };
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
|
|
|
|
var displayinfos = apps.Select(a =>
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-09-06 03:33:31 +08:00
|
|
|
|
RandomAccessStreamReference logo;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// todo: which size is valid?
|
|
|
|
|
logo = a.DisplayInfo.GetLogo(new Size(44, 44));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
var message = $"Can't get logo for {Name}";
|
|
|
|
|
Log.Error(message);
|
|
|
|
|
Log.Exception(e);
|
|
|
|
|
logo = RandomAccessStreamReference.CreateFromUri(new Uri(Constant.ErrorIcon));
|
|
|
|
|
}
|
|
|
|
|
var parsed = new Application
|
|
|
|
|
{
|
|
|
|
|
DisplayName = a.DisplayInfo.DisplayName,
|
|
|
|
|
Description = a.DisplayInfo.Description,
|
|
|
|
|
LogoStream = logo
|
|
|
|
|
};
|
|
|
|
|
return parsed;
|
2016-09-06 04:58:40 +08:00
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
|
|
return displayinfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Application[] MergedApps()
|
|
|
|
|
{
|
2016-09-06 05:41:09 +08:00
|
|
|
|
// todo can't find api, so just hard code it
|
|
|
|
|
if (Location.Contains("SystemApps") || Location.Contains("WindowsApps"))
|
2016-09-06 04:58:40 +08:00
|
|
|
|
{
|
2016-09-06 05:41:09 +08:00
|
|
|
|
var infos = AppInfos();
|
|
|
|
|
if (infos.Length > 0)
|
2016-09-06 04:58:40 +08:00
|
|
|
|
{
|
2016-09-06 05:41:09 +08:00
|
|
|
|
var displayInfos = AppDisplayInfos();
|
|
|
|
|
var apps = infos;
|
|
|
|
|
// todo: temp hack for multipla application mismatch problem
|
|
|
|
|
// e.g. mail and calendar, skype video and messaging
|
|
|
|
|
// https://github.com/Wox-launcher/Wox/issues/198#issuecomment-244778783
|
|
|
|
|
var length = infos.Length;
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
var j = length - i - 1;
|
|
|
|
|
apps[i].DisplayName = displayInfos[j].DisplayName;
|
|
|
|
|
apps[i].Description = displayInfos[j].Description;
|
|
|
|
|
apps[i].LogoStream = displayInfos[j].LogoStream;
|
|
|
|
|
}
|
|
|
|
|
return apps;
|
2016-09-06 04:58:40 +08:00
|
|
|
|
}
|
2016-09-06 03:33:31 +08:00
|
|
|
|
}
|
2016-09-06 05:41:09 +08:00
|
|
|
|
return new Application[] { };
|
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-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-09-06 03:33:31 +08:00
|
|
|
|
var applications = CurrentUserPackages().AsParallel().SelectMany(p => new UWP(p).Apps);
|
|
|
|
|
applications = applications.Where(a => a.Valid);
|
|
|
|
|
return applications.ToArray();
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-21 01:25:55 +08:00
|
|
|
|
return new Application[] { };
|
2016-08-20 08:17:28 +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-08-31 23:01:51 +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
|
|
|
|
|
{
|
|
|
|
|
return new Package[] { };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public RandomAccessStreamReference LogoStream { get; set; }
|
|
|
|
|
public string UserModelId { get; set; }
|
|
|
|
|
public string PublisherDisplayName { get; set; }
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public string BackgroundColor { get; set; }
|
|
|
|
|
public string LogoPath { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
|
|
|
|
|
public string Location { get; set; }
|
2016-09-06 03:33:31 +08:00
|
|
|
|
public bool Valid { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
|
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);
|
|
|
|
|
var score = new[] { score1, score2, score3 }.Max();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Launch(IPublicAPI api)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var appManager = new ApplicationActivationManager();
|
|
|
|
|
uint unusedPid;
|
|
|
|
|
const string noArgs = "";
|
|
|
|
|
const ACTIVATEOPTIONS noFlags = ACTIVATEOPTIONS.AO_NONE;
|
|
|
|
|
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-08-19 10:00:52 +08:00
|
|
|
|
var logo = !string.IsNullOrEmpty(LogoPath) ? ImageFromPath(LogoPath) : ImageFromStream(LogoStream);
|
|
|
|
|
var validBaground = !string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent";
|
|
|
|
|
var plated = validBaground ? PlatedImage(logo) : 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
|
|
|
|
internal static string LogoFromManifest(IAppxManifestApplication application, string location)
|
|
|
|
|
{
|
|
|
|
|
// todo use hidpi logo when use hidpi screen
|
|
|
|
|
var path1 = Path.Combine(location, application.GetStringValue("Square44x44Logo"));
|
|
|
|
|
path1 = LogoFromPath(path1);
|
|
|
|
|
if (!string.IsNullOrEmpty(path1))
|
|
|
|
|
{
|
|
|
|
|
return path1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var path2 = Path.Combine(location, application.GetStringValue("Square150x150Logo"));
|
|
|
|
|
path2 = LogoFromPath(path2);
|
|
|
|
|
if (!string.IsNullOrEmpty(path2))
|
|
|
|
|
{
|
|
|
|
|
return path2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Constant.ErrorIcon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string LogoFromPath(string path)
|
2016-08-19 10:00:52 +08:00
|
|
|
|
{
|
2016-09-06 00:45:29 +08:00
|
|
|
|
if (!File.Exists(path))
|
2016-08-20 06:15:40 +08:00
|
|
|
|
{
|
2016-09-06 00:45:29 +08:00
|
|
|
|
// https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
|
2016-09-06 03:33:31 +08:00
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
if (!string.IsNullOrEmpty(extension))
|
2016-09-06 00:45:29 +08:00
|
|
|
|
{
|
2016-09-06 03:33:31 +08:00
|
|
|
|
var paths = new List<string>();
|
|
|
|
|
var end = path.Length - extension.Length;
|
|
|
|
|
var prefix = path.Substring(0, end);
|
|
|
|
|
// todo: remove hard cod scale
|
|
|
|
|
paths.Add($"{prefix}.scale-200{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
2016-09-06 04:58:40 +08:00
|
|
|
|
|
2016-09-06 03:33:31 +08:00
|
|
|
|
// hack for C:\Windows\ImmersiveControlPanel
|
|
|
|
|
var directory = Directory.GetParent(path).FullName;
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
prefix = Path.Combine(directory, "images", filename);
|
|
|
|
|
paths.Add($"{prefix}.scale-200{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
|
|
|
|
|
foreach (var p in paths)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(p))
|
|
|
|
|
{
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
2016-09-06 00:45:29 +08:00
|
|
|
|
}
|
2016-09-06 03:33:31 +08:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// for js based application, e.g cut the rope
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BitmapImage ImageFromPath(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
// https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
|
|
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
if (!string.IsNullOrEmpty(extension))
|
2016-09-06 00:45:29 +08:00
|
|
|
|
{
|
2016-09-06 03:33:31 +08:00
|
|
|
|
var paths = new List<string>();
|
|
|
|
|
var prefix = path.Substring(0, extension.Length);
|
|
|
|
|
// todo: remove hard cod scale
|
|
|
|
|
paths.Add($"{prefix}.scale-200{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
// hack for C:\Windows\ImmersiveControlPanel
|
|
|
|
|
var directory = Directory.GetParent(path).FullName;
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
prefix = Path.Combine(directory, "images", filename);
|
|
|
|
|
paths.Add($"{prefix}.scale-200{extension}");
|
|
|
|
|
paths.Add($"{prefix}.scale-100{extension}");
|
|
|
|
|
foreach (var p in paths)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(p))
|
|
|
|
|
{
|
|
|
|
|
return new BitmapImage(new Uri(p));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-09-06 00:45:29 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
|
|
|
|
}
|
2016-08-20 06:15:40 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-09-06 00:45:29 +08:00
|
|
|
|
// for js based application, e.g cut the rope
|
|
|
|
|
var image = new BitmapImage(new Uri(path));
|
|
|
|
|
return image;
|
2016-08-20 06:15:40 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BitmapImage ImageFromStream(RandomAccessStreamReference reference)
|
|
|
|
|
{
|
|
|
|
|
IRandomAccessStreamWithContentType stream;
|
|
|
|
|
try
|
2016-08-18 08:56:26 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
stream = reference.OpenReadAsync().AsTask().Result;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
catch (Exception e)
|
2016-08-18 08:56:26 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var message = $"{e.Message} @ {DisplayName}";
|
|
|
|
|
Log.Error(message);
|
2016-09-06 03:33:31 +08:00
|
|
|
|
Log.Exception(e);
|
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
|
|
|
|
|
var image = new BitmapImage();
|
|
|
|
|
image.BeginInit();
|
|
|
|
|
image.StreamSource = stream.AsStream();
|
|
|
|
|
image.EndInit();
|
|
|
|
|
return image;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ImageSource PlatedImage(BitmapImage image)
|
|
|
|
|
{
|
|
|
|
|
var width = image.Width;
|
|
|
|
|
var height = image.Height;
|
|
|
|
|
var x = 0;
|
|
|
|
|
var y = 0;
|
|
|
|
|
|
|
|
|
|
var group = new DrawingGroup();
|
|
|
|
|
|
|
|
|
|
var converted = ColorConverter.ConvertFromString(BackgroundColor);
|
|
|
|
|
if (converted != null)
|
|
|
|
|
{
|
|
|
|
|
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();
|
2016-08-19 10:00:52 +08:00
|
|
|
|
const int dpiScale100 = 96;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
var bitmap = new RenderTargetBitmap(
|
|
|
|
|
Convert.ToInt32(width), Convert.ToInt32(height),
|
2016-08-19 10:00:52 +08:00
|
|
|
|
dpiScale100, dpiScale100,
|
2016-08-18 08:56:26 +08:00
|
|
|
|
PixelFormats.Pbgra32
|
|
|
|
|
);
|
|
|
|
|
bitmap.Render(visual);
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{DisplayName}: {Description}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
private enum Stgm : uint
|
|
|
|
|
{
|
|
|
|
|
Read = 0x0,
|
2016-08-20 06:24:21 +08:00
|
|
|
|
ShareExclusive = 0x10
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum Hresult : uint
|
|
|
|
|
{
|
2016-08-20 06:24:21 +08:00
|
|
|
|
Ok = 0x0000
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
|
|
|
|
private static extern Hresult SHCreateStreamOnFileEx(string fileName, Stgm grfMode, uint attributes, bool create, IStream reserved, out IStream stream);
|
|
|
|
|
}
|
|
|
|
|
}
|