Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-BEHAV-TEXTFIELD: the Info panel's metadata fields are shared text fields

Nathaniel Ledfordaa57db05a15e
diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rs
index bdee47ab1..7b0659c09 100644
--- a/crates/ui-egui/src/headless.rs
+++ b/crates/ui-egui/src/headless.rs
@@ -1811,6 +1811,49 @@ mod tests {
assert!(w["result"].to_string().contains("field:copyrightStatus"), "{w}");
}
+ /// Info panel fields are shared text fields. The caption takes several lines: Return starts a
+ /// new one and only leaving the field saves it. Esc gives up an edit (the title stays as it
+ /// was, nothing saved), and right-click ▸ Paste works there too.
+ #[test]
+ fn info_fields_take_lines_esc_and_the_menu() {
+ let mut h = demo([1300.0, 1000.0]);
+ let t = Duration::from_secs(10);
+ h.request("ui.set", json!({"view": "detail", "right": "info"}), t);
+ h.settle(SETTLE);
+ let meta = |h: &Headless| h.app.session.catalog.photo(h.app.session.active().unwrap()).unwrap().meta.clone();
+ let ask = |h: &mut Headless, method: &str, params: serde_json::Value| {
+ let r = h.request(method, params.clone(), t);
+ assert_eq!(r["ok"], true, "{method} {params}: {r}");
+ h.step();
+ h.step();
+ };
+ let (title, undo) = (meta(&h).title, h.app.session.undo.len());
+ // the caption: two lines, saved on leaving
+ ask(&mut h, "ui.clickWidget", json!({"id": "field:caption"}));
+ ask(&mut h, "ui.key", json!({"key": "A", "cmd": true}));
+ ask(&mut h, "ui.text", json!({"text": "Harbour at dusk"}));
+ ask(&mut h, "ui.key", json!({"key": "Enter"}));
+ ask(&mut h, "ui.text", json!({"text": "Lisbon, 2026"}));
+ assert_eq!(h.app.session.undo.len(), undo, "Return in the caption saves nothing yet");
+ ask(&mut h, "ui.key", json!({"key": "Tab"}));
+ assert_eq!(meta(&h).caption, "Harbour at dusk\nLisbon, 2026");
+ // the title: Esc gives the edit up
+ let undo = h.app.session.undo.len();
+ ask(&mut h, "ui.clickWidget", json!({"id": "field:title"}));
+ ask(&mut h, "ui.text", json!({"text": " (draft)"}));
+ ask(&mut h, "ui.key", json!({"key": "Escape"}));
+ h.settle(SETTLE);
+ assert_eq!((meta(&h).title, h.app.session.undo.len()), (title, undo), "Esc saved nothing");
+ // the menu
+ h.view.clipboard = "Wedding in Sintra".into();
+ ask(&mut h, "ui.clickWidget", json!({"id": "field:title"}));
+ ask(&mut h, "ui.key", json!({"key": "A", "cmd": true}));
+ ask(&mut h, "ui.clickWidget", json!({"id": "field:title", "button": "right"}));
+ ask(&mut h, "ui.clickWidget", json!({"id": "field:title:paste"}));
+ ask(&mut h, "ui.key", json!({"key": "Enter"}));
+ assert_eq!(meta(&h).title, "Wedding in Sintra");
+ }
+
/// Local: a folder's photos show without joining the library; the breadcrumb, Include
/// subfolders and Add to My Photos work from the grid header.
#[test]
diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs
index 239212d79..7df03d72e 100644
--- a/crates/ui-egui/src/panels/right.rs
+++ b/crates/ui-egui/src/panels/right.rs
@@ -734,15 +734,21 @@ fn meta_field(app: &mut LightcraftApp, ui: &mut egui::Ui, label: &str, key: &str
ui.label(egui::RichText::new(crate::i18n::tr(label)).size(11.5).color(t.text_dim));
let id = egui::Id::new(("info-field", key));
let mut text: String = ui.data(|d| d.get_temp(id)).unwrap_or_else(|| value.to_string());
- let edit = if lines > 1 { egui::TextEdit::multiline(&mut text).desired_rows(lines) } else { egui::TextEdit::singleline(&mut text) };
- let r = ui.add(edit.desired_width(f32::INFINITY));
- register(ui.ctx(), format!("field:{key}"), r.rect);
- if r.has_focus() {
+ let widget = format!("field:{key}");
+ let field = if lines > 1 {
+ crate::text_field::TextField::multiline(&widget, &mut text).rows(lines)
+ } else {
+ crate::text_field::TextField::singleline(&widget, &mut text)
+ };
+ let r = field.width(f32::INFINITY).show(ui);
+ // the typed text outlives frames while it is edited (the photo's value is drawn otherwise)
+ if r.editing {
ui.data_mut(|d| d.insert_temp(id, text.clone()));
} else {
ui.data_mut(|d| d.remove::<String>(id));
}
- if r.lost_focus() && text != value {
+ // Return (one line) or leaving saves; Esc gives the edit up
+ if r.committed() && text != value {
let _ = app.run("photo.setMeta", json!({key: text}));
}
ui.add_space(6.0);
diff --git a/crates/ui-egui/src/text_field.rs b/crates/ui-egui/src/text_field.rs
index 49bf0530b..c620ee0a4 100644
--- a/crates/ui-egui/src/text_field.rs
+++ b/crates/ui-egui/src/text_field.rs
@@ -58,6 +58,7 @@ pub struct TextField<'a> {
color: Option<egui::Color32>,
align: Option<egui::Align>,
margin: Option<egui::Margin>,
+ rows: Option<usize>,
}
/// The egui id of the field registered as `widget`, to give it the focus (⌘F to search).
@@ -80,6 +81,7 @@ impl<'a> TextField<'a> {
color: None,
align: None,
margin: None,
+ rows: None,
}
}
@@ -99,6 +101,12 @@ impl<'a> TextField<'a> {
self
}
+ /// How many lines a multi-line field shows.
+ pub fn rows(mut self, rows: usize) -> Self {
+ self.rows = Some(rows);
+ self
+ }
+
/// Draw the field's own box (on by default); off where the caller draws one around it.
pub fn frame(mut self, on: bool) -> Self {
self.frame = on;
@@ -177,6 +185,9 @@ impl<'a> TextField<'a> {
if let Some(margin) = self.margin {
edit = edit.margin(margin);
}
+ if let Some(rows) = self.rows {
+ edit = edit.desired_rows(rows);
+ }
let (escape, enter) = ui.input(|i| (i.key_pressed(egui::Key::Escape), i.key_pressed(egui::Key::Enter)));
// a right-click places the cursor as a left one does: the selection the menu acts on
// would be gone