From 0df8fd4b0dadf674c16b880bfb24a468d6ed34b6 Mon Sep 17 00:00:00 2001 From: JSR Date: Wed, 18 Mar 2026 22:20:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DiOS=E6=A8=A1=E6=8B=9F?= =?UTF-8?q?=E5=99=A8=E8=BF=90=E8=A1=8C=E5=B9=B6=E8=A1=A5=E9=BD=90Tauri?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AF=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/Cargo.toml | 4 ++ src-tauri/src/commands/app.rs | 2 +- src-tauri/src/commands/window.rs | 64 +++++++++++++++-- src-tauri/src/lib.rs | 113 +++++++++++++++++++++++++++++-- src-tauri/src/main.rs | 91 +------------------------ 5 files changed, 170 insertions(+), 104 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e7af112..9efbfaf 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -6,6 +6,10 @@ authors = ["SecScore Team"] edition = "2021" rust-version = "1.70" +[lib] +name = "secscore" +crate-type = ["staticlib", "cdylib", "lib"] + [build-dependencies] tauri-build = { version = "2", features = [] } diff --git a/src-tauri/src/commands/app.rs b/src-tauri/src/commands/app.rs index d052149..1c3a41f 100644 --- a/src-tauri/src/commands/app.rs +++ b/src-tauri/src/commands/app.rs @@ -83,6 +83,6 @@ pub async fn register_url_protocol( #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] { let _ = app; - Ok(IpcResponse::failure("URL protocol registration is not supported on this platform")) + Ok(IpcResponse::error("URL protocol registration is not supported on this platform")) } } diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index eb7e6ea..843e6fa 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -1,6 +1,8 @@ use parking_lot::RwLock; use std::sync::Arc; -use tauri::{AppHandle, Manager}; +use tauri::AppHandle; +#[cfg(desktop)] +use tauri::Manager; use crate::state::AppState; @@ -9,6 +11,12 @@ pub async fn window_minimize( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result<(), String> { + #[cfg(not(desktop))] + { + let _ = app; + } + + #[cfg(desktop)] if let Some(window) = app.get_webview_window("main") { window.minimize().map_err(|e| e.to_string())?; } @@ -20,7 +28,15 @@ pub async fn window_maximize( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result { - if let Some(window) = app.get_webview_window("main") { + #[cfg(not(desktop))] + { + let _ = app; + return Ok(false); + } + + #[cfg(desktop)] + { + if let Some(window) = app.get_webview_window("main") { let is_maximized = window.is_maximized().map_err(|e| e.to_string())?; if is_maximized { @@ -30,8 +46,9 @@ pub async fn window_maximize( window.maximize().map_err(|e| e.to_string())?; Ok(true) } - } else { - Err("Window not found".to_string()) + } else { + Err("Window not found".to_string()) + } } } @@ -40,6 +57,12 @@ pub async fn window_close( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result<(), String> { + #[cfg(not(desktop))] + { + let _ = app; + } + + #[cfg(desktop)] if let Some(window) = app.get_webview_window("main") { window.hide().map_err(|e| e.to_string())?; } @@ -51,10 +74,19 @@ pub async fn window_is_maximized( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result { - if let Some(window) = app.get_webview_window("main") { + #[cfg(not(desktop))] + { + let _ = app; + return Ok(false); + } + + #[cfg(desktop)] + { + if let Some(window) = app.get_webview_window("main") { window.is_maximized().map_err(|e| e.to_string()) - } else { - Ok(false) + } else { + Ok(false) + } } } @@ -63,6 +95,12 @@ pub async fn toggle_devtools( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result<(), String> { + #[cfg(not(desktop))] + { + let _ = app; + } + + #[cfg(desktop)] if let Some(window) = app.get_webview_window("main") { if window.is_devtools_open() { window.close_devtools(); @@ -80,6 +118,12 @@ pub async fn window_resize( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result<(), String> { + #[cfg(not(desktop))] + { + let _ = (width, height, app); + } + + #[cfg(desktop)] if let Some(window) = app.get_webview_window("main") { window .set_size(tauri::Size::Physical(tauri::PhysicalSize { width, height })) @@ -94,6 +138,12 @@ pub async fn window_set_resizable( app: AppHandle, _state: tauri::State<'_, Arc>>, ) -> Result<(), String> { + #[cfg(not(desktop))] + { + let _ = (resizable, app); + } + + #[cfg(desktop)] if let Some(window) = app.get_webview_window("main") { window.set_resizable(resizable).map_err(|e| e.to_string())?; } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 18d76fc..0040eba 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -5,22 +5,111 @@ pub mod services; pub mod state; pub mod utils; +use parking_lot::RwLock; +use std::sync::Arc; use crate::db::connection::create_sqlite_connection; use crate::db::connection::DatabaseType; use crate::db::migration::run_migration; +use crate::{commands::*, state::AppState}; +use tauri::{App, Manager}; +#[cfg(desktop)] use tauri::{ image::Image, menu::{Menu, MenuItem}, tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, - App, Manager, WindowEvent, + WindowEvent, }; -pub fn setup_app(app: &mut App) -> Result<(), Box> { - #[cfg(desktop)] - { - let _ = app; - } +#[cfg_attr(mobile, tauri::mobile_entry_point)] +pub fn run() { + tauri::Builder::default() + .plugin(tauri_plugin_shell::init()) + .setup(|app| { + let state = AppState::new(app.handle().clone()); + app.manage(Arc::new(RwLock::new(state))); + setup_app(app)?; + Ok(()) + }) + .invoke_handler(tauri::generate_handler![ + student_query, + student_create, + student_update, + student_delete, + student_import_from_xlsx, + tags_get_all, + tags_get_by_student, + tags_create, + tags_delete, + tags_update_student_tags, + reason_query, + reason_create, + reason_update, + reason_delete, + event_query, + event_create, + event_delete, + event_query_by_student, + leaderboard_query, + db_settlement_query, + db_settlement_create, + db_settlement_leaderboard, + settings_get_all, + settings_get, + settings_set, + auth_get_status, + auth_login, + auth_logout, + auth_set_passwords, + auth_generate_recovery, + auth_reset_by_recovery, + auth_clear_all, + theme_list, + theme_current, + theme_set, + theme_save, + theme_delete, + auto_score_get_rules, + auto_score_add_rule, + auto_score_update_rule, + auto_score_delete_rule, + auto_score_toggle_rule, + auto_score_get_status, + auto_score_sort_rules, + log_query, + log_clear, + log_set_level, + log_write, + data_export_json, + data_import_json, + window_minimize, + window_maximize, + window_close, + window_is_maximized, + toggle_devtools, + window_resize, + window_set_resizable, + db_test_connection, + db_switch_connection, + db_get_status, + db_sync, + fs_get_config_structure, + fs_read_json, + fs_write_json, + fs_read_text, + fs_write_text, + fs_delete_file, + fs_list_files, + fs_file_exists, + http_server_start, + http_server_stop, + http_server_status, + register_url_protocol, + ]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} +pub fn setup_app(app: &mut App) -> Result<(), Box> { setup_database(app)?; setup_tray(app)?; @@ -81,6 +170,7 @@ fn setup_database(app: &mut App) -> Result<(), Box> { Ok(()) } +#[cfg(desktop)] fn setup_tray(app: &mut App) -> Result<(), Box> { let show_item = MenuItem::with_id(app, "show", "显示窗口", true, None::<&str>)?; let hide_item = MenuItem::with_id(app, "hide", "隐藏窗口", true, None::<&str>)?; @@ -128,6 +218,12 @@ fn setup_tray(app: &mut App) -> Result<(), Box> { Ok(()) } +#[cfg(not(desktop))] +fn setup_tray(_app: &mut App) -> Result<(), Box> { + Ok(()) +} + +#[cfg(desktop)] fn setup_window_events(app: &mut App) -> Result<(), Box> { if let Some(window) = app.get_webview_window("main") { let window_clone = window.clone(); @@ -141,3 +237,8 @@ fn setup_window_events(app: &mut App) -> Result<(), Box> Ok(()) } + +#[cfg(not(desktop))] +fn setup_window_events(_app: &mut App) -> Result<(), Box> { + Ok(()) +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index c1c5b30..b978201 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,94 +1,5 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use parking_lot::RwLock; -use secscore::{commands::*, setup_app, state::AppState}; -use std::sync::Arc; -use tauri::Manager; - fn main() { - tauri::Builder::default() - .plugin(tauri_plugin_shell::init()) - .setup(|app| { - let state = AppState::new(app.handle().clone()); - app.manage(Arc::new(RwLock::new(state))); - setup_app(app)?; - Ok(()) - }) - .invoke_handler(tauri::generate_handler![ - student_query, - student_create, - student_update, - student_delete, - student_import_from_xlsx, - tags_get_all, - tags_get_by_student, - tags_create, - tags_delete, - tags_update_student_tags, - reason_query, - reason_create, - reason_update, - reason_delete, - event_query, - event_create, - event_delete, - event_query_by_student, - leaderboard_query, - db_settlement_query, - db_settlement_create, - db_settlement_leaderboard, - settings_get_all, - settings_get, - settings_set, - auth_get_status, - auth_login, - auth_logout, - auth_set_passwords, - auth_generate_recovery, - auth_reset_by_recovery, - auth_clear_all, - theme_list, - theme_current, - theme_set, - theme_save, - theme_delete, - auto_score_get_rules, - auto_score_add_rule, - auto_score_update_rule, - auto_score_delete_rule, - auto_score_toggle_rule, - auto_score_get_status, - auto_score_sort_rules, - log_query, - log_clear, - log_set_level, - log_write, - data_export_json, - data_import_json, - window_minimize, - window_maximize, - window_close, - window_is_maximized, - toggle_devtools, - window_resize, - window_set_resizable, - db_test_connection, - db_switch_connection, - db_get_status, - db_sync, - fs_get_config_structure, - fs_read_json, - fs_write_json, - fs_read_text, - fs_write_text, - fs_delete_file, - fs_list_files, - fs_file_exists, - http_server_start, - http_server_stop, - http_server_status, - register_url_protocol, - ]) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + secscore::run(); }