diff --git a/crates/print/src/ui/dialog.rs b/crates/print/src/ui/dialog.rs index bc738c314..747921268 100644 --- a/crates/print/src/ui/dialog.rs +++ b/crates/print/src/ui/dialog.rs @@ -53,6 +53,9 @@ const DONE_BUTTON: usize = 2; /// The image box of the preview. const IMAGE: PartKey = PartKey::new("image"); +/// The message a preview shows instead of a sheet. +const MESSAGE: PartKey = PartKey::new("message"); + /// How far a crop or registration mark stands from the image box, and how long it is, in screen points. const MARK_GAP: f32 = 4.0; const MARK_LEN: f32 = 10.0; @@ -472,7 +475,14 @@ impl Component for PrintPreview<'_> { } } + /// The image box is a button; a message standing in for the sheet is a text node with the message. fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) { + if let SheetContent::Message(message) = &self.sheet.content { + let node = access.node(MESSAGE, Role::LABEL); + node.name = Name::Text(cx.env.resolve(message).as_str().to_owned()); + node.bounds(cx.rect); + return; + } if self.sheet.image_box().is_none() { return; } diff --git a/crates/print/tests/it/dialog.rs b/crates/print/tests/it/dialog.rs index f3a098c5b..1b1b4ed4a 100644 --- a/crates/print/tests/it/dialog.rs +++ b/crates/print/tests/it/dialog.rs @@ -330,6 +330,24 @@ fn a_sheet_of_pages_places_each_page_inside_the_paper() { assert!(!number.clip.contains(reach), "the overhang past the paper is not shown"); } +/// A message standing in for the sheet is read out as text where it is painted, inside the preview. +#[trial] +fn a_message_in_place_of_the_sheet_is_in_the_access_tree() { + const WHY: &str = "The page range selects no pages."; + let mut settings = Settings::default(); + let mut fields = DialogFields::default(); + let printers = vec![Printer::new("Office")]; + let mut fixture = Fixture::new(move |ui| { + let sheet = PrintSheet::message(LETTER, WHY.into(), "No pages".into()); + let _ = PrintDialog::new(Bind::bound(&mut settings), &printers, Bind::bound(&mut fields), &sheet).layout(PrintLayout::Document).show(ui); + }); + fixture.room(Vec2::new(1400.0, 1000.0)); + fixture.settle(); + let room = fixture.by_label("Print preview image").expect("the preview is in the access tree").rect; + let message = fixture.by_label(WHY).expect("the message is in the access tree"); + assert!(room.expand(0.5).contains_rect(message.rect), "the message {:?} is read where the preview {room:?} paints it", message.rect); +} + /// Properties… on a hosted print dialog is filed in the pass that receives the press and taken once, in a frame that /// runs twice too: a press taken again, or lost to the second pass, leaves the app opening the driver's options twice /// or never. diff --git a/crates/review/src/annotate.rs b/crates/review/src/annotate.rs index 945f2a820..89a0e9638 100644 --- a/crates/review/src/annotate.rs +++ b/crates/review/src/annotate.rs @@ -313,12 +313,13 @@ impl Annotation { } impl Annotation { - /// The box the annotation takes on the page: its `rect`, except for the icon kinds (Note, - /// Attachment, Caret), which take an `icon_side` square at the rect's corner. `icon_side` is - /// the icon's side in page units at the current zoom. + /// The box the annotation takes on the page: its `rect`, which the icon kinds (Note, + /// Attachment, Caret) unite with the `icon_side` square drawn at the rect's corner, so the + /// whole drawn icon and the whole rect both count. `icon_side` is the icon's side in page + /// units at the current zoom. pub fn extent(&self, icon_side: f64) -> Rect { - if matches!(self.kind, AnnotationKind::Note | AnnotationKind::Attach | AnnotationKind::Caret) { - Rect::from_origin_size(self.rect.origin(), (icon_side, icon_side)) + if self.kind.is_fixed_size() { + self.rect.abs().union(Rect::from_origin_size(self.rect.origin(), (icon_side, icon_side))) } else { self.rect } diff --git a/crates/review/src/annotate_tests.rs b/crates/review/src/annotate_tests.rs index b0b1c27c8..bb93417c1 100644 --- a/crates/review/src/annotate_tests.rs +++ b/crates/review/src/annotate_tests.rs @@ -72,7 +72,7 @@ fn hit_prefers_handles_then_the_topmost_body() { } /// One pick serves the comment part, the hover outline and the marker: a marker before any body, the last listed -/// marker first; then the topmost body, a markup by its quads and a note by its icon. +/// marker first; then the topmost body, a markup by its quads and a note by its icon and its rect. #[trial] fn pick_at_takes_the_marker_before_any_body_and_the_topmost_body_after() { // The markup's quads lie away from its rect, so only the quads can pick it. @@ -81,7 +81,9 @@ fn pick_at_takes_the_marker_before_any_body_and_the_topmost_body_after() { let boxed = square(2, AnnotationKind::Rectangle); // A note's rect smaller than its icon: the icon's extent picks it. let note = annotation(3, AnnotationKind::Note, Rect::new(100.0, 100.0, 104.0, 104.0)); - let all = [markup, boxed, note]; + // A note's rect larger than its icon: the rect beyond the icon picks it too. + let wide = annotation(4, AnnotationKind::Note, Rect::new(300.0, 300.0, 340.0, 340.0)); + let all = [markup, boxed, note, wide]; let over_box = Point::new(25.0, 20.0); let markers = [(AnnotationId(1), Rect::new(20.0, 15.0, 32.0, 27.0)), (AnnotationId(3), Rect::new(22.0, 17.0, 30.0, 25.0))]; @@ -92,6 +94,7 @@ fn pick_at_takes_the_marker_before_any_body_and_the_topmost_body_after() { assert_eq!(pick_at(&all, Point::new(30.0, 70.0), 0.0, ICON, &[]), None, "a markup's rect outside its quads"); assert_eq!(pick_at(&all, Point::new(112.0, 112.0), 0.0, ICON, &[]), Some(AnnotationId(3)), "the note by its icon"); assert_eq!(pick_at(&all, Point::new(120.0, 120.0), 0.0, ICON, &[]), None); + assert_eq!(pick_at(&all, Point::new(335.0, 335.0), 0.0, ICON, &[]), Some(AnnotationId(4)), "the note by its rect beyond its icon"); } /// The marker is clamped inside the page whatever its bounds: at the page's right edge, with inverted or fractional diff --git a/crates/ui-panels/src/dialog/prompt.rs b/crates/ui-panels/src/dialog/prompt.rs index 04312ce01..8d3cff771 100644 --- a/crates/ui-panels/src/dialog/prompt.rs +++ b/crates/ui-panels/src/dialog/prompt.rs @@ -187,7 +187,9 @@ impl Extras<'_> { /// and one footer button per answer. /// /// - Answers: `FooterButton`s with a `DialogRole` each, placed in the platform's order. A bare letter given with -/// `FooterButton::letter` answers while the focus is inside the prompt and no text part owns keys. +/// `FooterButton::letter` answers while the focus is inside the prompt and no text part owns keys; on Windows and +/// Linux, Alt with the letter answers while the prompt is the top modal. A button's `FooterButton::chord` (Cmd+D +/// for Don't Save on macOS) answers while the prompt is the top modal. /// - Escape and the close action give the escape answer (Cancel, else the first Other answer, else the default /// answer); Enter, when no part takes it, gives the Accept answer while that button is enabled. Both come back as /// `PromptOutput::picked`. diff --git a/crates/ui-panels/src/dialog/unsaved.rs b/crates/ui-panels/src/dialog/unsaved.rs index 8755fa0ec..1a7346a34 100644 --- a/crates/ui-panels/src/dialog/unsaved.rs +++ b/crates/ui-panels/src/dialog/unsaved.rs @@ -95,8 +95,8 @@ impl UnsavedChangesDialog { self } - /// The answers in the order the buttons are given: what each means and how it is asked. On macOS Cmd+D also - /// answers Don't Save, as the platform's own save sheets do; Revert has no such chord. + /// The answers in the order the buttons are given: what each means and how it is asked. Cmd+D (Ctrl+D off macOS) + /// also answers Don't Save, as the platform's own save sheets do; Revert has no such chord. fn replies(&self, words: &DialogWords, cancel: Label, os: Os) -> Vec<(CloseAnswer, Reply)> { let mac = os == Os::Mac; self.offered() @@ -105,10 +105,10 @@ impl UnsavedChangesDialog { let reply = match answer { CloseAnswer::Save => Reply::new(if mac { &words.unsaved_save } else { &words.unsaved_yes }.clone(), DialogRole::Accept), CloseAnswer::DontSave if self.scope == CloseScope::Revert => Reply::new(words.unsaved_revert.clone(), DialogRole::Accept), - CloseAnswer::DontSave if mac => { - Reply::new(words.unsaved_dont_save.clone(), DialogRole::Other).chord(Chord::new(Mods::CMD, Key::D)) + CloseAnswer::DontSave => { + let word = if mac { &words.unsaved_dont_save } else { &words.unsaved_no }; + Reply::new(word.clone(), DialogRole::Other).chord(Chord::new(Mods::CMD, Key::D)) } - CloseAnswer::DontSave => Reply::new(words.unsaved_no.clone(), DialogRole::Other), CloseAnswer::Cancel => Reply::new(cancel.clone(), DialogRole::Cancel), }; (answer, reply) diff --git a/crates/ui-panels/src/sections/footer.rs b/crates/ui-panels/src/sections/footer.rs index 517dbe2a4..53e2c7cb5 100644 --- a/crates/ui-panels/src/sections/footer.rs +++ b/crates/ui-panels/src/sections/footer.rs @@ -15,7 +15,7 @@ use crate::runtime::keys; use crate::style::{ComponentStyle, GlyphRole, RuleKind, StyleCx}; use craft_behavior::{Insets, Metrics, OwnerClass, Sense}; use craft_host::target::Os; -use craft_keymap::{Chord, Key}; +use craft_keymap::{Chord, Key, Mods}; use craft_theme::{ Variant, variant::{Emphasis, Tone}, @@ -427,8 +427,9 @@ impl FooterButton { FooterButton { label: label.into(), role, letter: None, chord: None, enabled: true, disabled_reason: None, tooltip: None, stays_open: false } } - /// The letter that activates the button while its dialog holds focus and no text part owns the keys. The - /// button reports it as its `access_key`. + /// The letter that activates the button while its dialog holds focus and no text part owns the keys. On Windows + /// and Linux, Alt with the letter also activates it while its dialog is the top modal, whatever has the focus. The + /// button reports the letter as its `access_key`. #[must_use] pub const fn letter(mut self, letter: char) -> Self { self.letter = Some(letter); @@ -624,11 +625,12 @@ impl FooterBar { let lettered = letters_reach_footer(&ctx); // A text part does not own modified chords, so a focused field does not stop a button's chord. let chorded = crate::runtime::overlay::drawing_top_modal(&ctx); + let os = crate::runtime::windows::os_of(&ctx); // Initial focus lands on the first enabled Accept button on every OS. let default_at = buttons.iter().position(|button| button.role == DialogRole::Accept && button.enabled); let least = if column { LeastWidth::DialogSideButton } else { LeastWidth::DialogButton }; let place = |ui: &mut Ui, shown: &mut Shown| { - for at in display_order(&roles, crate::runtime::windows::os_of(ui.ctx()) == Os::Windows, column) { + for at in display_order(&roles, os == Os::Windows, column) { let button = &buttons[at]; let variant = match button.role { DialogRole::Accept => Variant::PRIMARY, @@ -656,9 +658,14 @@ impl FooterBar { // The capture, renewed each pass, holds the chord from every other component from the next input // hook on; `take_claimed` consumes the held press, so a second pass of the frame finds nothing // left and does not press the button again. - let letter = button.letter.and_then(Key::from_char).map(Chord::plain).filter(|_| lettered); + // Windows and Linux dialogs also take Alt with the letter, as their mnemonics do; on macOS Option + // with a letter types a character. + let key = button.letter.and_then(Key::from_char); + let letter = key.map(Chord::plain).filter(|_| lettered); + let alt = key.map(|key| Chord::new(Mods::ALT, key)).filter(|_| chorded && os != Os::Mac); let modified = button.chord.filter(|_| chorded); - for chord in letter.into_iter().chain(modified).filter(|_| button.enabled && !ui.is_sizing_pass()) { + let chords = letter.into_iter().chain(alt).chain(modified); + for chord in chords.filter(|_| button.enabled && !ui.is_sizing_pass()) { keys::capture(&ctx, owner, chord); if keys::take_claimed(&ctx, owner, chord) { shown.picked = Some(at); diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 52f8d83aa..e3747040a 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -273,34 +273,39 @@ fn a_startup_modal_waits_for_the_open_dialogs_and_opens_in_stage_order() { assert!(host.borrow().is_open::>(), "the update opens after the warning"); } -/// On macOS Cmd+D answers Don't Save on the unsaved-changes question and the app sees no press; Linux has no such -/// chord. +/// The platform's chords answer Don't Save on the unsaved-changes question and the app sees no press: Cmd+D on +/// macOS, Alt with the button's letter ("No") on Linux. Linux has no Ctrl+D, and macOS no Alt chord. #[trial] -fn cmd_d_answers_dont_save_on_macos() { - for (os, answers) in [(Os::Mac, true), (Os::Linux, false)] { +fn the_platform_chords_answer_dont_save() { + let cases = [ + (Os::Mac, egui::Key::D, Modifiers::COMMAND, true), + (Os::Linux, egui::Key::D, Modifiers::COMMAND, false), + (Os::Linux, egui::Key::N, Modifiers::ALT, true), + (Os::Mac, egui::Key::D, Modifiers::ALT, false), + ]; + for (os, key, mods, answers) in cases { let host = Rc::new(RefCell::new(DialogHost::new())); - let app_saw_d = Rc::new(Cell::new(false)); - let (shown, saw) = (Rc::clone(&host), Rc::clone(&app_saw_d)); + let app_saw_key = Rc::new(Cell::new(false)); + let (shown, saw) = (Rc::clone(&host), Rc::clone(&app_saw_key)); let mut fixture = Fixture::new(move |ui| { - let pressed = - ui.input(|input| input.events.iter().any(|event| matches!(event, egui::Event::Key { key: egui::Key::D, pressed: true, .. }))); + let pressed = ui.input(|input| input.events.iter().any(|event| matches!(event, egui::Event::Key { key: k, pressed: true, .. } if *k == key))); saw.set(saw.get() || pressed); shown.borrow_mut().run(ui); }); fixture.os(os); host.borrow_mut().open(UnsavedChangesDialog::new(CloseScope::Close, "poster.psd")); fixture.idle(6); - fixture.key(egui::Key::D, Modifiers::COMMAND).idle(4); + fixture.key(key, mods).idle(4); let result = host.borrow_mut().take::(); if answers { assert_eq!( result.and_then(DialogResult::ok).map(|answer| answer.answer), Some(CloseAnswer::DontSave), - "{os:?}: Cmd+D answers Don't Save" + "{os:?}: {mods:?}+{key:?} answers Don't Save" ); - assert!(!app_saw_d.get(), "{os:?}: the press reaches nothing but the question"); + assert!(!app_saw_key.get(), "{os:?}: the press reaches nothing but the question"); } else { - assert!(result.is_none() && host.borrow().is_open::(), "{os:?}: Ctrl+D leaves the question open"); + assert!(result.is_none() && host.borrow().is_open::(), "{os:?}: {mods:?}+{key:?} leaves the question open"); } } } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/password.rs b/crates/ui-patterns/src/patterns/form_dialogs/password.rs index 439d29bf0..ef00e6576 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/password.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/password.rs @@ -447,6 +447,7 @@ impl<'a> PasswordDialog<'a> { }; let mut dialog = Dialog::new(title) .buttons([FooterButton::new(ok, DialogRole::Accept), FooterButton::new(common.cancel.clone(), DialogRole::Cancel)]) + .close_on_enter(true) .close_guard(move |_, cx| cx.memory::().valid.unwrap_or(!setting)) .settle_buttons(move |cx, buttons| { let valid = cx.memory::().valid.unwrap_or(!setting); diff --git a/crates/ui-patterns/tests/it/form_dialogs/password.rs b/crates/ui-patterns/tests/it/form_dialogs/password.rs index b7b00dd27..5359927f9 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/password.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/password.rs @@ -2,6 +2,9 @@ use craft_ui::patterns::form_dialogs::{PasswordDialog, PasswordPurpose}; use craft_ui::testing::{Fixture, trial}; +use egui::Modifiers; +use std::cell::RefCell; +use std::rc::Rc; fn focused_on(fixture: &Fixture, label: &str) -> bool { let focused = craft_ui::testing::access::focused_key(fixture.tree()); @@ -21,6 +24,33 @@ fn the_reveal_button_hands_the_focus_back_to_the_field() { assert!(focused_on(&fixture, "Password"), "the field has the focus back, so typing goes on"); } +/// Enter in the field answers Open with the typed password; a new password answers only once both entries match. +#[trial] +fn enter_in_the_field_answers_while_the_entries_are_valid() { + for (purpose, retyped, answers) in + [(PasswordPurpose::open("poster.pdf"), None, true), (PasswordPurpose::Set, Some("other"), false), (PasswordPurpose::Set, Some("secret"), true)] + { + let answer = Rc::new(RefCell::new(None)); + let seen = Rc::clone(&answer); + let mut fixture = Fixture::new(move |ui| { + let shown = PasswordDialog::new(purpose.clone()).show(ui); + if let Some(given) = shown.answer { + *seen.borrow_mut() = Some(given.secret.expose().to_owned()); + } + }); + fixture.idle(30); + fixture.type_text("secret").idle(2); + if let Some(retyped) = retyped { + let field = fixture.by_label("Re-type Password").expect("a new password is typed twice"); + fixture.click(&field).idle(2); + fixture.type_text(retyped).idle(2); + } + fixture.key(egui::Key::Enter, Modifiers::NONE).idle(4); + let expected = answers.then(|| "secret".to_owned()); + assert_eq!(*answer.borrow(), expected, "Enter after {retyped:?}"); + } +} + /// The strength meter's fill and the top of its range: the fill grows with the rating and Strong reaches the top. fn meter(fixture: &Fixture) -> (f64, f64) { let meter = fixture.by_label("Password strength").expect("the strength meter shows once something is typed");