SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Theme drops: build for the web, and import there too (#8226) (#8534)
raykholo6b523d683916
diff --git a/apps/solidcraft-web/src/files.rs b/apps/solidcraft-web/src/files.rsindex 4e42f1d185..b7660968e6 100644--- a/apps/solidcraft-web/src/files.rs+++ b/apps/solidcraft-web/src/files.rs@@ -40,6 +40,8 @@ pub enum Intent {Open,/// Add its bodies to the open design (STEP, 3MF, STL).Insert,+ /// Import it as a theme: it was dropped on Preferences' Import tile (#8226).+ Theme,}/// A stored design, as the file panel lists it.@@ -615,13 +617,15 @@ fn choose_file(accept: &str) -> Result<js_sys::Promise, String> {}/// Read files dropped on the page.-pub fn take_drops(files: &Rc<Files>, raw: &mut egui::RawInput) {+/// `theme`: the pointer is over Preferences' Import tile, so the files are themes.+pub fn take_drops(files: &Rc<Files>, raw: &mut egui::RawInput, theme: bool) {+ let intent = if theme { Intent::Theme } else { Intent::Insert };for f in std::mem::take(&mut raw.dropped_files) {let files = files.clone();spawn_local(async move {let name = f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into());match f.bytes_async().await {- Ok(b) => files.arrive(name, b, Intent::Insert),+ Ok(b) => files.arrive(name, b, intent),Err(e) => files.say(format!("{name}: {e}"), true),}});diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rsindex 93c3d4d430..403898f5fc 100644--- a/apps/solidcraft-web/src/main.rs+++ b/apps/solidcraft-web/src/main.rs@@ -295,6 +295,10 @@ mod web {!companion});for (name, bytes, intent) in incoming {+ if intent == Intent::Theme {+ solidcraft_ui_egui::prefs::import_theme_bytes(&mut self.app, bytes);+ continue;+ }let design = solidcraft_engine::io::is_design_path(&name);let path = if design && intent == Intent::Open {// A design from the computer is kept in the browser (a copy), so Save works.@@ -311,7 +315,7 @@ mod web {};match intent {Intent::Open => self.app.open_path(&path),- Intent::Insert => self.app.drop_path(&path),+ Intent::Insert | Intent::Theme => self.app.drop_path(&path),}self.files.panel.set(false);}@@ -657,7 +661,8 @@ mod web {ctx.request_repaint_after(delay);}if self.launch.persists() {- files::take_drops(&self.files, raw);+ let theme = solidcraft_ui_egui::prefs::theme_drop_target(&self.app, ctx);+ files::take_drops(&self.files, raw, theme);} else {raw.dropped_files.clear();}diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rsindex 36b8ffc36d..c9b88166c9 100644--- a/crates/ui-egui/src/prefs.rs+++ b/crates/ui-egui/src/prefs.rs@@ -2813,20 +2813,43 @@ pub fn take_theme_paste(app: &mut SolveApp, ctx: &egui::Context) {/// Files dropped on the theme Import tile are themes to import, not designs to open (#8226).pub fn take_theme_drops(app: &mut SolveApp, ctx: &egui::Context) {- let Some(rect) = app.prefs_window.import_tile.filter(|_| app.prefs_window.open) else { return };- if !ctx.input(|i| !i.raw.dropped_files.is_empty() && i.pointer.latest_pos().is_some_and(|p| rect.contains(p))) {+ if !theme_drop_target(app, ctx) || !ctx.input(|i| !i.raw.dropped_files.is_empty()) {return;}let files = ctx.input_mut(|i| std::mem::take(&mut i.raw.dropped_files));for f in files.iter().take(8) {- let text = f- .bytes()- .and_then(|b| if b.len() > crate::theme_files::MAX_BYTES { Err("the file is too large for a theme".into()) } else { Ok(b) })- .and_then(|b| String::from_utf8(b).map_err(|_| "not a theme file".to_string()));- import_theme_text(app, text);+ // The browser reads dropped files only asynchronously: the web shell takes them first and+ // hands a theme's bytes to `import_theme_bytes` (#8226), so none reach this on wasm.+ #[cfg(not(target_arch = "wasm32"))]+ match f.bytes() {+ Ok(bytes) => import_theme_bytes(app, bytes),+ Err(e) => import_theme_text(app, Err(e.to_string())),+ }+ #[cfg(target_arch = "wasm32")]+ let _ = f;}}+/// Whether files dropped now land on the Import tile: the Preferences window is open and the+/// pointer was last seen over the tile.+pub fn theme_drop_target(app: &SolveApp, ctx: &egui::Context) -> bool {+ app.prefs_window+ .import_tile+ .filter(|_| app.prefs_window.open)+ .is_some_and(|rect| ctx.input(|i| i.pointer.latest_pos()).is_some_and(|p| rect.contains(p)))+}++/// A file dropped on the Import tile, as bytes: imported when it is a theme, refused with the reason+/// otherwise.+pub fn import_theme_bytes(app: &mut SolveApp, bytes: Vec<u8>) {+ let text = if bytes.len() > crate::theme_files::MAX_BYTES {+ Err("the file is too large for a theme".to_string())+ } else {+ String::from_utf8(bytes).map_err(|_| "not a theme file".to_string())+ };+ import_theme_text(app, text);+}+pub fn import_theme(app: &mut SolveApp, v: &serde_json::Value) -> Result<String, String> {let theme = match v {serde_json::Value::String(text) => crate::theme_files::parse(text)?,