Files
Genarrative/apps/desktop-shell/src-tauri/src/app.rs
T
kdletters 41bccdd9fc 收紧桌面壳主窗口启动门禁
按 label=main 解析 Tauri 主窗口配置并补写宿主上下文

缺少主窗口配置时阻断启动而不是静默跳过 WebView 创建

补充桌面壳配置门禁和 Rust 单测覆盖

同步宿主壳方案文档与共享决策记录
2026-06-19 05:41:13 +08:00

147 lines
6.0 KiB
Rust

use crate::host_bridge::{DesktopShareState, HostBridgeReplayState};
use crate::shell::deep_link::{
register_desktop_deep_link_events, register_desktop_deep_link_schemes,
};
use crate::shell::menu::register_desktop_app_menu;
use crate::shell::tray::{
register_desktop_tray, register_desktop_window_close_events,
resolve_desktop_single_instance_action, show_main_window, DesktopSingleInstanceAction,
};
use crate::shell::webview::{
desktop_window_config_with_runtime_platform, emit_current_desktop_lifecycle_event,
open_desktop_external_navigation, register_desktop_file_drop_events,
register_desktop_lifecycle_events, register_desktop_navigation_events,
register_desktop_network_events, replay_desktop_webview_state,
should_allow_desktop_webview_download, should_allow_desktop_webview_navigation,
should_replay_desktop_webview_state_on_page_load,
};
use crate::shell::window_state::desktop_window_state_plugin;
use tauri::webview::NewWindowResponse;
const DESKTOP_MAIN_WINDOW_LABEL: &str = "main";
fn desktop_main_window_config_from_windows(
windows: &[tauri::utils::config::WindowConfig],
) -> tauri::Result<tauri::utils::config::WindowConfig> {
windows
.iter()
.find(|window| window.label == DESKTOP_MAIN_WINDOW_LABEL)
.cloned()
.map(desktop_window_config_with_runtime_platform)
.ok_or(tauri::Error::WindowNotFound)
}
fn desktop_main_window_config(
app: &tauri::App,
) -> tauri::Result<tauri::utils::config::WindowConfig> {
desktop_main_window_config_from_windows(&app.config().app.windows)
}
pub(crate) fn run() {
tauri::Builder::default()
.manage(DesktopShareState::default())
.manage(HostBridgeReplayState::default())
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
match resolve_desktop_single_instance_action() {
DesktopSingleInstanceAction::ShowMainWindow => {
let _ = show_main_window(app);
}
}
}))
.plugin(desktop_window_state_plugin())
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_opener::init())
.setup(|app| {
register_desktop_app_menu(app)?;
let tray_registered = match register_desktop_tray(app) {
Ok(()) => true,
Err(error) => {
eprintln!("desktop tray registration failed: {error}");
false
}
};
let config = desktop_main_window_config(app)?;
let app_handle = app.handle().clone();
let new_window_app_handle = app.handle().clone();
let window = tauri::WebviewWindowBuilder::from_config(app.handle(), &config)?
.on_navigation(move |url| {
if should_allow_desktop_webview_navigation(url) {
true
} else {
open_desktop_external_navigation(&app_handle, url);
false
}
})
.on_new_window(move |url, _features| {
open_desktop_external_navigation(&new_window_app_handle, &url);
NewWindowResponse::Deny
})
.on_page_load(|window, payload| {
if should_replay_desktop_webview_state_on_page_load(payload.event()) {
replay_desktop_webview_state(&window);
}
})
.on_download(|_webview, event| should_allow_desktop_webview_download(&event))
.build()?;
register_desktop_window_close_events(&window, tray_registered);
register_desktop_lifecycle_events(&window);
let _ = emit_current_desktop_lifecycle_event(&window);
let _ = register_desktop_network_events(&window);
let _ = register_desktop_navigation_events(&window);
register_desktop_file_drop_events(&window);
register_desktop_deep_link_events(app)?;
register_desktop_deep_link_schemes(app);
Ok(())
})
.invoke_handler(tauri::generate_handler![
crate::host_bridge::host_bridge_request
])
.run(tauri::generate_context!())
.expect("failed to run Genarrative desktop shell");
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn desktop_main_window_config_uses_labeled_main_window() {
let mut secondary_window = tauri::utils::config::WindowConfig::default();
secondary_window.label = "secondary".to_string();
secondary_window.url = tauri::WebviewUrl::App(PathBuf::from("secondary.html"));
let mut main_window = tauri::utils::config::WindowConfig::default();
main_window.label = "main".to_string();
main_window.url = tauri::WebviewUrl::App(PathBuf::from("index.html"));
let config = desktop_main_window_config_from_windows(&[secondary_window, main_window])
.expect("main window config");
assert_eq!(config.label, "main");
match config.url {
tauri::WebviewUrl::App(path) => {
let path = path.to_string_lossy();
assert!(path.starts_with("index.html?"));
assert!(path.contains("clientRuntime=native_app"));
assert!(path.contains("hostShell=tauri_desktop"));
}
other => panic!("unexpected main window url {other:?}"),
}
}
#[test]
fn desktop_main_window_config_rejects_missing_main_window() {
let mut secondary_window = tauri::utils::config::WindowConfig::default();
secondary_window.label = "secondary".to_string();
let error = desktop_main_window_config_from_windows(&[secondary_window])
.expect_err("missing main window should fail startup");
assert!(matches!(error, tauri::Error::WindowNotFound));
}
}