Skip to content

Commit

Permalink
fix(core): toggle devtools via global shortcut (#7937)
Browse files Browse the repository at this point in the history
* fix(core): toggle devtools via global shortcut

* Update .changes/fix-toggle-devtools.md
  • Loading branch information
lucasfernog authored Oct 2, 2023
1 parent a3277a2 commit 3671edb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-toggle-devtools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:bug
---

Fix devtools not toggling on `ctrl+shift+i` or `cmd+alt+i` shortcuts.
8 changes: 5 additions & 3 deletions core/tauri/scripts/toggle-devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// SPDX-License-Identifier: MIT

(function () {
const osName = __TEMPLATE_os_name__

function toggleDevtoolsHotkey() {
const isHotkey = navigator.appVersion.includes("Mac")
? (event) => event.metaKey && event.altKey && event.key === "I"
: (event) => event.ctrlKey && event.shiftKey && event.key === "I";
const isHotkey = osName === 'macos' ?
(event) => event.metaKey && event.altKey && event.code === "KeyI" :
(event) => event.ctrlKey && event.shiftKey && event.code === "KeyI";

document.addEventListener("keydown", (event) => {
if (isHotkey(event)) {
Expand Down
16 changes: 13 additions & 3 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,12 @@ impl<R: Runtime> WindowManager<R> {
protocol_scheme: &'a str,
}

#[derive(Template)]
#[default_template("../scripts/toggle-devtools.js")]
struct ToggleDevtoolsScript<'a> {
os_name: &'a str,
}

let bundle_script = if with_global_tauri {
include_str!("../scripts/bundle.global.js")
} else {
Expand All @@ -815,9 +821,13 @@ impl<R: Runtime> WindowManager<R> {
};

#[cfg(any(debug_assertions, feature = "devtools"))]
let hotkeys = include_str!("../scripts/toggle-devtools.js");
let hotkeys = ToggleDevtoolsScript {
os_name: std::env::consts::OS,
}
.render_default(&Default::default())?
.into_string();
#[cfg(not(any(debug_assertions, feature = "devtools")))]
let hotkeys = "";
let hotkeys = String::default();

InitJavascript {
pattern_script,
Expand Down Expand Up @@ -846,7 +856,7 @@ impl<R: Runtime> WindowManager<R> {
event_initialization_script: &self.event_initialization_script(),
plugin_initialization_script,
freeze_prototype,
hotkeys,
hotkeys: &hotkeys,
}
.render_default(&Default::default())
.map(|s| s.into_string())
Expand Down
21 changes: 12 additions & 9 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3671edb

Please sign in to comment.