From 19e51cf0adeb6bf88bdf56ca6922ad61fe0f3ca6 Mon Sep 17 00:00:00 2001 From: Scott C Hughes <1591011+dottedt@users.noreply.github.com> Date: Mon, 12 Oct 2026 05:04:23 -0500 Subject: [PATCH] refactor(ui): unify plain text inputs on one text_field widget (#3789) (#3896) Plain text fields (Preferences search and hex, the Color Picker hex, and the brush search fields) each drew egui's default TextEdit frame, so they differed in padding and border and showed egui's white focus ring (its selection.stroke). Add widgets::text_field, which draws the same box as a value_field (field fill, field_border, radius_sm) with frame NONE, a consistent inner padding, no focus outline, and a scoped, non-blue text-selection colour (grey in the monochrome Pro themes, the theme accent elsewhere). Migrate those fields to it; numeric fields already use value_field. Tests: cargo fmt --all -- --check; cargo test -p photocraft-ui-egui --lib; cargo clippy -p photocraft-ui-egui --all-targets -- -A clippy::nonminimal_bool -A clippy::collapsible_match -D warnings; cargo xtask wasm --- crates/ui-egui/src/brush_picker.rs | 7 +-- crates/ui-egui/src/brushes_tab.rs | 2 +- crates/ui-egui/src/color_picker_ui.rs | 2 +- crates/ui-egui/src/prefs_ui.rs | 4 +- crates/ui-egui/src/theme.rs | 19 ++++++++ crates/ui-egui/src/widgets.rs | 64 +++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/brush_picker.rs b/crates/ui-egui/src/brush_picker.rs index 8a7ec9d4759..3cade4981c5 100644 --- a/crates/ui-egui/src/brush_picker.rs +++ b/crates/ui-egui/src/brush_picker.rs @@ -262,12 +262,7 @@ pub fn body( ui.horizontal(|ui| { icons::paint(ui, egui::Rect::from_min_size(ui.cursor().min + vec2(0.0, 3.0), vec2(16.0, 16.0)), "search", 14.0, t.text_faint); ui.add_space(20.0); - ui.add( - egui::TextEdit::singleline(&mut st.filter) - .hint_text(tl!("Search Brushes")) - .desired_width((size.x - 118.0).max(60.0)) - .id_salt("brush-picker-search"), - ); + widgets::text_field(ui, ui.id().with("brush-picker-search"), &mut st.filter, tl!("Search Brushes"), (size.x - 118.0).max(60.0), false); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { gear_menu(ui, current, presets, st, &mut picks); let new_tip = "Create new brush preset from the current settings"; diff --git a/crates/ui-egui/src/brushes_tab.rs b/crates/ui-egui/src/brushes_tab.rs index 59b3ba6df5d..b6d96f6bd26 100644 --- a/crates/ui-egui/src/brushes_tab.rs +++ b/crates/ui-egui/src/brushes_tab.rs @@ -892,7 +892,7 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) { ui.horizontal(|ui| { icons::paint(ui, egui::Rect::from_min_size(ui.cursor().min + vec2(0.0, 3.0), vec2(16.0, 16.0)), "search", 14.0, t.text_faint); ui.add_space(20.0); - ui.add(egui::TextEdit::singleline(&mut app.ui.brushes_panel.filter).hint_text(tl!("Search Brushes")).desired_width(WIDTH - 120.0)); + widgets::text_field(ui, ui.id().with("brush-search"), &mut app.ui.brushes_panel.filter, tl!("Search Brushes"), WIDTH - 120.0, false); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { let view = &mut app.ui.brushes_panel.view; let grid_tip = "Grid view"; diff --git a/crates/ui-egui/src/color_picker_ui.rs b/crates/ui-egui/src/color_picker_ui.rs index dc57de68b40..cfa7183a59e 100644 --- a/crates/ui-egui/src/color_picker_ui.rs +++ b/crates/ui-egui/src/color_picker_ui.rs @@ -638,7 +638,7 @@ fn fields(ui: &mut egui::Ui, f: &mut Map, mode: &str, now: &Compo } let typing = if ui.memory(|m| m.has_focus(id)) { ui.data(|d| d.get_temp::(id)) } else { None }; let mut h = typing.unwrap_or_else(|| hex(rgb).trim_start_matches('#').to_string()); - let r = ui.add(egui::TextEdit::singleline(&mut h).id(id).char_limit(6).desired_width(64.0).font(crate::theme::mono(12.0))); + let r = crate::widgets::text_field_ex(ui, id, &mut h, "", 64.0, true, Some(6)); if r.gained_focus() { crate::field_tab::select_all(ui.ctx(), id, &h); } diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs index 5fc1065a4c7..5b15e7bc9cd 100644 --- a/crates/ui-egui/src/prefs_ui.rs +++ b/crates/ui-egui/src/prefs_ui.rs @@ -1082,7 +1082,7 @@ fn hex_field(ui: &mut egui::Ui, key: &str, rgb: &mut [u8; 3]) { let id = egui::Id::new(("pref-hex", key)); let shown = format!("#{:02x}{:02x}{:02x}", rgb[0], rgb[1], rgb[2]); let mut text = ui.data(|d| d.get_temp::(id)).unwrap_or_else(|| shown.clone()); - let r = ui.add(egui::TextEdit::singleline(&mut text).font(crate::theme::mono(11.5)).desired_width(72.0).char_limit(7)); + let r = crate::widgets::text_field_ex(ui, id, &mut text, "", 72.0, true, Some(7)); if r.changed() && let Some(c) = prefs::parse_hex(&text) { @@ -1130,7 +1130,7 @@ fn prefs_body(ui: &mut egui::Ui, f: &mut Map, system: Option Color32 { + if self.pro { Color32::from_gray(if self.dark() { 96 } else { 201 }) } else { self.accent_soft } + } + pub fn dark(&self) -> bool { matches!( self.kind, @@ -1066,6 +1075,16 @@ mod tests { assert_eq!(ThemeKind::from_name("lightest"), Some(ThemeKind::ProLight)); } + #[test] + fn field_selection_is_grey_only_for_the_monochrome_pro_themes() { + // #3789: a text field's selection is a subtle grey in the monochrome Pro themes, not a + // second blue accent; coloured themes keep their accent. + assert_eq!(Tokens::for_kind(ThemeKind::Pro).field_selection(), Color32::from_gray(96)); + assert_eq!(Tokens::for_kind(ThemeKind::ProLight).field_selection(), Color32::from_gray(201)); + assert_eq!(Tokens::for_kind(ThemeKind::Studio).field_selection(), Tokens::for_kind(ThemeKind::Studio).accent_soft); + assert_eq!(Tokens::for_kind(ThemeKind::Classic).field_selection(), Tokens::for_kind(ThemeKind::Classic).accent_soft); + } + #[test] fn theme_names_parse() { assert_eq!(ThemeKind::from_name("Classic"), Some(ThemeKind::Classic)); diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 876ed82e7fc..8ec6f94a50b 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -661,6 +661,45 @@ pub fn popup_value_field(ui: &mut Ui, name: &str, value: &mut f32, range: std::o out } +/// Height of a [`text_field`]'s box, matching a [`value_field`]. +pub const TEXT_FIELD_H: f32 = 24.0; + +/// Inner padding of a [`text_field`]'s box: 6 pt either side, 2 pt above and below. +const TEXT_FIELD_PAD: Vec2 = vec2(6.0, 2.0); + +/// A single-line text field in PhotoCraft's shared field style (#3789): the same box as a +/// [`value_field`] -- the theme's field fill and `field_border`, rounded by `radius_sm` -- with a +/// consistent inner padding and no focus outline. egui's default white focus ring (its +/// `selection.stroke`) is what made the Preferences search field look different from the Color +/// Picker's hex field, so every plain, non-numeric input (search, hex, name, filter) is drawn +/// through here to look and behave the same. `id` carries the field's text and focus across +/// frames; `hint` shows while it is empty; `mono` picks the monospaced face for hex and other +/// code-like values. +pub fn text_field(ui: &mut Ui, id: egui::Id, text: &mut String, hint: &str, width: f32, mono: bool) -> Response { + text_field_ex(ui, id, text, hint, width, mono, None) +} + +/// [`text_field`] limited to `char_limit` characters (a hex field's six digits, say). +pub fn text_field_ex(ui: &mut Ui, id: egui::Id, text: &mut String, hint: &str, width: f32, mono: bool, char_limit: Option) -> Response { + let t = Tokens::get(ui.ctx()); + let (rect, _) = ui.allocate_exact_size(vec2(width.max(16.0), TEXT_FIELD_H), Sense::hover()); + surface(ui, rect, t.field, false); + if !t.bevel { + ui.painter().rect_stroke(rect, t.radius_sm, Stroke::new(1.0, t.field_border), StrokeKind::Inside); + } + let font = if mono { theme::mono(12.0) } else { egui::FontId::proportional(12.5) }; + let mut edit = egui::TextEdit::singleline(text).id(id).hint_text(hint).frame(egui::Frame::NONE).font(font); + if let Some(limit) = char_limit { + edit = edit.char_limit(limit); + } + // A child Ui keeps the field's selection colour local: `Visuals::selection` also fills selected + // list rows and slider trails, which must keep the theme's. + let layout = egui::Layout::centered_and_justified(egui::Direction::TopDown); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(rect.shrink2(TEXT_FIELD_PAD)).layout(layout)); + child.style_mut().visuals.selection.bg_fill = t.field_selection(); + child.add(edit) +} + fn first_field_id() -> egui::Id { egui::Id::new("pc-focus-first-field") } @@ -1653,6 +1692,31 @@ mod tests { } use egui_kittest::{Harness, kittest::Queryable}; + /// #3789: a plain text field is one boxed, outline-free input whose text-selection colour stays + /// local, so it isn't recoloured for the rest of the frame (selected rows, slider trails). + #[test] + fn text_field_is_a_boxed_input_whose_selection_colour_stays_local() { + use egui::accesskit::Role; + let mut h = Harness::new_ui_state( + |ui, s: &mut (String, egui::Color32, egui::Color32)| { + s.1 = ui.visuals().selection.bg_fill; + super::text_field(ui, egui::Id::new("tf"), &mut s.0, "hint", 140.0, false); + s.2 = ui.visuals().selection.bg_fill; + }, + (String::new(), egui::Color32::WHITE, egui::Color32::BLACK), + ); + crate::PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio); + h.run(); + assert_eq!(h.state().1, h.state().2, "the field's selection colour does not leak into its parent"); + h.get_by_role(Role::TextInput).click(); + h.run_steps(1); + for ch in "abc".chars() { + h.event(egui::Event::Text(ch.to_string())); + h.run_steps(1); + } + assert_eq!(h.state().0, "abc", "typing reaches the bound string"); + } + /// Sets up a test with one value field. Its state is its value and how many times it changed. fn field(value: f32, range: std::ops::RangeInclusive) -> Harness<'static, (f32, u32)> { let mut h = Harness::new_ui_state(