diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 4613b9069..1299051dc 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -1371,6 +1371,52 @@ impl Snapshot { save_graphics(app); } } + + /// Whether everything is as in the snapshot: nothing Cancel would put back. Destructured, so + /// a setting added to the snapshot can't be left out of the comparison. + fn matches(&self, app: &SolveApp) -> bool { + let Snapshot { + prefs, + dark, + layout, + grid, + navigation_bar, + tool_cursor, + shadow, + perspective, + style, + pick_bodies, + agent, + autosave, + auto_project, + keys, + } = self; + *prefs == app.preferences + && *dark == app.ui.dark + && *layout == app.ui.layout + && *grid == app.ui.show_grid + && *navigation_bar == app.ui.show_navigation_bar + && *tool_cursor == app.ui.tool_cursor + && *shadow == app.ui.ground_shadow + && *perspective == app.ui.perspective + && *style == app.ui.visual_style + && *pick_bodies == app.ui.pick_bodies + && *agent == app.ui.agent_cursor + && *autosave == app.autosave_minutes + && *auto_project == app.session.auto_project + && *keys == app.keymap.custom + } +} + +/// Whether Apply has anything to do (#8561): something changed since the dialog opened or since +/// the last Apply. False while the dialog is closed. +fn apply_pending(app: &mut SolveApp) -> bool { + let Some(snapshot) = app.prefs_window.snapshot.as_mut() else { + return false; + }; + // Update bookkeeping isn't a setting and Cancel keeps it (`restore`), so it isn't a change. + snapshot.prefs.update.clone_from(&app.preferences.update); + app.prefs_window.snapshot.as_ref().is_some_and(|s| !s.matches(app)) } /// The colour theme (#2255): Dark, Light, System, the built-in theme files and the imported @@ -1985,6 +2031,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { let mut button: Option<&str> = None; let before = app.preferences.clone(); let capturing = crate::keymap::capturing(app); + let pending = apply_pending(app); let window = ui_kit::Window::new(egui::Id::new("sc_prefs"), "settings", app.tr("ui.preferences")) .cards(10.0 / 3.0) .page_rows(24.0) @@ -1995,7 +2042,8 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { bottom: (Tokens::get().space_16 + Tokens::get().space_4) as i8, }) .fields() - .actions("OK", "Cancel", Some("Apply")); + .actions("OK", "Cancel", Some("Apply")) + .apply_enabled(pending); let window = if capturing { window.keep_open_on_escape() } else { window }; let mut selected = app.prefs_window.section; let (closed, action) = window.show_pages_with_header(ctx, &mut selected, WINDOW_PAGES, app, search_header, clear_search, |ui| { @@ -2041,6 +2089,11 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { _ if closed => close(app, true), _ => {} } + // The footer was drawn before this frame's edits (and before an Apply): show Apply's new + // state on the next frame rather than at the next input. + if app.prefs_window.open && apply_pending(app) != pending { + ctx.request_repaint(); + } } /// Close the dialog; `revert`: put back what was there when it opened (Cancel, Esc, ×). @@ -2707,6 +2760,7 @@ pub fn control(app: &mut SolveApp, p: &serde_json::Value) -> Result| { + crate::test_support::frame(&ctx, egui::RawInput { events, ..input() }, |ui| show(app, ui.ctx())); + crate::scenario::count_of("dialog:apply:enabled") + }; + let v = control(&mut app, &json!({"open": "Navigation"})).unwrap(); + assert_eq!(v["apply_enabled"], false, "nothing changed yet"); + assert_eq!(draw(&mut app, vec![]), Some(0.0), "just opened: Apply is disabled"); + + let v = control(&mut app, &json!({"zoom_to_cursor": false})).unwrap(); + assert_eq!(v["apply_enabled"], true); + assert_eq!(draw(&mut app, vec![]), Some(1.0), "after a change: Apply is enabled"); + let v = control(&mut app, &json!({"zoom_to_cursor": true})).unwrap(); + assert_eq!(v["apply_enabled"], false, "a change put back by hand leaves nothing to apply"); + control(&mut app, &json!({"zoom_to_cursor": false})).unwrap(); + + // Clicking Apply keeps the change and leaves nothing to apply. + draw(&mut app, vec![]); + let apply = crate::scenario::handle_at("dialog:apply").unwrap(); + draw(&mut app, crate::test_support::pointer(apply, true)); + draw(&mut app, crate::test_support::pointer(apply, false)); + assert!(app.prefs_window.open && !app.preferences.zoom_to_cursor, "Apply keeps the dialog and the change"); + assert_eq!(draw(&mut app, vec![]), Some(0.0), "after Apply: Apply is disabled"); + // A disabled Apply does nothing when clicked. + draw(&mut app, crate::test_support::pointer(apply, true)); + draw(&mut app, crate::test_support::pointer(apply, false)); + assert_eq!(draw(&mut app, vec![]), Some(0.0)); + + // Every setting Cancel would put back counts, the shortcuts and view settings included; + // update bookkeeping (not a setting, and Cancel keeps it) does not. + app.preferences.update.last_check += 1.0; + assert_eq!(draw(&mut app, vec![]), Some(0.0), "update bookkeeping is not a change"); + app.keymap.custom.insert("solid.extrude".into(), "Shift+E".into()); + assert_eq!(draw(&mut app, vec![]), Some(1.0), "a rebinding is a change"); + let v = control(&mut app, &json!({"button": "apply"})).unwrap(); + assert_eq!(v["apply_enabled"], false); + app.ui.show_grid = !app.ui.show_grid; + assert_eq!(draw(&mut app, vec![]), Some(1.0), "a view setting is a change"); + + // Cancel puts it back and closes; opened again, there is nothing to apply. + let v = control(&mut app, &json!({"button": "cancel"})).unwrap(); + assert_eq!(v["apply_enabled"], false); + let v = control(&mut app, &json!({"open": "General"})).unwrap(); + assert_eq!(v["apply_enabled"], false); + } + #[test] fn language_preference_switches_and_persists() { let mut app = SolveApp::new(solidcraft_engine::Session::default(), Default::default()); diff --git a/crates/ui-egui/src/ui_kit/actions.rs b/crates/ui-egui/src/ui_kit/actions.rs index 23a87e415..df3a4bf4f 100644 --- a/crates/ui-egui/src/ui_kit/actions.rs +++ b/crates/ui-egui/src/ui_kit/actions.rs @@ -111,6 +111,9 @@ pub(crate) struct Footer<'a> { /// Parent surface corners; the footer inherits only its two bottom corners. pub corners: Option, pub secondary_left: bool, + /// Nothing to apply yet (nothing changed since the dialog opened or since the last Apply): + /// Apply is drawn and reported disabled, and clicking it does nothing (#8561). + pub nothing_to_apply: bool, } pub(crate) fn footer(ui: &mut Ui, ok_label: &str, cancel_label: &str, ok_enabled: bool, options: Footer<'_>) -> Action { @@ -125,7 +128,7 @@ pub(super) fn footer_with_left( options: Footer<'_>, left: impl FnOnce(&mut Ui), ) -> Action { - let Footer { apply: apply_label, status, selected, danger, confirm, corners, secondary_left } = options; + let Footer { apply: apply_label, status, selected, danger, confirm, corners, secondary_left, nothing_to_apply } = options; let t = Tokens::get(); let ok_enabled = ok_enabled && !ok_label.is_empty(); // The footer is a full-width band (the card has no margin of its own): its own fill, a rule @@ -146,7 +149,11 @@ 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 look = ActionLook::new(ok_enabled, selected, danger); + let look = ActionLook::new(ok_enabled, !nothing_to_apply, selected, danger); + if apply_label.is_some() { + let live = if confirm { !nothing_to_apply } else { look.live(Action::Apply) }; + scenario::publish_count("dialog:apply:enabled", f64::from(live && ui.is_enabled())); + } 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| { @@ -184,7 +191,8 @@ pub(super) fn footer_with_left( .stroke(egui::Stroke::new(1.0, if marked { t.form_primary } else { t.control_border })) .corner_radius(t.control_radius); } - let response = ui.add(button); + // A confirmation's third choice (Don't Save, Keep) never waited for OK's inputs. + let response = ui.add_enabled(!nothing_to_apply, button); pointer_on_hover(ui, &response); scenario::publish_target("dialog:apply", response.rect); scenario::publish_handle(&format!("dialog:{label}"), response.rect.center()); @@ -207,13 +215,14 @@ pub(super) fn footer_with_left( /// What every OK, Apply and Cancel button shares, wherever the row of them sits. struct ActionLook { ok_enabled: bool, + apply_enabled: bool, selected: Option, primary_fill: egui::Color32, primary_ink: egui::Color32, } impl ActionLook { - fn new(ok_enabled: bool, selected: Option, danger: bool) -> Self { + fn new(ok_enabled: bool, apply_enabled: bool, selected: Option, danger: bool) -> Self { let t = Tokens::get(); let primary_fill = if t.borders.bevel.is_some() && !danger { t.control_fill @@ -229,7 +238,16 @@ impl ActionLook { } else { t.on_accent }; - Self { ok_enabled, selected, primary_fill, primary_ink } + Self { ok_enabled, apply_enabled, selected, primary_fill, primary_ink } + } + + /// Cancel always works; OK and Apply need valid inputs, and Apply also something to apply. + fn live(&self, kind: Action) -> bool { + match kind { + Action::Cancel => true, + Action::Apply => self.ok_enabled && self.apply_enabled, + Action::Ok | Action::None => self.ok_enabled, + } } /// OK carries the accent unless the keyboard marks another action. @@ -245,11 +263,11 @@ fn action_button(ui: &mut Ui, label: &str, kind: Action, look: &ActionLook, padd let ok_enabled = look.ok_enabled; let marked = look.selected == Some(kind); let primary = look.primary(kind); - let live = kind == Action::Cancel || ok_enabled; + let live = look.live(kind); ui.spacing_mut().button_padding = vec2(padding, 0.0); let text = RichText::new(crate::i18n::tr_label(label)).size(t.font_label).color(if primary && ok_enabled { look.primary_ink - } else if primary { + } else if primary || !live { t.form_dim } else { t.form_ink @@ -333,7 +351,7 @@ impl<'a> HeaderActions<'a> { let ok_enabled = ok_enabled && self.rect(Action::Ok).is_some(); scenario::publish_count("dialog:ok:enabled", f64::from(ok_enabled && ui.is_enabled())); let mut action = if ui.is_enabled() { keyboard(ui.ctx(), ok_enabled) } else { Action::None }; - let look = ActionLook::new(ok_enabled, None, false); + let look = ActionLook::new(ok_enabled, true, None, false); let padding = Tokens::get().space_8; for (kind, label, rect) in &self.slots { let builder = egui::UiBuilder::new() diff --git a/crates/ui-egui/src/ui_kit/actions_tests.rs b/crates/ui-egui/src/ui_kit/actions_tests.rs index d714b7e06..9fb9f2bb1 100644 --- a/crates/ui-egui/src/ui_kit/actions_tests.rs +++ b/crates/ui-egui/src/ui_kit/actions_tests.rs @@ -73,6 +73,85 @@ fn enabled_buttons_show_the_pointing_hand_and_disabled_ones_do_not() { } } +/// With nothing to apply (#8561), Apply is disabled for the pointer, the keyboard and +/// accessibility, and drawn with the theme's dim ink; with a change it works as before. OK and +/// Cancel are not affected. The same holds for a confirmation's third action on the left. +#[test] +fn apply_with_nothing_to_apply_is_disabled_for_pointer_keyboard_and_accessibility() { + use crate::theme::Tokens; + for confirm in [false, true] { + for nothing_to_apply in [true, false] { + let live = !nothing_to_apply; + let ctx = crate::fonts::test_context(); + ctx.enable_accesskit(); + let action = std::cell::Cell::new(Action::None); + let run = |events: Vec| { + scenario::clear_handles(); + let input = egui::RawInput { + screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(320.0, 400.0))), + events, + focused: true, + ..Default::default() + }; + crate::test_support::frame(&ctx, input, |ui| { + ui.set_width(280.0); + scope(ui, |ui| { + let options = Footer { apply: Some("Apply"), confirm, nothing_to_apply, ..Default::default() }; + action.set(footer(ui, "OK", "Cancel", true, options)); + }); + }) + }; + let case = format!("confirm {confirm}, nothing to apply {nothing_to_apply}"); + let output = run(vec![]); + assert_eq!(scenario::count_of("dialog:apply:enabled"), Some(f64::from(live)), "{case}"); + assert_eq!(scenario::count_of("dialog:ok:enabled"), Some(1.0), "{case}: OK keeps its own state"); + let node = output + .platform_output + .accesskit_update + .as_ref() + .and_then(|update| update.nodes.iter().find(|(_, node)| node.label() == Some("Apply"))) + .map(|(_, node)| node.clone()) + .unwrap_or_else(|| panic!("{case}: Apply is an accessible button")); + assert_eq!(node.role(), egui::accesskit::Role::Button, "{case}"); + assert_eq!(node.is_disabled(), nothing_to_apply, "{case}: accessibility reports the state"); + if !confirm { + let ink = crate::test_support::text_shapes(&output) + .find(|text| text.galley.job.text == "Apply") + .and_then(|text| text.galley.job.sections.first().map(|section| section.format.color)) + .unwrap_or_else(|| panic!("{case}: Apply's label is painted")); + let t = Tokens::get(); + assert_eq!(ink, if live { t.form_ink } else { t.form_dim }, "{case}: a disabled Apply looks disabled"); + } + + // The pointer: no hand and no click without something to apply. + let apply = scenario::handle_at("dialog:apply").unwrap_or_else(|| panic!("{case}: Apply has a handle")); + run(vec![egui::Event::PointerMoved(apply)]); + let cursor = run(vec![egui::Event::PointerMoved(apply)]).platform_output.cursor_icon; + assert_eq!(cursor, if live { egui::CursorIcon::PointingHand } else { egui::CursorIcon::Default }, "{case}"); + run(pointer(apply, true)); + run(pointer(apply, false)); + assert_eq!(action.get(), if live { Action::Apply } else { Action::None }, "{case}: click"); + + // The keyboard: Tab never lands on a disabled Apply, so Space/Enter can't press it. + ctx.memory_mut(|m| { + if let Some(id) = m.focused() { + m.surrender_focus(id); + } + }); + let mut focused = Vec::new(); + for _ in 0..4 { + for event in run(key(egui::Key::Tab)).platform_output.events { + if let egui::output::OutputEvent::FocusGained(info) = event { + focused.push(info.label.unwrap_or_default()); + } + } + } + assert!(focused.iter().any(|label| label == "Cancel"), "{case}: Tab reaches the footer: {focused:?}"); + assert_eq!(focused.iter().any(|label| label == "Apply"), live, "{case}: Tab and Apply: {focused:?}"); + } + } +} + #[test] fn positioned_shared_buttons_center_text_and_respect_disabled_clicks_in_both_themes() { use crate::theme::Tokens; diff --git a/crates/ui-egui/src/ui_kit/panel.rs b/crates/ui-egui/src/ui_kit/panel.rs index a011ab0b0..7a211281d 100644 --- a/crates/ui-egui/src/ui_kit/panel.rs +++ b/crates/ui-egui/src/ui_kit/panel.rs @@ -892,6 +892,7 @@ pub struct Window<'a> { ok: &'a str, cancel: &'a str, apply: Option<&'a str>, + nothing_to_apply: bool, enter_submits: bool, escape_closes: bool, closeable: bool, @@ -915,6 +916,7 @@ impl<'a> Window<'a> { ok: "OK", cancel: "Cancel", apply: None, + nothing_to_apply: false, enter_submits: true, escape_closes: true, closeable: true, @@ -946,6 +948,14 @@ impl<'a> Window<'a> { self } + /// Whether the third action has anything to do: for an Apply that keeps the window open, + /// whether anything changed since it opened or since the last Apply (#8561). Without it + /// the action is disabled for the pointer, the keyboard and accessibility. + pub(crate) fn apply_enabled(mut self, enabled: bool) -> Self { + self.nothing_to_apply = !enabled; + self + } + /// A card of editable fields: Enter belongs to the fields and never submits (confirmations /// let Enter give the primary action when nothing has focus). pub(crate) fn fields(mut self) -> Self { @@ -1045,7 +1055,14 @@ impl<'a> Window<'a> { let action = if self.ok.is_empty() && self.cancel.is_empty() && self.apply.is_none() && !self.left_slot { super::Action::None } else { - super::actions::footer_with_left(ui, self.ok, self.cancel, valid, super::Footer { apply: self.apply, ..Default::default() }, left) + super::actions::footer_with_left( + ui, + self.ok, + self.cancel, + valid, + super::Footer { apply: self.apply, nothing_to_apply: self.nothing_to_apply, ..Default::default() }, + left, + ) }; (close, action) }) diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 2f0b97ab8..84492e0e6 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -319,7 +319,7 @@ labels; `prefs:measure_axis_distances` identifies the checkbox. | `ui.home` | `open?`: bool; `sample?`: index | the start page: show or hide it, open a built-in sample (once built); returns `open` and the recent designs | | `ui.shortcut` | `command`, `key?`, `replace?` | a command's key, or rebinds it (`""` clears; a key another command uses is refused unless `replace`) | | `ui.help` | `item?`: `about`, `shortcuts` (read only), `report` (opens a report draft for review), `crashes` (the Crash Reports window) | the version, commit, build date and diagnostic text | -| `ui.prefs` | any of the [`ui.prefs` settings](#uiprefs-settings) below; also `open`, `button` and `stand_up` (listed there) | sets preferences (kept between runs), returns them all plus `dialog` (the open page or null), `dark`, `viewport_dark` (whether the 3D view is dark), `stand_up_offer` (the offered bodies, or null), `stand_up_note` (whether that offer is the note), `theme_followed` and `theme_follow_error` | +| `ui.prefs` | any of the [`ui.prefs` settings](#uiprefs-settings) below; also `open`, `button` and `stand_up` (listed there) | sets preferences (kept between runs), returns them all plus `dialog` (the open page or null), `apply_enabled` (whether the dialog's Apply has a change to keep), `dark`, `viewport_dark` (whether the 3D view is dark), `stand_up_offer` (the offered bodies, or null), `stand_up_note` (whether that offer is the note), `theme_followed` and `theme_follow_error` | | `ui.capture` | `action?`: `capture`, `revert`, `cancel` | answers the Capture Position question (asked when a command starts or the design is saved while components were moved without capturing); without `action`, what waits | | `ui.window` | `action`: minimize, maximize, restore, toggle, close | what the title bar's caption buttons do (on Windows and Linux the application bar is the title bar); close asks about unsaved designs first | | `ui.agent_cursor` | `show` (bool), `speed`: a number from 1 to 5 (times the normal speed) or `"instant"` (no animation: the command runs at once and the pointer jumps to where it acted), `follow_camera` (bool); all optional | the settings. While the cursor is shown, `engine.execute` first glides to the command's toolbar button and to each pick, then runs and replies; later requests wait their turn. The pointer shows the command's localized name, including commands inside a closed toolbar group; an animated batch names each command it visits. It also follows the pointer input sent with `ui.move`, `ui.click`, `ui.drag` and `ui.scroll`, as each event reaches the app, and a press leaves a ripple. `"animate": false` on a call skips it |