适配iOS模拟器运行并补齐Tauri移动端入口

This commit is contained in:
JSR
2026-03-18 22:20:47 +08:00
parent 139185b1c0
commit 0df8fd4b0d
5 changed files with 170 additions and 104 deletions
+4
View File
@@ -6,6 +6,10 @@ authors = ["SecScore Team"]
edition = "2021" edition = "2021"
rust-version = "1.70" rust-version = "1.70"
[lib]
name = "secscore"
crate-type = ["staticlib", "cdylib", "lib"]
[build-dependencies] [build-dependencies]
tauri-build = { version = "2", features = [] } tauri-build = { version = "2", features = [] }
+1 -1
View File
@@ -83,6 +83,6 @@ pub async fn register_url_protocol(
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{ {
let _ = app; 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"))
} }
} }
+51 -1
View File
@@ -1,6 +1,8 @@
use parking_lot::RwLock; use parking_lot::RwLock;
use std::sync::Arc; use std::sync::Arc;
use tauri::{AppHandle, Manager}; use tauri::AppHandle;
#[cfg(desktop)]
use tauri::Manager;
use crate::state::AppState; use crate::state::AppState;
@@ -9,6 +11,12 @@ pub async fn window_minimize(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<(), String> { ) -> Result<(), String> {
#[cfg(not(desktop))]
{
let _ = app;
}
#[cfg(desktop)]
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window.minimize().map_err(|e| e.to_string())?; window.minimize().map_err(|e| e.to_string())?;
} }
@@ -20,6 +28,14 @@ pub async fn window_maximize(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<bool, String> { ) -> Result<bool, String> {
#[cfg(not(desktop))]
{
let _ = app;
return Ok(false);
}
#[cfg(desktop)]
{
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
let is_maximized = window.is_maximized().map_err(|e| e.to_string())?; let is_maximized = window.is_maximized().map_err(|e| e.to_string())?;
@@ -34,12 +50,19 @@ pub async fn window_maximize(
Err("Window not found".to_string()) Err("Window not found".to_string())
} }
} }
}
#[tauri::command] #[tauri::command]
pub async fn window_close( pub async fn window_close(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<(), String> { ) -> Result<(), String> {
#[cfg(not(desktop))]
{
let _ = app;
}
#[cfg(desktop)]
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window.hide().map_err(|e| e.to_string())?; window.hide().map_err(|e| e.to_string())?;
} }
@@ -51,18 +74,33 @@ pub async fn window_is_maximized(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<bool, String> { ) -> Result<bool, String> {
#[cfg(not(desktop))]
{
let _ = app;
return Ok(false);
}
#[cfg(desktop)]
{
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window.is_maximized().map_err(|e| e.to_string()) window.is_maximized().map_err(|e| e.to_string())
} else { } else {
Ok(false) Ok(false)
} }
} }
}
#[tauri::command] #[tauri::command]
pub async fn toggle_devtools( pub async fn toggle_devtools(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<(), String> { ) -> Result<(), String> {
#[cfg(not(desktop))]
{
let _ = app;
}
#[cfg(desktop)]
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
if window.is_devtools_open() { if window.is_devtools_open() {
window.close_devtools(); window.close_devtools();
@@ -80,6 +118,12 @@ pub async fn window_resize(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<(), String> { ) -> Result<(), String> {
#[cfg(not(desktop))]
{
let _ = (width, height, app);
}
#[cfg(desktop)]
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window window
.set_size(tauri::Size::Physical(tauri::PhysicalSize { width, height })) .set_size(tauri::Size::Physical(tauri::PhysicalSize { width, height }))
@@ -94,6 +138,12 @@ pub async fn window_set_resizable(
app: AppHandle, app: AppHandle,
_state: tauri::State<'_, Arc<RwLock<AppState>>>, _state: tauri::State<'_, Arc<RwLock<AppState>>>,
) -> Result<(), String> { ) -> Result<(), String> {
#[cfg(not(desktop))]
{
let _ = (resizable, app);
}
#[cfg(desktop)]
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window.set_resizable(resizable).map_err(|e| e.to_string())?; window.set_resizable(resizable).map_err(|e| e.to_string())?;
} }
+106 -5
View File
@@ -5,22 +5,111 @@ pub mod services;
pub mod state; pub mod state;
pub mod utils; pub mod utils;
use parking_lot::RwLock;
use std::sync::Arc;
use crate::db::connection::create_sqlite_connection; use crate::db::connection::create_sqlite_connection;
use crate::db::connection::DatabaseType; use crate::db::connection::DatabaseType;
use crate::db::migration::run_migration; use crate::db::migration::run_migration;
use crate::{commands::*, state::AppState};
use tauri::{App, Manager};
#[cfg(desktop)]
use tauri::{ use tauri::{
image::Image, image::Image,
menu::{Menu, MenuItem}, menu::{Menu, MenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
App, Manager, WindowEvent, WindowEvent,
}; };
pub fn setup_app(app: &mut App) -> Result<(), Box<dyn std::error::Error>> { #[cfg_attr(mobile, tauri::mobile_entry_point)]
#[cfg(desktop)] pub fn run() {
{ tauri::Builder::default()
let _ = app; .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<dyn std::error::Error>> {
setup_database(app)?; setup_database(app)?;
setup_tray(app)?; setup_tray(app)?;
@@ -81,6 +170,7 @@ fn setup_database(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
#[cfg(desktop)]
fn setup_tray(app: &mut App) -> Result<(), Box<dyn std::error::Error>> { fn setup_tray(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
let show_item = MenuItem::with_id(app, "show", "显示窗口", true, None::<&str>)?; let show_item = MenuItem::with_id(app, "show", "显示窗口", true, None::<&str>)?;
let hide_item = MenuItem::with_id(app, "hide", "隐藏窗口", 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<dyn std::error::Error>> {
Ok(()) Ok(())
} }
#[cfg(not(desktop))]
fn setup_tray(_app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[cfg(desktop)]
fn setup_window_events(app: &mut App) -> Result<(), Box<dyn std::error::Error>> { fn setup_window_events(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
let window_clone = window.clone(); let window_clone = window.clone();
@@ -141,3 +237,8 @@ fn setup_window_events(app: &mut App) -> Result<(), Box<dyn std::error::Error>>
Ok(()) Ok(())
} }
#[cfg(not(desktop))]
fn setup_window_events(_app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
+1 -90
View File
@@ -1,94 +1,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![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() { fn main() {
tauri::Builder::default() secscore::run();
.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");
} }