Skip to content

Commit

Permalink
feat(shortcuts): add platform specific global shortcuts to toggle dev…
Browse files Browse the repository at this point in the history
…tools
  • Loading branch information
errmayank committed Aug 19, 2024
1 parent bd65983 commit 3715feb
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-eyes-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zaku": patch
---

Add platform specific global shortcuts to toggle devtools
78 changes: 71 additions & 7 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = { version = "=1.0.203", features = ["derive"] }
serde_json = { version = "=1.0.117" }
tauri = { version = "=2.0.0-rc.3", features = [] }
tauri-plugin-dialog = { version = "=2.0.0-rc.1" }
tauri-plugin-global-shortcut = { version = "=2.0.0-rc.1" }
tauri-plugin-http = { version = "=2.0.0-rc.0" }
tauri-plugin-store = { version = "=2.0.0-rc.1" }
toml = { version = "=0.8.17" }
5 changes: 4 additions & 1 deletion src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"store:allow-entries",
"core:event:default",
"store:default",
"dialog:default"
"dialog:default",
"global-shortcut:allow-is-registered",
"global-shortcut:allow-register",
"global-shortcut:allow-unregister"
]
}
1 change: 1 addition & 0 deletions src-tauri/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod commands;
pub mod space;
pub mod utils;
38 changes: 38 additions & 0 deletions src-tauri/src/core/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use tauri::{AppHandle, Manager};
use tauri_plugin_global_shortcut::{Code, Modifiers};

pub enum ShortcutCombination {
CommandOptionI,
ControlShiftI,
}

pub fn shortcut_combination(
shortcut: &tauri_plugin_global_shortcut::Shortcut,
) -> Option<ShortcutCombination> {
let modifiers = shortcut.mods;
let code = shortcut.key;

if modifiers.contains(Modifiers::SUPER)
&& modifiers.contains(Modifiers::ALT)
&& code == Code::KeyI
{
return Some(ShortcutCombination::CommandOptionI);
} else if modifiers.contains(Modifiers::CONTROL)
&& modifiers.contains(Modifiers::SHIFT)
&& code == Code::KeyI
{
return Some(ShortcutCombination::ControlShiftI);
} else {
return None;
}
}

pub fn toggle_devtools(app_handle: &AppHandle) {
let webview_window = app_handle.get_webview_window("main").unwrap();

if webview_window.is_devtools_open() {
webview_window.close_devtools();
} else {
webview_window.open_devtools();
}
}
57 changes: 56 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub mod types;

use constants::ZakuStoreKey;

use core::{commands, space};
use core::utils::ShortcutCombination;
use core::{commands, space, utils};
use types::AppState;

fn main() {
Expand Down Expand Up @@ -55,6 +56,60 @@ fn main() {

return Ok(());
})
.setup(|app| {
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
{
use tauri_plugin_global_shortcut::ShortcutState;

#[cfg(target_os = "macos")]
let shortcuts = ["Command+Option+I"];

#[cfg(target_os = "windows")]
let shortcuts = ["Control+Shift+I"];

#[cfg(target_os = "linux")]
let shortcuts = ["Control+Shift+I", "Command+Option+I"];

app.handle().plugin(
tauri_plugin_global_shortcut::Builder::new()
.with_shortcuts(shortcuts)
.unwrap()
.with_handler(|handler_app, shortcut, event| {
if event.state == ShortcutState::Pressed {
match utils::shortcut_combination(shortcut) {
#[cfg(target_os = "macos")]
Some(ShortcutCombination::CommandOptionI) => {
utils::toggle_devtools(handler_app.app_handle());
}
#[cfg(target_os = "macos")]
Some(ShortcutCombination::ControlShiftI) => {}

#[cfg(target_os = "windows")]
Some(ShortcutCombination::ControlShiftI) => {
utils::toggle_devtools(handler_app.app_handle());
}
#[cfg(target_os = "windows")]
Some(ShortcutCombination::CommandOptionI) => {}

#[cfg(target_os = "linux")]
Some(ShortcutCombination::ControlShiftI) => {
utils::toggle_devtools(handler_app.app_handle());
}
#[cfg(target_os = "linux")]
Some(ShortcutCombination::CommandOptionI) => {
utils::toggle_devtools(handler_app.app_handle());
}

None => {}
}
}
})
.build(),
)?;
}

return Ok(());
})
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_dialog::init())
.invoke_handler(tauri::generate_handler![
Expand Down

0 comments on commit 3715feb

Please sign in to comment.