diff --git a/.changes/open-devtools.md b/.changes/open-devtools.md new file mode 100644 index 000000000..416969967 --- /dev/null +++ b/.changes/open-devtools.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Added `Window#open_devtools` API. diff --git a/.changes/runtime-open-devtools.md b/.changes/runtime-open-devtools.md new file mode 100644 index 000000000..72d31a52d --- /dev/null +++ b/.changes/runtime-open-devtools.md @@ -0,0 +1,6 @@ +--- +"tauri-runtime": patch +"tauri-runtime-wry": patch +--- + +Added `open_devtools` to the `Dispatcher` trait. diff --git a/core/tauri-runtime-wry/Cargo.toml b/core/tauri-runtime-wry/Cargo.toml index debdf9102..db22ed8bf 100644 --- a/core/tauri-runtime-wry/Cargo.toml +++ b/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" ] diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 9fd911672..b4a298c14 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/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), InnerPosition(Sender>>), @@ -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 { @@ -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() diff --git a/core/tauri-runtime/Cargo.toml b/core/tauri-runtime/Cargo.toml index 8be699d6b..77759a45f 100644 --- a/core/tauri-runtime/Cargo.toml +++ b/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 = [ ] diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index ec827e9df..ddb66e683 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/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(&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. diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 5a82de2f9..2e3febb89 100644 --- a/core/tauri/Cargo.toml +++ b/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"] diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index b62cdb53e..99e664036 100644 --- a/core/tauri/src/lib.rs +++ b/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. diff --git a/core/tauri/src/test/mock_runtime.rs b/core/tauri/src/test/mock_runtime.rs index e70e211fb..954c88566 100644 --- a/core/tauri/src/test/mock_runtime.rs +++ b/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 { Ok(1.0) } diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 1eaf0373c..de323bb67 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -403,6 +403,31 @@ impl Window { }) } + /// 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. diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 6bc7fbeae..ed1521fa3 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/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", diff --git a/examples/api/src-tauri/src/main.rs b/examples/api/src-tauri/src/main.rs index 88300079b..8da65e657 100644 --- a/examples/api/src-tauri/src/main.rs +++ b/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| {