2020-04-15 00:24:11 +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.
|
|
|
|
|
|
2020-04-20 23:22:36 +08:00
|
|
|
|
using System;
|
2020-04-15 00:24:11 +08:00
|
|
|
|
using System.Diagnostics;
|
2020-04-20 23:22:36 +08:00
|
|
|
|
using System.IO;
|
2020-04-15 00:24:11 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-04-20 23:22:36 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
2020-04-15 00:24:11 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.CustomAction;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class Helper
|
|
|
|
|
{
|
|
|
|
|
public static bool AllowRunnerToForeground()
|
|
|
|
|
{
|
|
|
|
|
var result = false;
|
|
|
|
|
var processes = Process.GetProcessesByName("PowerToys");
|
|
|
|
|
if (processes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var pid = processes[0].Id;
|
|
|
|
|
result = AllowSetForegroundWindow(pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sendCustomAction = new SendCustomAction(moduleName);
|
|
|
|
|
sendCustomAction.Action = moduleCustomAction;
|
|
|
|
|
return sendCustomAction.ToJsonString();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 23:22:36 +08:00
|
|
|
|
public static FileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback)
|
|
|
|
|
{
|
|
|
|
|
var watcher = new FileSystemWatcher();
|
|
|
|
|
watcher.Path = Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{moduleName}");
|
|
|
|
|
watcher.Filter = fileName;
|
|
|
|
|
watcher.NotifyFilter = NotifyFilters.LastWrite;
|
|
|
|
|
watcher.Changed += (o, e) => onChangedCallback();
|
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
|
|
|
|
|
|
return watcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string LocalApplicationDataFolder()
|
|
|
|
|
{
|
|
|
|
|
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 00:24:11 +08:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern bool AllowSetForegroundWindow(int dwProcessId);
|
2020-04-21 12:01:21 +08:00
|
|
|
|
|
|
|
|
|
private static interop.LayoutMapManaged layoutMap = new interop.LayoutMapManaged();
|
|
|
|
|
|
|
|
|
|
public static string GetKeyName(uint key)
|
|
|
|
|
{
|
|
|
|
|
return layoutMap.GetKeyName(key);
|
|
|
|
|
}
|
2020-04-15 00:24:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|