fw sync: an edit comment composer opens with the caret after the text (TextField::keep_caret_on_open)

Upstream's photocraft#15540 selects a text field's whole text when a layer opens on it, so the
port's edit composer (ComposerDraft::Edit / EditVerbatim, port-only kinds) replaced the comment
being edited with the first key typed; production appends ("Original" + " edited").
TextOptions::keep_caret_on_open (TextField::keep_caret_on_open, off by default) leaves the codec's
caret on open; the composer sets it for edits only, so a new comment and every other field keep
upstream's rule. The popover call landed with 5abaa7360; this adds the option it uses.
Trial: note_popup::an_edit_composer_opens_with_the_caret_after_the_text (New still selects all).
This commit is contained in:
Dylan M. Taylor
2026-11-23 03:49:07 -05:00
parent 5abaa7360e
commit fbd0bd096c
4 changed files with 32 additions and 3 deletions
+16
View File
@@ -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}"); 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:?}");
}
}
+4 -3
View File
@@ -496,9 +496,10 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text
let len = decl.shown.chars().count(); 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 // A dialog that opens on this field selects its whole text whatever the codec's caret, so typing replaces it
// (photocraft#15540). // (photocraft#15540).
let opened = crate::runtime::focus::focused_rec(&ctx) let opened = !decl.options.keep_caret_on_open
.and_then(|rec| crate::runtime::focus::origin(&ctx, &rec)) && crate::runtime::focus::focused_rec(&ctx)
.is_some_and(|origin| origin == crate::runtime::focus::FocusOrigin::Open); .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 } { match if opened || select_all { Caret::All } else { decl.caret } {
Caret::All => adapter::select(&ctx, widget, 0, len), Caret::All => adapter::select(&ctx, widget, 0, len),
// A bound caret is where the app put it: focus arriving keeps it. // A bound caret is where the app put it: focus arriving keeps it.
@@ -90,6 +90,9 @@ pub struct TextOptions {
pub steps_value: bool, pub steps_value: bool,
/// The whole text is selected when the field takes focus, whatever the codec's caret rule says. /// The whole text is selected when the field takes focus, whatever the codec's caret rule says.
pub select_on_focus: bool, 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. /// Selects the current text before this pass's typing, unless an IME composition is active.
pub select_all: bool, 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 /// 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, enabled: true,
steps_value: false, steps_value: false,
select_on_focus: false, select_on_focus: false,
keep_caret_on_open: false,
select_all: false, select_all: false,
submits: false, submits: false,
mixed: false, mixed: false,
+8
View File
@@ -169,6 +169,14 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
self 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 /// 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. /// request made during a composition is dropped.
#[must_use] #[must_use]