diff --git a/apps/solidcraft-web/src/disk.rs b/apps/solidcraft-web/src/disk.rs index 10e0ca596..a8abac6f7 100644 --- a/apps/solidcraft-web/src/disk.rs +++ b/apps/solidcraft-web/src/disk.rs @@ -147,6 +147,21 @@ pub fn accept_list(extensions: &[&str]) -> String { list.join(",") } +/// The `accept` of a file input for `extensions` on this device (`apple_touch`: an iPhone or +/// iPad). The picker there offers only files of a type the system knows from the list and greys +/// out the rest, `.solidcraft` designs among them, since no system type has that extension +/// (#6977). So it offers every file there, and the opener checks what it gets. +pub fn input_accept(extensions: &[&str], apple_touch: bool) -> String { + if apple_touch { String::new() } else { accept_list(extensions) } +} + +/// The browser runs on an iPhone or iPad (every browser there is WebKit), from its +/// `navigator.userAgent` and `navigator.maxTouchPoints`. iPadOS presents itself as a Mac; only its +/// touch screen tells them apart. +pub fn apple_touch(user_agent: &str, touch_points: i32) -> bool { + ["iPhone", "iPad", "iPod"].iter().any(|d| user_agent.contains(d)) || (user_agent.contains("Macintosh") && touch_points > 1) +} + /// The largest file an upload for `extensions` reads: images go into a canvas, which has a size /// limit, so a bigger one is refused before it is read. `None`: the importer checks the file. pub fn upload_limit(extensions: &[&str]) -> Option { @@ -671,6 +686,26 @@ mod tests { assert_eq!(upload_limit(&["dxf"]), None); } + /// An iPhone or iPad greys out a `.solidcraft` design the `accept` list names, as it knows no + /// type for that extension, so its inputs offer every file; other devices keep the list (#6977). + #[test] + fn file_inputs_on_an_iphone_or_ipad_offer_every_file() { + let iphone = + "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1"; + let mac = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15"; + let chrome_ios = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/138.0.7204.156 Mobile/15E148 Safari/604.1"; + let chrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"; + assert!(apple_touch(iphone, 5)); + assert!(apple_touch(chrome_ios, 5), "every browser on iOS has the same picker"); + assert!(apple_touch(mac, 5), "an iPad presents itself as a Mac"); + assert!(!apple_touch(mac, 0), "a Mac has no touch screen"); + assert!(!apple_touch(chrome, 10), "a touch screen elsewhere filters as before"); + assert!(!apple_touch("", 0), "hostile input is just not an iPhone"); + let designs = ["solidcraft", "json", "step"]; + assert_eq!(input_accept(&designs, true), ""); + assert_eq!(input_accept(&designs, false), ".solidcraft,.json,.step"); + } + #[test] fn the_save_dialog_offers_the_type_of_the_name() { assert_eq!(save_type("Plate.solidcraft").2, ".solidcraft"); diff --git a/apps/solidcraft-web/src/files.rs b/apps/solidcraft-web/src/files.rs index 228924dc1..0f610567c 100644 --- a/apps/solidcraft-web/src/files.rs +++ b/apps/solidcraft-web/src/files.rs @@ -513,9 +513,9 @@ fn input_picker(files: &Rc, intent: Intent) { input.set_type("file"); // Everything the browser build imports, from the format registry: cadmpeg is pure Rust, so // FreeCAD and Rhino files are part of it; plus the DXF and SVG drawings Open and Insert take. - let accept = - solidcraft_engine::io::formats::open_extensions(true).iter().chain(&["dxf", "svg"]).map(|e| format!(".{e}")).collect::>().join(","); - input.set_accept(&accept); + let mut extensions = solidcraft_engine::io::formats::open_extensions(true); + extensions.extend(["dxf", "svg"]); + input.set_accept(&disk::input_accept(&extensions, apple_touch())); // Attached (hidden) while it is open: some browsers ignore a click on a detached input. let _ = input.set_attribute("style", "display:none"); if let Some(body) = doc.body() { @@ -546,7 +546,7 @@ fn input_picker(files: &Rc, intent: Intent) { /// `None` when the dialog was closed or the file could not be read. pub fn pick_upload(files: &Rc, extensions: &[&str]) -> impl std::future::Future> + use<> { // Shown now, in the click: a file dialog needs the user's gesture. - let chosen = choose_file(&disk::accept_list(extensions)); + let chosen = choose_file(&disk::input_accept(extensions, apple_touch())); let limit = disk::upload_limit(extensions); let files = files.clone(); async move { @@ -573,6 +573,14 @@ pub fn pick_upload(files: &Rc, extensions: &[&str]) -> impl std::future:: } } +/// The page runs on an iPhone or iPad, whose file inputs offer every file (`disk::input_accept`). +fn apple_touch() -> bool { + web_sys::window().is_some_and(|w| { + let nav = w.navigator(); + disk::apple_touch(&nav.user_agent().unwrap_or_default(), nav.max_touch_points()) + }) +} + /// Show a hidden `` offering `accept` (call in the click). The promise answers /// the picked `File`, or `null` when the dialog is closed (the input's `cancel` event). fn choose_file(accept: &str) -> Result {