minor organization, rearrangement & refinement of code

This commit is contained in:
2025-12-08 15:39:38 +08:00
parent 527ef028d6
commit 4f62242517
5 changed files with 103 additions and 104 deletions

View File

@@ -1,13 +1,8 @@
use tauri::Manager; use tracing::info;
use tauri_plugin_positioner::WindowExt;
use tracing::{error, info};
use crate::{ use crate::{
get_app_handle,
services::{ services::{
auth::get_tokens, auth::get_tokens, preferences::create_preferences_window, scene::create_scene_window,
overlay::{overlay_fullscreen, SCENE_WINDOW_LABEL},
preferences::create_preferences_window,
}, },
state::init_app_data, state::init_app_data,
}; };
@@ -18,7 +13,7 @@ pub async fn start_fdoll() {
async fn construct_app() { async fn construct_app() {
init_app_data().await; init_app_data().await;
create_scene(); create_scene_window();
create_preferences_window(); create_preferences_window();
} }
@@ -30,71 +25,16 @@ pub async fn bootstrap() {
} }
None => { None => {
info!("No active session, user needs to authenticate"); 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..."); info!("Authentication successful, creating scene...");
tauri::async_runtime::spawn(async { tauri::async_runtime::spawn(async {
info!("Creating scene after auth success..."); info!("Creating scene after auth success...");
construct_app().await; 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.");
}

View File

@@ -1,5 +1,5 @@
use crate::state::init_app_data; use crate::get_app_handle;
use crate::{lock_r, lock_w, state::FDOLL, APP_HANDLE}; use crate::{lock_r, lock_w, state::FDOLL};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use flate2::{read::GzDecoder, write::GzEncoder, Compression}; use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use keyring::Entry; use keyring::Entry;
@@ -60,6 +60,9 @@ pub enum OAuthError {
#[error("IO error: {0}")] #[error("IO error: {0}")]
IoError(#[from] std::io::Error), IoError(#[from] std::io::Error),
#[error("Failed to open auth portal: {0}")]
OpenPortalFailed(tauri_plugin_opener::Error),
} }
/// Parameters received from the OAuth callback. /// Parameters received from the OAuth callback.
@@ -471,23 +474,14 @@ pub async fn exchange_code_for_auth_pass(
/// init_auth_code_retrieval(); /// init_auth_code_retrieval();
/// // User will be prompted to login in their browser /// // User will be prompted to login in their browser
/// ``` /// ```
pub fn init_auth_code_retrieval<F>(on_success: F) pub fn init_auth_code_retrieval<F>(on_success: F) -> Result<(), OAuthError>
where where
F: FnOnce() + Send + 'static, F: FnOnce() + Send + 'static,
{ {
info!("init_auth_code_retrieval called"); info!("init_auth_code_retrieval called");
let app_config = lock_r!(FDOLL).app_config.clone(); let app_config = lock_r!(FDOLL).app_config.clone();
let opener = match APP_HANDLE.get() { let app_handle = get_app_handle();
Some(handle) => {
info!("APP_HANDLE retrieved successfully");
handle.opener()
}
None => {
error!("Cannot initialize auth: app handle not available");
return;
}
};
let code_verifier = generate_code_verifier(64); let code_verifier = generate_code_verifier(64);
let code_challenge = generate_code_challenge(&code_verifier); let code_challenge = generate_code_challenge(&code_verifier);
@@ -513,7 +507,7 @@ where
} }
Err(e) => { Err(e) => {
error!("Invalid auth URL configuration: {}", e); error!("Invalid auth URL configuration: {}", e);
return; return Err(OAuthError::InvalidConfig);
} }
}; };
@@ -540,7 +534,7 @@ where
} }
Err(e) => { Err(e) => {
error!("Failed to bind callback server: {}", e); error!("Failed to bind callback server: {}", e);
return; return Err(OAuthError::ServerBindError(e.to_string()));
} }
}; };
@@ -589,6 +583,7 @@ where
info!("Authentication successful!"); info!("Authentication successful!");
crate::services::ws::init_ws_client().await; crate::services::ws::init_ws_client().await;
on_success(); on_success();
return;
} }
} }
Err(e) => { Err(e) => {
@@ -605,10 +600,12 @@ where
}); });
info!("Opening auth URL: {}", url); 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); error!("Failed to open auth portal: {}", e);
return Err(OAuthError::OpenPortalFailed(e));
} else { } else {
info!("Successfully called open_url for auth portal"); info!("Successfully called open_url for auth portal");
return Ok(());
} }
} }

View File

@@ -1,5 +1,5 @@
pub mod auth; pub mod auth;
pub mod cursor; pub mod cursor;
pub mod overlay;
pub mod preferences; pub mod preferences;
pub mod scene;
pub mod ws; pub mod ws;

View File

@@ -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(())
}

View File

@@ -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.");
}