Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

refactor(ui): unify plain text inputs on one text_field widget (#3789) (#3896)

Scott C Hughes19e51cf0adeb
diff --git a/crates/ui-egui/src/brush_picker.rs b/crates/ui-egui/src/brush_picker.rs
index 8a7ec9d47..3cade4981 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 59b3ba6df..b6d96f6bd 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 dc57de68b..cfa7183a5 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<String, Value>, mode: &str, now: &Compo
}
let typing = if ui.memory(|m| m.has_focus(id)) { ui.data(|d| d.get_temp::<String>(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 5fc1065a4..5b15e7bc9 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::<String>(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<String, Value>, system: Option<egui
// The dialog follows the language being edited, so a change shows before OK.
let lang = crate::i18n::Lang::from_pref(values.pointer("/interface/language").and_then(Value::as_str).unwrap_or("auto"));
let mut search = f.get("__search").and_then(Value::as_str).unwrap_or("").to_string();
- ui.add(egui::TextEdit::singleline(&mut search).hint_text(tl!("Search settings…")).desired_width(f32::INFINITY));
+ crate::widgets::text_field(ui, ui.id().with("prefs-search"), &mut search, tl!("Search settings…"), ui.available_width(), false);
let found = preference_sections(&values, &search);
if !found.iter().any(|(id, _)| *id == section)
&& let Some((first, _)) = found.first()
diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs
index 259e5e006..0719c5cec 100644
--- a/crates/ui-egui/src/theme.rs
+++ b/crates/ui-egui/src/theme.rs
@@ -702,6 +702,15 @@ impl Tokens {
self.histogram_bg
}
+ /// The background behind selected text in a text field (#3789): a subtle shade rather than
+ /// egui's default blue. The monochrome Pro themes use a grey so a selection doesn't read as a
+ /// second accent; coloured themes keep their own accent. Scoped to text fields by
+ /// [`crate::widgets::text_field`], because `Visuals::selection` also fills selected list rows
+ /// and slider trails, which must keep the theme's.
+ pub fn field_selection(&self) -> 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 876ed82e7..8ec6f94a5 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<usize>) -> 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<f32>) -> Harness<'static, (f32, u32)> {
let mut h = Harness::new_ui_state(