Skip to content

Commit cbc095e

Browse files
feat: add WebviewWindow/Webview::devtools (#11451)
* feat: add `WebviewWindow/Webview::devtools` closes #10849 * clippy * fix ToTokens * document default behavior * move builder usage --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 2a75c64 commit cbc095e

File tree

11 files changed

+119
-3
lines changed

11 files changed

+119
-3
lines changed

.changes/config-devtools.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri": "patch:feat"
3+
"tauri-utils": "patch:feat"
4+
"@tauri-apps/api": "patch:feat"
5+
---
6+
7+
Add `app > windows > devtools` config option and when creating the webview from JS, to enable or disable devtools for a specific webview.
8+

.changes/webview-builder-devtools.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri": "patch:feat"
3+
"tauri-runtime": "patch:feat"
4+
"tauri-runtime-wry": "patch:feat"
5+
---
6+
7+
Add `WebviewWindowBuilder::devtools` and `WebviewBuilder::devtools` to enable or disable devtools for a specific webview.
8+

crates/tauri-cli/config.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@
498498
"description": "Sets whether the custom protocols should use `https://<scheme>.localhost` instead of the default `http://<scheme>.localhost` on Windows and Android. Defaults to `false`.\n\n ## Note\n\n Using a `https` scheme will NOT allow mixed content when trying to fetch `http` endpoints and therefore will not match the behavior of the `<scheme>://localhost` protocols used on macOS and Linux.\n\n ## Warning\n\n Changing this value between releases will change the IndexedDB, cookies and localstorage location and your app will not be able to access the old data.",
499499
"default": false,
500500
"type": "boolean"
501+
},
502+
"devtools": {
503+
"description": "Enable web inspector which is usually called browser devtools. Enabled by default.\n\n This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.\n\n ## Platform-specific\n\n - macOS: This will call private functions on **macOS**.\n - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.\n - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.",
504+
"type": [
505+
"boolean",
506+
"null"
507+
]
501508
}
502509
},
503510
"additionalProperties": false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4274,7 +4274,7 @@ fn create_webview<T: UserEvent>(
42744274

42754275
#[cfg(any(debug_assertions, feature = "devtools"))]
42764276
{
4277-
webview_builder = webview_builder.with_devtools(true);
4277+
webview_builder = webview_builder.with_devtools(webview_attributes.devtools.unwrap_or(true));
42784278
}
42794279

42804280
#[cfg(target_os = "android")]

crates/tauri-runtime/src/webview.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub struct WebviewAttributes {
211211
pub zoom_hotkeys_enabled: bool,
212212
pub browser_extensions_enabled: bool,
213213
pub use_https_scheme: bool,
214+
pub devtools: Option<bool>,
214215
}
215216

216217
impl From<&WindowConfig> for WebviewAttributes {
@@ -220,7 +221,8 @@ impl From<&WindowConfig> for WebviewAttributes {
220221
.focused(config.focus)
221222
.zoom_hotkeys_enabled(config.zoom_hotkeys_enabled)
222223
.use_https_scheme(config.use_https_scheme)
223-
.browser_extensions_enabled(config.browser_extensions_enabled);
224+
.browser_extensions_enabled(config.browser_extensions_enabled)
225+
.devtools(config.devtools);
224226
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
225227
{
226228
builder = builder.transparent(config.transparent);
@@ -267,6 +269,7 @@ impl WebviewAttributes {
267269
zoom_hotkeys_enabled: false,
268270
browser_extensions_enabled: false,
269271
use_https_scheme: false,
272+
devtools: None,
270273
}
271274
}
272275

@@ -406,6 +409,21 @@ impl WebviewAttributes {
406409
self.use_https_scheme = enabled;
407410
self
408411
}
412+
413+
/// Whether web inspector, which is usually called browser devtools, is enabled or not. Enabled by default.
414+
///
415+
/// This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.
416+
///
417+
/// ## Platform-specific
418+
///
419+
/// - macOS: This will call private functions on **macOS**.
420+
/// - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
421+
/// - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
422+
#[must_use]
423+
pub fn devtools(mut self, enabled: Option<bool>) -> Self {
424+
self.devtools = enabled;
425+
self
426+
}
409427
}
410428

411429
/// IPC handler.

crates/tauri-schema-generator/schemas/config.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@
498498
"description": "Sets whether the custom protocols should use `https://<scheme>.localhost` instead of the default `http://<scheme>.localhost` on Windows and Android. Defaults to `false`.\n\n ## Note\n\n Using a `https` scheme will NOT allow mixed content when trying to fetch `http` endpoints and therefore will not match the behavior of the `<scheme>://localhost` protocols used on macOS and Linux.\n\n ## Warning\n\n Changing this value between releases will change the IndexedDB, cookies and localstorage location and your app will not be able to access the old data.",
499499
"default": false,
500500
"type": "boolean"
501+
},
502+
"devtools": {
503+
"description": "Enable web inspector which is usually called browser devtools. Enabled by default.\n\n This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.\n\n ## Platform-specific\n\n - macOS: This will call private functions on **macOS**.\n - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.\n - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.",
504+
"type": [
505+
"boolean",
506+
"null"
507+
]
501508
}
502509
},
503510
"additionalProperties": false

