From c50f5364c63394022dd67ccf32ee7e2a173407ca Mon Sep 17 00:00:00 2001 From: Kai Evans Date: Sun, 25 Oct 2026 05:47:23 +0000 Subject: [PATCH] Selection fields: only the active one looks active (#5256) (#5259) * Selection fields: only the active one looks active * Selection fields: a filled field keeps its tinted border where a theme fixes the control border's colour --- crates/ui-egui/src/revolve_edit_tests.rs | 42 ++++++++++++++++++ crates/ui-egui/src/scenario.rs | 4 ++ crates/ui-egui/src/ui_kit/fields.rs | 29 ++++++++++-- crates/ui-egui/src/ui_kit/fields_tests.rs | 54 +++++++++++++++++++++++ docs/ui-kit.md | 2 +- 5 files changed, 127 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/revolve_edit_tests.rs b/crates/ui-egui/src/revolve_edit_tests.rs index bd589143e..4f34753a6 100644 --- a/crates/ui-egui/src/revolve_edit_tests.rs +++ b/crates/ui-egui/src/revolve_edit_tests.rs @@ -241,3 +241,45 @@ fn sketch_axis_cancel_restores_original_document_and_history() { assert_eq!(h.app.session.undo.len(), undo); assert!(matches!(&h.app.session.doc.find_feature("Turn").unwrap().kind,FeatureKind::Revolve{axis:AxisRef::SketchAxis{axis},..} if axis=="y")); } + +#[test] +fn only_the_active_selection_field_looks_active() { + let mut h = make(Harness::new(), json!("Y")); + open(&mut h); + let t = crate::theme::Tokens::get(); + let border = |h: &Harness, label: &str| { + let field = crate::scenario::rect_of(&format!("dialog:field:{label}")).unwrap(); + h.shapes + .iter() + .rev() + .find_map(|s| match &s.shape { + egui::Shape::Rect(r) if r.rect == field || r.rect == field.shrink(t.space_2) => Some(r.stroke.color), + _ => None, + }) + .unwrap() + }; + let click = |h: &mut Harness, handle: &str| { + let at = crate::scenario::rect_of(handle).map(|r| r.center()).or_else(|| crate::scenario::handle_at(handle)).unwrap(); + h.step(&json!({"click":[at.x, at.y]})).unwrap(); + h.frames(2); + }; + for label in ["Profile", "Axis"] { + assert_eq!(crate::scenario::count_of(&format!("dialog:selection:{label}:filled")), Some(1.0), "{label} holds its item"); + } + assert_eq!(h.app.dialog.as_ref().unwrap().active, 0); + assert_eq!((border(&h, "Profile") == t.form_primary, border(&h, "Axis") == t.form_primary), (true, false)); + click(&mut h, "dialog:field:Axis"); + assert_eq!(h.app.dialog.as_ref().unwrap().active, 1); + assert_eq!((border(&h, "Profile") == t.form_primary, border(&h, "Axis") == t.form_primary), (false, true)); + let filled = border(&h, "Profile"); + click(&mut h, "dialog:Angle"); + assert_eq!(h.app.dialog.as_ref().unwrap().active, 1, "picks still go to Axis while Angle is typed into"); + assert_eq!(border(&h, "Axis"), t.form_primary); + click(&mut h, "dialog:clear:Axis"); + click(&mut h, "dialog:field:Profile"); + assert_eq!(crate::scenario::count_of("dialog:selection:Axis:filled"), Some(0.0)); + assert_eq!((border(&h, "Profile"), border(&h, "Axis")), (t.form_primary, t.control_border)); + click(&mut h, "dialog:field:Axis"); + assert_eq!(border(&h, "Profile"), filled, "filled inactive"); + assert_ne!(filled, t.control_border, "filled inactive differs from empty inactive"); +} diff --git a/crates/ui-egui/src/scenario.rs b/crates/ui-egui/src/scenario.rs index 1f5fa1d8f..42ae52b14 100644 --- a/crates/ui-egui/src/scenario.rs +++ b/crates/ui-egui/src/scenario.rs @@ -146,6 +146,8 @@ pub struct Harness { /// The last frame's platform output (with its accessibility tree: dialog widgets are found /// by their text there). pub output: egui::PlatformOutput, + /// The last frame's painted shapes. + pub shapes: Vec, /// Files the scenario saves and opens live here; file pickers answer from it. pub dir: PathBuf, /// The file the next Open picker returns (a `choose_file` step). @@ -183,6 +185,7 @@ impl Harness { ctx }, output: Default::default(), + shapes: Vec::new(), dir: scratch_dir(), next_file: Rc::new(RefCell::new(None)), queue: None, @@ -237,6 +240,7 @@ impl Harness { // Nothing is painted: drop the frame's texture changes. out.textures_delta.clear(); self.output = out.platform_output; + self.shapes = out.shapes; } pub fn frames(&mut self, n: usize) { diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs index 9b7277b47..701febc61 100644 --- a/crates/ui-egui/src/ui_kit/fields.rs +++ b/crates/ui-egui/src/ui_kit/fields.rs @@ -287,6 +287,7 @@ pub(crate) struct Selection { pub filled: bool, } +/// Three looks: the active input in accent, a filled one in a quiet neutral tint, an empty one plain. pub(crate) fn selection_field(ui: &mut Ui, text: &str, options: Selection) -> Response { let Selection { active, filled } = options; let t = Tokens::get(); @@ -294,11 +295,22 @@ pub(crate) fn selection_field(ui: &mut Ui, text: &str, options: Selection) -> Re if active && !filled { ui.painter().rect_filled(rect, t.control_radius, t.selected_bg); } + let (fill, border) = match (active, filled) { + (true, true) => (t.selected_bg, t.form_primary), + (true, false) => (t.control_fill, t.form_primary), + (false, true) => (t.control_fill.lerp_to_gamma(t.form_ink, 0.06), t.control_border.lerp_to_gamma(t.form_ink, 0.35)), + (false, false) => (t.control_fill, t.control_border), + }; ui.painter().rect( if active && !filled { rect.shrink(t.space_2) } else { rect }, t.control_radius, - if filled { t.selected_bg } else { t.control_fill }, - if active || filled { egui::Stroke::new(1.0, t.form_primary) } else { t.control_border_stroke(t.control_border) }, + fill, + match (active, filled) { + (true, _) => egui::Stroke::new(1.0, border), + // A theme's control border can fix its own colour; a filled field keeps its tint. + (false, true) => egui::Stroke::new(t.control_border_stroke(border).width, border), + (false, false) => t.control_border_stroke(border), + }, egui::StrokeKind::Inside, ); let end = if filled { t.space_24 } else { t.space_8 }; @@ -306,7 +318,18 @@ pub(crate) fn selection_field(ui: &mut Ui, text: &str, options: Selection) -> Re rect.min + vec2(t.form_inset.min(rect.width()), 0.0), egui::pos2((rect.right() - end).max(rect.left() + t.form_inset.min(rect.width())), rect.bottom()), ); - paint_text(ui, text_rect, text, crate::fonts::medium(t.font_label), if filled { t.selected_ink } else { t.form_dim }, false); + paint_text( + ui, + text_rect, + text, + crate::fonts::medium(t.font_label), + match (active, filled) { + (true, true) => t.selected_ink, + (false, true) => t.form_ink, + _ => t.form_dim, + }, + false, + ); publish(ui, &response, egui::WidgetType::Button); scenario::publish_rect(&format!("dialog:field:{}", label(ui)), rect); scenario::publish_count(&format!("dialog:selection:{}:filled", label(ui)), f64::from(filled)); diff --git a/crates/ui-egui/src/ui_kit/fields_tests.rs b/crates/ui-egui/src/ui_kit/fields_tests.rs index a112b75a1..38c3f9d44 100644 --- a/crates/ui-egui/src/ui_kit/fields_tests.rs +++ b/crates/ui-egui/src/ui_kit/fields_tests.rs @@ -278,3 +278,57 @@ fn count_steps_stay_within_limits_and_keep_expressions() { assert_eq!(super::step_count("5", i64::MAX, &wide).as_deref(), Some("9223372036854775807"), "steps saturate rather than overflow"); assert_eq!(super::step_count("5", i64::MIN, &wide).as_deref(), Some("-9223372036854775803")); } + +#[test] +fn selection_field_has_three_distinct_looks_in_every_theme() { + type Look = (Vec<(egui::Color32, egui::Color32)>, Option); + let look = |active: bool, filled: bool| -> Look { + let ctx = crate::fonts::test_context(); + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { + ui.set_width(280.0); + scope(ui, |ui| { + row(ui, "Profiles", |ui| { + selection_field(ui, "1 selected", Selection { active, filled }); + }); + }); + }); + output.textures_delta.clear(); + let field = scenario::rect_of("dialog:field:Profiles").unwrap(); + let rects = output + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Rect(r) if field.contains_rect(r.rect) => Some((r.fill, r.stroke.color)), + _ => None, + }) + .collect(); + let ink = output.shapes.iter().find_map(|s| match &s.shape { + egui::Shape::Text(text) if text.galley.job.text == "1 selected" => Some(text.fallback_color), + _ => None, + }); + (rects, ink) + }; + let mut palettes: Vec<(String, Option, bool)> = vec![("dark".into(), None, true), ("light".into(), None, false)]; + for theme in crate::theme_files::builtin() { + let dark = theme.base.is_dark(); + palettes.push((theme.id.clone(), Some(crate::theme::Custom { dark, tokens: crate::theme_files::resolve(theme) }), dark)); + } + let previous = crate::theme::is_dark(); + for (name, custom, dark) in palettes { + crate::theme::set_dark(dark); + crate::theme::set_custom(custom); + let t = crate::theme::Tokens::get(); + let [active_filled, active_empty, filled, empty] = [(true, true), (true, false), (false, true), (false, false)].map(|(a, f)| look(a, f)); + let accented = |l: &Look| l.0.iter().any(|&(fill, stroke)| stroke == t.form_primary || fill == t.selected_bg); + assert!(accented(&active_filled) && accented(&active_empty), "{name}: the active input is accented"); + assert!(!accented(&filled), "{name}: a filled inactive input is not accented: {filled:?}"); + assert!(!accented(&empty), "{name}: an empty inactive input is not accented: {empty:?}"); + let outline = |l: &Look| l.0.last().copied().unwrap(); + assert_ne!(outline(&filled).0, outline(&empty).0, "{name}: filled and empty fills differ"); + assert_ne!(outline(&filled).1, outline(&empty).1, "{name}: filled and empty borders differ"); + assert_eq!(filled.1, Some(t.form_ink), "{name}: a filled input's count is in text colour"); + assert_eq!(empty.1, Some(t.form_dim), "{name}: an empty input's prompt is dim"); + } + crate::theme::set_custom(None); + crate::theme::set_dark(previous); +} diff --git a/docs/ui-kit.md b/docs/ui-kit.md index 5fe0ff40c..6509901b6 100644 --- a/docs/ui-kit.md +++ b/docs/ui-kit.md @@ -101,7 +101,7 @@ table. Panel/Window/Confirm compositions also have dedicated geometry and intera | `text_area` | Bounded multiline JSON/data editor | field tokens | | `dropdown`, `dropdown_menu` | Indexed choices or caller-supplied menu controls | `control_height`, `text_dim`, form tokens | | `searchable_dropdown`, `SearchOption` | Filtered choices, custom-value action and pending message | dropdown tokens | -| `selection_field`, `Selection` | Selection chip with explicit `active` and `filled` state | `selected_bg/ink`, `accent`, `control_*`, `form_inset`, `space_8/24` | +| `selection_field`, `Selection` | Selection chip with explicit `active` and `filled` state, in three looks: the active input (where the next pick goes) has a `form_primary` border and `selected_bg` tint; a filled inactive one a quiet neutral tint, a stronger neutral border and its count in `form_ink`; an empty inactive one the plain control with a dim prompt | `selected_bg/ink`, `form_primary/ink/dim`, `control_*`, `form_inset`, `space_2/8/24` | | `value_pair` | Paired captions/expressions; optional independent field names | row/field tokens, `space_8` | | `fraction_field`, `scalar_field` | Locale-aware bounded dimensionless values; scalar fields fill their control column | form control/font tokens | | `number_field` | Integer/decimal field with caller range, drag speed and suffix | form control/font tokens |