mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
[Settings, Common.UI, runner exe] Unify exe/dll naming (#15005)
* Unify exe/dll naming - PowerToys.Runner Align naming with other exes - PowerToys Runner -> PowerToys.Runner * Unify exe/dll naming - Microsoft.PowerToys.Common.UI Project name - Microsoft.PowerToys.Common.UI -> Common.UI dll name - Microsoft.PowerToys.Common.UI.dll -> PowerToys.Common.UI.dll * Unify exe/dll naming - Settings Project names - Microsoft.PowerToys.Settings* -> Settings* Dll names - Microsoft.PowerToys.Settings*.dll -> PowerToys.Settings*.dll * Revert file autoformat * [Docs] Update paths to settings projects/files * Fix tests - Update path
This commit is contained in:
139
src/settings-ui/Settings.UI.Library/Utilities/Helper.cs
Normal file
139
src/settings-ui/Settings.UI.Library/Utilities/Helper.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
// 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;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.CustomAction;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
||||
{
|
||||
public static class Helper
|
||||
{
|
||||
public static readonly IFileSystem FileSystem = new FileSystem();
|
||||
|
||||
public static bool AllowRunnerToForeground()
|
||||
{
|
||||
var result = false;
|
||||
var processes = Process.GetProcessesByName("PowerToys");
|
||||
if (processes.Length > 0)
|
||||
{
|
||||
var pid = processes[0].Id;
|
||||
result = NativeMethods.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)
|
||||
{
|
||||
Action = moduleCustomAction,
|
||||
};
|
||||
|
||||
return sendCustomAction.ToJsonString();
|
||||
}
|
||||
|
||||
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback)
|
||||
{
|
||||
var path = FileSystem.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{moduleName}");
|
||||
|
||||
if (!FileSystem.Directory.Exists(path))
|
||||
{
|
||||
FileSystem.Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
var watcher = FileSystem.FileSystemWatcher.CreateNew();
|
||||
watcher.Path = path;
|
||||
watcher.Filter = fileName;
|
||||
watcher.NotifyFilter = NotifyFilters.LastWrite;
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
watcher.Changed += (o, e) => onChangedCallback();
|
||||
|
||||
return watcher;
|
||||
}
|
||||
|
||||
private static string LocalApplicationDataFolder()
|
||||
{
|
||||
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
}
|
||||
|
||||
public static string GetPowerToysInstallationFolder()
|
||||
{
|
||||
var settingsPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
||||
return Directory.GetParent(settingsPath).FullName;
|
||||
}
|
||||
|
||||
private static readonly interop.LayoutMapManaged LayoutMap = new interop.LayoutMapManaged();
|
||||
|
||||
public static string GetKeyName(uint key)
|
||||
{
|
||||
return LayoutMap.GetKeyName(key);
|
||||
}
|
||||
|
||||
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};
|
||||
if (version1 == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(version1));
|
||||
}
|
||||
else if (version2 == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(version2));
|
||||
}
|
||||
|
||||
var v1 = version1.Substring(1).Split('.').Select(int.Parse).ToArray();
|
||||
var v2 = version2.Substring(1).Split('.').Select(int.Parse).ToArray();
|
||||
|
||||
if (v1.Length != 3 || v2.Length != 3)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
21
src/settings-ui/Settings.UI.Library/Utilities/IIOProvider.cs
Normal file
21
src/settings-ui/Settings.UI.Library/Utilities/IIOProvider.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
||||
{
|
||||
public interface IIOProvider
|
||||
{
|
||||
bool FileExists(string path);
|
||||
|
||||
bool DirectoryExists(string path);
|
||||
|
||||
bool CreateDirectory(string path);
|
||||
|
||||
void DeleteDirectory(string path);
|
||||
|
||||
void WriteAllText(string path, string content);
|
||||
|
||||
string ReadAllText(string path);
|
||||
}
|
||||
}
|
||||
81
src/settings-ui/Settings.UI.Library/Utilities/Logger.cs
Normal file
81
src/settings-ui/Settings.UI.Library/Utilities/Logger.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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.Globalization;
|
||||
using System.IO.Abstractions;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
private static readonly IFileSystem FileSystem = new FileSystem();
|
||||
private static readonly IPath Path = FileSystem.Path;
|
||||
private static readonly IDirectory Directory = FileSystem.Directory;
|
||||
|
||||
private static readonly string ApplicationLogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\PowerToys\\Settings Logs");
|
||||
|
||||
static Logger()
|
||||
{
|
||||
if (!Directory.Exists(ApplicationLogPath))
|
||||
{
|
||||
Directory.CreateDirectory(ApplicationLogPath);
|
||||
}
|
||||
|
||||
// Using InvariantCulture since this is used for a log file name
|
||||
var logFilePath = Path.Combine(ApplicationLogPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".txt");
|
||||
|
||||
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
|
||||
|
||||
Trace.AutoFlush = true;
|
||||
}
|
||||
|
||||
public static void LogInfo(string message)
|
||||
{
|
||||
Log(message, "INFO");
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
Log(message, "ERROR");
|
||||
#if DEBUG
|
||||
Debugger.Break();
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void LogError(string message, Exception e)
|
||||
{
|
||||
Log(
|
||||
message + Environment.NewLine +
|
||||
e?.Message + Environment.NewLine +
|
||||
"Inner exception: " + Environment.NewLine +
|
||||
e?.InnerException?.Message + Environment.NewLine +
|
||||
"Stack trace: " + Environment.NewLine +
|
||||
e?.StackTrace,
|
||||
"ERROR");
|
||||
#if DEBUG
|
||||
Debugger.Break();
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void Log(string message, string type)
|
||||
{
|
||||
Trace.WriteLine(type + ": " + DateTime.Now.TimeOfDay);
|
||||
Trace.Indent();
|
||||
Trace.WriteLine(GetCallerInfo());
|
||||
Trace.WriteLine(message);
|
||||
Trace.Unindent();
|
||||
}
|
||||
|
||||
private static string GetCallerInfo()
|
||||
{
|
||||
StackTrace stackTrace = new StackTrace();
|
||||
|
||||
var methodName = stackTrace.GetFrame(3)?.GetMethod();
|
||||
var className = methodName?.DeclaringType.Name;
|
||||
return "[Method]: " + methodName?.Name + " [Class]: " + className;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
||||
{
|
||||
internal static class NativeMethods
|
||||
{
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool AllowSetForegroundWindow(int dwProcessId);
|
||||
|
||||
public const int SWRESTORE = 9;
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
public static extern bool SetForegroundWindow(IntPtr handle);
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
public static extern bool ShowWindow(IntPtr handle, int nCmdShow);
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
public static extern bool IsIconic(IntPtr handle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// 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.IO.Abstractions;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
||||
{
|
||||
public class SystemIOProvider : IIOProvider
|
||||
{
|
||||
private readonly IDirectory _directory;
|
||||
private readonly IFile _file;
|
||||
|
||||
public SystemIOProvider()
|
||||
: this(new FileSystem())
|
||||
{
|
||||
}
|
||||
|
||||
public SystemIOProvider(IFileSystem fileSystem)
|
||||
: this(fileSystem?.Directory, fileSystem?.File)
|
||||
{
|
||||
}
|
||||
|
||||
private SystemIOProvider(IDirectory directory, IFile file)
|
||||
{
|
||||
_directory = directory ?? throw new ArgumentNullException(nameof(directory));
|
||||
_file = file ?? throw new ArgumentNullException(nameof(file));
|
||||
}
|
||||
|
||||
public bool CreateDirectory(string path)
|
||||
{
|
||||
var directoryInfo = _directory.CreateDirectory(path);
|
||||
return directoryInfo != null;
|
||||
}
|
||||
|
||||
public void DeleteDirectory(string path)
|
||||
{
|
||||
_directory.Delete(path, recursive: true);
|
||||
}
|
||||
|
||||
public bool DirectoryExists(string path)
|
||||
{
|
||||
return _directory.Exists(path);
|
||||
}
|
||||
|
||||
public bool FileExists(string path)
|
||||
{
|
||||
return _file.Exists(path);
|
||||
}
|
||||
|
||||
public string ReadAllText(string path)
|
||||
{
|
||||
return _file.ReadAllText(path);
|
||||
}
|
||||
|
||||
public void WriteAllText(string path, string content)
|
||||
{
|
||||
_file.WriteAllText(path, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user