Skip to content

Commit 55aa22d

Browse files
authored
feat(core): add Window#open_devtools API, closes #1213 (#3350)
1 parent 5803017 commit 55aa22d

12 files changed

Lines changed: 83 additions & 10 deletions

File tree

.changes/open-devtools.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Added `Window#open_devtools` API.

.changes/runtime-open-devtools.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch
3+
"tauri-runtime-wry": patch
4+
---
5+
6+
Added `open_devtools` to the `Dispatcher` trait.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ exclude = [ ".license_template", "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { version = "0.13", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { version = "0.13.1", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.2.1", path = "../tauri-runtime" }
1818
tauri-utils = { version = "1.0.0-beta.3", path = "../tauri-utils" }
1919
uuid = { version = "0.8.2", features = [ "v4" ] }
2020
infer = "0.4"
2121

2222
[target."cfg(windows)".dependencies]
2323
ico = "0.1"
24-
webview2-com = "0.10.0"
24+
webview2-com = "0.11.0"
2525

2626
[target."cfg(windows)".dependencies.windows]
2727
version = "0.30.0"
@@ -35,5 +35,6 @@ gtk = { version = "0.15", features = [ "v3_20" ] }
3535

3636
[features]
3737
dox = [ "wry/dox" ]
38+
devtools = [ "wry/devtool", "tauri-runtime/devtools" ]
3839
system-tray = [ "wry/tray", "tauri-runtime/system-tray" ]
3940
macos-private-api = [ "wry/fullscreen", "wry/transparent", "tauri-runtime/macos-private-api" ]

core/tauri-runtime-wry/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ unsafe impl Send for GtkWindow {}
913913

