canvas: compose on a selected shape; the first preedit starts editing it

While the slide has the keyboard and one shape is selected, the canvas is the
app's own IME editor: it publishes an IME output for the shape's box, and the
key stage's first preedit starts editing the shape through
Session::compose_text. The framework text editor adopts that composition when
it takes the focus. Keys and typed text stay on the key stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dylan M. Taylor
2026-11-28 15:50:37 +00:00
co-authored by Claude Opus 5.5
parent fab5453a6e
commit 55c4780409
5 changed files with 55 additions and 0 deletions
+1
View File
@@ -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}),
+10
View File
@@ -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());
}
}
_ => {}
}
}
+1
View File
@@ -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);
+41
View File
@@ -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);
});
}
+2
View File
@@ -181,6 +181,8 @@ pub struct SlideApp {
pub canvas_menu: Option<canvas::context_menu::CanvasMenu>,
/// Optional motion-mode request for the text editor host: `Some(false)` turns the caret blink off.
pub(crate) caret_blink: Option<bool>,
/// 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.