OpenHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Ui.Common.Helper
  7. {
  8. public static partial class OpenHelper
  9. {
  10. [LibraryImport("shell32.dll", SetLastError = true)]
  11. public static partial int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, IntPtr apidl, uint dwFlags);
  12. [LibraryImport("shell32.dll", SetLastError = true)]
  13. public static partial void ILFree(IntPtr pidlList);
  14. [LibraryImport("shell32.dll", SetLastError = true)]
  15. public static partial IntPtr ILCreateFromPathW([MarshalAs(UnmanagedType.LPWStr)] string pszPath);
  16. public static void OpenFolder(string path)
  17. {
  18. if (Directory.Exists(path))
  19. {
  20. Process.Start(new ProcessStartInfo
  21. {
  22. FileName = path,
  23. UseShellExecute = true,
  24. Verb = "open"
  25. });
  26. }
  27. else
  28. {
  29. Logger.Notice.Print(LogClass.Application, $"Directory \"{path}\" doesn't exist!");
  30. }
  31. }
  32. public static void LocateFile(string path)
  33. {
  34. if (File.Exists(path))
  35. {
  36. if (OperatingSystem.IsWindows())
  37. {
  38. IntPtr pidlList = ILCreateFromPathW(path);
  39. if (pidlList != IntPtr.Zero)
  40. {
  41. try
  42. {
  43. Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
  44. }
  45. finally
  46. {
  47. ILFree(pidlList);
  48. }
  49. }
  50. }
  51. else if (OperatingSystem.IsMacOS())
  52. {
  53. ObjectiveC.NSString nsStringPath = new(path);
  54. IntPtr nsUrl = ObjectiveC.objc_getClass("NSURL");
  55. var urlPtr = ObjectiveC.IntPtr_objc_msgSend(nsUrl, "fileURLWithPath:", nsStringPath);
  56. IntPtr nsArray = ObjectiveC.objc_getClass("NSArray");
  57. IntPtr urlArray = ObjectiveC.IntPtr_objc_msgSend(nsArray, "arrayWithObject:", urlPtr);
  58. IntPtr nsWorkspace = ObjectiveC.objc_getClass("NSWorkspace");
  59. IntPtr sharedWorkspace = ObjectiveC.IntPtr_objc_msgSend(nsWorkspace, "sharedWorkspace");
  60. ObjectiveC.objc_msgSend(sharedWorkspace, "activateFileViewerSelectingURLs:", urlArray);
  61. }
  62. else if (OperatingSystem.IsLinux())
  63. {
  64. Process.Start("dbus-send", $"--session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file://{path}\" string:\"\"");
  65. }
  66. else
  67. {
  68. OpenFolder(Path.GetDirectoryName(path));
  69. }
  70. }
  71. else
  72. {
  73. Logger.Notice.Print(LogClass.Application, $"File \"{path}\" doesn't exist!");
  74. }
  75. }
  76. public static void OpenUrl(string url)
  77. {
  78. if (OperatingSystem.IsWindows())
  79. {
  80. Process.Start(new ProcessStartInfo("cmd", $"/c start {url.Replace("&", "^&")}"));
  81. }
  82. else if (OperatingSystem.IsLinux())
  83. {
  84. Process.Start("xdg-open", url);
  85. }
  86. else if (OperatingSystem.IsMacOS())
  87. {
  88. ObjectiveC.NSString nsStringPath = new(url);
  89. IntPtr nsUrl = ObjectiveC.objc_getClass("NSURL");
  90. var urlPtr = ObjectiveC.IntPtr_objc_msgSend(nsUrl, "URLWithString:", nsStringPath);
  91. IntPtr nsWorkspace = ObjectiveC.objc_getClass("NSWorkspace");
  92. IntPtr sharedWorkspace = ObjectiveC.IntPtr_objc_msgSend(nsWorkspace, "sharedWorkspace");
  93. ObjectiveC.bool_objc_msgSend(sharedWorkspace, "openURL:", urlPtr);
  94. }
  95. else
  96. {
  97. Logger.Notice.Print(LogClass.Application, $"Cannot open url \"{url}\" on this platform!");
  98. }
  99. }
  100. }
  101. }