Files
Genarrative/apps/ai-game-creator-shell/src-tauri/src/context_menu.rs
T
lhk229 55375b1406
Project CI / AI game creator shell Rust crates (push) Successful in 1m18s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m30s
Project CI / Backend tests (push) Successful in 3m58s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 10m12s
Project CI / Native shell tests (push) Successful in 5m49s
Project CI / Frontend tests (push) Successful in 2m18s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 9m50s
Project CI / AI game creator shell web tests (push) Failing after 2m11s
Project CI / Repository checks (push) Successful in 2m55s
清理测试编译warning (#509)
Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/509
Co-authored-by: Linghong <ink29535@proton.me>
Co-committed-by: Linghong <ink29535@proton.me>
2026-10-01 15:29:23 +08:00

52 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#[cfg(not(test))]
mod runtime;
#[cfg(not(test))]
pub(crate) use runtime::init;
/// Release 客户端只取消浏览器的默认上下文菜单。
///
/// 这里刻意只调用 `preventDefault`:`contextmenu` 事件仍会继续沿 DOM
/// 传播,业务组件可以照常接收并打开自己的菜单。捕获阶段用于覆盖没有
/// 自己右键处理的页面空白区域,但不会调用 `stopPropagation`。
const RELEASE_CONTEXT_MENU_INIT_SCRIPT: &str = r#"
document.addEventListener(
'contextmenu',
(event) => {
event.preventDefault();
},
true,
);
"#;
fn context_menu_init_script(is_release: bool) -> Option<&'static str> {
is_release.then_some(RELEASE_CONTEXT_MENU_INIT_SCRIPT)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn release_build_gets_a_context_menu_init_script() {
assert_eq!(
context_menu_init_script(true),
Some(RELEASE_CONTEXT_MENU_INIT_SCRIPT)
);
}
#[test]
fn debug_build_keeps_the_default_context_menu_strategy_untouched() {
assert_eq!(context_menu_init_script(false), None);
}
#[test]
fn init_script_prevents_only_the_default_action() {
let script = RELEASE_CONTEXT_MENU_INIT_SCRIPT;
assert!(script.contains("contextmenu"));
assert!(script.contains("event.preventDefault()"));
assert!(script.contains("true"));
assert!(!script.contains("stopPropagation"));
assert!(!script.contains("stopImmediatePropagation"));
}
}