Skip to content

Commit 5c59e59

Browse files
committed
refactor: ensure running as admin is cross plat
1 parent e86507f commit 5c59e59

1 file changed

Lines changed: 81 additions & 35 deletions

File tree

SnapX.Core/Job/TaskHelpers.cs

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ private static ReadOnlyMemory<Color> CreateGrayscalePalette(int maxGrayLevels)
467467
// Create a list of grayscale colors (from black to white)
468468
var grayscaleColors = new Color[maxGrayLevels];
469469

470-
for (int i = 0; i < maxGrayLevels; i++)
470+
for (var i = 0; i < maxGrayLevels; i++)
471471
{
472472
var grayValue = i * 255 / (maxGrayLevels - 1);
473473
grayscaleColors[i] = new Color(new Rgba32(grayValue, grayValue, grayValue, 255));
@@ -477,40 +477,38 @@ private static ReadOnlyMemory<Color> CreateGrayscalePalette(int maxGrayLevels)
477477
}
478478
public static void SaveImageAsFile(Image img, TaskSettings taskSettings, bool overwriteFile = false)
479479
{
480-
using (ImageData imageData = PrepareImage(img, taskSettings))
481-
{
482-
string? screenshotsFolder = GetScreenshotsFolder(taskSettings);
483-
string? fileName = GetFileName(taskSettings, imageData.ImageFormat.GetDescription(), img);
484-
string? filePath = Path.Combine(screenshotsFolder, fileName);
480+
using var imageData = PrepareImage(img, taskSettings);
481+
var screenshotsFolder = GetScreenshotsFolder(taskSettings);
482+
var fileName = GetFileName(taskSettings, imageData.ImageFormat.GetDescription(), img);
483+
var filePath = Path.Combine(screenshotsFolder, fileName);
485484

486-
if (!overwriteFile)
487-
{
488-
filePath = HandleExistsFile(filePath, taskSettings);
489-
}
490-
491-
if (!string.IsNullOrEmpty(filePath))
492-
{
493-
imageData.Write(filePath);
494-
DebugHelper.WriteLine("Image saved to file: " + filePath);
495-
}
485+
if (!overwriteFile)
486+
{
487+
filePath = HandleExistsFile(filePath, taskSettings);
496488
}
489+
490+
if (string.IsNullOrEmpty(filePath)) return;
491+
imageData.Write(filePath);
492+
DebugHelper.WriteLine("Image saved to file: " + filePath);
497493
}
498494
public static string? HandleExistsFile(string? filePath, TaskSettings taskSettings)
499495
{
500-
if (File.Exists(filePath))
496+
if (!File.Exists(filePath)) return filePath;
497+
switch (taskSettings.ImageSettings.FileExistAction)
501498
{
502-
switch (taskSettings.ImageSettings.FileExistAction)
503-
{
504-
case FileExistAction.Ask:
505-
new NotImplementedException("FileExistAction.Ask not implemented").ShowError();
506-
break;
507-
case FileExistAction.UniqueName:
508-
filePath = FileHelpers.GetUniqueFilePath(filePath);
509-
break;
510-
case FileExistAction.Cancel:
511-
filePath = "";
512-
break;
513-
}
499+
case FileExistAction.Ask:
500+
new NotImplementedException("FileExistAction.Ask not implemented").ShowError();
501+
break;
502+
case FileExistAction.UniqueName:
503+
filePath = FileHelpers.GetUniqueFilePath(filePath);
504+
break;
505+
case FileExistAction.Cancel:
506+
filePath = "";
507+
break;
508+
case FileExistAction.Overwrite:
509+
break;
510+
default:
511+
throw new ArgumentOutOfRangeException();
514512
}
515513

516514
return filePath;
@@ -689,25 +687,73 @@ public static void RunShareXAsAdmin(string arguments = null)
689687
{
690688
try
691689
{
692-
using (Process process = new Process())
690+
using var process = new Process();
691+
var exePath = Assembly.GetExecutingAssembly().Location;
692+
var isWindows = OperatingSystem.IsWindows();
693+
var isLinux = OperatingSystem.IsLinux();
694+
var isMacOS = OperatingSystem.IsMacOS();
695+
var isFreeBSD = OperatingSystem.IsFreeBSD();
696+
697+
ProcessStartInfo psi;
698+
699+
if (isWindows)
693700
{
694-
ProcessStartInfo psi = new ProcessStartInfo()
701+
psi = new ProcessStartInfo()
695702
{
696-
FileName = Assembly.GetExecutingAssembly().Location,
703+
FileName = exePath,
697704
Arguments = arguments,
698705
UseShellExecute = true,
699706
Verb = "runas"
700707
};
701-
702-
process.StartInfo = psi;
703-
process.Start();
704708
}
709+
else if (isLinux)
710+
{
711+
psi = new ProcessStartInfo()
712+
{
713+
FileName = "pkexec",
714+
ArgumentList = { exePath },
715+
UseShellExecute = false
716+
};
717+
if (!string.IsNullOrEmpty(arguments))
718+
psi.ArgumentList.Add(arguments);
719+
}
720+
else if (isMacOS)
721+
{
722+
psi = new ProcessStartInfo()
723+
{
724+
FileName = "osascript",
725+
ArgumentList =
726+
{
727+
"-e", $"do shell script \"'{exePath}' {(arguments ?? "")}\" with administrator privileges"
728+
},
729+
UseShellExecute = false
730+
};
731+
}
732+
else if (isFreeBSD)
733+
{
734+
psi = new ProcessStartInfo()
735+
{
736+
FileName = "doas",
737+
ArgumentList = { exePath },
738+
UseShellExecute = false
739+
};
740+
if (!string.IsNullOrEmpty(arguments))
741+
psi.ArgumentList.Add(arguments);
742+
}
743+
else
744+
{
745+
return;
746+
}
747+
748+
process.StartInfo = psi;
749+
process.Start();
705750
}
706751
catch
707752
{
708753
}
709754
}
710755

756+
711757
public static void SearchImageUsingGoogleLens(string? url)
712758
{
713759
new GoogleLensSharingService().CreateSharer(null, null).ShareURL(url);

0 commit comments

Comments
 (0)