|
|
|
|
|
pub mod error; |
|
|
|
|
|
pub mod error_printer; |
|
|
|
|
|
pub mod app_config; |
|
|
|
|
|
pub mod mdict_app; |
|
|
|
|
|
pub mod library_mgr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub mod history; |
|
|
|
|
|
mod history_cmd; |
|
|
|
|
|
pub mod favorites; |
|
|
|
|
|
mod favorites_cmd; |
|
|
|
|
|
mod config_cmd; |
|
|
|
|
|
mod library_cmd; |
|
|
|
|
|
mod conversion_cmd; |
|
|
|
|
|
mod search_cmd; |
|
|
|
|
|
mod system_cmd; |
|
|
mod mdx_profile; |
|
|
mod mdx_db; |
|
|
mod mdx_db_group; |
|
|
mod request_handler; |
|
|
mod mdx_url_parser; |
|
|
mod action_handlers; |
|
|
|
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
mod hotkey_manager; |
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
mod hotkey_cmd; |
|
|
mod utils; |
|
|
|
|
|
use humantime::format_rfc3339; |
|
|
use log::{LevelFilter, error, info, debug}; |
|
|
use tauri::Manager; |
|
|
use tauri::http::{Response, StatusCode}; |
|
|
|
|
|
use crate::utils::log_if_err; |
|
|
|
|
|
|
|
|
use crate::config_cmd::*; |
|
|
use crate::conversion_cmd::*; |
|
|
use crate::favorites_cmd::*; |
|
|
use crate::history_cmd::*; |
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
use crate::hotkey_cmd::*; |
|
|
use crate::library_cmd::*; |
|
|
use crate::search_cmd::*; |
|
|
use crate::system_cmd::*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! generate_all_commands_handler { |
|
|
() => { |
|
|
tauri::generate_handler![ |
|
|
|
|
|
search_search_incremental, |
|
|
search_get_content_url, |
|
|
search_get_entry_count, |
|
|
search_find_index, |
|
|
search_fulltext_search, |
|
|
search_get_result_key_list, |
|
|
search_get_group_indexes, |
|
|
|
|
|
history_add_to_history, |
|
|
history_get_all_history, |
|
|
history_get_history_entry_by_id, |
|
|
history_remove_from_history, |
|
|
history_clear_history, |
|
|
history_get_history_count, |
|
|
history_set_max_history_size, |
|
|
history_get_max_history_size, |
|
|
history_import_history, |
|
|
|
|
|
favorites_add_favorite, |
|
|
favorites_remove_favorite, |
|
|
favorites_toggle_favorite, |
|
|
favorites_is_favorited, |
|
|
favorites_get_sorted_and_filtered_favorites, |
|
|
favorites_get_all_favorites, |
|
|
favorites_clear_all_favorites, |
|
|
favorites_import_favorites, |
|
|
favorites_get_favorites_count, |
|
|
|
|
|
config_get_app_config, |
|
|
config_get_value, |
|
|
config_set_value, |
|
|
config_get_global_settings, |
|
|
config_get_view_settings, |
|
|
config_update_view_settings, |
|
|
config_list_fonts_in_directory, |
|
|
config_save_app_config, |
|
|
config_reload_app_config, |
|
|
config_reload_resources, |
|
|
|
|
|
library_create_dict_group, |
|
|
library_delete_dict_group, |
|
|
library_rename_dict_group, |
|
|
library_refresh_library, |
|
|
library_get_group, |
|
|
library_get_profile, |
|
|
library_list_groups, |
|
|
library_update_profile_disabled_status, |
|
|
library_adjust_profile_order, |
|
|
library_adjust_group_order, |
|
|
library_open_main_database, |
|
|
library_get_current_main_profile_id, |
|
|
library_get_main_db_profile, |
|
|
library_rebuild_index, |
|
|
|
|
|
library_convert_db, |
|
|
library_create_fts_index, |
|
|
library_cancel_conversion, |
|
|
|
|
|
system_set_base_url, |
|
|
|
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
hotkey_register, |
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
hotkey_unregister, |
|
|
] |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)] |
|
|
pub fn run() { |
|
|
unsafe { std::env::set_var("RUST_BACKTRACE", "1"); } |
|
|
|
|
|
tauri::Builder::default() |
|
|
.setup(|app| { |
|
|
let app_handle = app.handle(); |
|
|
let data_dir = app_handle.path().app_data_dir().map_err(|e| e.to_string())?; |
|
|
|
|
|
|
|
|
fern::Dispatch::new() |
|
|
.format(|out, message, record| { |
|
|
out.finish(format_args!( |
|
|
"[{} {} {}] {}", |
|
|
format_rfc3339(std::time::SystemTime::now()), |
|
|
record.level(), |
|
|
record.target(), |
|
|
message |
|
|
)) |
|
|
}) |
|
|
.level(LevelFilter::Debug) |
|
|
.chain(std::io::stdout()) |
|
|
.apply().unwrap(); |
|
|
|
|
|
|
|
|
log_if_err(&mdict_app::init_mdict_app(data_dir.clone(), Vec::new(), &app_handle)); |
|
|
|
|
|
|
|
|
let window = app.get_webview_window("main").expect("Failed to get main window"); |
|
|
let app_handle_clone = app_handle.clone(); |
|
|
window.on_window_event(move |event| { |
|
|
if let tauri::WindowEvent::Focused(focused) = event { |
|
|
if *focused { |
|
|
info!("Window gained focus, notifying frontend"); |
|
|
|
|
|
|
|
|
use tauri::Emitter; |
|
|
if let Err(e) = app_handle_clone.emit("window-focus", ()) { |
|
|
error!("Failed to emit window-focus event: {}", e); |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
#[cfg(all(not(target_os = "android"), not(target_os = "ios"), feature = "global-hotkey"))] |
|
|
{ |
|
|
if let Err(e) = hotkey_manager::init_hotkey_manager(app_handle.clone()) { |
|
|
error!("Failed to initialize hotkey manager: {}", e); |
|
|
} |
|
|
|
|
|
|
|
|
let app_handle_for_hotkey = app_handle.clone(); |
|
|
tauri::async_runtime::spawn(async move { |
|
|
if let Ok(config) = mdict_app::with_config_read(|config| Ok(config.clone())) { |
|
|
let hotkey_letter = config.global_settings.get("hotkey_letter") |
|
|
.and_then(|v| v.as_str()) |
|
|
.unwrap_or(""); |
|
|
let hotkey_modifier = config.global_settings.get("hotkey_modifier") |
|
|
.and_then(|v| v.as_str()) |
|
|
.unwrap_or(""); |
|
|
|
|
|
if !hotkey_letter.is_empty() && !hotkey_modifier.is_empty() { |
|
|
info!("Registering hotkey from config: {} + {}", hotkey_modifier, hotkey_letter); |
|
|
if let Err(e) = hotkey_manager::register_hotkey(app_handle_for_hotkey, hotkey_letter, hotkey_modifier) { |
|
|
error!("Failed to register hotkey on startup: {}", e); |
|
|
} |
|
|
} else { |
|
|
info!("No hotkey configured"); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
let _hotkey_task = std::thread::spawn(move || { |
|
|
loop { |
|
|
hotkey_manager::check_hotkey_events(); |
|
|
std::thread::sleep(std::time::Duration::from_millis(100)); |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
Ok(()) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
.register_asynchronous_uri_scheme_protocol( |
|
|
#[cfg(target_os = "windows")] |
|
|
"http", |
|
|
#[cfg(not(target_os = "windows"))] |
|
|
"mdx", |
|
|
|_app, request, responder| { |
|
|
tauri::async_runtime::spawn(async move { |
|
|
let uri = request.uri().to_string(); |
|
|
|
|
|
|
|
|
|
|
|
let base_url = mdict_app::with_read_access(|app| { |
|
|
Ok(app.get_base_url().to_string()) |
|
|
}).unwrap_or("mdx://mdict.cn/service/".to_string()); |
|
|
|
|
|
if !uri.starts_with(&base_url.replace("http.localhost", "localhost")) { |
|
|
if uri.contains("/service/") { |
|
|
debug!("Request does not match base_url, skipping: {}", uri); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
match request_handler::handle_request(&request, &base_url) { |
|
|
Ok(response) => responder.respond(response), |
|
|
Err(err) => { |
|
|
error!("Error in protocol handler: {}", err); |
|
|
let error_html = format!("<html><body><h1>Protocol Error</h1><p>{}</p></body></html>", err); |
|
|
let mut error_response = Response::new(error_html.into_bytes()); |
|
|
*error_response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; |
|
|
error_response.headers_mut().insert("Content-Type", "text/html; charset=utf-8".parse().unwrap()); |
|
|
responder.respond(error_response); |
|
|
} |
|
|
} |
|
|
}); |
|
|
} |
|
|
) |
|
|
.plugin(tauri_plugin_opener::init()) |
|
|
.plugin(tauri_plugin_dialog::init()) |
|
|
.plugin(tauri_plugin_clipboard_manager::init()) |
|
|
.invoke_handler(generate_all_commands_handler!()) |
|
|
.run(tauri::generate_context!()) |
|
|
.expect("error while running tauri application"); |
|
|
} |