From 9e7202b2e09bfca62ddc3e00f9667e1dfd4d0826 Mon Sep 17 00:00:00 2001 From: lars-berger Date: Wed, 11 Dec 2024 05:12:12 +0800 Subject: [PATCH] move `create_init_url` to `asset_server` mod --- packages/desktop/src/asset_server.rs | 33 +++++++++++++++++++++++++- packages/desktop/src/widget_factory.rs | 28 +++++----------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/packages/desktop/src/asset_server.rs b/packages/desktop/src/asset_server.rs index 490e83b8..2f3bb636 100644 --- a/packages/desktop/src/asset_server.rs +++ b/packages/desktop/src/asset_server.rs @@ -1,4 +1,8 @@ -use std::{io::Cursor, path::PathBuf, sync::Arc}; +use std::{ + io::Cursor, + path::{Path, PathBuf}, + sync::Arc, +}; use rocket::{ fs::NamedFile, @@ -30,6 +34,33 @@ pub fn setup_asset_server( }); } +pub async fn create_init_url( + widget_factory: &WidgetFactory, + parent_dir: &Path, + html_path: &Path, +) -> anyhow::Result { + let url = tauri::Url::parse_with_params( + "http://127.0.0.1:6124/__zebar/init", + &[ + ( + "token", + // Generate a unique token to identify requests from the + // widget to the asset server. + &widget_factory.upsert_or_get_token(parent_dir).await, + ), + ( + "redirect", + &format!( + "/{}", + html_path.strip_prefix(parent_dir)?.to_unicode_string() + ), + ), + ], + )?; + + Ok(url) +} + #[get("/__zebar/init?&")] pub fn init( token: String, diff --git a/packages/desktop/src/widget_factory.rs b/packages/desktop/src/widget_factory.rs index 67211319..2b827fce 100644 --- a/packages/desktop/src/widget_factory.rs +++ b/packages/desktop/src/widget_factory.rs @@ -12,7 +12,7 @@ use base64::prelude::*; use serde::Serialize; use tauri::{ path::BaseDirectory, AppHandle, Manager, PhysicalPosition, PhysicalSize, - Url, WebviewUrl, WebviewWindowBuilder, WindowEvent, + WebviewUrl, WebviewWindowBuilder, WindowEvent, }; use tokio::{ sync::{broadcast, Mutex}, @@ -26,6 +26,7 @@ use crate::common::macos::WindowExtMacOs; #[cfg(target_os = "windows")] use crate::common::windows::{remove_app_bar, WindowExtWindows}; use crate::{ + asset_server::create_init_url, common::PathExt, config::{ AnchorPoint, Config, DockConfig, DockEdge, WidgetConfig, @@ -257,26 +258,9 @@ impl WidgetFactory { ) } - let url = Url::parse_with_params( - "http://127.0.0.1:6124/__zebar/init", - &[ - ( - "token", - // Generate a unique token to identify requests from the - // widget to the asset server. - &self.upsert_or_get_token(parent_dir).await, - ), - ( - "redirect", - &format!( - "/{}", - html_path.strip_prefix(parent_dir)?.to_unicode_string() - ), - ), - ], - )?; - - let webview_url = WebviewUrl::External(url); + let webview_url = WebviewUrl::External( + create_init_url(self, &parent_dir, &html_path).await?, + ); let mut state = WidgetState { id: widget_id.clone(), @@ -832,7 +816,7 @@ impl WidgetFactory { /// /// If the directory does not have an existing token, a new one is /// generated and inserted. - async fn upsert_or_get_token(&self, directory: &Path) -> String { + pub async fn upsert_or_get_token(&self, directory: &Path) -> String { let mut asset_server_tokens = self.asset_server_tokens.lock().await; // Find existing token for this path.