crates/tauri-utils/src/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,16 @@ pub struct WindowConfig {
15331533
/// Changing this value between releases will change the IndexedDB, cookies and localstorage location and your app will not be able to access the old data.
15341534
#[serde(default, alias = "use-https-scheme")]
15351535
pub use_https_scheme: bool,
1536+
/// Enable web inspector which is usually called browser devtools. Enabled by default.
1537+
///
1538+
/// This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.
1539+
///
1540+
/// ## Platform-specific
1541+
///
1542+
/// - macOS: This will call private functions on **macOS**.
1543+
/// - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
1544+
/// - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
1545+
pub devtools: Option<bool>,
15361546
}
15371547

15381548
impl Default for WindowConfig {
@@ -1583,6 +1593,7 @@ impl Default for WindowConfig {
15831593
zoom_hotkeys_enabled: false,
15841594
browser_extensions_enabled: false,
15851595
use_https_scheme: false,
1596+
devtools: None,
15861597
}
15871598
}
15881599
}
@@ -2556,6 +2567,7 @@ mod build {
25562567
let zoom_hotkeys_enabled = self.zoom_hotkeys_enabled;
25572568
let browser_extensions_enabled = self.browser_extensions_enabled;
25582569
let use_https_scheme = self.use_https_scheme;
2570+
let devtools = opt_lit(self.devtools.as_ref());
25592571

25602572
literal_struct!(
25612573
tokens,
@@ -2604,7 +2616,8 @@ mod build {
26042616
parent,
26052617
zoom_hotkeys_enabled,
26062618
browser_extensions_enabled,
2607-
use_https_scheme
2619+
use_https_scheme,
2620+
devtools
26082621
);
26092622
}
26102623
}

crates/tauri/src/webview/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,21 @@ fn main() {
815815
self.webview_attributes.use_https_scheme = enabled;
816816
self
817817
}
818+
819+
/// Whether web inspector, which is usually called browser devtools, is enabled or not. Enabled by default.
820+
///
821+
/// This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.
822+
///
823+
/// ## Platform-specific
824+
///
825+
/// - macOS: This will call private functions on **macOS**
826+
/// - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
827+
/// - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
828+
#[must_use]
829+
pub fn devtools(mut self, enabled: bool) -> Self {
830+
self.webview_attributes.devtools.replace(enabled);
831+
self
832+
}
818833
}
819834

820835
/// Webview.

crates/tauri/src/webview/webview_window.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,21 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
920920
self.webview_builder = self.webview_builder.use_https_scheme(enabled);
921921
self
922922
}
923+
924+
/// Whether web inspector, which is usually called browser devtools, is enabled or not. Enabled by default.
925+
///
926+
/// This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.
927+
///
928+
/// ## Platform-specific
929+
///
930+
/// - macOS: This will call private functions on **macOS**.
931+
/// - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
932+
/// - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
933+
#[must_use]
934+
pub fn devtools(mut self, enabled: bool) -> Self {
935+
self.webview_builder = self.webview_builder.devtools(enabled);
936+
self
937+
}
923938
}
924939

925940
/// A type that wraps a [`Window`] together with a [`Webview`].

packages/api/src/webview.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,20 @@ interface WebviewOptions {
749749
* @since 2.1.0
750750
*/
751751
useHttpsScheme?: boolean
752+
/**
753+
* Whether web inspector, which is usually called browser devtools, is enabled or not. Enabled by default.
754+
*
755+
* This API works in **debug** builds, but requires `devtools` feature flag to enable it in **release** builds.
756+
*
757+
* #### Platform-specific
758+
*
759+
* - macOS: This will call private functions on **macOS**.
760+
* - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
761+
* - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
762+
*
763+
* @since 2.1.0
764+
*/
765+
devtools?: boolean
752766
}
753767

754768
export { Webview, getCurrentWebview, getAllWebviews }

packages/api/src/window.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,17 @@ interface WindowOptions {
22912291
* @since 2.0.0
22922292
*/
22932293
visibleOnAllWorkspaces?: boolean
2294+
/**
2295+
* Window effects.
2296+
*
2297+
* Requires the window to be transparent.
2298+
*
2299+
* #### Platform-specific:
2300+
*
2301+
* - **Windows**: If using decorations or shadows, you may want to try this workaround <https://github.com/tauri-apps/tao/issues/72#issuecomment-975607891>
2302+
* - **Linux**: Unsupported
2303+
*/
2304+
windowEffects?: Effects
22942305
}
22952306

22962307
function mapMonitor(m: Monitor | null): Monitor | null {

0 commit comments

Comments
 (0)