Files
Pake/src-tauri/src/app/window.rs
2025-08-14 10:01:35 +08:00

96 lines
3.3 KiB
Rust

use crate::app::config::PakeConfig;
use crate::util::get_data_dir;
use std::{path::PathBuf, str::FromStr};
use tauri::{App, Config, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
#[cfg(target_os = "macos")]
use tauri::{Theme, TitleBarStyle};
#[cfg(target_os = "windows")]
use tauri::Theme;
pub fn set_window(app: &mut App, config: &PakeConfig, tauri_config: &Config) -> WebviewWindow {
let package_name = tauri_config.clone().product_name.unwrap();
let _data_dir = get_data_dir(app.handle(), package_name);
let window_config = config
.windows
.first()
.expect("At least one window configuration is required");
let user_agent = config.user_agent.get();
let url = match window_config.url_type.as_str() {
"web" => WebviewUrl::App(window_config.url.parse().unwrap()),
"local" => WebviewUrl::App(PathBuf::from(&window_config.url)),
_ => panic!("url type can only be web or local"),
};
let config_script = format!(
"window.pakeConfig = {}",
serde_json::to_string(&window_config).unwrap()
);
let mut window_builder = WebviewWindowBuilder::new(app, "pake", url)
.title("")
.visible(false)
.user_agent(user_agent)
.resizable(window_config.resizable)
.fullscreen(window_config.fullscreen)
.inner_size(window_config.width, window_config.height)
.always_on_top(window_config.always_on_top)
.disable_drag_drop_handler()
.initialization_script(&config_script)
.initialization_script(include_str!("../inject/component.js"))
.initialization_script(include_str!("../inject/event.js"))
.initialization_script(include_str!("../inject/style.js"))
.initialization_script(include_str!("../inject/custom.js"));
// Configure proxy if specified
if !config.proxy_url.is_empty() {
if let Ok(proxy_url) = Url::from_str(&config.proxy_url) {
window_builder = window_builder.proxy_url(proxy_url);
#[cfg(debug_assertions)]
println!("Proxy configured: {}", config.proxy_url);
}
}
#[cfg(target_os = "macos")]
{
let title_bar_style = if window_config.hide_title_bar {
TitleBarStyle::Overlay
} else {
TitleBarStyle::Visible
};
window_builder = window_builder.title_bar_style(title_bar_style);
if window_config.dark_mode {
window_builder = window_builder.theme(Some(Theme::Dark));
}
}
#[cfg(target_os = "windows")]
{
window_builder = window_builder
.data_directory(_data_dir)
.title(app.package_info().name.clone());
// Set theme to None for automatic system theme detection on Windows
// This allows the window to respond to system theme changes automatically
window_builder = window_builder.theme(None);
}
#[cfg(target_os = "linux")]
{
window_builder = window_builder
.data_directory(_data_dir)
.title(app.package_info().name.clone());
// Set theme to None for automatic system theme detection on Linux
// This allows the window to respond to system theme changes automatically
window_builder = window_builder.theme(None);
}
window_builder.build().expect("Failed to build window")
}