Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Graphics: a background colour of your choice for the 3D view (#7688) (#8815)

phanoob5b0a86a5b276
diff --git a/assets/locales/pending/viewport-background-colour.json b/assets/locales/pending/viewport-background-colour.json
new file mode 100644
index 0000000000..3782c200f4
--- /dev/null
+++ b/assets/locales/pending/viewport-background-colour.json
@@ -0,0 +1,55 @@
+{
+ "en": {
+ "prefs.viewport_background": "Background",
+ "prefs.viewport_background_theme": "Theme's gradient",
+ "prefs.viewport_background_colour": "Color"
+ },
+ "en-GB": {
+ "prefs.viewport_background_colour": "Colour"
+ },
+ "fr": {
+ "prefs.viewport_background": "Fond",
+ "prefs.viewport_background_theme": "Dégradé du thème",
+ "prefs.viewport_background_colour": "Couleur"
+ },
+ "de": {
+ "prefs.viewport_background": "Hintergrund",
+ "prefs.viewport_background_theme": "Verlauf des Designs",
+ "prefs.viewport_background_colour": "Farbe"
+ },
+ "it": {
+ "prefs.viewport_background": "Sfondo",
+ "prefs.viewport_background_theme": "Sfumatura del tema",
+ "prefs.viewport_background_colour": "Colore"
+ },
+ "es": {
+ "prefs.viewport_background": "Fondo",
+ "prefs.viewport_background_theme": "Degradado del tema",
+ "prefs.viewport_background_colour": "Color"
+ },
+ "ar": {
+ "prefs.viewport_background": "الخلفية",
+ "prefs.viewport_background_theme": "تدرج السمة",
+ "prefs.viewport_background_colour": "لون"
+ },
+ "he": {
+ "prefs.viewport_background": "רקע",
+ "prefs.viewport_background_theme": "מעבר הצבעים של ערכת הנושא",
+ "prefs.viewport_background_colour": "צבע"
+ },
+ "ja": {
+ "prefs.viewport_background": "背景",
+ "prefs.viewport_background_theme": "テーマのグラデーション",
+ "prefs.viewport_background_colour": "色"
+ },
+ "vi": {
+ "prefs.viewport_background": "Nền",
+ "prefs.viewport_background_theme": "Dải màu của chủ đề",
+ "prefs.viewport_background_colour": "Màu"
+ },
+ "zh": {
+ "prefs.viewport_background": "背景",
+ "prefs.viewport_background_theme": "主题渐变",
+ "prefs.viewport_background_colour": "颜色"
+ }
+}
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index 6ccb765c04..98cb282c21 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -412,12 +412,17 @@ pub fn request_scope(app: &SolveApp, params: &Value) -> Result<Option<vfs::PathS
/// mode, the section the viewport shows: an open Section Analysis dialog's preview, a sketch's
/// Slice), with the grid and the origin's axes as `guides` says.
pub(crate) fn view_scene(app: &SolveApp, guides: solidcraft_engine::view::Guides) -> solidcraft_engine::render::Scene {
+ let mut palette = crate::viewport::scene_palette(app.viewport_dark(), app.preferences.color_vision);
+ // The view's own background colour (#7688).
+ if let Some([r, g, b]) = app.preferences.viewport_background {
+ palette.background = (solidcraft_engine::render::Rgb(r, g, b), solidcraft_engine::render::Rgb(r, g, b));
+ }
let mut scene = solidcraft_engine::view::scene_with_thread_markings(
&app.session,
&app.cam,
crate::viewport::section_plane(app),
solidcraft_engine::view::world_curve_parts,
- &crate::viewport::scene_palette(app.viewport_dark(), app.preferences.color_vision),
+ &palette,
guides,
app.preferences.cosmetic_threads,
);
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index ed6c747b43..f4ba4e0a92 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -2328,6 +2328,7 @@ impl SolveApp {
prefs::apply(self, ctx);
// The view's own dark or light only changes tokens, not egui's visuals: no restyle.
theme::set_viewport_dark(self.preferences.viewport_theme.dark());
+ theme::set_viewport_background(self.preferences.viewport_background);
if !self.styled || theme::is_dark() != self.ui.dark || theme::vision() != self.preferences.color_vision {
theme::set_dark(self.ui.dark);
theme::set_vision(self.preferences.color_vision);
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index 2d19429caa..a4c2c04edc 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -960,6 +960,9 @@ preference_settings! {
/// 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_INTERFACE;
+ /// The 3D view's background, one colour for the whole application (#7688), or `None` (the
+ /// default) for the theme's gradient.
+ viewport_background: Option<[u8; 3]> = None, PAGE_INTERFACE;
/// The application bar's logo in the theme's accent instead of the theme's logo colours.
logo_accent: bool = false, PAGE_INTERFACE;
/// Imported theme files (`theme_files`), kept here so they also work in the browser.
@@ -4685,6 +4688,7 @@ mod tests {
("dialog_place", json!("left_column"), PAGE_INTERFACE),
("color_vision", json!("protan"), PAGE_INTERFACE),
("viewport_theme", json!("light"), PAGE_INTERFACE),
+ ("viewport_background", json!([255, 255, 255]), PAGE_INTERFACE),
("view_confirm_corner", json!(true), PAGE_INTERFACE),
("status_bar", json!(true), PAGE_INTERFACE),
("nav", json!("blender"), PAGE_NAVIGATION),
@@ -4898,6 +4902,44 @@ mod tests {
assert_eq!(read, crate::theme::ViewportTheme::Theme);
}
+ /// The 3D view's background in one colour of the user's (#7688): `ui.prefs` sets it, the next
+ /// frame fills the view with it in either theme while the window keeps its colours, a picture
+ /// of the view has it too, and it is saved with the preferences. `null` gives back the theme's
+ /// gradient; a value that is not a colour is refused, and read from saved preferences it is
+ /// the gradient.
+ #[test]
+ fn the_3d_view_takes_a_background_colour_of_its_own() {
+ use crate::theme::{ColorVision, Tokens};
+ use serde_json::json;
+ let mut h = crate::scenario::Harness::new();
+ assert_eq!(h.app.preferences.viewport_background, None);
+ for (theme, view_dark) in [("light", false), ("dark", true)] {
+ let out = h.call("ui.prefs", json!({"theme": theme, "viewport_background": [255, 255, 255]}));
+ assert!(out["error"].is_null(), "{out}");
+ h.frames(2);
+ let (t, white) = (Tokens::get(), egui::Color32::WHITE);
+ assert_eq!((t.viewport_top, t.viewport_bottom, Tokens::view().viewport_top), (white, white, white), "{theme}");
+ assert_eq!(t.panel, Tokens::themed(view_dark, ColorVision::Off).panel, "{theme}: the window");
+ let scene = crate::control::view_scene(&h.app, solidcraft_engine::view::Guides::default());
+ let white = solidcraft_engine::render::Rgb(255, 255, 255);
+ assert_eq!(scene.background, Some((white, white)), "{theme}: a picture of the view");
+ }
+
+ let mut other = crate::SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
+ other.load_prefs(&h.app.prefs());
+ assert_eq!(other.preferences.viewport_background, Some([255, 255, 255]));
+
+ h.call("ui.prefs", json!({"viewport_background": null}));
+ h.frames(2);
+ let (t, dark) = (Tokens::get(), Tokens::dark());
+ assert_eq!((t.viewport_top, t.viewport_bottom), (dark.viewport_top, dark.viewport_bottom), "the theme's gradient again");
+
+ for bad in [json!([256, 0, 0]), json!("white"), json!([1, 2])] {
+ assert!(h.call("ui.prefs", json!({"viewport_background": bad}))["error"].is_string(), "{bad}");
+ assert_eq!(lenient(&json!({"viewport_background": bad})).viewport_background, None, "{bad}");
+ }
+ }
+
/// The theme strip's Import… reads the theme file through `vfs`, like the other imports: in the
/// web app a picked file is kept there, where `std::fs` can't read it (#7643). On the disk it
/// is the same file; a folder scope active while it is read refuses a path outside it, which
diff --git a/crates/ui-egui/src/prefs/interface.rs b/crates/ui-egui/src/prefs/interface.rs
index 9b5b7f368a..c9d0572bcf 100644
--- a/crates/ui-egui/src/prefs/interface.rs
+++ b/crates/ui-egui/src/prefs/interface.rs
@@ -45,6 +45,18 @@ pub(super) fn interface(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) {
app.preferences.viewport_theme = *view;
}
});
+ // The view's background: the theme's gradient, or one colour of the user's (#7688).
+ let label = app.tr("prefs.viewport_background");
+ ui_kit::row(ui, label, |ui| {
+ let labels = [crate::i18n::tr("prefs.viewport_background_theme"), crate::i18n::tr("prefs.viewport_background_colour")];
+ let mut selected = usize::from(app.preferences.viewport_background.is_some());
+ if ui_kit::settings_segments(ui, label, &mut selected, &labels) {
+ app.preferences.viewport_background = (selected == 1).then_some([255, 255, 255]);
+ }
+ if let Some(colour) = &mut app.preferences.viewport_background {
+ ui_kit::color_field(ui, colour);
+ }
+ });
// 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/theme.rs b/crates/ui-egui/src/theme.rs
index 16e63738c6..dfa7bb3aea 100644
--- a/crates/ui-egui/src/theme.rs
+++ b/crates/ui-egui/src/theme.rs
@@ -31,6 +31,9 @@ thread_local! {
/// The 3D view's own dark or light when Preferences give it one ([`ViewportTheme`], #4146),
/// per thread like the rest. `None`: the view is as dark as the window.
static VIEWPORT_DARK: Cell<Option<bool>> = const { Cell::new(None) };
+ /// The 3D view's background colour when Preferences give it one (#7688), per thread like the
+ /// rest. `None`: the theme's gradient.
+ static VIEWPORT_BACKGROUND: Cell<Option<[u8; 3]>> = const { Cell::new(None) };
}
/// Three structural border levels plus independent content and control outlines.
@@ -362,6 +365,11 @@ pub fn viewport_dark() -> bool {
VIEWPORT_DARK.get().unwrap_or_else(is_dark)
}
+/// The 3D view's own background colour, or `None` for the theme's gradient (#7688).
+pub fn viewport_background() -> Option<[u8; 3]> {
+ VIEWPORT_BACKGROUND.get()
+}
+
/// The common conversion boundary for compiled icon palette data.
pub(crate) const fn icon_color(rgb: [u8; 3]) -> Color32 {
Color32::from_rgb(rgb[0], rgb[1], rgb[2])
@@ -879,14 +887,24 @@ impl Tokens {
/// mode being chosen before it is applied (#7152).
pub fn get_in(mode: ColorVision) -> Tokens {
let window = Tokens::themed(is_dark(), mode);
- if viewport_dark() == is_dark() { window } else { window.with_viewport(Tokens::themed(viewport_dark(), mode)) }
+ let tokens = if viewport_dark() == is_dark() { window } else { window.with_viewport(Tokens::themed(viewport_dark(), mode)) };
+ tokens.with_background(viewport_background())
}
/// The tokens of the 3D view's side (#4146), for a window colour drawn straight onto the view
/// with nothing behind it (a status line, a label or a warning beside the geometry), so it
/// reads on the view's background. As [`Tokens::get`] unless the view has its own side.
pub fn view() -> Tokens {
- Tokens::themed(viewport_dark(), vision())
+ Tokens::themed(viewport_dark(), vision()).with_background(viewport_background())
+ }
+
+ /// These tokens with the 3D view's background one flat colour (#7688), or as they are for `None`.
+ pub fn with_background(mut self, colour: Option<[u8; 3]>) -> Tokens {
+ if let Some([r, g, b]) = colour {
+ self.viewport_top = Color32::from_rgb(r, g, b);
+ self.viewport_bottom = self.viewport_top;
+ }
+ self
}
/// These tokens with the colours drawn in the 3D view taken from `view`: the background and
@@ -1021,6 +1039,11 @@ pub fn set_viewport_dark(dark: Option<bool>) {
VIEWPORT_DARK.set(dark);
}
+/// Give the 3D view a background colour of its own (#7688), or `None` for the theme's gradient.
+pub fn set_viewport_background(colour: Option<[u8; 3]>) {
+ VIEWPORT_BACKGROUND.set(colour);
+}
+
fn visuals(dark: bool) -> egui::Visuals {
let t = Tokens::themed(dark, vision());
let mut v = if dark { egui::Visuals::dark() } else { egui::Visuals::light() };