Răsfoiți Sursa

feat(core): add `Window#open_devtools` API, closes #1213 (#3350)

Lucas Fernandes Nogueira 3 ani în urmă
părinte
comite
55aa22de80

+ 5 - 0
.changes/open-devtools.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Added `Window#open_devtools` API.

+ 6 - 0
.changes/runtime-open-devtools.md

@@ -0,0 +1,6 @@
+---
+"tauri-runtime": patch
+"tauri-runtime-wry": patch
+---
+
+Added `open_devtools` to the `Dispatcher` trait.

+ 3 - 2
core/tauri-runtime-wry/Cargo.toml

@@ -13,7 +13,7 @@ exclude = [ ".license_template", "CHANGELOG.md", "/target" ]
 readme = "README.md"
 
 [dependencies]
-wry = { version = "0.13", default-features = false, features = [ "file-drop", "protocol" ] }
+wry = { version = "0.13.1", default-features = false, features = [ "file-drop", "protocol" ] }
 tauri-runtime = { version = "0.2.1", path = "../tauri-runtime" }
 tauri-utils = { version = "1.0.0-beta.3", path = "../tauri-utils" }
 uuid = { version = "0.8.2", features = [ "v4" ] }
@@ -21,7 +21,7 @@ infer = "0.4"
 
 [target."cfg(windows)".dependencies]
 ico = "0.1"
-webview2-com = "0.10.0"
+webview2-com = "0.11.0"
 
 [target."cfg(windows)".dependencies.windows]
 version = "0.30.0"
@@ -35,5 +35,6 @@ gtk = { version = "0.15", features = [ "v3_20" ] }
 
 [features]
 dox = [ "wry/dox" ]
+devtools = [ "wry/devtool", "tauri-runtime/devtools" ]
 system-tray = [ "wry/tray", "tauri-runtime/system-tray" ]
 macos-private-api = [ "wry/fullscreen", "wry/transparent", "tauri-runtime/macos-private-api" ]

