shell.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Types and functions related to shell.
  5. use crate::ShellScope;
  6. use std::str::FromStr;
  7. /// Program to use on the [`open()`] call.
  8. pub enum Program {
  9. /// Use the `open` program.
  10. Open,
  11. /// Use the `start` program.
  12. Start,
  13. /// Use the `xdg-open` program.
  14. XdgOpen,
  15. /// Use the `gio` program.
  16. Gio,
  17. /// Use the `gnome-open` program.
  18. GnomeOpen,
  19. /// Use the `kde-open` program.
  20. KdeOpen,
  21. /// Use the `wslview` program.
  22. WslView,
  23. /// Use the `Firefox` program.
  24. Firefox,
  25. /// Use the `Google Chrome` program.
  26. Chrome,
  27. /// Use the `Chromium` program.
  28. Chromium,
  29. /// Use the `Safari` program.
  30. Safari,
  31. }
  32. impl FromStr for Program {
  33. type Err = super::Error;
  34. fn from_str(s: &str) -> Result<Self, Self::Err> {
  35. let p = match s.to_lowercase().as_str() {
  36. "open" => Self::Open,
  37. "start" => Self::Start,
  38. "xdg-open" => Self::XdgOpen,
  39. "gio" => Self::Gio,
  40. "gnome-open" => Self::GnomeOpen,
  41. "kde-open" => Self::KdeOpen,
  42. "wslview" => Self::WslView,
  43. "firefox" => Self::Firefox,
  44. "chrome" | "google chrome" => Self::Chrome,
  45. "chromium" => Self::Chromium,
  46. "safari" => Self::Safari,
  47. _ => return Err(super::Error::UnknownProgramName(s.to_string())),
  48. };
  49. Ok(p)
  50. }
  51. }
  52. impl Program {
  53. pub(crate) fn name(self) -> &'static str {
  54. match self {
  55. Self::Open => "open",
  56. Self::Start => "start",
  57. Self::XdgOpen => "xdg-open",
  58. Self::Gio => "gio",
  59. Self::GnomeOpen => "gnome-open",
  60. Self::KdeOpen => "kde-open",
  61. Self::WslView => "wslview",
  62. #[cfg(target_os = "macos")]
  63. Self::Firefox => "Firefox",
  64. #[cfg(not(target_os = "macos"))]
  65. Self::Firefox => "firefox",
  66. #[cfg(target_os = "macos")]
  67. Self::Chrome => "Google Chrome",
  68. #[cfg(not(target_os = "macos"))]
  69. Self::Chrome => "google-chrome",
  70. #[cfg(target_os = "macos")]
  71. Self::Chromium => "Chromium",
  72. #[cfg(not(target_os = "macos"))]
  73. Self::Chromium => "chromium",
  74. #[cfg(target_os = "macos")]
  75. Self::Safari => "Safari",
  76. #[cfg(not(target_os = "macos"))]
  77. Self::Safari => "safari",
  78. }
  79. }
  80. }
  81. /// Opens path or URL with program specified in `with`, or system default if `None`.
  82. ///
  83. /// The path will be matched against the shell open validation regex, defaulting to `^https?://`.
  84. /// A custom validation regex may be supplied in the config in `tauri > allowlist > scope > open`.
  85. pub fn open(scope: &ShellScope, path: String, with: Option<Program>) -> crate::api::Result<()> {
  86. scope
  87. .open(&path, with)
  88. .map_err(|err| crate::api::Error::Shell(format!("failed to open: {}", err)))
  89. }