Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
improve controlactions
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Jul 2, 2024
1 parent b149aa7 commit 55f8fba
Showing 1 changed file with 69 additions and 40 deletions.
109 changes: 69 additions & 40 deletions src/OneWare.Essentials/Helpers/PlatformHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -91,7 +91,7 @@ public static bool ExistsOnPath(string fileName)

return null;
}

public static void OpenHyperLink(string link)
{
try
Expand All @@ -105,7 +105,8 @@ public static void OpenHyperLink(string link)
}
catch (Exception e)
{
ContainerLocator.Container.Resolve<ILogger>()?.Error("Failed open: " + link + " | " + e.Message, e, true, true);
ContainerLocator.Container.Resolve<ILogger>()
?.Error("Failed open: " + link + " | " + e.Message, e, true, true);
}
}

Expand All @@ -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
{
Expand All @@ -127,12 +128,13 @@ public static void OpenExplorerPath(string path)
}
catch (Exception e)
{
ContainerLocator.Container.Resolve<ILogger>().Error("Can't open " + path + " in explorer. " + e, e, true, true);
ContainerLocator.Container.Resolve<ILogger>()
.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);
Expand All @@ -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);
Expand All @@ -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("\"", "\\\"");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!");
}
}
Expand Down

0 comments on commit 55f8fba

Please sign in to comment.