+ 21 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -913,6 +913,8 @@ unsafe impl Send for GtkWindow {}
 
 #[derive(Debug, Clone)]
 pub enum WindowMessage {
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  OpenDevTools,
   // Getters
   ScaleFactor(Sender<f64>),
   InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -1090,6 +1092,14 @@ impl Dispatch for WryDispatcher {
     id
   }
 
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  fn open_devtools(&self) {
+    let _ = send_user_message(
+      &self.context,
+      Message::Window(self.window_id, WindowMessage::OpenDevTools),
+    );
+  }
+
   // Getters
 
   fn scale_factor(&self) -> Result<f64> {
@@ -1933,6 +1943,12 @@ fn handle_user_message(
       {
         let window = webview.inner.window();
         match window_message {
+          #[cfg(any(debug_assertions, feature = "devtools"))]
+          WindowMessage::OpenDevTools => {
+            if let WindowHandle::Webview(w) = &webview.inner {
+              w.devtool();
+            }
+          }
           // Getters
           WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(),
           WindowMessage::InnerPosition(tx) => tx
@@ -2683,6 +2699,11 @@ fn create_webview(
     webview_builder.webview.clipboard = true;
   }
 
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  {
+    webview_builder = webview_builder.with_dev_tool(true);
+  }
+
   let webview = webview_builder
     .with_web_context(web_context)
     .build()

+ 2 - 1
core/tauri-runtime/Cargo.toml

@@ -33,7 +33,7 @@ http-range = "0.1.4"
 infer = "0.4"
 
 [target."cfg(windows)".dependencies]
-webview2-com = "0.10.0"
+webview2-com = "0.11.0"
 
 [target."cfg(windows)".dependencies.windows]
 version = "0.30.0"
@@ -45,5 +45,6 @@ features = [
 gtk = { version = "0.15", features = [ "v3_20" ] }
 
 [features]
+devtools = [ ]
 system-tray = [ ]
 macos-private-api = [ ]

+ 3 - 0
core/tauri-runtime/src/lib.rs

@@ -365,6 +365,9 @@ pub trait Dispatch: Debug + Clone + Send + Sized + 'static {
   /// Registers a window event handler.
   fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid;
 
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  fn open_devtools(&self);
+
   // GETTERS
 
   /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.

+ 2 - 1
core/tauri/Cargo.toml

@@ -20,7 +20,7 @@ version = "1.0.0-beta.8"
 
 [package.metadata.docs.rs]
 default-features = false
-features = ["compression", "wry", "isolation", "custom-protocol", "api-all", "cli", "updater", "system-tray", "dox"]
+features = ["compression", "wry", "isolation", "custom-protocol", "api-all", "cli", "updater", "system-tray", "devtools", "dox"]
 rustdoc-args = [ "--cfg", "doc_cfg" ]
 default-target = "x86_64-unknown-linux-gnu"
 targets = [
@@ -120,6 +120,7 @@ dialog = ["rfd"]
 notification = ["notify-rust"]
 cli = ["clap"]
 system-tray = ["tauri-runtime/system-tray", "tauri-runtime-wry/system-tray"]
+devtools = ["tauri-runtime/devtools", "tauri-runtime-wry/devtools"]
 dox = ["tauri-runtime-wry/dox"]
 macos-private-api = ["tauri-runtime/macos-private-api", "tauri-runtime-wry/macos-private-api"]
 window-data-url = ["data-url"]

+ 2 - 0
core/tauri/src/lib.rs

@@ -14,6 +14,8 @@
 //! - **isolation**: Enables the isolation pattern. Enabled by default if the `tauri > pattern > use` config option is set to `isolation` on the `tauri.conf.json` file.
 //! - **custom-protocol**: Feature managed by the Tauri CLI. When enabled, Tauri assumes a production environment instead of a development one.
 //! - **updater**: Enables the application auto updater. Enabled by default if the `updater` config is defined on the `tauri.conf.json` file.
+//! - **devtools**: Enables the developer tools (Web inspector) and [`Window::open_devtools`]. Enabled by default on debug builds.
+//! On macOS it uses private APIs, so you can't enable it if your app will be published to the App Store.
 //! - **http-api**: Enables the [`api::http`] module.
 //! - **reqwest-client**: Uses `reqwest` as HTTP client on the `http` APIs. Improves performance, but increases the bundle size.
 //! - **command**: Enables the [`api::process::Command`] APIs.

+ 3 - 0
core/tauri/src/test/mock_runtime.rs

@@ -261,6 +261,9 @@ impl Dispatch for MockDispatcher {
     Uuid::new_v4()
   }
 
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  fn open_devtools(&self) {}
+
   fn scale_factor(&self) -> Result<f64> {
     Ok(1.0)
   }

+ 25 - 0
core/tauri/src/window.rs

@@ -403,6 +403,31 @@ impl<R: Runtime> Window<R> {
       })
   }
 
+  /// Opens the developer tools window (Web Inspector).
+  /// The devtools is only enabled on debug builds or with the `devtools` feature flag.
+  ///
+  /// ## Platform-specific
+  ///
+  /// - **macOS**: This is a private API on macOS,
+  /// so you cannot use this if your application will be published on the App Store.
+  ///
+  /// # Example
+  ///
+  /// ```rust,no_run
+  /// use tauri::Manager;
+  /// tauri::Builder::default()
+  ///   .setup(|app| {
+  ///     #[cfg(debug_assertions)]
+  ///     app.get_window("main").unwrap().open_devtools();
+  ///     Ok(())
+  ///   });
+  /// ```
+  #[cfg(any(debug_assertions, feature = "devtools"))]
+  #[cfg_attr(doc_cfg, doc(cfg(any(debug_assertions, feature = "devtools"))))]
+  pub fn open_devtools(&self) {
+    self.window.dispatcher.open_devtools();
+  }
+
   // Getters
 
   /// Gets a handle to the window menu.

+ 6 - 6
examples/api/src-tauri/Cargo.lock

@@ -3907,9 +3907,9 @@ dependencies = [
 
 [[package]]
 name = "webview2-com"
-version = "0.10.0"
+version = "0.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "381febeeb86214fd262941a2b26322c33c38bf8b565b0ddfd4a7a8d4003053a9"
+checksum = "1975ce3573344c099935fe3903f1708dac69efe8539f1efee3ae54d8f9315fbb"
 dependencies = [
  "webview2-com-macros",
  "webview2-com-sys",
@@ -3930,9 +3930,9 @@ dependencies = [
 
 [[package]]
 name = "webview2-com-sys"
-version = "0.10.0"
+version = "0.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2e3542bb16fe61e951f87c9146571344f1e773327498efa65704a4cff472662"
+checksum = "9a746838a94b7391f707209a246e3436d81d1e71832126a65a897d3ee5511040"
 dependencies = [
  "regex",
  "serde",
@@ -4133,9 +4133,9 @@ dependencies = [
 
 [[package]]
 name = "wry"
-version = "0.13.0"
+version = "0.13.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7f4875fbbfc2c63f6c57c4ef84f678b1b57e3b8795443add7fbd02f3e8017e30"
+checksum = "194b2750d8fe10fef189af5e2ca09e56cb8c5458a365d2b32842b024351f58c9"
 dependencies = [
  "cocoa",
  "core-graphics",

+ 5 - 0
examples/api/src-tauri/src/main.rs

@@ -59,6 +59,11 @@ fn main() {
 
   #[allow(unused_mut)]
   let mut app = tauri::Builder::default()
+    .setup(|app| {
+      #[cfg(debug_assertions)]
+      app.get_window("main").unwrap().open_devtools();
+      Ok(())
+    })
     .on_page_load(|window, _| {
       let window_ = window.clone();
       window.listen("js-event", move |event| {