mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 20:43:19 +01:00
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:
4 files changed
+32
-3
No files matched your search
@@ -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:?}");
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user