TextEditor: a press on the canvas background commits the session

This commit is contained in:
aiongg
2026-11-13 01:08:19 +00:00
parent 5b22727818
commit 47a49e542d
2 changed files with 28 additions and 5 deletions
+11 -4
View File
@@ -7,8 +7,9 @@
//! over the canvas. The text itself is painted by the app.
//!
//! The editor is its own root with one editor part over the rect it is shown at (the text's screen bounds): a press
//! inside it is the editor's, a press outside it is not. A press outside commits the session (Photoshop): until the
//! canvas background-press route lands, the app reads `CanvasOutput.press` for it, as the users of `Guides` do.
//! inside it is the editor's, a press outside it is not. A press on the canvas background (one no canvas control
//! claims, [`CanvasSpace::background`](craft_ui::canvas::CanvasSpace::background)) commits the session (Photoshop);
//! the press still reaches the canvas's tool.
//!
//! Offsets are chars of the host's text.
//!
@@ -277,7 +278,7 @@ pub struct TextEditorOutput {
/// The selection or caret moved.
pub selection_changed: bool,
/// The typing session as one gesture over the selection. The first text change gives `Begin`, later changes
/// `Update`. `Commit` is Cmd+Enter, or Esc when Esc commits ([`TextEditor::esc_commits`], Photoshop's Use ESC
/// `Update`. `Commit` is Cmd+Enter, a press on the canvas background, or Esc when Esc commits ([`TextEditor::esc_commits`], Photoshop's Use ESC
/// Key to Commit Text, on by default): the app ends the session, keeps the text and records one undo step.
/// `Cancel` is Esc when it doesn't commit: the app ends the session and undoes it. A session with no text
/// change gives no `Commit` or `Cancel`.
@@ -307,7 +308,7 @@ impl Outcome for TextEditorOutput {
/// thicker.
/// - Clicks: one places the caret, Shift-click extends; quick clicks in one spot select more: two a word, three the
/// laid-out line, four the paragraph, five all text, the sixth starts over (Photoshop). Dragging extends by char,
/// and by word or line after a double or triple click.
/// and by word or line after a double or triple click. A press on the canvas outside the text commits.
/// - Keys: Left and Right by char (a selection collapses to its edge), with Cmd or Alt by word, Up and Down by
/// line, Cmd+Up and Cmd+Down to the text's start or end, Home and End to the line's edges, Shift extends any of
/// them. Backspace and Delete by char, Alt by word, Cmd+Backspace to the line start. Cmd+A all, Cmd+C, Cmd+X and
@@ -536,6 +537,12 @@ impl Component for TextEditor<'_> {
if let Some(range) = ending {
edits.drop_marked(range);
}
// A press elsewhere on the canvas keeps the marked text as typed, as a press on the text does.
if canvas_space(&cx.env).is_some_and(|space| space.background().pressed) {
edits.end_composition();
edits.session.dragging = None;
edits.done.commit = true;
}
for command in fired {
let command = command.along_flow(edits.host.vertical());
+17 -1
View File
@@ -4,6 +4,7 @@ use std::cell::{Cell, RefCell};
use std::ops::Range;
use std::rc::Rc;
use craft_behavior::CanvasView;
use craft_behavior::kurbo::Point;
use craft_text::quotes::QuoteStyle;
use craft_text::ui::editor::{CaretLine, EditKind, HostLine, StyleKey, TextEditor, TextEditorHost, TextSelection};
@@ -106,7 +107,10 @@ fn session_on(host: Host, quotes: Option<QuoteStyle>) -> Session {
let (shown_host, shown_selection, shown_commits) = (Rc::clone(&host), Rc::clone(&selection), Rc::clone(&commits));
let mut fixture = Fixture::new(move |ui| {
let (mut host, mut selection) = (shown_host.borrow_mut(), shown_selection.borrow_mut());
Canvas::new("Document").show(ui, |ui, body| {
let room = ui.available_rect_before_wrap();
// Document (0, 0) at the room's top left, at 100 %.
let view = CanvasView::new(room, Point::new(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0);
Canvas::new("Document").view(view).show(ui, |ui, body| {
let bounds = Rect::from_min_size(body.rect.min, vec2(200.0, 20.0));
let editor = TextEditor::new("Text", &mut *host, Bind::bound(&mut *selection));
let output = if let Some(style) = quotes { editor.smart_quotes(style) } else { editor }.show_at(ui, bounds);
@@ -141,6 +145,18 @@ fn command_enter_commits_one_session_and_a_session_without_edits_commits_nothing
assert_eq!(editor.text(), "abcd");
}
#[trial]
fn a_press_on_the_canvas_background_commits_the_session_and_one_on_the_text_does_not() {
let mut editor = session("abc", None);
editor.focus();
editor.fixture.type_text("d").settle();
editor.fixture.click_at(pos2(40.0, 10.0)).settle();
assert_eq!(editor.commits.get(), 0, "a press on the text is the editor's");
editor.fixture.click_at(pos2(300.0, 300.0)).settle();
assert_eq!(editor.commits.get(), 1, "a press on the canvas background commits the session");
assert_eq!(editor.text(), "abcd");
}
/// In vertical text Alt+Down follows the flow and loosens the tracking; a host that applies no formatting leaves
/// Alt+Up to move the caret a line up.
#[trial]