From 2fdcc5ae49cbac5544ecf44451973a14a438b132 Mon Sep 17 00:00:00 2001 From: dudedagger42 <76926918+dudedagger42@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:53:10 +0000 Subject: [PATCH] Appearance: a picture of your own behind the model (#11353) * Appearance: a picture of your own behind the model Co-Authored-By: Claude Opus 5.5 * Background picture: a label that fits its row Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../locales/pending/background-picture.json | 32 +++++++ crates/ui-egui/src/colour_tokens_tests.rs | 1 + crates/ui-egui/src/gpu.rs | 94 +++++++++++++++++++ crates/ui-egui/src/prefs.rs | 2 + crates/ui-egui/src/prefs/interface.rs | 14 +++ crates/ui-egui/src/viewport.rs | 1 + docs/control-protocol.md | 1 + 7 files changed, 145 insertions(+) create mode 100644 assets/locales/pending/background-picture.json diff --git a/assets/locales/pending/background-picture.json b/assets/locales/pending/background-picture.json new file mode 100644 index 000000000..e9ad1f745 --- /dev/null +++ b/assets/locales/pending/background-picture.json @@ -0,0 +1,32 @@ +{ + "en": { + "prefs.background_image": "Picture", + "prefs.background_choose": "Choose picture…", + "prefs.background_unreadable": "Could not read the background picture" + }, + "ar": { + "prefs.background_image": "الصورة", + "prefs.background_choose": "اختيار صورة…", + "prefs.background_unreadable": "تعذرت قراءة صورة الخلفية" + }, + "he": { + "prefs.background_image": "תמונה", + "prefs.background_choose": "בחירת תמונה…", + "prefs.background_unreadable": "לא ניתן לקרוא את תמונת הרקע" + }, + "ja": { + "prefs.background_image": "画像", + "prefs.background_choose": "画像を選択…", + "prefs.background_unreadable": "背景画像を読み込めませんでした" + }, + "vi": { + "prefs.background_image": "Ảnh", + "prefs.background_choose": "Chọn ảnh…", + "prefs.background_unreadable": "Không đọc được ảnh nền" + }, + "zh": { + "prefs.background_image": "图片", + "prefs.background_choose": "选择图片…", + "prefs.background_unreadable": "无法读取背景图片" + } +} diff --git a/crates/ui-egui/src/colour_tokens_tests.rs b/crates/ui-egui/src/colour_tokens_tests.rs index a888673a4..15be6a9fa 100644 --- a/crates/ui-egui/src/colour_tokens_tests.rs +++ b/crates/ui-egui/src/colour_tokens_tests.rs @@ -143,6 +143,7 @@ fn analysis_colours_reach_the_shader_in_its_order() { /// raise the file's count here and give the reason. Fewer is always fine. The icon sources may /// hold none at all. const ALLOWED_LITERALS: &[(&str, usize, &str)] = &[ + ("gpu.rs", 1, "the white tint that shows the background picture as it is"), ("navigation.rs", 1, "the navigation cube's face shading, made from a token"), ("canvas.rs", 3, "white rings around manipulator handles"), ( diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 399cec50a..160b21eaf 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -30,6 +30,67 @@ pub(crate) fn paint_background(painter: &egui::Painter, rect: egui::Rect, tokens painter.add(egui::Shape::mesh(mesh)); } +/// The part of a picture (as texture coordinates) that `rect` shows in a window `window`: the +/// picture covers the window from its centre, so a narrower window keeps the middle of it, and +/// panels and the view's own size don't move it. +fn background_uv(image: egui::Vec2, rect: egui::Rect, window: egui::Rect) -> egui::Rect { + let scale = (window.width() / image.x).max(window.height() / image.y).max(f32::EPSILON); + let uv = |p: egui::Pos2| ((p - window.center()) / scale + image / 2.0) / image; + egui::Rect::from_min_max(uv(rect.min).to_pos2(), uv(rect.max).to_pos2()) +} + +/// A picture's pixels, at most `limit` on a side (what the GPU takes). +fn decode_background(bytes: &[u8], limit: u32) -> Result { + let mut image = image::load_from_memory(bytes).map_err(|e| e.to_string())?; + if image.width().max(image.height()) > limit { + image = image.resize(limit, limit, image::imageops::FilterType::Triangle); + } + let rgba = image.into_rgba8(); + Ok(egui::ColorImage::from_rgba_unmultiplied([rgba.width() as usize, rgba.height() as usize], rgba.as_raw())) +} + +#[derive(Clone)] +struct BackgroundPicture { + path: String, + decoded: std::sync::Arc>>>, + texture: Option, +} + +/// The background picture over the view's background (the Background picture preference), read +/// off the window thread. Until it is ready, and if it can't be read, the view's background stays. +pub(crate) fn paint_background_picture(app: &mut crate::SolveApp, painter: &egui::Painter, rect: egui::Rect) { + let path = &app.preferences.background_image; + if path.is_empty() { + return; + } + let ctx = painter.ctx().clone(); + let id = egui::Id::new("sc_background_picture"); + let mut picture = ctx.data(|d| d.get_temp::(id)).filter(|p| p.path == *path).unwrap_or_else(|| { + let picture = BackgroundPicture { path: path.clone(), decoded: Default::default(), texture: None }; + let (path, slot, repaint) = (path.clone(), picture.decoded.clone(), ctx.clone()); + let limit = ctx.input(|i| i.max_texture_side) as u32; + std::thread::spawn(move || { + let result = std::fs::read(&path).map_err(|e| e.to_string()).and_then(|bytes| decode_background(&bytes, limit)); + *slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(result); + repaint.request_repaint(); + }); + picture + }); + let done = picture.decoded.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); + match done { + Some(Ok(image)) => { + let options = egui::TextureOptions { mipmap_mode: Some(egui::TextureFilter::Linear), ..egui::TextureOptions::LINEAR }; + picture.texture = Some(ctx.load_texture("background-picture", image, options)); + } + Some(Err(error)) => app.set_status(format!("{}: {error}", crate::i18n::tr("prefs.background_unreadable")), true), + None => {} + } + if let Some(texture) = &picture.texture { + painter.image(texture.id(), rect, background_uv(texture.size_vec2(), rect, ctx.content_rect()), egui::Color32::WHITE); + } + ctx.data_mut(|d| d.insert_temp(id, picture)); +} + /// The GPU plan evaluation compiling beside the window: there once it is ready ([`tick_tessellation`]). pub type TessReady = std::sync::Arc>; @@ -1600,3 +1661,36 @@ mod tests { assert!(lost.is_lost()); } } + +#[cfg(test)] +mod background_picture_tests { + use egui::{Rect, pos2, vec2}; + + /// A window as wide as the picture shows all of it; a narrower one keeps its middle, at scale. + #[test] + fn a_picture_covers_the_window_from_its_centre() { + let image = vec2(5120.0, 1440.0); + let wide = Rect::from_min_size(pos2(0.0, 0.0), image); + let all = super::background_uv(image, wide, wide); + assert!(all.min.distance(pos2(0.0, 0.0)) < 1e-4 && all.max.distance(pos2(1.0, 1.0)) < 1e-4, "{all:?}"); + let small = Rect::from_min_size(pos2(0.0, 0.0), vec2(2560.0, 1440.0)); + let middle = super::background_uv(image, small, small); + assert!(middle.min.distance(pos2(0.25, 0.0)) < 1e-4 && middle.max.distance(pos2(0.75, 1.0)) < 1e-4, "{middle:?}"); + // The view inside the window shows its own part, so the panels don't move the picture. + let view = Rect::from_min_size(pos2(1280.0, 0.0), vec2(1280.0, 720.0)); + let part = super::background_uv(image, view, small); + assert!(part.min.distance(pos2(0.5, 0.0)) < 1e-4 && part.max.distance(pos2(0.75, 0.5)) < 1e-4, "{part:?}"); + // A tall window keeps the picture's middle the other way. + let tall = Rect::from_min_size(pos2(0.0, 0.0), vec2(500.0, 1000.0)); + let side = super::background_uv(vec2(100.0, 100.0), tall, tall); + assert!(side.min.distance(pos2(0.25, 0.0)) < 1e-4 && side.max.distance(pos2(0.75, 1.0)) < 1e-4, "{side:?}"); + } + + #[test] + fn a_big_picture_is_scaled_to_the_limit_and_junk_says_so() { + assert!(super::decode_background(b"not a picture", 8192).is_err()); + let mut png = std::io::Cursor::new(Vec::new()); + image::RgbaImage::new(40, 20).write_to(&mut png, image::ImageFormat::Png).unwrap(); + assert_eq!(super::decode_background(png.get_ref(), 10).unwrap().size, [10, 5]); + } +} diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index c5c44e6be..738fc5751 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -1255,6 +1255,8 @@ preference_settings! { /// `dark`, `light` or `system` (follow the operating system), or a theme file's id: a /// built-in one (`gruvbox-dark`, …) or one of `custom_themes` (#2255). theme: String = "dark".into(), PAGE_APPEARANCE; + /// A picture behind the model in place of the view's background; empty for none. + background_image: String = String::new(), PAGE_APPEARANCE; /// The 3D view's colours: the theme's own, or the Light or Dark theme's whatever the window /// uses (#4146). viewport_theme: crate::theme::ViewportTheme = crate::theme::ViewportTheme::Theme, PAGE_APPEARANCE; diff --git a/crates/ui-egui/src/prefs/interface.rs b/crates/ui-egui/src/prefs/interface.rs index 8ec94b623..9421717a6 100644 --- a/crates/ui-egui/src/prefs/interface.rs +++ b/crates/ui-egui/src/prefs/interface.rs @@ -88,6 +88,20 @@ pub(super) fn appearance(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) { } } }); + // A picture of the user's behind the model. + let label = app.tr("prefs.background_image"); + ui_kit::row(ui, label, |ui| { + if ui_kit::button(ui, app.tr("prefs.background_choose"), ui_kit::Button::Normal).clicked() { + app.pick_open_for(&["png", "jpg", "jpeg", "webp", "bmp", "tif", "tiff"], |app, path| { + if let Some(path) = path { + app.preferences.background_image = path; + } + }); + } + if !app.preferences.background_image.is_empty() && ui_kit::button(ui, app.tr_label("Reset"), ui_kit::Button::Ghost).clicked() { + app.preferences.background_image.clear(); + } + }); // The logo in the theme's own logo colours or in its accent. let label = app.tr("prefs.logo"); ui_kit::row(ui, label, |ui| { diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 9908c40c8..ed3403126 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -4743,6 +4743,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { list_hover(app); let painter = ui.painter_at(rect); crate::gpu::paint_background(&painter, rect, &t); + crate::gpu::paint_background_picture(app, &painter, rect); // ---- model ---- crate::frame_watch::step(app, "viewport before the model"); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 4872ce5b5..6619d3abb 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -701,6 +701,7 @@ One setting per line, sorted by name. A change that adds a setting adds its own - `angle_decimals` - `assembly_move_handles` (default true): explicit Assembly Move uses the permitted joint-motion handles; false keeps the previous Move panel. - `auto_operation` (default true: feature windows choose Join, New Body or Cut from what their material touches until one is picked; false: each keeps the operation it opens with, Extrude its own Join or Cut) +- `background_image` (string, default empty: a picture's file, shown behind the model and covering the window from its centre). - `browser_bodies_folders` (`several` (default): a component with one body and no groups lists it straight under the component in the browser, without a Bodies folder; `always`; `never`: components list their bodies and groups straight under them, and the folder's menu items are on the component's menu; the design's own Bodies folder, and one hidden with its eye, always show; #4217) - `browser_components_open` (default false: each component placement in the browser starts closed until it is opened, by its fold arrow, Expand All or Find in Browser; true: every placement starts open, as before #9274) - `browser_eye_left` (default false: each browser and History tree row's eye, handle `eye:`, sits in a column at the rows' right end, clear of the fold arrow and the radio; true: between them, as before #8718)