Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(desktop): align startup artwork and native theme (#963)

Gener24458533849a
diff --git a/src/desktop/src-tauri/gen/apple/Sources/alook-desktop/main.mm b/src/desktop/src-tauri/gen/apple/Sources/alook-desktop/main.mm
index e8bb2d0e5..4f653c477 100644
--- a/src/desktop/src-tauri/gen/apple/Sources/alook-desktop/main.mm
+++ b/src/desktop/src-tauri/gen/apple/Sources/alook-desktop/main.mm
@@ -137,6 +137,7 @@ static void alookLayoutStartupOverlay(UIViewController *viewController, UIView *
static void alookPrepareStartupOverlay(UIViewController *viewController) {
if (!alookIsTauriRootController(viewController)) return;
+ alookApplyTheme(viewController, alookEffectiveDarkTheme(viewController));
UIView *overlay = alookInstallStartupOverlay(viewController);
if (overlay != nil) alookLayoutStartupOverlay(viewController, overlay);
}
diff --git a/src/desktop/src-tauri/src/commands.rs b/src/desktop/src-tauri/src/commands.rs
index eb9c4dd44..79ea9e7f0 100644
--- a/src/desktop/src-tauri/src/commands.rs
+++ b/src/desktop/src-tauri/src/commands.rs
@@ -394,27 +394,37 @@ fn should_close_splash(native_ready: bool, closed: bool) -> bool {
}
#[cfg(desktop)]
-pub fn splash_html() -> String {
+pub(crate) struct DesktopLoadingLogo {
+ pub styles: &'static str,
+ pub markup: String,
+ pub script: String,
+}
+
+#[cfg(desktop)]
+fn normalize_logo_script(script: &str) -> String {
+ script.replace("\r\n", "\n").replace('\r', "\n")
+}
+
+#[cfg(desktop)]
+pub(crate) fn desktop_loading_logo() -> DesktopLoadingLogo {
let logo = include_str!("../../../../assets/alook.svg");
let motion = include_str!("../../../web/src/components/brand/alook-loading/motion.js");
- let mut html = String::with_capacity(logo.len() + motion.len() + 5_120);
- html.push_str(
- r#"<!doctype html><html><head><meta charset="utf-8"><style>
-*{margin:0;padding:0;box-sizing:border-box}
-html,body{width:100%;height:100%;overflow:hidden;background:transparent;display:flex;align-items:center;justify-content:center;-webkit-user-select:none;user-select:none}
+ let styles = r#"
#stage{position:relative;width:192px;height:192px;flex:none}
-#canvas{position:absolute;width:1080px;height:1080px;transform:scale(.1777777778);transform-origin:top left}
+#canvas{position:absolute;width:1080px;height:1080px;transform:scale(.1777777778);transform-origin:top left;visibility:hidden}
#clip{position:absolute;left:210px;top:210px;width:660px;height:660px}
#orbit,.face,.art{position:absolute;inset:0;width:660px;height:660px}
.face,.art{transform-origin:center}
.art svg{display:block;width:660px;height:660px;overflow:visible}
-#source{display:none}
-</style></head><body><div id="stage" role="status" aria-label="Loading"><div id="canvas"><div id="clip"><div id="orbit"></div></div></div></div><template id="source">"#,
+#initial{position:absolute;left:50%;top:50%;width:117.3333333333px;height:117.3333333333px;transform:translate(-50%,-50%)}
+#initial svg{display:block;width:100%;height:100%}
+"#;
+ let markup = format!(
+ r#"<div id="stage" role="status" aria-label="Loading"><div id="canvas" aria-hidden="true"><div id="clip"><div id="orbit"></div></div></div><div id="initial" aria-hidden="true">{logo}</div></div>"#,
);
- html.push_str(logo);
- html.push_str(r##"</template><script type="module">"##);
- html.push_str(motion);
- html.push_str(
+ let mut script = String::with_capacity(motion.len() + 4_096);
+ script.push_str(motion);
+ script.push_str(
r##"
const bots=[
{face:"red",x:-100,y:-45,scale:.8,tilt:8},
@@ -423,7 +433,7 @@ const bots=[
{face:"blue",x:-85,y:0,scale:.77,tilt:7},
{face:"orange",x:55,y:130,scale:.76,tilt:-5}
];
-const source=document.querySelector("#source").content;
+const source=document.querySelector("#initial");
const clip=document.querySelector("#clip");
const orbitElement=document.querySelector("#orbit");
const namespace="http://www.w3.org/2000/svg";
@@ -481,9 +491,29 @@ function sync(){
media.addEventListener("change",sync);
document.addEventListener("visibilitychange",sync);
sync();
-</script></body></html>"##,
+document.querySelector("#canvas").style.visibility="visible";
+source.remove();
+"##,
);
- html
+ DesktopLoadingLogo {
+ styles,
+ markup,
+ script: normalize_logo_script(&script),
+ }
+}
+
+#[cfg(desktop)]
+pub fn splash_html() -> String {
+ let logo = desktop_loading_logo();
+ format!(
+ r#"<!doctype html><html><head><meta charset="utf-8"><style>
+*{{margin:0;padding:0;box-sizing:border-box}}
+html,body{{width:100%;height:100%;overflow:hidden;background:transparent;display:flex;align-items:center;justify-content:center;-webkit-user-select:none;user-select:none}}
+{styles}</style></head><body>{markup}<script type="module">{script}</script></body></html>"#,
+ styles = logo.styles,
+ markup = logo.markup,
+ script = logo.script,
+ )
}
#[cfg(desktop)]
@@ -972,6 +1002,33 @@ mod tests {
assert!(app_source.contains("should_hide_on_close(window.label())"));
}
+ #[test]
+ fn desktop_startup_uses_the_native_theme_before_inset_and_first_show() {
+ let app_source = include_str!("lib.rs");
+ let setup = app_source
+ .rsplit("builder.setup(|app| {")
+ .next()
+ .unwrap()
+ .split("builder = builder.on_page_load")
+ .next()
+ .unwrap();
+ let read_theme = setup.find("match window.theme()").unwrap();
+ let set_background = setup
+ .find("commands::set_window_theme(window.clone(), theme == tauri::Theme::Dark)")
+ .unwrap();
+ let inset = setup
+ .find("macos_window::setup_inset_webview(&window)")
+ .unwrap();
+ let handoff = setup
+ .find("webview_recovery::finish_native_creation(&window)")
+ .unwrap();
+ assert!(read_theme < set_background && set_background < inset && inset < handoff);
+ assert!(!setup.contains("set_window_theme(window.clone(), false)"));
+ assert!(setup
+ .contains("Err(error) => eprintln!(\"startup window theme unavailable: {error}\")"));
+ assert!(app_source.contains("#[cfg(target_os = \"macos\")]"));
+ }
+
#[test]
fn splash_closes_only_after_native_readiness_and_only_once() {
assert!(!should_close_splash(false, false));
@@ -991,7 +1048,11 @@ mod tests {
assert!(html.contains(canonical));
assert!(html.contains("#stage{position:relative;width:192px;height:192px"));
assert!(html.contains("<script type=\"module\">"));
- assert_eq!(html.match_indices(web_motion).count(), 1);
+ assert_eq!(
+ html.match_indices(&normalize_logo_script(web_motion))
+ .count(),
+ 1
+ );
assert_eq!(html.matches("export const DURATION = 540").count(), 1);
assert_eq!(html.matches("export const motionAt").count(), 1);
assert!(!html.contains("const DURATION=540"));
@@ -1034,6 +1095,49 @@ mod tests {
assert!(html.contains("expression.style.opacity=String(expressionOpacity)"));
}
+ #[test]
+ fn desktop_logo_is_visible_before_script_and_survives_failed_enhancement() {
+ let html = splash_html();
+ let canonical = include_str!("../../../../assets/alook.svg");
+ let body = html.split("<body>").nth(1).unwrap();
+ let initial = body.split("<script").next().unwrap();
+ assert_eq!(initial.matches(canonical).count(), 1);
+ assert!(initial.contains("<div id=\"initial\" aria-hidden=\"true\"><svg"));
+ assert!(!initial.contains("<template"));
+ assert!(html.contains("#initial{position:absolute;left:50%;top:50%;width:117.3333333333px;height:117.3333333333px"));
+ assert!(html.contains("transform-origin:top left;visibility:hidden}"));
+ let script = desktop_loading_logo().script;
+ let enhance = script.rfind("sync();").unwrap();
+ let reveal = script
+ .find("document.querySelector(\"#canvas\").style.visibility=\"visible\"")
+ .unwrap();
+ let remove = script.find("source.remove();").unwrap();
+ assert!(enhance < reveal && reveal < remove);
+ assert_eq!(script.matches("source.remove();").count(), 1);
+ assert!(!script.contains("location."));
+ }
+
+ #[test]
+ fn desktop_logo_script_normalizes_all_html_line_endings_before_embedding() {
+ let logo = desktop_loading_logo();
+ let windows = logo.script.replace('\n', "\r\n");
+ let old_mac = logo.script.replace('\n', "\r");
+ assert!(windows.contains("\r\n"));
+ assert!(old_mac.contains('\r'));
+ assert!(!logo.script.contains('\r'));
+ assert_eq!(normalize_logo_script(&windows), logo.script);
+ assert_eq!(normalize_logo_script(&old_mac), logo.script);
+ let html = splash_html();
+ let embedded = html
+ .split("<script type=\"module\">")
+ .nth(1)
+ .unwrap()
+ .split("</script>")
+ .next()
+ .unwrap();
+ assert_eq!(embedded, logo.script);
+ }
+
#[test]
fn login_shell_path_accepts_only_one_line_of_absolute_entries() {
let root = std::env::current_dir().unwrap();
diff --git a/src/desktop/src-tauri/src/lib.rs b/src/desktop/src-tauri/src/lib.rs
index fa3245fa4..0604f22fc 100644
--- a/src/desktop/src-tauri/src/lib.rs
+++ b/src/desktop/src-tauri/src/lib.rs
@@ -201,7 +201,12 @@ fn run_desktop(mut builder: tauri::Builder<tauri::Wry>) {
#[cfg(target_os = "macos")]
{
if let Some(window) = app.get_webview_window("main") {
- commands::set_window_theme(window.clone(), false);
+ match window.theme() {
+ Ok(theme) => {
+ commands::set_window_theme(window.clone(), theme == tauri::Theme::Dark);
+ }
+ Err(error) => eprintln!("startup window theme unavailable: {error}"),
+ }
macos_window::setup_inset_webview(&window);
}
}
diff --git a/src/desktop/src-tauri/src/webview_recovery.rs b/src/desktop/src-tauri/src/webview_recovery.rs
index d9cb26949..ea6ad0795 100644
--- a/src/desktop/src-tauri/src/webview_recovery.rs
+++ b/src/desktop/src-tauri/src/webview_recovery.rs
@@ -1,3 +1,7 @@
+#[cfg(desktop)]
+use base64::{engine::general_purpose::STANDARD, Engine as _};
+#[cfg(desktop)]
+use sha2::{Digest, Sha256};
use std::sync::atomic::{AtomicBool, Ordering};
use tauri::http::{Request, Response, StatusCode};
use tauri::Manager;
@@ -93,19 +97,18 @@ fn navigation_allowed(url: &Url, development: bool) -> bool {
fn response(request: Request<Vec<u8>>) -> Response<Vec<u8>> {
let (status, csp, body) = match request.uri().path() {
- BOOTSTRAP_PATH => (
- StatusCode::OK,
- "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
- bootstrap_html().into_bytes(),
- ),
+ BOOTSTRAP_PATH => {
+ let (csp, html) = bootstrap_document();
+ (StatusCode::OK, csp, html.into_bytes())
+ },
RECOVERY_PATH => (
StatusCode::OK,
- "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
+ "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'".to_owned(),
recovery_html().as_bytes().to_vec(),
),
_ => (
StatusCode::NOT_FOUND,
- "default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
+ "default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'".to_owned(),
Vec::new(),
),
};
@@ -365,12 +368,37 @@ fn initialization_failure(window: &tauri::WebviewWindow) {
}
}
-fn bootstrap_html() -> String {
- format!(
- r#"<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>Alook</title><style>:root{{color-scheme:light dark;--background:{light}}}html,body{{width:100%;height:100%;margin:0;overflow:hidden;background:var(--background)}}@media(prefers-color-scheme:dark){{:root{{--background:{dark}}}}}</style></head><body></body></html>"#,
+fn bootstrap_document() -> (String, String) {
+ #[cfg(desktop)]
+ {
+ let logo = crate::commands::desktop_loading_logo();
+ let hash = STANDARD.encode(Sha256::digest(logo.script.as_bytes()));
+ let csp = format!("default-src 'none'; style-src 'unsafe-inline'; script-src 'sha256-{hash}'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'");
+ let html = format!(
+ r#"<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>Alook</title><style>:root{{color-scheme:light dark;--background:{light}}}html,body{{width:100%;height:100%;margin:0;overflow:hidden;background:var(--background)}}body{{display:grid;place-items:center}}@media(prefers-color-scheme:dark){{:root{{--background:{dark}}}}}{styles}</style></head><body>{markup}<script type="module">{script}</script></body></html>"#,
+ light = SPLASH_BACKGROUND_LIGHT,
+ dark = SPLASH_BACKGROUND_DARK,
+ styles = logo.styles,
+ markup = logo.markup,
+ script = logo.script,
+ );
+ (csp, html)
+ }
+ #[cfg(mobile)]
+ {
+ mobile_bootstrap_document()
+ }
+}
+
+#[cfg(any(mobile, test))]
+fn mobile_bootstrap_document() -> (String, String) {
+ let logo = include_str!("../../../../assets/alook.svg");
+ let html = format!(
+ r#"<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>Alook</title><style>:root{{color-scheme:light dark;--background:{light}}}html,body{{width:100%;height:100%;margin:0;overflow:hidden;background:var(--background)}}body{{display:grid;place-items:center}}#logo{{width:106px;height:106px}}#logo svg{{display:block;width:100%;height:100%}}@media(prefers-color-scheme:dark){{:root{{--background:{dark}}}}}</style></head><body><div role="status" aria-label="Loading"><div id="logo" aria-hidden="true">{logo}</div></div></body></html>"#,
light = SPLASH_BACKGROUND_LIGHT,
dark = SPLASH_BACKGROUND_DARK,
- )
+ );
+ ("default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'".to_owned(), html)
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -1066,7 +1094,7 @@ mod tests {
}
#[test]
- fn bootstrap_is_static_and_unknown_paths_fail_closed() {
+ fn bootstrap_policy_and_unknown_paths_fail_closed() {
let request = Request::builder()
.uri("alook-recovery://localhost/bootstrap")
.body(Vec::new())
@@ -1074,10 +1102,22 @@ mod tests {
let bootstrap_response = response(request);
let html = String::from_utf8(bootstrap_response.body().clone()).unwrap();
assert_eq!(bootstrap_response.status(), 200);
- assert_eq!(
- bootstrap_response.headers()["content-security-policy"],
- "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
- );
+ let csp = bootstrap_response.headers()["content-security-policy"]
+ .to_str()
+ .unwrap();
+ for directive in [
+ "default-src 'none'",
+ "style-src 'unsafe-inline'",
+ "base-uri 'none'",
+ "form-action 'none'",
+ "frame-ancestors 'none'",
+ ] {
+ assert!(csp.contains(directive));
+ }
+ #[cfg(desktop)]
+ assert_eq!(csp.matches("script-src 'sha256-").count(), 1);
+ #[cfg(mobile)]
+ assert!(!csp.contains("script-src"));
assert_eq!(
bootstrap_response.headers()["x-content-type-options"],
"nosniff"
@@ -1086,6 +1126,7 @@ mod tests {
bootstrap_response.headers()["referrer-policy"],
"no-referrer"
);
+ #[cfg(mobile)]
assert!(!html.contains("<script"));
assert!(!html.contains("network-error"));
@@ -1099,16 +1140,75 @@ mod tests {
}
#[test]
- fn bootstrap_is_theme_background_without_another_startup_logo() {
- let html = bootstrap_html();
+ fn bootstrap_shows_canonical_logo_in_the_current_theme() {
+ let (_, html) = bootstrap_document();
assert!(html.contains("--background:#fff"));
assert!(html.contains("--background:#100d0a"));
- assert!(html.contains("<body></body>"));
+ let canonical = include_str!("../../../../assets/alook.svg");
+ assert_eq!(html.matches(canonical).count(), 1);
+ assert!(html.contains("role=\"status\" aria-label=\"Loading\""));
+ assert!(!html.contains("<template"));
assert!(!html.contains("<img"));
assert!(!html.contains("data:image"));
assert!(!html.contains("--alook-mobile-splash-offset-y"));
}
+ #[cfg(desktop)]
+ #[test]
+ fn desktop_bootstrap_hash_matches_its_exact_normalized_inline_module() {
+ let request = Request::builder()
+ .uri("alook-recovery://localhost/bootstrap")
+ .body(Vec::new())
+ .unwrap();
+ let result = response(request);
+ let csp = result.headers()["content-security-policy"]
+ .to_str()
+ .unwrap();
+ let html = String::from_utf8(result.body().clone()).unwrap();
+ assert_eq!(html.matches("<script").count(), 1);
+ let script = html
+ .split("<script type=\"module\">")
+ .nth(1)
+ .unwrap()
+ .split("</script>")
+ .next()
+ .unwrap();
+ assert_eq!(script, crate::commands::desktop_loading_logo().script);
+ assert!(!script.contains('\r'));
+ let hash = STANDARD.encode(Sha256::digest(script.as_bytes()));
+ assert_eq!(csp, format!("default-src 'none'; style-src 'unsafe-inline'; script-src 'sha256-{hash}'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"));
+ let tampered = format!("{script} ");
+ let altered_hash = STANDARD.encode(Sha256::digest(tampered.as_bytes()));
+ assert_ne!(hash, altered_hash);
+ assert!(!csp.contains(&altered_hash));
+ let windows = script.replace('\n', "\r\n");
+ let legacy = script.replace('\n', "\r");
+ for raw in [windows, legacy] {
+ let parsed = raw.replace("\r\n", "\n").replace('\r', "\n");
+ assert_eq!(STANDARD.encode(Sha256::digest(parsed.as_bytes())), hash);
+ assert_ne!(STANDARD.encode(Sha256::digest(raw.as_bytes())), hash);
+ }
+ let splash = crate::commands::splash_html();
+ assert!(splash.contains(&format!("<script type=\"module\">{script}</script>")));
+ }
+
+ #[test]
+ fn mobile_bootstrap_is_static_with_unchanged_script_restrictions() {
+ let (csp, html) = mobile_bootstrap_document();
+ assert_eq!(csp, "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'");
+ assert!(!html.contains("<script"));
+ assert!(!html.contains("<template"));
+ assert_eq!(
+ html.matches(include_str!("../../../../assets/alook.svg"))
+ .count(),
+ 1
+ );
+ assert!(html.contains("#logo{width:106px;height:106px}"));
+ assert!(html.contains("--background:#fff"));
+ assert!(html.contains("--background:#100d0a"));
+ assert!(!html.contains("requestAnimationFrame"));
+ }
+
#[test]
fn bootstrap_detection_accepts_native_and_mapped_transports() {
for target in [