diff --git a/src/OneWare.Essentials/Helpers/PlatformHelper.cs b/src/OneWare.Essentials/Helpers/PlatformHelper.cs index 9f40cd2..6eafb9d 100644 --- a/src/OneWare.Essentials/Helpers/PlatformHelper.cs +++ b/src/OneWare.Essentials/Helpers/PlatformHelper.cs @@ -21,11 +21,11 @@ public enum PlatformId Wasm, Unknown } - + public static class PlatformHelper { public static PlatformId Platform { get; } - + public static string ExecutableExtension { get; } = string.Empty; static PlatformHelper() @@ -63,12 +63,12 @@ static PlatformHelper() Platform = PlatformId.Wasm; } } - + public static bool Exists(string path) { return File.Exists(path) || ExistsOnPath(path); } - + public static bool ExistsOnPath(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) return false; @@ -91,7 +91,7 @@ public static bool ExistsOnPath(string fileName) return null; } - + public static void OpenHyperLink(string link) { try @@ -105,7 +105,8 @@ public static void OpenHyperLink(string link) } catch (Exception e) { - ContainerLocator.Container.Resolve()?.Error("Failed open: " + link + " | " + e.Message, e, true, true); + ContainerLocator.Container.Resolve() + ?.Error("Failed open: " + link + " | " + e.Message, e, true, true); } } @@ -115,8 +116,8 @@ public static void OpenExplorerPath(string path) { if (Path.HasExtension(path)) path = Path.GetDirectoryName(path) ?? ""; else path = Path.GetFullPath(path); - - if(string.IsNullOrEmpty(path)) return; + + if (string.IsNullOrEmpty(path)) return; Process.Start(new ProcessStartInfo { @@ -127,12 +128,13 @@ public static void OpenExplorerPath(string path) } catch (Exception e) { - ContainerLocator.Container.Resolve().Error("Can't open " + path + " in explorer. " + e, e, true, true); + ContainerLocator.Container.Resolve() + .Error("Can't open " + path + " in explorer. " + e, e, true, true); } } - + #region File Management - + public static void CopyFile(string sourcePath, string destinationPath, bool overwrite = false) { File.Copy(sourcePath, destinationPath, overwrite); @@ -142,14 +144,14 @@ public static void CopyFile(string sourcePath, string destinationPath, bool over public static void CopyDirectory(string sourcePath, string destPath) { var dir = new DirectoryInfo(sourcePath); - + if (!dir.Exists) throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); - + var dirs = dir.GetDirectories(); - + Directory.CreateDirectory(destPath); - + foreach (var file in dir.GetFiles()) { var targetFilePath = Path.Combine(destPath, file.Name); @@ -163,7 +165,7 @@ public static void CopyDirectory(string sourcePath, string destPath) CopyDirectory(subDir.FullName, newDestinationDir); } } - + public static void ExecBash(string cmd) { var escapedArgs = cmd.Replace("\"", "\\\""); @@ -203,16 +205,18 @@ public static async Task WriteTextFileAsync(string path, string text) public static void ChmodFile(string path) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && RuntimeInformation.ProcessArchitecture is not Architecture.Wasm) + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + RuntimeInformation.ProcessArchitecture is not Architecture.Wasm) ExecBash($"chmod 777 '{path}'"); } public static void ChmodFolder(string path) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && RuntimeInformation.ProcessArchitecture is not Architecture.Wasm) + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + RuntimeInformation.ProcessArchitecture is not Architecture.Wasm) ExecBash($"chmod -R 777 '{path}'"); } - + #endregion #region BringWindowToFront WINDOWS @@ -277,44 +281,69 @@ public static void ActivateWindow(IntPtr mainWindowHandle, IntPtr displayHandle) } #endregion - + #region Window Styles - - public static Thickness WindowsOnlyBorder => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime ? new Thickness(1) : new - Thickness(0); - public static CornerRadius WindowsCornerRadius => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime + public static Thickness WindowsOnlyBorder => + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime + ? new Thickness(1) + : new + Thickness(0); + + public static CornerRadius WindowsCornerRadius => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Application.Current?.ApplicationLifetime is + IClassicDesktopStyleApplicationLifetime ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(8) : new CornerRadius(0)) : new CornerRadius(0); - - public static CornerRadius WindowsCornerRadiusBottom => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime - ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0,0,8,8) : new CornerRadius(0)) - : new CornerRadius(0); - - public static CornerRadius WindowsCornerRadiusBottomLeft => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime - ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0,0,0,8) : new CornerRadius(0)) - : new CornerRadius(0); - - public static CornerRadius WindowsCornerRadiusBottomRight => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime - ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0,0,8,0) : new CornerRadius(0)) + + public static CornerRadius WindowsCornerRadiusBottom => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Application.Current?.ApplicationLifetime is + IClassicDesktopStyleApplicationLifetime + ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0, 0, 8, 8) : new CornerRadius(0)) : new CornerRadius(0); + + public static CornerRadius WindowsCornerRadiusBottomLeft => + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime + ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0, 0, 0, 8) : new CornerRadius(0)) + : new CornerRadius(0); + + public static CornerRadius WindowsCornerRadiusBottomRight => + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime + ? (Environment.OSVersion.Version.Build >= 22000 ? new CornerRadius(0, 0, 8, 0) : new CornerRadius(0)) + : new CornerRadius(0); + #endregion - + #region Keys - + public static KeyModifiers ControlKey => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? KeyModifiers.Meta : KeyModifiers.Control; - + + public static bool IsControl(KeyEventArgs e) + { + switch (Platform) + { + case PlatformId.OsxArm64: + case PlatformId.OsxX64: + return e.KeyModifiers.HasFlag(ControlKey) || e.Key is Key.LWin or Key.RWin; + default: + return e.KeyModifiers.HasFlag(ControlKey) || e.Key is Key.LeftCtrl or Key.RightCtrl; + } + } + #endregion - + private static readonly IPEndPoint DefaultLoopbackEndpoint = new(IPAddress.Loopback, 0); public static int GetAvailablePort() { using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(DefaultLoopbackEndpoint); - + return (socket.LocalEndPoint as IPEndPoint)?.Port ?? throw new Exception("Error getting free port!"); } }