diff --git a/crates/ui-egui/src/params_dialog.rs b/crates/ui-egui/src/params_dialog.rs index edce94980..1ea229965 100644 --- a/crates/ui-egui/src/params_dialog.rs +++ b/crates/ui-egui/src/params_dialog.rs @@ -456,6 +456,8 @@ fn table(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[&R Column::new("Comment", 1.0).flex(), Column::new("Actions", 0.0).icon_only().min_content_width(ui_kit::table_row_height()), ]; + // An expression being edited may open over the cells to its right, up to the table's edge. + let right = ui.max_rect().right().min(ui.clip_rect().right()); ui_kit::table_custom(ui, salt, &columns, crate::i18n::tr("manage.empty"), |ui, table| { // The Name heading sorts every section at once, each inside itself (#8563). table.headers(ui, |ui, index| { @@ -543,11 +545,16 @@ fn table(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[&R let mut expression = d.editing.get(&r.name).cloned().unwrap_or_else(|| r.expression.clone()); let live = (expression != r.expression).then(|| check(app, &expression, &r.unit)); let invalid = matches!(live, Some(Err(_))) || (live.is_none() && r.error.is_some()); + let (shown, error) = match (&live, &r.error) { + (Some(Err(message)), _) | (None, Some(message)) => (message.clone(), true), + (Some(Ok(value)), _) => (value.clone(), false), + (None, None) => (show_value(r), false), + }; row.cell(|ui| { let id = ui.make_persistent_id(("pexpr", salt, &r.name)); let focused = ui.memory(|m| m.has_focus(id) || m.had_focus_last_frame(id)); - let response = ui_kit::cell_editor(ui, id, &mut expression, "Expression", invalid); - crate::scenario::publish_handle(&format!("param:{}", r.name), response.rect.center()); + let response = ui_kit::wide_cell_editor(ui, id, &mut expression, "Expression", invalid, right, (&shown, error)); + crate::scenario::publish_target_as(&format!("param:{}", r.name), &format!("param:{}", r.name), response.rect); if salt == "fav" { crate::scenario::publish_handle(&format!("param:favorite:{}", r.name), response.rect.center()); } @@ -574,12 +581,7 @@ fn table(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[&R } }); row.cell(|ui| { - let (text, error) = match (&live, &r.error) { - (Some(Err(message)), _) | (None, Some(message)) => (message.clone(), true), - (Some(Ok(value)), _) => (value.clone(), false), - (None, None) => (show_value(r), false), - }; - ui_kit::text(ui, &text, ui_kit::Text::Value(&format!("value:{}", r.name), error)).on_hover_text(&text); + ui_kit::text(ui, &shown, ui_kit::Text::Value(&format!("value:{}", r.name), error)).on_hover_text(&shown); }); row.cell(|ui| { let mut comment = d.comments.get(&r.name).cloned().unwrap_or_else(|| r.comment.clone()); @@ -606,7 +608,7 @@ fn table(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[&R } if salt == "user" { if d.adding { - ui.push_id("parameter_draft", |ui| add_row(app, ui, table, d)); + ui.push_id("parameter_draft", |ui| add_row(app, ui, table, d, right)); } if table.add_prompt(ui, "Add parameter", "Add parameter").clicked() { d.adding = true; @@ -636,7 +638,7 @@ fn enter_for(ui: &mut egui::Ui, focused: bool) -> bool { } /// The add row, one cell per column of the user table it ends. -fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>, d: &mut ParamsDialog) { +fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>, d: &mut ParamsDialog, right: f32) { let name_id = ui.make_persistent_id("pnew_name"); let expr_id = ui.make_persistent_id("pnew_expr"); let comment_id = ui.make_persistent_id("pnew_comment"); @@ -667,10 +669,14 @@ fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>, crate::scenario::publish_handle("param:new:unit", response.rect.center()); }); let unit = UNITS.get(d.new_unit).copied().unwrap_or("mm"); + // A blank expression is not yet an error: nothing to show, and nothing to add. + let live = if d.new_expr.trim().is_empty() { Ok(String::new()) } else { new_check(app, d, unit) }; + let (value, invalid) = match live { + Ok(v) => (v, false), + Err(e) => (e, true), + }; row.cell(|ui| { - // A blank expression is not yet an error: nothing to show, and nothing to add. - let invalid = !d.new_expr.trim().is_empty() && new_check(app, d, unit).is_err(); - let response = ui_kit::cell_editor(ui, expr_id, &mut d.new_expr, "Expression", invalid); + let response = ui_kit::wide_cell_editor(ui, expr_id, &mut d.new_expr, "Expression", invalid, right, (&value, invalid)); select_all_on_tab(ui, &response, &d.new_expr); crate::scenario::publish_handle("param:new:expression", response.rect.center()); let completed = complete(ui, &response, &mut d.new_expr); @@ -678,11 +684,6 @@ fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>, left |= !completed && response.lost_focus(); }); row.cell(|ui| { - let live = if d.new_expr.trim().is_empty() { Ok(String::new()) } else { new_check(app, d, unit) }; - let (value, invalid) = match live { - Ok(v) => (v, false), - Err(e) => (e, true), - }; ui_kit::text(ui, &value, ui_kit::Text::Value("new_value", invalid)).on_hover_text(&value); }); row.cell(|ui| { @@ -1566,4 +1567,37 @@ mod tests { assert!(crate::scenario::rect_of("dialog:body:viewport").unwrap().height() > 0.0); } } + /// #9358: a formula longer than its column opens wide while it is edited, over the cells to + /// its right, so all of it shows; Enter applies it and the field goes back into its column. + /// A formula that fits stays in its cell. + #[test] + fn a_long_expression_opens_wide_while_edited_and_enter_applies_it() { + let long = "width + width + width + width + width + width + width + width + width + width"; + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.app.run("parameters.add", json!({"name":"width","expression":"1 mm"})).unwrap(); + h.app.run("parameters.add", json!({"name":"length","expression":long})).unwrap(); + h.step(&json!({"start":"parameters.change"})).unwrap(); + h.frames(3); + let cell = crate::scenario::rect_of("param:length").unwrap(); + assert!(crate::scenario::rect_of("param:width").unwrap().width() <= cell.width() + 0.5); + + h.step(&json!({"click":{"handle":"param:length"}})).unwrap(); + h.frames(2); + let wide = crate::scenario::rect_of("param:length").unwrap(); + assert!(wide.width() > cell.width() * 1.5, "the edited formula opens wide: {wide:?} vs {cell:?}"); + assert!((wide.left() - cell.left()).abs() < 1.0, "it opens from its own cell: {wide:?} vs {cell:?}"); + + // The open field wraps, so the end of the text is Ctrl+End. + h.run(&json!([{"key":"End","cmd":true}, {"text":" * 2"}, {"key":"Enter"}])).unwrap(); + h.frames(2); + assert_eq!(h.app.session.doc.param("length").unwrap().expr, format!("{long} * 2")); + let after = crate::scenario::rect_of("param:length").unwrap(); + assert!(after.width() <= cell.width() + 0.5, "applied, the field is back in its column: {after:?}"); + + h.step(&json!({"click":{"handle":"param:width"}})).unwrap(); + h.frames(2); + let short = crate::scenario::rect_of("param:width").unwrap(); + assert!(short.width() <= cell.width() + 0.5, "a formula that fits stays in its cell: {short:?} vs {cell:?}"); + } } diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs index c8d13c541..7718a7464 100644 --- a/crates/ui-egui/src/ui_kit/fields.rs +++ b/crates/ui-egui/src/ui_kit/fields.rs @@ -562,6 +562,92 @@ fn cell_editor_frame(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str, response } +/// Table cell editor that opens wide while it has the focus and its text no longer fits +/// (#9358): over the cells to its right up to `right`, wrapping onto as many lines as the text +/// needs, with `note` (the live value, or an error when its flag is set) under it. Otherwise, +/// and when there is no room to its right, it is [`cell_editor`]. Enter does not break the +/// line: the caller commits on it as for the cell. Tokens: cell_editor tokens, popup frame. +pub fn wide_cell_editor(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str, invalid: bool, right: f32, note: (&str, bool)) -> Response { + let t = Tokens::get(); + let cell = egui::Rect::from_min_size(ui.cursor().min, vec2(dimension(ui.available_width()), row_height())); + let focused = ui.memory(|m| m.has_focus(id) || m.had_focus_last_frame(id)); + let font = egui::FontId::monospace(t.font_label); + let text_width = ui.fonts_mut(|f| f.layout_no_wrap(value.clone(), font.clone(), t.form_ink)).size().x; + let fits = text_width + 2.0 * t.form_inset <= cell.width(); + if !focused || fits || right <= cell.right() || !ui.is_rect_visible(cell) { + return cell_editor(ui, id, value, hint, invalid); + } + // The cell keeps its place in the row; the editor itself floats over it. + let (rect, _) = ui.allocate_exact_size(cell.size(), egui::Sense::hover()); + ui.painter().rect_filled(rect, t.control_radius, if invalid { t.form_error_fill } else { t.input_fill() }); + let width = right - rect.left(); + let (ctx, enabled) = (ui.ctx().clone(), ui.is_enabled()); + // The border sits outside the cell, so the text stays where it was. + let border = t.primary_border(t.dialog_border); + let area = Container::new(&ctx, Surface::Popup) + .area(id.with("wide")) + .fixed_pos(rect.min - vec2(border.width, border.width)) + .constrain(true) + .order(egui::Order::Foreground); + let shown = area.show(&ctx, |ui| { + Container::popup_frame(ui.style()) + .fill(t.control_fill) + .stroke(border) + .corner_radius(t.control_radius) + .inner_margin(0.0) + .show(ui, |ui| { + ui.set_width(width); + let editor = text_input(egui::TextEdit::multiline(value)) + .id(id) + .return_key(None) + .desired_rows(1) + .desired_width(width) + .hint_text(crate::i18n::tr_label(hint)) + .font(font) + .background_color(if invalid { t.form_error_fill } else { t.input_fill() }) + .text_color(if invalid { t.form_error } else { t.form_ink }); + let mut response = ui.add(editor); + // The area's first frame is a sizing pass that disables its widgets, and egui + // takes the focus from a disabled one: hand it straight back. + if ui.is_sizing_pass() { + response.request_focus(); + } + // It wraps but stays one line: Enter ends the edit as in a single-line field. + // Raw events, as the table reads them: an accepted completion has already taken its Enter. + let enter = ui.input(|i| { + i.raw.events.iter().any(|e| { + matches!(e, egui::Event::Key { key: egui::Key::Enter, pressed: true, repeat: false, modifiers, .. } if *modifiers == egui::Modifiers::NONE) + }) + }); + if response.has_focus() && enter { + response.surrender_focus(); + } + super::text_menu::menu(ui, &mut response, value, super::text_menu::Field::LINE); + publish(ui, &response, egui::WidgetType::TextEdit); + crate::theme::TabAccent::number_focus(ui, &response); + t.input_bevel(ui, &response); + let (text, error) = note; + if !text.is_empty() { + ui.add_space(t.space_2); + ui.horizontal(|ui| { + ui.add_space(t.form_inset); + ui.add(egui::Label::new(RichText::new(text).size(t.font_small).color(if error { t.form_error } else { t.form_dim })).wrap()); + }); + ui.add_space(t.space_2); + } + response + }) + .inner + }); + let response = shown.inner; + response.widget_info(|| { + let mut info = egui::WidgetInfo::labeled(egui::WidgetType::TextEdit, enabled, crate::i18n::tr_label(hint)); + info.current_text_value = Some(value.clone()); + info + }); + response +} + /// Search input with native accessibility and a stable caller ID. Tokens: cell_editor tokens. pub(crate) fn search_field(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str) -> Response { if !super::layout::in_catalog(ui) { diff --git a/crates/ui-egui/src/ui_kit/mod.rs b/crates/ui-egui/src/ui_kit/mod.rs index 121e49853..78800da04 100644 --- a/crates/ui-egui/src/ui_kit/mod.rs +++ b/crates/ui-egui/src/ui_kit/mod.rs @@ -59,8 +59,8 @@ pub(crate) use confirm::{Confirm, Tone}; pub use container::{Container, DockEdges, Edge, Surface}; pub(crate) use disclosure::*; pub(crate) use drag::*; -pub use fields::cell_editor; pub(crate) use fields::*; +pub use fields::{cell_editor, wide_cell_editor}; pub(crate) use file_view::{FileView, RecentRow, RecoveryMessage, SampleCard}; pub(crate) use framing::*; pub(crate) use history::*; diff --git a/docs/ui-kit.md b/docs/ui-kit.md index 83961c0fe..1a20f971b 100644 --- a/docs/ui-kit.md +++ b/docs/ui-kit.md @@ -180,6 +180,7 @@ table. Panel/Window/Confirm compositions also have dedicated geometry and intera | `password_field` | Stable secret editor; excludes the secret from scenario/accessibility metadata | field tokens | | `message_field` | Growing message editor; owner retains scroll and Enter routing | field tokens | | `cell_editor`, `bounded_cell_editor`, `search_field`, `library_cell` | Stable-ID inline/search editors; bounded variant caps Unicode characters; `library_cell` attaches a Library dropdown inside the editor border | field tokens, `text/error` | +| `wide_cell_editor` | A table cell editor that, while focused and too narrow for its text, opens over the cells to its right and wraps, with the live value or error under it (#9358) | field tokens, popup frame, `text/error` | | `menu_filter` | The filter at the top of a long menu: search glyph, frameless editor and a rule under them, so it does not read as an entry; the caller filters and owns focus | `space_8/16`, `font_label`, `form_dim/ink`, `dialog_rule` | | `text_area` | Bounded multiline JSON/data editor | field tokens | | `dropdown`, `dropdown_menu`, `compact_dropdown_menu` | Indexed choices or caller-supplied menu controls | `control_height`, `text_dim`, form tokens |