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

This commit is contained in:
Lucas Fernandes Nogueira 2022-02-07 10:04:33 -03:00 committed by GitHub
parent 58030172ed
commit 55aa22de80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 83 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -365,6 +365,9 @@ pub trait Dispatch: Debug + Clone + Send + Sized + 'static {
/// Registers a window event handler. /// Registers a window event handler.
fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid; 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 // GETTERS
/// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.

View File

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

View File

@ -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. //! - **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. //! - **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. //! - **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. //! - **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. //! - **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. //! - **command**: Enables the [`api::process::Command`] APIs.

View File

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

View File

@ -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 // Getters
/// Gets a handle to the window menu. /// Gets a handle to the window menu.

View File

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

View File

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