SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Preferences: share picture choices without conflicting with panel redesigns (#10937)
Nathan Dane1094503e9748
diff --git a/crates/ui-egui/src/prefs_search.rs b/crates/ui-egui/src/prefs_search.rsindex ec2cf7e9aa..feb62187bd 100644--- a/crates/ui-egui/src/prefs_search.rs+++ b/crates/ui-egui/src/prefs_search.rs@@ -441,6 +441,16 @@ pub(crate) fn note_current(ui: &Ui, text: &str) {});}+/// Keep per-option aliases searchable when a compound row learns its choices.+pub(crate) fn note_choices(ui: &Ui, choices: &[&str]) {+ let mut text = choices.join(" ");+ if texts_hit(ui, choices) {+ text.push(' ');+ text.push_str(&query(ui));+ }+ note_current(ui, &text);+}+/// After a hidden run, show the control on the next pass if its value now matches.pub(crate) fn recheck(ui: &Ui, texts: &[&str]) {if wanted(ui, texts) {@@ -906,6 +916,99 @@ mod tests {/// The search field and its empty result name the window they search (#8318): "Search/// settings" under a "Preferences" title read as two names for one window.+ #[test]+ fn shared_standalone_cards_and_chips_filter_index_and_reveal_their_bounds() {+ use crate::ui_kit::{self, Card};+ for kind in 0..4 {+ let ctx = crate::fonts::test_context();+ let mut target = Rect::NOTHING;+ let mut miss = Rect::NOTHING;+ let mut draw = |filter: &str| {+ crate::test_support::frame(&ctx, Default::default(), |ui| {+ let _search = begin(ui, filter);+ begin_page(ui, crate::prefs::PAGE_LAYOUT, "Layout");+ for label in ["Performance", "Quality"] {+ let card =+ Card { label, sub: None, selected: false, enabled: false, options: None, check: false, aspect: 1.0, figure: &|_, _| {} };+ let response = match kind {+ 0 => ui_kit::picture_card(ui, &card, 100.0),+ 1 => ui_kit::toggle_tile(ui, &card, 100.0),+ 2 => ui_kit::preset_card(ui, label, "", false, false, &|_, _, _| {}, 100.0),+ _ => ui_kit::settings_chip(ui, label, Default::default()),+ };+ assert!(!response.clicked());+ if label == "Quality" {+ target = response.rect;+ } else {+ miss = response.rect;+ }+ }+ store_nav(ui);+ });+ };+ draw("Quality");+ assert!(!miss.is_positive(), "non-matching control {kind} has no layout");+ assert!(target.is_positive());+ let (_, _, hits) = nav(&ctx);+ assert_eq!(hits.len(), 1, "control {kind} owns one real keyboard target");+ assert_eq!((hits[0].page, hits[0].label.as_str()), (crate::prefs::PAGE_LAYOUT, "Quality"));+ set_reveal(&ctx, Some(Reveal { page: crate::prefs::PAGE_LAYOUT, label: "Quality".into(), until: f64::MAX }));+ crate::scenario::clear_handles();+ // The search is cleared before the selected setting is revealed.+ crate::test_support::frame(&ctx, Default::default(), |ui| {+ let _search = begin(ui, "");+ begin_page(ui, crate::prefs::PAGE_LAYOUT, "Layout");+ let card = Card {+ label: "Quality",+ sub: None,+ selected: false,+ enabled: false,+ options: None,+ check: false,+ aspect: 1.0,+ figure: &|_, _| {},+ };+ let response = match kind {+ 0 => ui_kit::picture_card(ui, &card, 100.0),+ 1 => ui_kit::toggle_tile(ui, &card, 100.0),+ 2 => ui_kit::preset_card(ui, "Quality", "", false, false, &|_, _, _| {}, 100.0),+ _ => ui_kit::settings_chip(ui, "Quality", Default::default()),+ };+ assert_eq!(crate::scenario::handle_at("prefs:revealed"), Some(response.rect.center()));+ });+ }+ }++ #[test]+ fn shared_picture_choices_index_the_row_or_option_once_and_learn_aliases() {+ use crate::ui_kit::{self, PictureChoice};+ let choices =+ [PictureChoice { key: "on", label: "Anti-aliasing", figure: |_, _| {} }, PictureChoice { key: "off", label: "Off", figure: |_, _| {} }];+ for (query, expected) in [("Sampling", "Sampling"), ("Anti-aliasing", "Anti-aliasing"), ("msaa", "Anti-aliasing")] {+ let ctx = crate::fonts::test_context();+ for _ in 0..4 {+ crate::test_support::frame(&ctx, Default::default(), |ui| {+ let _search = begin(ui, query);+ begin_page(ui, crate::prefs::PAGE_GRAPHICS, "Graphics");+ assert_eq!(ui_kit::settings_picture_choices(ui, "shared", "Sampling", &choices, 0, true), None);+ store_nav(ui);+ });+ }+ let (_, _, hits) = nav(&ctx);+ assert_eq!(hits.len(), 1, "{query} has one target");+ assert_eq!(hits[0].label, expected);+ assert_eq!(hits[0].page, crate::prefs::PAGE_GRAPHICS);+ set_reveal(&ctx, Some(Reveal { page: crate::prefs::PAGE_GRAPHICS, label: expected.into(), until: f64::MAX }));+ crate::test_support::frame(&ctx, Default::default(), |ui| {+ let _search = begin(ui, "");+ begin_page(ui, crate::prefs::PAGE_GRAPHICS, "Graphics");+ ui_kit::settings_picture_choices(ui, "shared", "Sampling", &choices, 0, true);+ let rect = crate::scenario::rect_of(if expected == "Sampling" { "dialog:row:Sampling" } else { "prefs:shared:on" }).unwrap();+ assert_eq!(crate::scenario::handle_at("prefs:revealed"), Some(rect.center()));+ });+ }+ }+#[test]fn the_search_field_names_the_window_it_searches() {let name = crate::i18n::tr("ui.preferences").to_lowercase();diff --git a/crates/ui-egui/src/ui_kit/gallery.rs b/crates/ui-egui/src/ui_kit/gallery.rsindex a4e6ce7a90..3dbf0610fe 100644--- a/crates/ui-egui/src/ui_kit/gallery.rs+++ b/crates/ui-egui/src/ui_kit/gallery.rs@@ -524,6 +524,34 @@ const ELEMENTS: &[Element] = &[)});}),+ element!("settings_picture_choices", |ui, state| {+ let choices = [+ PictureChoice { key: "bottom", label: "Bottom", figure: super::preferences_figure_data::paint_history_bottom },+ PictureChoice { key: "left", label: "Left", figure: super::preferences_figure_data::paint_history_left },+ ];+ if let Some(selected) = settings_picture_choices(ui, "gallery_history", "History", &choices, state.selected % choices.len(), true) {+ state.selected = selected;+ }+ settings_picture_choices(ui, "gallery_history_disabled", "Disabled history", &choices, 0, false);+ }),+ element!("settings_picture_choices_sized", |ui, state| {+ let choices = [+ PictureChoice { key: "origin", label: "Origin", figure: super::preferences_figure_data::paint_orbit_origin },+ PictureChoice { key: "centre", label: "View center", figure: super::preferences_figure_data::paint_orbit_centre },+ ];+ let t = Tokens::get();+ if let Some(selected) = settings_picture_choices_sized(+ ui,+ "gallery_pivots",+ "Orbit around",+ &choices,+ state.selected % choices.len(),+ true,+ PictureChoiceSize { width: t.space_24 * 8.0 + t.space_4, aspect: 184.0 / 99.5, caption: false },+ ) {+ state.selected = selected;+ }+ }),element!("settings_permission_choices", |ui, state| {settings_choices(ui,diff --git a/crates/ui-egui/src/ui_kit/layout.rs b/crates/ui-egui/src/ui_kit/layout.rsindex 4f87b1800d..6413ca4780 100644--- a/crates/ui-egui/src/ui_kit/layout.rs+++ b/crates/ui-egui/src/ui_kit/layout.rs@@ -461,6 +461,7 @@ pub(crate) fn figure_rows(enter}+#[allow(dead_code, reason = "composed by the independent Preferences panels of #9863")]/// Memory belongs to the enclosing dialog ID; animation is avoided so each row keeps its fixed height./// Persisted disclosure with a rule and uppercase title. Tokens: `control_height, space_2, radius_sm, hover, dialog_rule, form_section_icon_size, form_gap_y, font_caption, font_section_tracking, text_dim`./// Static section caption for a group that stays expanded.diff --git a/crates/ui-egui/src/ui_kit/layout_tests.rs b/crates/ui-egui/src/ui_kit/layout_tests.rsindex e239666a28..0d7abaf355 100644--- a/crates/ui-egui/src/ui_kit/layout_tests.rs+++ b/crates/ui-egui/src/ui_kit/layout_tests.rs@@ -70,6 +70,87 @@ fn carousel_window_clamps_offsets_and_keeps_a_card_on_narrow_pages() {assert_eq!(carousel_window(6, 99, 100.0, 88.0, 8.0, 26.0), (5, 1));}+#[test]+fn shared_picture_choices_wrap_inside_the_row_and_reserve_their_height() {+ let choices = [+ PictureChoice { key: "first", label: "First", figure: |_, _| {} },+ PictureChoice { key: "second", label: "Second", figure: |_, _| {} },+ PictureChoice { key: "third", label: "Third", figure: |_, _| {} },+ ];+ for dark in [false, true] {+ let _theme = crate::theme::scoped_tokens(if dark { Tokens::dark() } else { Tokens::light() });+ for width in [120.0, 260.0, 600.0] {+ for sized in [false, true] {+ let ctx = crate::fonts::test_context();+ crate::test_support::frame(+ &ctx,+ egui::RawInput {+ screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 1000.0))),+ ..Default::default()+ },+ |ui| {+ ui.set_width(width);+ scope(ui, |ui| {+ let picked = if sized {+ settings_picture_choices_sized(+ ui,+ "shared",+ "Choices",+ &choices,+ usize::MAX,+ true,+ PictureChoiceSize { width: 110.0, aspect: 1.0, caption: false },+ )+ } else {+ settings_picture_choices(ui, "shared", "Choices", &choices, usize::MAX, true)+ };+ assert_eq!(picked, None, "drawing does not change an unknown selection");+ let row_rect = scenario::rect_of("dialog:row:Choices").unwrap();+ for choice in &choices {+ let rect = scenario::rect_of(&format!("prefs:shared:{}", choice.key)).unwrap();+ assert!(row_rect.expand(0.5).contains_rect(rect), "width={width}, sized={sized}: {rect:?} outside {row_rect:?}");+ }+ let first = scenario::rect_of("prefs:shared:first").unwrap();+ let last = scenario::rect_of("prefs:shared:third").unwrap();+ assert_eq!(last.top() > first.top(), width < 600.0);+ let next = ui.label("Next").rect;+ assert!(next.top() >= last.bottom());+ });+ },+ );+ }+ }+ }+}++#[test]+fn shared_picture_choices_return_clicks_and_respect_both_disabled_states() {+ use std::cell::Cell;+ let choices =+ [PictureChoice { key: "first", label: "First", figure: |_, _| {} }, PictureChoice { key: "second", label: "Second", figure: |_, _| {} }];+ for (enabled, parent_enabled) in [(true, true), (false, true), (true, false)] {+ let ctx = crate::fonts::test_context();+ ctx.enable_accesskit();+ let picked = Cell::new(None);+ let draw = |ui: &mut Ui| {+ ui.add_enabled_ui(parent_enabled, |ui| {+ picked.set(settings_picture_choices(ui, "shared", "Choices", &choices, 0, enabled));+ });+ };+ frame(&ctx, vec![], draw);+ let at = scenario::handle_at("prefs:shared:second").unwrap();+ frame(&ctx, pointer(at, true), draw);+ frame(&ctx, pointer(at, false), draw);+ assert_eq!(picked.get(), (enabled && parent_enabled).then_some(1));+ picked.set(None);+ frame(&ctx, key(egui::Key::Tab), draw);+ frame(&ctx, key(egui::Key::Space), draw);+ if !enabled || !parent_enabled {+ assert_eq!(picked.get(), None);+ }+ }+}+#[test]fn theme_carousel_moves_one_card_and_disables_its_ends_without_selecting() {let ctx = crate::fonts::test_context();diff --git a/crates/ui-egui/src/ui_kit/preferences_cards.rs b/crates/ui-egui/src/ui_kit/preferences_cards.rsindex e803944755..2d594bbfc4 100644--- a/crates/ui-egui/src/ui_kit/preferences_cards.rs+++ b/crates/ui-egui/src/ui_kit/preferences_cards.rs@@ -203,11 +203,114 @@ fn tick(p: &Painter, r: Rect, ink: Color32) {/// A radio card: a figure, a label and an optional sub-label. Click it to choose it.pub(crate) fn picture_card(ui: &mut Ui, card: &Card, width: f32) -> Response {+ crate::prefs_search::note_unlabelled(ui, &[card.sub.unwrap_or("")]);paint_card(ui, card, width, false)}+/// A real radio choice backed by the canonical preference figure painter.+pub(crate) struct PictureChoice<'a> {+ pub key: &'a str,+ pub label: &'a str,+ pub figure: fn(&Painter, Rect),+}++impl PictureChoice<'_> {+ fn card(&self, selected: bool, enabled: bool, aspect: f32) -> Card<'_> {+ Card { label: self.label, sub: None, selected, enabled, options: None, check: selected, aspect, figure: &self.figure }+ }+}++/// Indexed, wrapping picture choices. The row and every option point at the shipped cards.+pub(crate) fn settings_picture_choices(+ ui: &mut Ui,+ key: &str,+ label: &str,+ options: &[PictureChoice<'_>],+ selected: usize,+ enabled: bool,+) -> Option<usize> {+ let t = Tokens::get();+ settings_picture_choices_sized(+ ui,+ key,+ label,+ options,+ selected,+ enabled,+ PictureChoiceSize { width: t.space_24 * 4.0 + t.space_16 + t.space_4, aspect: 226.0 / 148.0, caption: true },+ )+}++/// Page-specific card proportions; the ordinary History/Browser composition keeps its defaults.+pub(crate) struct PictureChoiceSize {+ pub width: f32,+ pub aspect: f32,+ /// Omit a repeated caption when the enclosing section already names the setting.+ pub caption: bool,+}++pub(crate) fn settings_picture_choices_sized(+ ui: &mut Ui,+ key: &str,+ label: &str,+ options: &[PictureChoice<'_>],+ selected: usize,+ enabled: bool,+ size: PictureChoiceSize,+) -> Option<usize> {+ let t = Tokens::get();+ let available = dimension(ui.available_width());+ let width = options.iter().map(|o| super::ContentWidth::name(ui, o.label) + t.space_12).fold(size.width, f32::max).min(available);+ let ppp = ui.ctx().pixels_per_point().max(0.5);+ let width = (width * ppp).floor() / ppp;+ let columns = (((available + t.space_10) / (width + t.space_10).max(1.0)).floor() as usize).max(1);+ let rows = options.len().div_ceil(columns);+ let height = options.first().map_or(0.0, |o| card_height(&t, width, &o.card(selected == 0, enabled, size.aspect))) * rows as f32+ + rows.saturating_sub(1) as f32 * t.space_10+ + if size.caption { t.space_16 + t.space_10 } else { 0.0 };+ super::settings_form(ui, 0.0, |ui| {+ super::layout::sized_row(ui, label, height, |ui| {+ crate::prefs_search::note_choices(ui, &options.iter().map(|o| o.label).collect::<Vec<_>>());+ ui.vertical(|ui| {+ ui.spacing_mut().item_spacing = vec2(t.space_10, t.space_10);+ // Use the real control child, including the scrollbar and form bounds. Round+ // down so the final card border fits together with every intervening gap.+ let available = dimension(ui.available_width());+ let width = (width.min(available) * ppp).floor() / ppp;+ let columns = (((available + t.space_10) / (width + t.space_10).max(1.0)).floor() as usize).max(1);+ if size.caption {+ super::settings_inline_note(ui, label);+ }+ let mut picked = None;+ let mut indexed = crate::prefs_search::texts_hit(ui, &[label]);+ for (row, choices) in options.chunks(columns).enumerate() {+ ui.horizontal_top(|ui| {+ for (column, option) in choices.iter().enumerate() {+ let index = row * columns + column;+ let response = picture_card(ui, &option.card(selected == index, enabled, size.aspect), width);+ scenario::publish_target(&format!("prefs:{key}:{}", option.key), response.rect);+ scenario::publish_target(&format!("dialog:{label}:{}", option.label), response.rect);+ if !indexed && crate::prefs_search::texts_hit(ui, &[option.label]) {+ crate::prefs_search::remember(ui, option.label, response.rect);+ indexed = true;+ }+ crate::prefs_search::follow(ui, option.label, response.rect);+ if response.clicked() && enabled {+ picked = Some(index);+ }+ }+ });+ }+ picked+ })+ .inner+ })+ })+}+/// A toggle tile: the same picture with a checkbox in the corner. Click it to flip `card.selected`.pub(crate) fn toggle_tile(ui: &mut Ui, card: &Card, width: f32) -> Response {+ crate::prefs_search::note_unlabelled(ui, &[card.sub.unwrap_or("")]);paint_card(ui, card, width, true)}@@ -231,8 +334,10 @@ pub(crate) fn preset_card(let width = dimension(width);let height = t.space_24 * 3.0 - t.space_2;let (rect, response) = ui.allocate_exact_size(vec2(width, height), if enabled { Sense::click() } else { Sense::hover() });- crate::prefs_search::remember(ui, name, rect);- crate::prefs_search::follow(ui, name, rect);+ if super::layout::label(ui).is_empty() {+ crate::prefs_search::remember(ui, name, rect);+ crate::prefs_search::follow(ui, name, rect);+ }let look = card_look(selected, enabled, response.hovered(), response.has_focus());let dim = |c: Color32| c.gamma_multiply(look.alpha);let p = ui.painter();diff --git a/crates/ui-egui/src/ui_kit/preferences_small.rs b/crates/ui-egui/src/ui_kit/preferences_small.rsindex f533071d32..45b189b340 100644--- a/crates/ui-egui/src/ui_kit/preferences_small.rs+++ b/crates/ui-egui/src/ui_kit/preferences_small.rs@@ -140,6 +140,10 @@ pub(crate) struct SettingsChip<'a> {/// Bounded informational pill; an optional removal target lives inside the same outline.pub(crate) fn settings_chip(ui: &mut Ui, label: &str, chip: SettingsChip<'_>) -> egui::Response {+ crate::prefs_search::note_unlabelled(ui, &[label]);+ if crate::prefs_search::querying(ui) && !crate::prefs_search::visible(ui, &[label]) {+ return crate::prefs_search::hidden_response(ui);+ }let t = Tokens::get();let ink = if chip.accent { t.selected_ink } else { t.form_dim };let font = if chip.accent {@@ -155,6 +159,10 @@ pub(crate) fn settings_chip(ui: &mut Ui, label: &str, chip: SettingsChip<'_>) ->let natural = ui.painter().layout_no_wrap(label.to_owned(), font.clone(), ink).size().x + t.space_16 + icon_width + remove_width;let height = if chip.compact { t.space_24 - t.space_2 } else { t.space_24 };let (rect, response) = ui.allocate_exact_size(vec2(natural.min(super::metrics::dimension(ui.max_rect().width())), height), Sense::hover());+ if super::layout::label(ui).is_empty() {+ crate::prefs_search::remember(ui, label, rect);+ crate::prefs_search::follow(ui, label, rect);+ }ui.painter().rect(rect,t.radius_xl,diff --git a/docs/ui-kit.md b/docs/ui-kit.mdindex b15154f7b4..353f023af7 100644--- a/docs/ui-kit.md+++ b/docs/ui-kit.md@@ -883,6 +883,7 @@ these shared pieces; the pages themselves arrive in later steps. Sizes follow| --- | --- | --- || `picture_card(ui, &Card, width)` | A radio card: a figure, a label, an optional sub-label, an optional "has options" disc (its text is the hover) and an optional accent check. The figure keeps its aspect ratio and fills the card's inside width; the card sizes to the `width` its container gives it (`card_width(available, columns, gap)` divides a row). Selected: 2 pt `form_primary` border, `selected_bg` fill, `selected_ink` semibold label. Disabled: dimmed, no hover, no click, no focus ring. Keyboard focus draws a ring outside the border. Reports `RadioButton` to accessibility. | `form_primary`, `selected_bg`, `selected_ink`, `hover`, `control_fill`, `dialog_border`, `form_ink`, `form_dim`, `on_accent`, `radius_lg`, `space_*`, `font_label/small` || `toggle_tile(ui, &Card, width)` | The same picture with a 15 pt checkbox in the top-right corner (`Card::selected` is "on"). Reports `Checkbox`. | as above, `control_border` |+| `settings_picture_choices`, `settings_picture_choices_sized`, `PictureChoice`, `PictureChoiceSize` | A labelled, wrapping radio group with stable option handles and one search result for the row or matching choice. Returns the picked index. The sized variant accepts the card width, figure aspect and caption visibility used by independent Preferences pages. | card tokens, `space_10/12/16/24` || `preset_card(ui, name, word, selected, enabled, &icon, width)` | A 70 pt card with a 38 pt icon box (`icon` paints into it in the ink it is given), a name, one word and an accent check. Used for Performance, Quality and Custom. | as above, `pref_well` || `help_icon(ui, text)` | The (i) after a label. Shows `text` as a tooltip and exposes it as the widget's label. | `form_dim`, `form_ink` || `shortcut_binding(ui, chord, editable)` | Complete mono keycaps using the command-search painter; one focusable recorder target per binding, or a read-only label. Empty chords show dim Not assigned. `shortcut_binding_width` measures the same font and padding, including literal plus keys. Gallery: `shortcut_binding`. | `font_small/label`, `control_height`, `form_dim`, `form_primary`, `space_4/8/10`, `radius_sm` |@@ -891,6 +892,10 @@ these shared pieces; the pages themselves arrive in later steps. Sizes follow`card_look(selected, enabled, hovered, focused)` holds the state rules (border width, fill, ink, weight,dimming, focus ring) apart from the painting so they can be tested.+Standalone picture cards, toggle tiles, preset cards and settings chips participate in Preferences+search and reveal their own bounds. Labelled rows retain ownership of their search result.+`section_heading` is available to production callers for groups that stay expanded.+### Figures`ui_kit/preferences_figures.rs` paints the illustrations of the Preferences boards. The geometry of