Pass pakeConfig to web

This commit is contained in:
Tw93
2024-05-09 15:32:16 +08:00
parent 7bcaeb6d7e
commit 161ec7f247
5 changed files with 25 additions and 21 deletions

View File

@@ -2,13 +2,14 @@
"windows": [ "windows": [
{ {
"url": "https://weread.qq.com", "url": "https://weread.qq.com",
"url_type": "web",
"transparent": true, "transparent": true,
"fullscreen": false, "fullscreen": false,
"width": 1200, "width": 1200,
"height": 780, "height": 780,
"resizable": true, "resizable": true,
"url_type": "web", "always_on_top": false,
"always_on_top": false "disabled_web_shortcuts": false
} }
], ],
"user_agent": { "user_agent": {

View File

@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct WindowConfig { pub struct WindowConfig {
pub url: String, pub url: String,
pub transparent: bool, pub transparent: bool,
@@ -10,9 +10,10 @@ pub struct WindowConfig {
pub resizable: bool, pub resizable: bool,
pub url_type: String, pub url_type: String,
pub always_on_top: bool, pub always_on_top: bool,
pub disabled_web_shortcuts: bool,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PlatformSpecific<T> { pub struct PlatformSpecific<T> {
pub macos: T, pub macos: T,
pub linux: T, pub linux: T,
@@ -44,7 +45,7 @@ where
pub type UserAgent = PlatformSpecific<String>; pub type UserAgent = PlatformSpecific<String>;
pub type FunctionON = PlatformSpecific<bool>; pub type FunctionON = PlatformSpecific<bool>;
#[derive(Debug, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PakeConfig { pub struct PakeConfig {
pub windows: Vec<WindowConfig>, pub windows: Vec<WindowConfig>,
pub user_agent: UserAgent, pub user_agent: UserAgent,

View File

@@ -19,6 +19,11 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
_ => panic!("url type can only be web or local"), _ => 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 = WindowBuilder::new(app, "pake", url) let mut window_builder = WindowBuilder::new(app, "pake", url)
.title("") .title("")
.user_agent(user_agent) .user_agent(user_agent)
@@ -28,18 +33,13 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
.inner_size(window_config.width, window_config.height) .inner_size(window_config.width, window_config.height)
.disable_file_drop_handler() .disable_file_drop_handler()
.always_on_top(window_config.always_on_top) .always_on_top(window_config.always_on_top)
.initialization_script(&config_script)
.initialization_script(include_str!("../inject/component.js")) .initialization_script(include_str!("../inject/component.js"))
.initialization_script(include_str!("../inject/event.js")) .initialization_script(include_str!("../inject/event.js"))
.initialization_script(include_str!("../inject/style.js")) .initialization_script(include_str!("../inject/style.js"))
//This is necessary to allow for file injection by external developers for customization purposes. //This is necessary to allow for file injection by external developers for customization purposes.
.initialization_script(include_str!("../inject/custom.js")); .initialization_script(include_str!("../inject/custom.js"));
// For dynamic display of header styles
if window_config.transparent {
let transparent_script = "window.pakeWindowTitleTransparent = true;";
window_builder = window_builder.initialization_script(transparent_script);
}
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let title_bar_style = if window_config.transparent { let title_bar_style = if window_config.transparent {

View File

@@ -88,14 +88,16 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
}); });
document.addEventListener('keyup', (event) => { if (window['pakeConfig']?.disabled_web_shortcuts !== true) {
if (/windows|linux/i.test(navigator.userAgent) && event.ctrlKey) { document.addEventListener('keyup', (event) => {
handleShortcut(event); if (/windows|linux/i.test(navigator.userAgent) && event.ctrlKey) {
} handleShortcut(event);
if (/macintosh|mac os x/i.test(navigator.userAgent) && event.metaKey) { }
handleShortcut(event); if (/macintosh|mac os x/i.test(navigator.userAgent) && event.metaKey) {
} handleShortcut(event);
}); }
});
}
// Collect blob urls to blob by overriding window.URL.createObjectURL // Collect blob urls to blob by overriding window.URL.createObjectURL
function collectUrlToBlobs() { function collectUrlToBlobs() {

View File

@@ -412,7 +412,7 @@ window.addEventListener('DOMContentLoaded', _event => {
} }
`; `;
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0; const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
if (window.pakeWindowTitleTransparent && isMac) { if (window['pakeConfig']?.transparent && isMac) {
const topPaddingStyleElement = document.createElement('style'); const topPaddingStyleElement = document.createElement('style');
topPaddingStyleElement.innerHTML = topPaddingCSS; topPaddingStyleElement.innerHTML = topPaddingCSS;
document.head.appendChild(topPaddingStyleElement); document.head.appendChild(topPaddingStyleElement);