From 4f622425171a95688ac7c43fddaeae1dd897abcf Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Mon, 8 Dec 2025 15:39:38 +0800 Subject: [PATCH] minor organization, rearrangement & refinement of code --- src-tauri/src/app.rs | 76 +++-------------------------- src-tauri/src/services/auth.rs | 29 +++++------ src-tauri/src/services/mod.rs | 2 +- src-tauri/src/services/overlay.rs | 19 -------- src-tauri/src/services/scene.rs | 81 +++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 104 deletions(-) delete mode 100644 src-tauri/src/services/overlay.rs create mode 100644 src-tauri/src/services/scene.rs diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs index 9d97e7c..6bfdbfa 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/app.rs @@ -1,13 +1,8 @@ -use tauri::Manager; -use tauri_plugin_positioner::WindowExt; -use tracing::{error, info}; +use tracing::info; use crate::{ - get_app_handle, services::{ - auth::get_tokens, - overlay::{overlay_fullscreen, SCENE_WINDOW_LABEL}, - preferences::create_preferences_window, + auth::get_tokens, preferences::create_preferences_window, scene::create_scene_window, }, state::init_app_data, }; @@ -18,7 +13,7 @@ pub async fn start_fdoll() { async fn construct_app() { init_app_data().await; - create_scene(); + create_scene_window(); create_preferences_window(); } @@ -30,71 +25,16 @@ pub async fn bootstrap() { } None => { info!("No active session, user needs to authenticate"); - crate::services::auth::init_auth_code_retrieval(|| { + match crate::services::auth::init_auth_code_retrieval(|| { info!("Authentication successful, creating scene..."); tauri::async_runtime::spawn(async { info!("Creating scene after auth success..."); construct_app().await; }); - }); + }) { + Ok(it) => it, + Err(err) => todo!("Handle authentication error: {}", err), + }; } } } - -pub fn create_scene() { - info!("Starting scene creation..."); - let webview_window = match tauri::WebviewWindowBuilder::new( - get_app_handle(), - SCENE_WINDOW_LABEL, - tauri::WebviewUrl::App("/scene".into()), - ) - .title("Friendolls Scene") - .inner_size(600.0, 500.0) - .resizable(false) - .decorations(false) - .transparent(true) - .shadow(false) - .visible(true) - .skip_taskbar(true) - .always_on_top(true) - .visible_on_all_workspaces(true) - .build() - { - Ok(window) => { - info!("Scene window builder succeeded"); - window - } - Err(e) => { - error!("Failed to build scene window: {}", e); - return; - } - }; - - if let Err(e) = webview_window.move_window(tauri_plugin_positioner::Position::Center) { - error!("Failed to move scene window to center: {}", e); - return; - } - - let window = match get_app_handle().get_window(webview_window.label()) { - Some(window) => window, - None => { - error!("Failed to get scene window after creation"); - return; - } - }; - - if let Err(e) = overlay_fullscreen(&window) { - error!("Failed to set overlay fullscreen: {}", e); - return; - } - - if let Err(e) = window.set_ignore_cursor_events(true) { - error!("Failed to set ignore cursor events: {}", e); - return; - } - - #[cfg(debug_assertions)] - webview_window.open_devtools(); - - info!("Scene window initialized successfully."); -} diff --git a/src-tauri/src/services/auth.rs b/src-tauri/src/services/auth.rs index fcd140d..aaeaafe 100644 --- a/src-tauri/src/services/auth.rs +++ b/src-tauri/src/services/auth.rs @@ -1,5 +1,5 @@ -use crate::state::init_app_data; -use crate::{lock_r, lock_w, state::FDOLL, APP_HANDLE}; +use crate::get_app_handle; +use crate::{lock_r, lock_w, state::FDOLL}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use flate2::{read::GzDecoder, write::GzEncoder, Compression}; use keyring::Entry; @@ -60,6 +60,9 @@ pub enum OAuthError { #[error("IO error: {0}")] IoError(#[from] std::io::Error), + + #[error("Failed to open auth portal: {0}")] + OpenPortalFailed(tauri_plugin_opener::Error), } /// Parameters received from the OAuth callback. @@ -471,23 +474,14 @@ pub async fn exchange_code_for_auth_pass( /// init_auth_code_retrieval(); /// // User will be prompted to login in their browser /// ``` -pub fn init_auth_code_retrieval(on_success: F) +pub fn init_auth_code_retrieval(on_success: F) -> Result<(), OAuthError> where F: FnOnce() + Send + 'static, { info!("init_auth_code_retrieval called"); let app_config = lock_r!(FDOLL).app_config.clone(); - let opener = match APP_HANDLE.get() { - Some(handle) => { - info!("APP_HANDLE retrieved successfully"); - handle.opener() - } - None => { - error!("Cannot initialize auth: app handle not available"); - return; - } - }; + let app_handle = get_app_handle(); let code_verifier = generate_code_verifier(64); let code_challenge = generate_code_challenge(&code_verifier); @@ -513,7 +507,7 @@ where } Err(e) => { error!("Invalid auth URL configuration: {}", e); - return; + return Err(OAuthError::InvalidConfig); } }; @@ -540,7 +534,7 @@ where } Err(e) => { error!("Failed to bind callback server: {}", e); - return; + return Err(OAuthError::ServerBindError(e.to_string())); } }; @@ -589,6 +583,7 @@ where info!("Authentication successful!"); crate::services::ws::init_ws_client().await; on_success(); + return; } } Err(e) => { @@ -605,10 +600,12 @@ where }); info!("Opening auth URL: {}", url); - if let Err(e) = opener.open_url(url, None::<&str>) { + if let Err(e) = app_handle.opener().open_url(url, None::<&str>) { error!("Failed to open auth portal: {}", e); + return Err(OAuthError::OpenPortalFailed(e)); } else { info!("Successfully called open_url for auth portal"); + return Ok(()); } } diff --git a/src-tauri/src/services/mod.rs b/src-tauri/src/services/mod.rs index eca2914..3f5aedb 100644 --- a/src-tauri/src/services/mod.rs +++ b/src-tauri/src/services/mod.rs @@ -1,5 +1,5 @@ pub mod auth; pub mod cursor; -pub mod overlay; pub mod preferences; +pub mod scene; pub mod ws; diff --git a/src-tauri/src/services/overlay.rs b/src-tauri/src/services/overlay.rs deleted file mode 100644 index 89f3470..0000000 --- a/src-tauri/src/services/overlay.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::get_app_handle; -pub static SCENE_WINDOW_LABEL: &str = "scene"; -pub fn overlay_fullscreen(window: &tauri::Window) -> Result<(), tauri::Error> { - // Get the primary monitor - let monitor = get_app_handle().primary_monitor()?.unwrap(); - // Get the work area (usable space, excluding menu bar/dock/notch) - let work_area = monitor.work_area(); - // Set window position to top-left of the work area - window.set_position(tauri::PhysicalPosition { - x: work_area.position.x, - y: work_area.position.y, - })?; - // Set window size to match work area size - window.set_size(tauri::PhysicalSize { - width: work_area.size.width, - height: work_area.size.height, - })?; - Ok(()) -} diff --git a/src-tauri/src/services/scene.rs b/src-tauri/src/services/scene.rs new file mode 100644 index 0000000..fd5fbab --- /dev/null +++ b/src-tauri/src/services/scene.rs @@ -0,0 +1,81 @@ +use crate::get_app_handle; +use tauri::Manager; +use tauri_plugin_positioner::WindowExt; +use tracing::{error, info}; +pub static SCENE_WINDOW_LABEL: &str = "scene"; + +pub fn overlay_fullscreen(window: &tauri::Window) -> Result<(), tauri::Error> { + // Get the primary monitor + let monitor = get_app_handle().primary_monitor()?.unwrap(); + // Get the work area (usable space, excluding menu bar/dock/notch) + let work_area = monitor.work_area(); + // Set window position to top-left of the work area + window.set_position(tauri::PhysicalPosition { + x: work_area.position.x, + y: work_area.position.y, + })?; + // Set window size to match work area size + window.set_size(tauri::PhysicalSize { + width: work_area.size.width, + height: work_area.size.height, + })?; + Ok(()) +} + +pub fn create_scene_window() { + info!("Starting scene creation..."); + let webview_window = match tauri::WebviewWindowBuilder::new( + get_app_handle(), + SCENE_WINDOW_LABEL, + tauri::WebviewUrl::App("/scene".into()), + ) + .title("Friendolls Scene") + .inner_size(600.0, 500.0) + .resizable(false) + .decorations(false) + .transparent(true) + .shadow(false) + .visible(true) + .skip_taskbar(true) + .always_on_top(true) + .visible_on_all_workspaces(true) + .build() + { + Ok(window) => { + info!("Scene window builder succeeded"); + window + } + Err(e) => { + error!("Failed to build scene window: {}", e); + return; + } + }; + + if let Err(e) = webview_window.move_window(tauri_plugin_positioner::Position::Center) { + error!("Failed to move scene window to center: {}", e); + return; + } + + let window = match get_app_handle().get_window(webview_window.label()) { + Some(window) => window, + None => { + error!("Failed to get scene window after creation"); + return; + } + }; + + if let Err(e) = overlay_fullscreen(&window) { + error!("Failed to set overlay fullscreen: {}", e); + return; + } + + if let Err(e) = window.set_ignore_cursor_events(true) { + error!("Failed to set ignore cursor events: {}", e); + return; + } + + #[cfg(debug_assertions)] + webview_window.open_devtools(); + + info!("Scene window initialized successfully."); +}