diff --git a/crates/review/tests/it/note_popup.rs b/crates/review/tests/it/note_popup.rs index 548827188e..333fcd9f84 100644 --- a/crates/review/tests/it/note_popup.rs +++ b/crates/review/tests/it/note_popup.rs @@ -323,3 +323,19 @@ fn an_edit_composer_reads_save_and_a_new_one_reads_post() { assert!(fixture.by_label(other).is_none(), "{kind:?}: not {other}"); } } + +/// An edit composer opens with the caret after the comment's text, so typing adds to it; a new comment's composer +/// opens on its field as the dialog rule has it (the whole text selected). +#[trial] +fn an_edit_composer_opens_with_the_caret_after_the_text() { + for (kind, want) in [(ComposerDraft::Edit, "Original edited"), (ComposerDraft::EditVerbatim, "Original edited"), (ComposerDraft::New, " edited")] { + let draft = Rc::new(RefCell::new(String::from("Original"))); + let shown = Rc::clone(&draft); + let mut fixture = Fixture::new(move |ui| { + let _ = CommentComposer::new(Some(pos2(200.0, 200.0)), &mut shown.borrow_mut()).draft_kind(kind).show(ui); + }); + fixture.room(vec2(800.0, 600.0)).settle(); + fixture.type_text(" edited").settle(); + assert_eq!(draft.borrow().as_str(), want, "{kind:?}"); + } +} diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index d8cbcd6ae8..fd5609e631 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -496,9 +496,10 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text let len = decl.shown.chars().count(); // A dialog that opens on this field selects its whole text whatever the codec's caret, so typing replaces it // (photocraft#15540). - let opened = crate::runtime::focus::focused_rec(&ctx) - .and_then(|rec| crate::runtime::focus::origin(&ctx, &rec)) - .is_some_and(|origin| origin == crate::runtime::focus::FocusOrigin::Open); + let opened = !decl.options.keep_caret_on_open + && crate::runtime::focus::focused_rec(&ctx) + .and_then(|rec| crate::runtime::focus::origin(&ctx, &rec)) + .is_some_and(|origin| origin == crate::runtime::focus::FocusOrigin::Open); match if opened || select_all { Caret::All } else { decl.caret } { Caret::All => adapter::select(&ctx, widget, 0, len), // A bound caret is where the app put it: focus arriving keeps it. diff --git a/crates/ui-contract/src/primitives/text_input.rs b/crates/ui-contract/src/primitives/text_input.rs index a1a0d49210..d8b20fc5b6 100644 --- a/crates/ui-contract/src/primitives/text_input.rs +++ b/crates/ui-contract/src/primitives/text_input.rs @@ -90,6 +90,9 @@ pub struct TextOptions { pub steps_value: bool, /// The whole text is selected when the field takes focus, whatever the codec's caret rule says. pub select_on_focus: bool, + /// A layer that opens on the field (`FocusOrigin::Open`) leaves the caret where the codec's rule puts it instead + /// of selecting the whole text: a field that opens to add to its text (an edited comment) rather than replace it. + pub keep_caret_on_open: bool, /// Selects the current text before this pass's typing, unless an IME composition is active. pub select_all: bool, /// Enter does something in the field itself (it saves or runs a search): the field takes the `Submit` intent, so the @@ -153,6 +156,7 @@ impl Default for TextOptions { enabled: true, steps_value: false, select_on_focus: false, + keep_caret_on_open: false, select_all: false, submits: false, mixed: false, diff --git a/crates/ui-widgets/src/text/text_field.rs b/crates/ui-widgets/src/text/text_field.rs index c85af1444f..8ff6699049 100644 --- a/crates/ui-widgets/src/text/text_field.rs +++ b/crates/ui-widgets/src/text/text_field.rs @@ -169,6 +169,14 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> { self } + /// A layer that opens on this field leaves its caret where the codec puts it (after the text) instead of selecting + /// the whole text, so typing adds to it: a card that opens to edit text already written. Off by default. + #[must_use] + pub fn keep_caret_on_open(mut self) -> Self { + self.options.keep_caret_on_open = true; + self + } + /// With `on`, selects the whole text in this pass, so typing replaces it, unless the field is composing text; a /// request made during a composition is dropped. #[must_use]