diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 3e93a1a78..6d4a58bea 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -172,6 +172,7 @@ impl SlideApp { ruler_tab_kind: Default::default(), canvas_menu: None, caret_blink: None, + selection_ime: Default::default(), last_fill: json!("accent1"), last_outline: json!("tx1"), table_pen: json!({"width": 1.0}), diff --git a/crates/ui-egui/src/app/keys.rs b/crates/ui-egui/src/app/keys.rs index 6b3b95602..8c28dd295 100644 --- a/crates/ui-egui/src/app/keys.rs +++ b/crates/ui-egui/src/app/keys.rs @@ -62,6 +62,16 @@ impl SlideApp { self.set_status(error.to_string()); } } + // The canvas composes for a selected shape (`text_host::show_selection_ime`): the first preedit starts + // editing it, and the framework editor takes the composition from there. A commit or deletion with + // nothing edited is a stale one and must not replace the shape's text. + egui::Event::Ime(egui::ImeEvent::Preedit { text, active_range_chars }) if !focus::has_any(ctx) => { + if self.session.active().is_some_and(|d| d.selection.text.is_none() && d.selection.shapes.len() == 1) + && let Err(error) = self.session.compose_text(&text, active_range_chars.map(|range| (range.start, range.end))) + { + self.set_status(error.to_string()); + } + } _ => {} } } diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index b6b0b71d4..aa8b0b96f 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -99,6 +99,7 @@ pub(crate) fn show(app: &mut SlideApp, ui: &mut Ui) { guides::show_guides(app, ui, &space, canvas_id); handles::show(app, ui, &space); ask = text_host::show_editor(app, ui, &space).and_then(|output| output.context); + text_host::show_selection_ime(app, ui, &space); rulers::show_text_ruler(app, ui, &space, room, strips_clip); squiggles::show_marks(app, ui, &space); media::show_controls(app, ui, &space); diff --git a/crates/ui-egui/src/canvas/text_host.rs b/crates/ui-egui/src/canvas/text_host.rs index 2a713e5fc..7c80b5d17 100644 --- a/crates/ui-egui/src/canvas/text_host.rs +++ b/crates/ui-egui/src/canvas/text_host.rs @@ -434,3 +434,44 @@ pub(crate) fn show_editor(app: &mut crate::SlideApp, ui: &mut Ui, space: &Canvas } Some(output) } + +/// Publishes the input method for the one selected shape while the slide has the keyboard and no editor focus owns it: +/// the canvas is then the app's own IME editor, so the platform composes and the first preedit starts editing the shape +/// (`Session::compose_text`, from the key stage). It also covers the frame that edit starts in, until the framework +/// editor takes the focus and adopts the composition. Keys and typed text stay with the key stage. +pub(crate) fn show_selection_ime(app: &mut crate::SlideApp, ui: &Ui, space: &CanvasSpace) { + let ctx = ui.ctx(); + let viewport = ctx.viewport_id(); + let blocked = crate::dialogs::modal_open(ctx) || app.palette_open || app.backstage_open() || app.opening.is_some() || focus::has_any(ctx); + let target = app.session.active().filter(|_| !blocked).and_then(|st| { + let id = match &st.selection.text { + Some(edited) if !edited.notes => edited.shape, + Some(_) => return None, + None if st.selection.shapes.len() == 1 => st.selection.shapes[0], + None => return None, + }; + let shape = st.shape(id)?; + let x = deckcraft_engine::cmd::xfrm_of(&st.doc, &st.selection, shape); + let a = x.affine(); + let rect = screen_bounds(quad(deckcraft_geom::Rect::new(0.0, 0.0, x.w, x.h)).map(|p| space.view.to_screen(a * p))); + Some((egui::Id::unique(("slide-selection-ime", st.uid, id.0)), st.revision, rect)) + }); + let Some((owner, revision, rect)) = target else { + app.selection_ime.observe(viewport, None); + return; + }; + let observation = egui::ime_context::ImeObservation::new( + ctx.cumulative_frame_nr_for(viewport), + ctx.cumulative_pass_nr_for(viewport), + egui::ime_context::ImeTarget::new(owner, 0), + ) + .purpose(egui::IMEPurpose::Normal) + .unavailable_revision(revision); + let Some(update) = app.selection_ime.observe(viewport, Some(observation)) else { return }; + ctx.output_mut(|output| { + let mut ime = egui::output::IMEOutput::new(update.owner, update.context_generation, update.purpose, rect, rect); + ime.surrounding_text = update.surrounding_text; + ime.change_cause = update.change_cause; + output.ime = Some(ime); + }); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 7483adeab..adea37091 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -181,6 +181,8 @@ pub struct SlideApp { pub canvas_menu: Option, /// Optional motion-mode request for the text editor host: `Some(false)` turns the caret blink off. pub(crate) caret_blink: Option, + /// The input method context of the selected shape the canvas composes for (`text_host::show_selection_ime`). + pub(crate) selection_ime: egui::ime_context::ImeContextTracker, /// Shape Fill's last command colour, initially Accent 1. pub last_fill: Value, /// Shape Outline's last command colour, initially Text 1.