feat: enable devtools in release mode, disable right click contextmenu in release mode (#52)

This commit is contained in:
Huakun Shen 2025-01-09 14:43:22 -05:00 committed by GitHub
parent 7c68a8d70f
commit e096e10bc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 31 deletions

View File

@ -1,6 +1,17 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"
import "../app.css" import "../app.css"
import { ModeWatcher, ThemeWrapper } from "@kksh/svelte5" import { ModeWatcher, ThemeWrapper } from "@kksh/svelte5"
import { dev } from "$app/environment"
onMount(() => {
if (!dev) {
document.addEventListener("contextmenu", function (event) {
event.preventDefault()
console.warn("contextmenu disabled in release mode", event)
})
}
})
let { children } = $props() let { children } = $props()
</script> </script>

View File

@ -2,54 +2,39 @@ use tauri::{Manager, Runtime};
#[tauri::command] #[tauri::command]
pub fn open_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> { pub fn open_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> {
#[cfg(debug_assertions)] let win = window.get_webview_window(window.label());
{ if let Some(win) = win {
let win = window.get_webview_window(window.label()); win.open_devtools();
if let Some(win) = win {
win.open_devtools();
}
} }
Ok(()) Ok(())
} }
#[tauri::command] #[tauri::command]
pub fn close_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> { pub fn close_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> {
#[cfg(debug_assertions)] let win = window.get_webview_window(window.label());
{ if let Some(win) = win {
let win = window.get_webview_window(window.label()); win.close_devtools();
if let Some(win) = win {
win.close_devtools();
}
} }
Ok(()) Ok(())
} }
#[tauri::command] #[tauri::command]
pub fn is_devtools_open<R: Runtime>(window: tauri::Window<R>) -> Result<bool, String> { pub fn is_devtools_open<R: Runtime>(window: tauri::Window<R>) -> Result<bool, String> {
#[cfg(debug_assertions)] let win = window.get_webview_window(window.label());
{ if let Some(win) = win {
let win = window.get_webview_window(window.label()); Ok(win.is_devtools_open())
if let Some(win) = win { } else {
Ok(win.is_devtools_open()) Err("Window not found".to_string())
} else {
Err("Window not found".to_string())
}
} }
#[cfg(not(debug_assertions))]
Err("Devtools is not available in release mode".to_string())
} }
#[tauri::command] #[tauri::command]
pub fn toggle_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> { pub fn toggle_devtools<R: Runtime>(window: tauri::Window<R>) -> Result<(), String> {
#[cfg(debug_assertions)] let is_open = is_devtools_open(window.clone()).expect("failed to check if devtools is open");
{ if is_open {
let is_open = close_devtools(window).expect("failed to close devtools");
is_devtools_open(window.clone()).expect("failed to check if devtools is open"); } else {
if is_open { open_devtools(window).expect("failed to open devtools");
close_devtools(window).expect("failed to close devtools");
} else {
open_devtools(window).expect("failed to open devtools");
}
} }
Ok(()) Ok(())
} }