914914
#[derive(Debug, Clone)]
915915
pub enum WindowMessage {
916+
#[cfg(any(debug_assertions, feature = "devtools"))]
917+
OpenDevTools,
916918
// Getters
917919
ScaleFactor(Sender<f64>),
918920
InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -1090,6 +1092,14 @@ impl Dispatch for WryDispatcher {
10901092
id
10911093
}
10921094

1095+
#[cfg(any(debug_assertions, feature = "devtools"))]
1096+
fn open_devtools(&self) {
1097+
let _ = send_user_message(
1098+
&self.context,
1099+
Message::Window(self.window_id, WindowMessage::OpenDevTools),
1100+
);
1101+
}
1102+
10931103
// Getters
10941104

10951105
fn scale_factor(&self) -> Result<f64> {
@@ -1933,6 +1943,12 @@ fn handle_user_message(
19331943
{
19341944
let window = webview.inner.window();
19351945
match window_message {
1946+
#[cfg(any(debug_assertions, feature = "devtools"))]
1947+
WindowMessage::OpenDevTools => {
1948+
if let WindowHandle::Webview(w) = &webview.inner {
1949+
w.devtool();
1950+
}
1951+
}
19361952
// Getters
19371953
WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(),
19381954
WindowMessage::InnerPosition(tx) => tx
@@ -2683,6 +2699,11 @@ fn create_webview(
26832699
webview_builder.webview.clipboard = true;
26842700
}
26852701

2702+
#[cfg(any(debug_assertions, feature = "devtools"))]
2703+
{
2704+
webview_builder = webview_builder.with_dev_tool(true);
2705+
}
2706+
26862707
let webview = webview_builder
26872708
.with_web_context(web_context)
26882709
.build()

core/tauri-runtime/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ http-range = "0.1.4"
3333
infer = "0.4"
3434

3535
[target."cfg(windows)".dependencies]
36-
webview2-com = "0.10.0"
36+
webview2-com = "0.11.0"
3737

3838
[target."cfg(windows)".dependencies.windows]
3939
version = "0.30.0"
@@ -45,5 +45,6 @@ features = [
4545
gtk = { version = "0.15", features = [ "v3_20" ] }
4646

4747
[features]
48+
devtools = [ ]
4849
system-tray = [ ]
4950
macos-private-api = [ ]

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ pub trait Dispatch: Debug + Clone + Send + Sized + 'static {
365365
/// Registers a window event handler.
366366
fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid;
367367

368+
#[cfg(any(debug_assertions, feature = "devtools"))]
369+
fn open_devtools(&self);
370+
368371
// GETTERS
369372

370373
/// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.

core/tauri/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ version = "1.0.0-beta.8"
2020

2121
[package.metadata.docs.rs]
2222
default-features = false
23-
features = ["compression", "wry", "isolation", "custom-protocol", "api-all", "cli", "updater", "system-tray", "dox"]
23+
features = ["compression", "wry", "isolation", "custom-protocol", "api-all", "cli", "updater", "system-tray", "devtools", "dox"]
2424
rustdoc-args = [ "--cfg", "doc_cfg" ]
2525
default-target = "x86_64-unknown-linux-gnu"
2626
targets = [
@@ -120,6 +120,7 @@ dialog = ["rfd"]
120120
notification = ["notify-rust"]
121121
cli = ["clap"]
122122
system-tray = ["tauri-runtime/system-tray", "tauri-runtime-wry/system-tray"]
123+
devtools = ["tauri-runtime/devtools", "tauri-runtime-wry/devtools"]
123124
dox = ["tauri-runtime-wry/dox"]
124125
macos-private-api = ["tauri-runtime/macos-private-api", "tauri-runtime-wry/macos-private-api"]
125126
window-data-url = ["data-url"]

core/tauri/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
//! - **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.
1515
//! - **custom-protocol**: Feature managed by the Tauri CLI. When enabled, Tauri assumes a production environment instead of a development one.
1616
//! - **updater**: Enables the application auto updater. Enabled by default if the `updater` config is defined on the `tauri.conf.json` file.
17+
//! - **devtools**: Enables the developer tools (Web inspector) and [`Window::open_devtools`]. Enabled by default on debug builds.
18+
//! On macOS it uses private APIs, so you can't enable it if your app will be published to the App Store.
1719
//! - **http-api**: Enables the [`api::http`] module.
1820
//! - **reqwest-client**: Uses `reqwest` as HTTP client on the `http` APIs. Improves performance, but increases the bundle size.
1921
//! - **command**: Enables the [`api::process::Command`] APIs.

core/tauri/src/test/mock_runtime.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ impl Dispatch for MockDispatcher {
261261
Uuid::new_v4()
262262
}
263263

264+
#[cfg(any(debug_assertions, feature = "devtools"))]
265+
fn open_devtools(&self) {}
266+
264267
fn scale_factor(&self) -> Result<f64> {
265268
Ok(1.0)
266269
}

core/tauri/src/window.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ impl<R: Runtime> Window<R> {
403403
})
404404
}
405405

406+
/// Opens the developer tools window (Web Inspector).
407+
/// The devtools is only enabled on debug builds or with the `devtools` feature flag.
408+
///
409+
/// ## Platform-specific
410+
///
411+
/// - **macOS**: This is a private API on macOS,
412+
/// so you cannot use this if your application will be published on the App Store.
413+
///
414+
/// # Example
415+
///
416+
/// ```rust,no_run
417+
/// use tauri::Manager;
418+
/// tauri::Builder::default()
419+
/// .setup(|app| {
420+
/// #[cfg(debug_assertions)]
421+
/// app.get_window("main").unwrap().open_devtools();
422+
/// Ok(())
423+
/// });
424+
/// ```
425+
#[cfg(any(debug_assertions, feature = "devtools"))]
426+
#[cfg_attr(doc_cfg, doc(cfg(any(debug_assertions, feature = "devtools"))))]
427+
pub fn open_devtools(&self) {
428+
self.window.dispatcher.open_devtools();
429+
}
430+
406431
// Getters
407432

408433
/// Gets a handle to the window menu.

0 commit comments

Comments
 (0)