mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
behavior, ui-contract: Enter reaches an editor as EditorInput::Newline { shift }, each routed intent keeping the Shift of the press it came from, so an editor tells Shift+Enter from Enter in the order they came
This commit is contained in:
@@ -87,7 +87,12 @@ pub enum EditorInput {
|
||||
/// An edit command from the command queue.
|
||||
Edit(EditCommand),
|
||||
/// Enter.
|
||||
Newline,
|
||||
#[non_exhaustive]
|
||||
Newline {
|
||||
/// Shift was held: an editor that gives Shift+Enter its own meaning (a line break within the paragraph) tells
|
||||
/// it from a plain Enter.
|
||||
shift: bool,
|
||||
},
|
||||
/// Tab.
|
||||
Tab,
|
||||
/// Shift+Tab, which reaches only a document canvas's editor part that keeps Tab.
|
||||
@@ -107,6 +112,11 @@ impl EditorInput {
|
||||
EditorInput::Delete { forward, by, shift }
|
||||
}
|
||||
|
||||
/// A `Newline`, with Shift held when `shift`.
|
||||
pub const fn newline(shift: bool) -> Self {
|
||||
EditorInput::Newline { shift }
|
||||
}
|
||||
|
||||
/// A `DeleteSurrounding` of `before` and `after` characters.
|
||||
pub const fn delete_surrounding(before: usize, after: usize) -> Self {
|
||||
EditorInput::DeleteSurrounding { before, after, unit: SurroundingUnit::Chars }
|
||||
|
||||
@@ -393,7 +393,7 @@ impl CellGrid<'_> {
|
||||
edits.push(GridEdit::BeginEdit { typed });
|
||||
}
|
||||
}
|
||||
(Mode::Ready, _) | (_, EditorInput::Newline | EditorInput::Tab | EditorInput::Cancel) => {}
|
||||
(Mode::Ready, _) | (_, EditorInput::Newline { .. } | EditorInput::Tab | EditorInput::Cancel) => {}
|
||||
(_, EditorInput::Move(..)) if state.pointing() => {}
|
||||
(_, input) => {
|
||||
state.point = None;
|
||||
|
||||
@@ -544,7 +544,7 @@ impl Component for TextEditor<'_> {
|
||||
}
|
||||
EditorInput::ImeCommit => ending = edits.session.preedit.take(),
|
||||
EditorInput::Text(text) => edits.done.changed |= edits.insert_typed(&text),
|
||||
EditorInput::Newline if edits.session.preedit.is_none() => edits.done.changed |= edits.insert_text("\n", EditKind::Typed),
|
||||
EditorInput::Newline { .. } if edits.session.preedit.is_none() => edits.done.changed |= edits.insert_text("\n", EditKind::Typed),
|
||||
EditorInput::Tab => edits.done.changed |= edits.insert_text("\t", EditKind::Typed),
|
||||
EditorInput::Move(step, mods) => edits.motion(step, mods),
|
||||
EditorInput::Delete { forward, by, .. } => edits.done.changed |= edits.delete(forward, by),
|
||||
|
||||
@@ -193,7 +193,7 @@ impl NotesEditor<'_> {
|
||||
};
|
||||
match input {
|
||||
EditorInput::Text(text) => insert(plain, cursor, &text),
|
||||
EditorInput::Newline => insert(plain, cursor, "\n"),
|
||||
EditorInput::Newline { .. } => insert(plain, cursor, "\n"),
|
||||
EditorInput::Tab => insert(plain, cursor, "\t"),
|
||||
EditorInput::Edit(EditCommand::Paste) => clip.is_some_and(|text| insert(plain, cursor, text)),
|
||||
EditorInput::Edit(EditCommand::Cut) if !selected.is_empty() => insert(plain, cursor, ""),
|
||||
|
||||
@@ -219,9 +219,17 @@ struct Item {
|
||||
intent: Intent,
|
||||
/// A physical left or right arrow: under right-to-left text it reads as the opposite logical move.
|
||||
horizontal: bool,
|
||||
/// The place among the frame's events of the press or text it came from ([`Batch::place`]); `None` for an intent
|
||||
/// of no one event.
|
||||
place: Option<usize>,
|
||||
/// The press or text it came from; `None` for an intent of no one event.
|
||||
source: Option<Source>,
|
||||
}
|
||||
|
||||
/// The press or text an intent came from.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Source {
|
||||
/// Its place among the frame's events ([`Batch::place`]).
|
||||
place: usize,
|
||||
/// Shift was held at the press; never for a text.
|
||||
shift: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The intents routed to a component and what it leaves untaken: Submit, Cancel, Escape and context requests.
|
||||
|
||||
use super::route::push;
|
||||
use super::{Nudge, Stage, current};
|
||||
use super::{Nudge, Source, Stage, current};
|
||||
use crate::runtime::focus::{self};
|
||||
use craft_behavior::focus::{EscapeFacts, EscapeStep, escape_step};
|
||||
use craft_behavior::{AccessRequest, CancelRule, EditCommand, EditorInput, Intent, MoveMods, NavMove, PartRef};
|
||||
@@ -13,9 +13,9 @@ use std::sync::Arc;
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Intents {
|
||||
items: Vec<(Intent, bool)>,
|
||||
/// The place among the frame's events of each of `items` that came from one press or text (`keys::Item::place`);
|
||||
/// read by index, so an item pushed without one has none.
|
||||
places: Vec<Option<usize>>,
|
||||
/// The press or text each of `items` came from (`keys::Item::source`); read by index, so an item pushed without
|
||||
/// one has none.
|
||||
sources: Vec<Option<Source>>,
|
||||
took_arrows: Vec<NavMove>,
|
||||
nudge: Option<Nudge>,
|
||||
chords: Vec<Chord>,
|
||||
@@ -196,10 +196,10 @@ impl Intents {
|
||||
}
|
||||
|
||||
/// Takes the next intent an editor reads as input: typed text, a move, a delete, an edit command, Enter as a
|
||||
/// newline, Tab and Shift+Tab, and Escape as a cancel. An arrow move the editor's `keep` does not keep stays for the
|
||||
/// component's navigation (18.4), and with `KeepKeys::None` so do Home, End and the page keys: the editor is
|
||||
/// a part of a grid that navigates until a cell opens. With it, the place among the frame's events of the press or
|
||||
/// text it came from, `None` for an intent of no one event.
|
||||
/// newline with its Shift, Tab and Shift+Tab, and Escape as a cancel. An arrow move the editor's `keep` does not
|
||||
/// keep stays for the component's navigation (18.4), and with `KeepKeys::None` so do Home, End and the page keys:
|
||||
/// the editor is a part of a grid that navigates until a cell opens. With it, the place among the frame's events of
|
||||
/// the press or text it came from, `None` for an intent of no one event.
|
||||
pub(crate) fn take_editor_input(&mut self, keep: focus::KeepKeys) -> Option<(EditorInput, Option<usize>)> {
|
||||
let kept = |item: &Intent| match item {
|
||||
Intent::Move(NavMove::Up | NavMove::Down, _) => matches!(keep, focus::KeepKeys::BlockArrows | focus::KeepKeys::Tab),
|
||||
@@ -207,9 +207,10 @@ impl Intents {
|
||||
Intent::Move(NavMove::Start | NavMove::End | NavMove::PageUp | NavMove::PageDown, _) => keep != focus::KeepKeys::None,
|
||||
_ => true,
|
||||
};
|
||||
let at = self.items.iter().position(|(item, taken)| !taken && kept(item) && editor_input_of(item).is_some())?;
|
||||
let at = self.items.iter().position(|(item, taken)| !taken && kept(item) && editor_input_of(item, false).is_some())?;
|
||||
self.items[at].1 = true;
|
||||
editor_input_of(&self.items[at].0).map(|input| (input, self.places.get(at).copied().flatten()))
|
||||
let source = self.sources.get(at).copied().flatten();
|
||||
editor_input_of(&self.items[at].0, source.is_some_and(|source| source.shift)).map(|input| (input, source.map(|source| source.place)))
|
||||
}
|
||||
|
||||
/// Takes the next `Intent::Access` request that `wanted` accepts.
|
||||
@@ -234,13 +235,14 @@ impl Intents {
|
||||
}
|
||||
}
|
||||
|
||||
fn editor_input_of(intent: &Intent) -> Option<EditorInput> {
|
||||
/// The editor input of `intent`, from a press with Shift held when `shift`.
|
||||
fn editor_input_of(intent: &Intent, shift: bool) -> Option<EditorInput> {
|
||||
match intent {
|
||||
Intent::Text(text) => Some(EditorInput::Text(Arc::clone(text))),
|
||||
Intent::Move(mv, mods) => Some(EditorInput::Move(*mv, *mods)),
|
||||
Intent::Delete { forward, by, shift, .. } => Some(EditorInput::delete_with_shift(*forward, *by, *shift)),
|
||||
Intent::Edit(command) => Some(EditorInput::Edit(*command)),
|
||||
Intent::Submit => Some(EditorInput::Newline),
|
||||
Intent::Submit => Some(EditorInput::newline(shift)),
|
||||
Intent::Tab => Some(EditorInput::Tab),
|
||||
Intent::BackTab => Some(EditorInput::BackTab),
|
||||
Intent::Cancel => Some(EditorInput::Cancel),
|
||||
@@ -257,7 +259,7 @@ pub fn intents_of(ctx: &Context, id: Option<Id>, rtl: bool) -> Intents {
|
||||
(true, NavMove::Forward) => NavMove::Back,
|
||||
(_, mv) => mv,
|
||||
};
|
||||
let places = routed.items.iter().map(|item| item.place).collect();
|
||||
let sources = routed.items.iter().map(|item| item.source).collect();
|
||||
let items = routed
|
||||
.items
|
||||
.into_iter()
|
||||
@@ -268,7 +270,7 @@ pub fn intents_of(ctx: &Context, id: Option<Id>, rtl: bool) -> Intents {
|
||||
.collect();
|
||||
Intents {
|
||||
items,
|
||||
places,
|
||||
sources,
|
||||
took_arrows: Vec::new(),
|
||||
nudge: routed.nudge,
|
||||
chords: routed.chords,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The begin hook's routing: key presses, shortcuts and AccessKit requests to the intents of their owners.
|
||||
|
||||
use super::input::chrome_toggle_of;
|
||||
use super::{Batch, EscapePress, Item, MenuKey, Node, Nudge, Press, Routed, Shortcuts, Taken, current, with};
|
||||
use super::{Batch, EscapePress, Item, MenuKey, Node, Nudge, Press, Routed, Shortcuts, Source, Taken, current, with};
|
||||
use crate::runtime::commands::{self, FirePhase};
|
||||
use crate::runtime::egui::input::{Action, ActionKind};
|
||||
use crate::runtime::focus::{self, StopRec};
|
||||
@@ -75,7 +75,7 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting {
|
||||
let route = press_route(ctx, os, rec, press, driven.or(field_list));
|
||||
apply_press(ctx, rec, press, route, &mut routed, &mut activated)
|
||||
};
|
||||
stamp(&mut routed, press.place);
|
||||
stamp(&mut routed, Source { place: press.place, shift: press.mods.shift });
|
||||
}
|
||||
// The focused stop reads its typing from egui's input when it is a text field (its egui text widget) or an app's
|
||||
// own widget; any other taker reads the text as an intent only, and the text leaves egui's input.
|
||||
@@ -100,7 +100,7 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting {
|
||||
for (to, intent) in route {
|
||||
push(&mut routed, Some(to), intent, false);
|
||||
}
|
||||
stamp(&mut routed, typed.place);
|
||||
stamp(&mut routed, Source { place: typed.place, shift: false });
|
||||
}
|
||||
} else {
|
||||
// With nothing focused, a plain Enter submits to the window, as an unfocused Escape cancels: a tool's pending
|
||||
@@ -213,14 +213,14 @@ fn remap_mods(mods: &[(usize, Mods)], kept: &[bool]) -> Vec<(usize, Mods)> {
|
||||
}
|
||||
|
||||
pub(super) fn push(routed: &mut HashMap<Option<Id>, Routed>, target: Option<Id>, intent: Intent, horizontal: bool) {
|
||||
routed.entry(target).or_default().items.push(Item { intent, horizontal, place: None });
|
||||
routed.entry(target).or_default().items.push(Item { intent, horizontal, source: None });
|
||||
}
|
||||
|
||||
/// Gives the intents routed so far without a place the place `place` of the press or text they came from: routing
|
||||
/// stamps each event's intents right after it pushed them, so only that event's are without one.
|
||||
fn stamp(routed: &mut HashMap<Option<Id>, Routed>, place: usize) {
|
||||
/// Gives the intents routed so far without a source the press or text `source` they came from: routing stamps each
|
||||
/// event's intents right after it pushed them, so only that event's are without one.
|
||||
fn stamp(routed: &mut HashMap<Option<Id>, Routed>, source: Source) {
|
||||
for item in routed.values_mut().flat_map(|routed| routed.items.iter_mut()) {
|
||||
item.place.get_or_insert(place);
|
||||
item.source.get_or_insert(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -335,6 +335,24 @@ impl Rig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Shift+Enter reaches an editor as a newline with Shift held, told from a plain Enter in the same frame, in the order
|
||||
/// they were pressed: an editor gives Shift+Enter its own meaning (a line break within the paragraph). Guards against
|
||||
/// the press's Shift being lost between the press and the editor input.
|
||||
#[trial]
|
||||
fn shift_enter_reaches_an_editor_as_a_newline_with_shift_in_order() {
|
||||
let mut rig = Rig::new();
|
||||
rig.focus();
|
||||
rig.frame(vec![key_press(Key::Enter, Modifiers::SHIFT), key_press(Key::Enter, Modifiers::NONE), key_press(Key::Enter, Modifiers::SHIFT)]);
|
||||
let inputs = rig.take();
|
||||
assert!(
|
||||
matches!(
|
||||
inputs.as_slice(),
|
||||
[EditorInput::Newline { shift: true, .. }, EditorInput::Newline { shift: false, .. }, EditorInput::Newline { shift: true, .. }]
|
||||
),
|
||||
"each Enter keeps its own Shift, in order: {inputs:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Text, Backspace, Tab and Enter reach an editor as one input stream in the order they were typed; a read-only
|
||||
/// editor still receives moves but none of the inputs that change text.
|
||||
#[trial]
|
||||
@@ -347,7 +365,7 @@ fn editor_input_reaches_an_editor_part_and_read_only_filtering_drops_edits() {
|
||||
rig.frame(vec![key_press(Key::Enter, Modifiers::NONE)]);
|
||||
let inputs = rig.take();
|
||||
assert!(
|
||||
matches!(inputs.as_slice(), [EditorInput::Text(text), EditorInput::Delete { forward: false, .. }, EditorInput::Tab, EditorInput::Newline] if &**text == "x"),
|
||||
matches!(inputs.as_slice(), [EditorInput::Text(text), EditorInput::Delete { forward: false, .. }, EditorInput::Tab, EditorInput::Newline { shift: false, .. }] if &**text == "x"),
|
||||
"the inputs arrive in the pass they are typed, in order: {inputs:?}"
|
||||
);
|
||||
|
||||
@@ -647,7 +665,7 @@ fn keys_that_end_a_composition_do_not_reach_the_editor_while_it_composes() {
|
||||
assert!(inputs.is_empty(), "the Enter that confirmed the commit, a frame late, is no newline: {inputs:?}");
|
||||
rig.frame(vec![key_press(Key::Enter, Modifiers::NONE)]);
|
||||
let inputs = rig.take();
|
||||
assert!(matches!(inputs.as_slice(), [EditorInput::Newline]), "after the guard frame Enter is a newline: {inputs:?}");
|
||||
assert!(matches!(inputs.as_slice(), [EditorInput::Newline { .. }]), "after the guard frame Enter is a newline: {inputs:?}");
|
||||
|
||||
rig.frame(vec![preedit("x")]);
|
||||
assert!(matches!(rig.take().as_slice(), [EditorInput::Preedit { .. }]), "a new composition starts");
|
||||
|
||||
Reference in New Issue
Block a user