2020-08-18 01:00:56 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2020-11-03 01:33:43 +08:00
|
|
|
|
using System.IO.Abstractions;
|
2020-08-18 01:00:56 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2020-10-23 00:45:48 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.CustomAction;
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
2020-10-23 00:45:48 +08:00
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
2020-08-18 01:00:56 +08:00
|
|
|
|
{
|
2020-10-10 08:58:52 +08:00
|
|
|
|
public static class Helper
|
2020-08-18 01:00:56 +08:00
|
|
|
|
{
|
2020-11-03 01:33:43 +08:00
|
|
|
|
public static readonly IFileSystem FileSystem = new FileSystem();
|
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
|
public static bool AllowRunnerToForeground()
|
|
|
|
|
{
|
|
|
|
|
var result = false;
|
|
|
|
|
var processes = Process.GetProcessesByName("PowerToys");
|
|
|
|
|
if (processes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var pid = processes[0].Id;
|
2020-10-10 08:58:52 +08:00
|
|
|
|
result = NativeMethods.AllowSetForegroundWindow(pid);
|
2020-08-18 01:00:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetSerializedCustomAction(string moduleName, string actionName, string actionValue)
|
|
|
|
|
{
|
|
|
|
|
var customAction = new CustomActionDataModel
|
|
|
|
|
{
|
|
|
|
|
Name = actionName,
|
|
|
|
|
Value = actionValue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var moduleCustomAction = new ModuleCustomAction
|
|
|
|
|
{
|
|
|
|
|
ModuleAction = customAction,
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:33:18 +08:00
|
|
|
|
var sendCustomAction = new SendCustomAction(moduleName)
|
|
|
|
|
{
|
|
|
|
|
Action = moduleCustomAction,
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
|
return sendCustomAction.ToJsonString();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 01:33:43 +08:00
|
|
|
|
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback)
|
2020-08-18 01:00:56 +08:00
|
|
|
|
{
|
2020-11-03 01:33:43 +08:00
|
|
|
|
var path = FileSystem.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{moduleName}");
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
2020-11-03 01:33:43 +08:00
|
|
|
|
if (!FileSystem.Directory.Exists(path))
|
2020-08-18 01:00:56 +08:00
|
|
|
|
{
|
2020-11-03 01:33:43 +08:00
|
|
|
|
FileSystem.Directory.CreateDirectory(path);
|
2020-08-18 01:00:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 01:33:43 +08:00
|
|
|
|
var watcher = FileSystem.FileSystemWatcher.CreateNew();
|
|
|
|
|
watcher.Path = path;
|
|
|
|
|
watcher.Filter = fileName;
|
|
|
|
|
watcher.NotifyFilter = NotifyFilters.LastWrite;
|
|
|
|
|
watcher.EnableRaisingEvents = true;
|
2020-09-10 06:33:18 +08:00
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
|
watcher.Changed += (o, e) => onChangedCallback();
|
|
|
|
|
|
|
|
|
|
return watcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string LocalApplicationDataFolder()
|
|
|
|
|
{
|
|
|
|
|
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:33:18 +08:00
|
|
|
|
private static readonly interop.LayoutMapManaged LayoutMap = new interop.LayoutMapManaged();
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
|
|
public static string GetKeyName(uint key)
|
|
|
|
|
{
|
2020-09-10 06:33:18 +08:00
|
|
|
|
return LayoutMap.GetKeyName(key);
|
2020-08-18 01:00:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetProductVersion()
|
|
|
|
|
{
|
|
|
|
|
return interop.CommonManaged.GetProductVersion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int CompareVersions(string version1, string version2)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Split up the version strings into int[]
|
|
|
|
|
// Example: v10.0.2 -> {10, 0, 2};
|
2020-10-20 04:32:05 +08:00
|
|
|
|
if (version1 == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(version1));
|
|
|
|
|
}
|
|
|
|
|
else if (version2 == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(version2));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
|
var v1 = version1.Substring(1).Split('.').Select(int.Parse).ToArray();
|
|
|
|
|
var v2 = version2.Substring(1).Split('.').Select(int.Parse).ToArray();
|
|
|
|
|
|
2020-10-10 08:58:52 +08:00
|
|
|
|
if (v1.Length != 3 || v2.Length != 3)
|
2020-08-18 01:00:56 +08:00
|
|
|
|
{
|
|
|
|
|
throw new FormatException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (v1[0] - v2[0] != 0)
|
|
|
|
|
{
|
|
|
|
|
return v1[0] - v2[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (v1[1] - v2[1] != 0)
|
|
|
|
|
{
|
|
|
|
|
return v1[1] - v2[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v1[2] - v2[2];
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw new FormatException("Bad product version format");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public const uint VirtualKeyWindows = interop.Constants.VK_WIN_BOTH;
|
|
|
|
|
}
|
|
|
|
|
}
|