diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index 8d8a85a0e..eeb07900d 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -445,6 +445,14 @@ fn char_at(lines: &[HostLine], pos: TextPos) -> usize { line.chars.start + line.text[..byte].chars().count() } +/// The I-beam is the editor's over its text and while its own press is held; elsewhere the canvas's tool or the rest +/// of the UI sets the pointer. +fn show_i_beam(cx: &mut Cx<'_, TextEditor<'_>>, phase: &PressPhase, vertical: bool) { + if !matches!(phase, PressPhase::Idle) { + cx.cursor(if vertical { CursorIcon::VerticalText } else { CursorIcon::Text }, CursorPriority::Hover); + } +} + /// The clipboard's text when the pass's input asks to paste and the clipboard has it ready. fn pasted_text(cx: &mut Cx<'_, TextEditor<'_>>, inputs: &[EditorInput]) -> Option> { if !inputs.iter().any(|input| matches!(input, EditorInput::Edit(EditCommand::Paste))) { @@ -620,7 +628,7 @@ impl Component for TextEditor<'_> { if let Some(text) = copied { cx.clipboard().put(&[ClipItem::Text(text.into())]); } - cx.cursor(if vertical { CursorIcon::VerticalText } else { CursorIcon::Text }, CursorPriority::Hover); + show_i_beam(cx, &press.phase, vertical); if done.changed { cx.propose(selection); } else if selection != before { diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index 7abc5e213..ef7e1ee25 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -210,6 +210,25 @@ fn a_secondary_click_on_the_text_reports_a_context_click_and_starts_no_drag() { assert_eq!(editor.commits.get() + editor.cancels.get(), 0, "the session stays open"); } +/// The I-beam is the editor's only over its text and while its own press is held: elsewhere the pointer is the +/// canvas's or the rest of the UI's. +#[trial] +fn the_i_beam_shows_over_the_text_and_during_its_drag_but_not_elsewhere() { + let mut editor = session("abc def", None); + editor.focus(); + let i_beam = Some(craft_behavior::Cursor::System(craft_behavior::CursorIcon::Text)); + let cursor = |editor: &Session| craft_ui::runtime::cursor::current(editor.fixture.ctx()); + let (on_text, away) = (pos2(40.0, 10.0), pos2(300.0, 300.0)); + editor.fixture.frame(vec![pointer::moved(on_text)]).settle(); + assert_eq!(cursor(&editor), i_beam, "over the text"); + editor.fixture.frame(vec![pointer::moved(away)]).settle(); + assert_ne!(cursor(&editor), i_beam, "away from the text, with the session open"); + editor.fixture.frame(vec![pointer::moved(on_text)]).frame(vec![pointer::press(on_text)]).frame(vec![pointer::moved(away)]); + assert_eq!(cursor(&editor), i_beam, "a drag that leaves the text keeps it"); + editor.fixture.frame(vec![pointer::release(away)]).settle(); + assert_ne!(cursor(&editor), i_beam, "after the release"); +} + fn preedit(text: &str) -> Event { Event::Ime(ImeEvent::Preedit { text: text.to_owned(), active_range_chars: None }) }