From 2d629069c6bb6210e0297423d7dd43cd5d19bbb5 Mon Sep 17 00:00:00 2001 From: Lars Jansen Date: Tue, 27 Oct 2026 02:42:23 +0000 Subject: [PATCH] Dialogs: OK and Cancel at the top (#6708) (#6713) * claim: #6708 * feat(dialogs): OK and Cancel at the top (#6708) Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../locales/pending/dialog-buttons-top.json | 18 ++ crates/ui-egui/src/dialogs_all_tests.rs | 114 ++++++++++ crates/ui-egui/src/dialogs_sketch_tests.rs | 22 ++ crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/prefs.rs | 32 +++ crates/ui-egui/src/prefs/interface.rs | 17 ++ crates/ui-egui/src/ui_kit/actions.rs | 212 +++++++++++++----- crates/ui-egui/src/ui_kit/mod.rs | 2 +- crates/ui-egui/src/ui_kit/panel.rs | 138 +++++++++++- crates/ui-egui/src/ui_kit/panel_tests.rs | 155 +++++++++++++ .../tests/scenarios/dialog_buttons_top.json | 27 +++ docs/book/src/guide/ui-tour.md | 4 +- docs/control-protocol.md | 2 +- docs/ui-kit.md | 6 +- 14 files changed, 686 insertions(+), 64 deletions(-) create mode 100644 assets/locales/pending/dialog-buttons-top.json create mode 100644 crates/ui-egui/tests/scenarios/dialog_buttons_top.json diff --git a/assets/locales/pending/dialog-buttons-top.json b/assets/locales/pending/dialog-buttons-top.json new file mode 100644 index 000000000..d311acd33 --- /dev/null +++ b/assets/locales/pending/dialog-buttons-top.json @@ -0,0 +1,18 @@ +{ + "en": { + "prefs.dialog_buttons": "Dialog buttons", + "prefs.top": "Top" + }, + "ja": { + "prefs.dialog_buttons": "ダイアログのボタン", + "prefs.top": "上" + }, + "ko": { + "prefs.dialog_buttons": "대화 상자 버튼", + "prefs.top": "위쪽" + }, + "zh": { + "prefs.dialog_buttons": "对话框按钮", + "prefs.top": "顶部" + } +} diff --git a/crates/ui-egui/src/dialogs_all_tests.rs b/crates/ui-egui/src/dialogs_all_tests.rs index 8bcb85968..bf25da503 100644 --- a/crates/ui-egui/src/dialogs_all_tests.rs +++ b/crates/ui-egui/src/dialogs_all_tests.rs @@ -481,6 +481,85 @@ fn remaining_dialogs_keep_kit_chrome_and_cancel_restores_preview() { } } +/// Interface › Dialog buttons › Top (#6708): every card made by the shared panel moves its +/// actions into the header row, with the footer's enabled states, handles and close behaviour. +#[test] +fn top_dialog_buttons_keep_every_cards_states_handles_and_close() { + for command in [ + "solid.revolve", + "solid.shell", + "solid.box", + "joint.create", + "solid.thread", + "inspect.measure", + "contact.create", + "motion.study", + "explode.create", + "parts.insert", + "joint.drive", + "inspect.interference", + "file.save_mesh", + "file.export_view_dxf", + "sheet.flat_pattern", + ] { + let mut seen = Vec::new(); + for placement in ["bottom", "top"] { + let mut h = fixture(); + h.call("ui.resize", json!({"width":1600,"height":1200})); + h.call("ui.prefs", json!({"dialog_buttons": placement})); + let design = h.app.session.doc.to_json(); + h.app.start(command); + h.frames(4); + let header = scenario::rect_of("dialog:header").unwrap_or_else(|| panic!("{command} {placement}: no header")); + let cancel = scenario::handle_at("dialog:cancel").unwrap_or_else(|| panic!("{command} {placement}: no cancel")); + seen.push(( + scenario::count_of("dialog:ok:enabled"), + scenario::rect_of("dialog:ok").is_some(), + scenario::rect_of("dialog:apply").is_some(), + )); + if placement == "top" { + assert!(scenario::rect_of("dialog:footer").is_none(), "{command}: no footer row"); + assert!(header.contains(cancel), "{command}: cancel in the header {header:?}"); + assert!(scenario::rect_of("dialog:ok").is_none_or(|ok| header.contains_rect(ok)), "{command}: OK in the header"); + assert_eq!(scenario::handle_at("dialog:close"), Some(cancel), "{command}: Cancel is the close"); + } else { + assert!(scenario::rect_of("dialog:footer").unwrap().contains(cancel), "{command}: cancel in the footer"); + } + h.step(&json!({"click":{"handle":"dialog:close"}})).unwrap(); + assert!(h.app.dialog.is_none(), "{command} {placement}: closed"); + assert_eq!(h.app.session.doc.to_json(), design, "{command} {placement}: closing keeps the design"); + } + assert_eq!(seen.first(), seen.get(1), "{command}: OK and Apply as at the bottom"); + } +} + +/// With the buttons at the top, OK, Enter and Esc do what they do with them at the bottom. +#[test] +fn top_dialog_buttons_apply_and_cancel_like_the_footer() { + for how in ["click", "enter", "escape"] { + let mut h = fixture(); + h.call("ui.resize", json!({"width":1600,"height":1200})); + h.call("ui.prefs", json!({"dialog_buttons": "top"})); + h.app.dialog = Some(representative(&h, "solid.revolve")); + h.settle(); + assert_eq!(scenario::count_of("dialog:ok:enabled"), Some(1.0)); + let (design, bodies) = (h.app.session.doc.to_json(), h.app.session.world_state().bodies.len()); + let step = match how { + "click" => json!({"click":{"handle":"dialog:OK"}}), + "enter" => json!({"key":"Enter"}), + _ => json!({"key":"Escape"}), + }; + h.step(&step).unwrap(); + h.settle(); + assert!(h.app.dialog.is_none(), "{how}: the dialog closes"); + if how == "escape" { + assert_eq!(h.app.session.doc.to_json(), design, "Esc leaves the design as it was"); + } else { + assert_eq!(h.app.session.world_state().bodies.len(), bodies + 1, "{how}: the revolve is made"); + } + } +} + /// OK and Cancel in the view's corner (#6709) do what the dialog's own do: the check applies and /// closes, the cross cancels without a change, and the check is grey (and does nothing) whenever /// the dialog's OK is. @@ -528,6 +607,41 @@ fn view_corner_ok_and_cancel_answer_the_dialog_like_its_own_buttons() { assert!(h.app.dialog.is_none(), "the cross closes Measure"); } +/// Both options at once (#6708 with #6709): the check in the view's corner greys out with the OK +/// in the title row, the pair keeps clear of the dialog, and either OK applies it. +#[test] +fn top_dialog_buttons_and_the_view_corner_agree() { + let open = |command: &str| { + let mut h = fixture(); + h.call("ui.resize", json!({"width":1600,"height":1200})); + h.call("ui.prefs", json!({"dialog_buttons": "top", "view_confirm_corner": true})); + if command == "solid.revolve" { + h.app.dialog = Some(representative(&h, command)); + } else { + h.app.start(command); + } + h.settle(); + h + }; + for (command, enabled) in [("solid.revolve", 1.0), ("solid.shell", 0.0)] { + let _h = open(command); + let header = scenario::rect_of("dialog:header").unwrap(); + assert!(scenario::rect_of("dialog:footer").is_none(), "{command}"); + assert!(header.contains_rect(scenario::rect_of("dialog:ok").unwrap()), "{command}: OK in the title row"); + assert_eq!(scenario::count_of("dialog:ok:enabled"), Some(enabled), "{command}"); + assert_eq!(scenario::count_of("view:confirm:ok:enabled"), Some(enabled), "{command}: the check follows the title row's OK"); + assert!(!scenario::rect_of("view:confirm").unwrap().intersects(header), "{command}: the pair keeps clear of the dialog"); + } + for handle in ["dialog:ok", "view:confirm:ok"] { + let mut h = open("solid.revolve"); + let bodies = h.app.session.world_state().bodies.len(); + h.step(&json!({"click":{"handle":handle}})).unwrap(); + h.settle(); + assert!(h.app.dialog.is_none(), "{handle}: the dialog closes"); + assert_eq!(h.app.session.world_state().bodies.len(), bodies + 1, "{handle}: the revolve is made"); + } +} + fn resized_box_editor(command: &str, params: Value) -> (Harness, u64) { let mut h = Harness::new(); for (id, params) in [ diff --git a/crates/ui-egui/src/dialogs_sketch_tests.rs b/crates/ui-egui/src/dialogs_sketch_tests.rs index 844fe8221..0123a5d1c 100644 --- a/crates/ui-egui/src/dialogs_sketch_tests.rs +++ b/crates/ui-egui/src/dialogs_sketch_tests.rs @@ -128,6 +128,28 @@ fn apply_commits_once_keeps_values_and_waits_for_new_selection_ok_closes() { } } +/// Interface › Dialog buttons › Top (#6708): Apply sits between Cancel and OK in the header row +/// and still commits once, keeping the dialog open. +#[test] +fn apply_in_the_header_row_commits_once_and_keeps_the_dialog() { + let mut h = fixture(); + h.call("ui.prefs", json!({"dialog_buttons":"top"})); + h.app.dialog = Some(picked(&h, "sketch.offset")); + h.settle(); + let header = scenario::rect_of("dialog:header").unwrap(); + assert!(scenario::rect_of("dialog:footer").is_none()); + let (cancel, apply, ok) = + (scenario::rect_of("dialog:cancel").unwrap(), scenario::rect_of("dialog:apply").unwrap(), scenario::rect_of("dialog:ok").unwrap()); + assert!(header.contains_rect(cancel) && header.contains_rect(apply) && header.contains_rect(ok)); + assert!(cancel.right() < apply.left() && apply.right() < ok.left()); + let undo = h.app.session.undo.len(); + h.step(&json!({"click":{"handle":"dialog:apply"}})).unwrap(); + h.settle(); + assert_eq!(h.app.session.undo.len(), undo + 1, "one Apply, one undo"); + assert!(h.app.dialog.is_some(), "Apply keeps the dialog"); + assert_eq!(scenario::rect_of("dialog:apply"), Some(apply), "the buttons stay where they were"); +} + #[test] fn picked_list_removes_one_item_and_field_still_activates_input() { let mut h = fixture(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d29a7815e..b9fad80b3 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2124,6 +2124,7 @@ impl SolveApp { self.ui.show_timeline = true; } self.ui.layout.publish(ui.ctx()); + self.preferences.dialog_buttons.publish(ui.ctx()); scenario::clear_handles(); self.poll_feature_edit(ui.ctx()); self.poll_deferred(ui.ctx()); diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 3f39e1a3b..0f619039a 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -628,6 +628,8 @@ preference_settings! { layout: crate::shell::LayoutMode = Default::default(), PAGE_INTERFACE; /// Show command names beneath ribbon icons, independently of workspace layout. ribbon_labels: bool = true, PAGE_INTERFACE; + /// Where a dialog's OK and Cancel sit: under its inputs (the default) or in its title row (#6708). + dialog_buttons: crate::ui_kit::DialogButtons = Default::default(), PAGE_INTERFACE; /// Interface scale (1 = 100 %). ui_scale: f32 = 1.0, PAGE_INTERFACE, accept(0.5..=2.5); /// Interface font: an installed font family (or a font file), or empty for the built-in one @@ -3397,6 +3399,7 @@ mod tests { ("ui_font", json!("test font"), PAGE_INTERFACE), ("layout", json!("compact"), PAGE_INTERFACE), ("ribbon_labels", json!(false), PAGE_INTERFACE), + ("dialog_buttons", json!("top"), PAGE_INTERFACE), ("timeline_left", json!(true), PAGE_INTERFACE), ("timeline_under_browser", json!(true), PAGE_INTERFACE), ("history_sketches", json!("under_feature"), PAGE_INTERFACE), @@ -4027,6 +4030,35 @@ mod tests { assert_eq!(h.app.preferences.history_sketches, HistorySketches::UnderFeature); } + #[test] + fn dialog_buttons_default_bottom_and_follow_preferences_transactions() { + use crate::ui_kit::DialogButtons; + let old: Prefs = serde_json::from_value(json!({"ribbon_labels": false})).unwrap(); + assert_eq!(old.dialog_buttons, DialogButtons::Bottom, "settings from before keep the footer"); + assert_eq!(lenient(&json!({"dialog_buttons": "sideways"})).dialog_buttons, DialogButtons::Bottom); + let mut h = crate::scenario::Harness::new(); + h.call("ui.prefs", json!({"open": "Interface"})); + super::reveal_preference(&mut h, "prefs:dialog_buttons:top"); + h.step(&json!({"click": {"handle": "prefs:dialog_buttons:top"}})).unwrap(); + assert_eq!(h.app.preferences.dialog_buttons, DialogButtons::Top); + h.call("ui.prefs", json!({"button": "cancel"})); + assert_eq!(h.app.preferences.dialog_buttons, DialogButtons::Bottom); + + let reply = h.call("ui.prefs", json!({"dialog_buttons": "top"})); + assert_eq!(reply["result"]["dialog_buttons"], "top"); + let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default()); + restored.load_prefs(&h.app.prefs()); + assert_eq!(restored.preferences.dialog_buttons, DialogButtons::Top); + let before = h.app.preferences.clone(); + assert!(control(&mut h.app, &json!({"dialog_buttons": "sideways"})).is_err()); + assert_eq!(h.app.preferences, before, "an unknown value changes nothing"); + h.call("ui.prefs", json!({"open": "Interface"})); + h.call("ui.prefs", json!({"button": "defaults"})); + assert_eq!(h.app.preferences.dialog_buttons, DialogButtons::Bottom); + h.call("ui.prefs", json!({"button": "cancel"})); + assert_eq!(h.app.preferences.dialog_buttons, DialogButtons::Top); + } + #[test] fn free_orbit_defaults_off_and_follows_preferences_transactions() { let old: Prefs = serde_json::from_value(json!({"nav":"solidworks"})).unwrap(); diff --git a/crates/ui-egui/src/prefs/interface.rs b/crates/ui-egui/src/prefs/interface.rs index 579bb4ff8..6d02ce082 100644 --- a/crates/ui-egui/src/prefs/interface.rs +++ b/crates/ui-egui/src/prefs/interface.rs @@ -42,6 +42,23 @@ pub(super) fn interface(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) { app.preferences.ribbon_labels = selected == 0; } }); + // Where every feature dialog puts its OK and Cancel (#6708). + ui_kit::row(ui, app.tr("prefs.dialog_buttons"), |ui| { + let placements = [crate::ui_kit::DialogButtons::Bottom, crate::ui_kit::DialogButtons::Top]; + let mut selected = placements.iter().position(|p| *p == app.preferences.dialog_buttons).unwrap_or(0); + if ui_kit::segmented(ui, &mut selected, &[app.tr("prefs.bottom"), app.tr("prefs.top")], Default::default()) + && let Some(placement) = placements.get(selected) + { + app.preferences.dialog_buttons = *placement; + } + }); + // Handles that don't depend on the language, by the saved value. + let row = app.tr("prefs.dialog_buttons"); + for (label, key) in [("prefs.bottom", "bottom"), ("prefs.top", "top")] { + if let Some(at) = crate::scenario::handle_at(&format!("dialog:{row}:{}", app.tr(label))) { + crate::scenario::publish_handle(&format!("prefs:dialog_buttons:{key}"), at); + } + } ui_kit::row(ui, app.tr("prefs.history"), |ui| { let mut selected = if app.preferences.timeline_left { if app.preferences.timeline_under_browser { 2 } else { 1 } } else { 0 }; if ui_kit::segmented( diff --git a/crates/ui-egui/src/ui_kit/actions.rs b/crates/ui-egui/src/ui_kit/actions.rs index fe290d1b8..823ca7e67 100644 --- a/crates/ui-egui/src/ui_kit/actions.rs +++ b/crates/ui-egui/src/ui_kit/actions.rs @@ -121,20 +121,7 @@ pub(super) fn footer_with_left( let inner = band.shrink2(vec2(t.space_12, t.space_10)); scenario::publish_rect("dialog:footer:actions", inner); let mut action = if ui.is_enabled() { keyboard(ui.ctx(), ok_enabled) } else { Action::None }; - let primary_fill = if t.borders.bevel.is_some() && !danger { - t.control_fill - } else if danger { - t.form_error - } else { - t.form_primary - }; - let primary_ink = if t.borders.bevel.is_some() && !danger { - t.form_ink - } else if danger && crate::theme::contrast_ratio(t.on_accent, primary_fill) < 4.5 { - t.tab_on_fill_dark - } else { - t.on_accent - }; + let look = ActionLook::new(ok_enabled, selected, danger); let mut actions_left = inner.right(); let row = ui.scope_builder(egui::UiBuilder::new().max_rect(inner).layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { @@ -143,49 +130,16 @@ pub(super) fn footer_with_left( if (kind == Action::Apply && (apply_label.is_none() || confirm)) || (kind == Action::Ok && ok_label.is_empty()) || label.is_empty() { continue; } - let marked = selected == Some(kind); - let primary = kind == Action::Ok && selected.is_none_or(|s| s == Action::Ok); - let live = kind == Action::Cancel || ok_enabled; - ui.spacing_mut().button_padding = vec2(if primary { t.space_16 } else { t.space_12 }, 0.0); - let text = RichText::new(crate::i18n::tr_label(label)).size(t.font_label).color(if primary && ok_enabled { - primary_ink - } else if primary { - t.form_dim - } else { - t.form_ink - }); - let response = ui.add_enabled( - live, - egui::Button::new(if primary && ok_enabled { text.bold_face() } else { text }) - .selected(marked && !primary) - .fill(if primary && ok_enabled { - primary_fill - } else if marked { - t.selected_bg - } else { - t.control_fill - }) - .stroke(if primary && ok_enabled { - egui::Stroke::NONE - } else if marked && !primary { - egui::Stroke::new(1.0, t.form_primary) - } else { - t.control_border_stroke(t.control_border) - }) - .wrap_mode(egui::TextWrapMode::Extend) - .corner_radius(t.control_radius) - .min_size(vec2(if primary { t.space_24 * 3.0 } else { 0.0 }, t.form_button_height)), + let primary = look.primary(kind); + let response = action_button( + ui, + label, + kind, + &look, + if primary { t.space_16 } else { t.space_12 }, + if primary { t.space_24 * 3.0 } else { 0.0 }, ); - t.button_bevel(ui, &response, false); - pointer_on_hover(ui, &response); actions_left = actions_left.min(response.rect.left()); - let key = match kind { - Action::Ok => "dialog:ok", - Action::Apply => "dialog:apply", - _ => "dialog:cancel", - }; - scenario::publish_target(key, response.rect); - scenario::publish_handle(&format!("dialog:{label}"), response.rect.center()); if response.clicked() { action = kind; } @@ -225,6 +179,154 @@ pub(super) fn footer_with_left( action } +/// What every OK, Apply and Cancel button shares, wherever the row of them sits. +struct ActionLook { + ok_enabled: bool, + selected: Option, + primary_fill: egui::Color32, + primary_ink: egui::Color32, +} + +impl ActionLook { + fn new(ok_enabled: bool, selected: Option, danger: bool) -> Self { + let t = Tokens::get(); + let primary_fill = if t.borders.bevel.is_some() && !danger { + t.control_fill + } else if danger { + t.form_error + } else { + t.form_primary + }; + let primary_ink = if t.borders.bevel.is_some() && !danger { + t.form_ink + } else if danger && crate::theme::contrast_ratio(t.on_accent, primary_fill) < 4.5 { + t.tab_on_fill_dark + } else { + t.on_accent + }; + Self { ok_enabled, selected, primary_fill, primary_ink } + } + + /// OK carries the accent unless the keyboard marks another action. + fn primary(&self, kind: Action) -> bool { + kind == Action::Ok && self.selected.is_none_or(|s| s == Action::Ok) + } +} + +/// One OK, Apply or Cancel button, `padding` at its sides and at least `min_width` wide, with its +/// scenario handles (`dialog:ok`, `dialog:apply`, `dialog:cancel` and `dialog: