Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Text: IME composition in place, commit as one step (#322)

aiongg1276181cde93
diff --git a/Cargo.lock b/Cargo.lock
index f551976b0..e7ae33f16 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -879,6 +879,8 @@ dependencies = [
"egui",
"log",
"muda",
+ "objc2 0.6.4",
+ "objc2-app-kit 0.3.2",
"rfd",
"serde",
"serde_json",
diff --git a/ROADMAP.md b/ROADMAP.md
index fb5b68c88..dc180cd0d 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -136,6 +136,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope
- Object Export Options (`object.exportOptions`): tagged-PDF artifact, EPUB/HTML rasterize (the object as an image), alignment and page break before, alt text.
- Scripts panel (Window ▸ Utilities ▸ Scripts, `script.run`): saved command scripts (with `$N.path` references to earlier results) run as one undo step; edit, add, load and delete them in the panel. Scripts use DesignCraft's command language, not ExtendScript.
- Drag and drop text editing: drag selected text to move it (Alt copies) with its formatting (`text.release`).
+- Input methods (IME) in the Type tool: a Chinese, Japanese or Korean composition shows its marked text in place in the story, underlined (the clause being converted twice), with the IME's candidate window at the caret through zoom, scrolling, view rotation and vertical text; the commit is one `text.insert` and one undo step, a cancelled composition leaves the document as it was; keys, shortcuts, the clipboard and Undo wait while the IME composes, and a click or another command keeps the marked text as typed. The control channel's `ui.ime` and the MCP `ime` tool (headless too) send IME events. Not yet checked by hand with a system IME (macOS, Windows, Linux, web).
- Image Import Options: place any page of a multi-page PDF (`file.place {pdfPage}`; the app asks which page), shown on screen and embedded as that page in PDF export.
- Define Lists: named numbered lists that continue across stories in page order (`list.define`, paragraph `listName` / `startAt`), round-tripped through IDML; `null` in `type.para` / `type.char` now removes an override.
- View › Overprint Preview (⌥⇧⌘Y): overprinting fills, strokes and 100% [Black] text mix with what's beneath on screen.
diff --git a/apps/designcraft-web/src/web.rs b/apps/designcraft-web/src/web.rs
index fe7cca4ae..1fb71d7b3 100644
--- a/apps/designcraft-web/src/web.rs
+++ b/apps/designcraft-web/src/web.rs
@@ -106,7 +106,7 @@ impl eframe::App for WebShell {
}
fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) {
- designcraft_ui_egui::drop_key_name_text(raw, ctx.text_edit_focused());
+ designcraft_ui_egui::drop_key_name_text(raw, ctx.text_edit_focused() || self.app.canvas_ime());
self.app.raw_input_hook(raw);
}
diff --git a/apps/designcraft/Cargo.toml b/apps/designcraft/Cargo.toml
index 663b7792a..cd70c3244 100644
--- a/apps/designcraft/Cargo.toml
+++ b/apps/designcraft/Cargo.toml
@@ -32,3 +32,6 @@ winit = { version = "0.30", default-features = false, features = ["x11"] }
[target.'cfg(target_os = "macos")'.dependencies]
muda = { version = "0.21", default-features = false }
+# The input context, to drop the IME's marked text when the Type tool ends a composition.
+objc2 = "0.6"
+objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSTextInputContext"] }
diff --git a/apps/designcraft/src/main.rs b/apps/designcraft/src/main.rs
index c7946d0a9..6211fd098 100644
--- a/apps/designcraft/src/main.rs
+++ b/apps/designcraft/src/main.rs
@@ -57,6 +57,10 @@ impl eframe::App for App {
}
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
self.app.ui(ui);
+ #[cfg(target_os = "macos")]
+ if self.app.take_ime_discard() {
+ discard_marked_text();
+ }
}
fn on_exit(&mut self) {
// A normal exit before the third frame is not a crash.
@@ -67,6 +71,17 @@ impl eframe::App for App {
}
}
+/// Tell the macOS input method to drop its composition (the Type tool kept the marked text as
+/// typed). winit's IME toggle only clears its own copy, so the IME would type it again.
+#[cfg(target_os = "macos")]
+fn discard_marked_text() {
+ if let Some(mtm) = objc2::MainThreadMarker::new()
+ && let Some(ic) = objc2_app_kit::NSTextInputContext::currentInputContext(mtm)
+ {
+ ic.discardMarkedText();
+ }
+}
+
/// The per-user settings directory: `ui.json`, `prefs.json`, `gpu.json` and `logs/` live here.
fn config_dir() -> Option<std::path::PathBuf> {
if cfg!(target_os = "macos") {
diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs
index e3ede0aa4..62ce87683 100644
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@ -465,7 +465,10 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
let tracking = s.doc()?.doc.settings.track_changes;
let autocorrect = (s.prefs.autocorrect && !tracking && !raw).then(|| s.prefs.autocorrect_list.clone());
let typing = typing_format(s).cloned();
- s.typing_format = None;
+ // A preview (IME marked text) leaves the typing format to the text typed in the end.
+ if s.active().is_none_or(|d| d.interaction.is_none()) {
+ s.typing_format = None;
+ }
s.edit(|d, sel| {
let t = sel.text.ok_or_else(|| bad("text.insert", "no insertion point"))?;
let st = d.text_story_mut(t.story, t.cell).ok_or(designcraft_doc::DocError::NoStory(t.story))?;
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 8f89aefa7..de10b0a8d 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -354,11 +354,18 @@ impl Session {
}
pub fn set_active(&mut self, i: usize) {
if i < self.docs.len() && self.active != Some(i) {
+ // The composition belongs to the document it shows in.
+ if let Err(e) = self.end_composition() {
+ log::warn!("ending the IME composition: {e}");
+ }
self.stop_data_preview();
self.active = Some(i);
}
}
pub fn add_document(&mut self, d: DocState) -> usize {
+ if let Err(e) = self.end_composition() {
+ log::warn!("ending the IME composition: {e}");
+ }
self.stop_data_preview();
self.docs.push(d);
self.active = Some(self.docs.len() - 1);
@@ -390,6 +397,12 @@ impl Session {
/// Run command `id`. A panic inside it becomes [`EngineError::Internal`] and leaves the
/// document as it was (see [`guard`]).
pub fn execute(&mut self, id: &str, params: &Value) -> Result<Value> {
+ // A command from outside the IME ends its composition first, keeping the marked text as
+ // typed: Undo then takes back that typing, and nothing runs on top of the preview the
+ // composition would drop. Queries and recovery saves leave it alone.
+ if self.tool.composing() && id != "file.recovery.save" && find_command(id).is_some_and(|c| c.journal || c.undoable) {
+ self.end_composition()?;
+ }
self.guarded(id, |s| s.execute_unguarded(id, params))
}
@@ -554,4 +567,6 @@ mod tests;
#[cfg(test)]
mod tests_idml;
#[cfg(test)]
+mod tests_ime;
+#[cfg(test)]
mod tests_table;
diff --git a/crates/engine/src/tests_ime.rs b/crates/engine/src/tests_ime.rs
new file mode 100644
index 000000000..66a908e64
--- /dev/null
+++ b/crates/engine/src/tests_ime.rs
@@ -0,0 +1,147 @@
+//! Input method (IME) composition in the Type tool: marked text previews in place, a commit is
+//! one `text.insert`, a cancel leaves nothing behind, and keys wait for the IME.
+
+use designcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
+use serde_json::json;
+
+use super::*;
+
+fn session() -> Session {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ s
+}
+
+/// A text frame holding `text`, with the Type tool's caret at its end; returns the story id.
+fn typing_into(s: &mut Session, text: &str) -> u64 {
+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap();
+ s.set_tool("type");
+ if !text.is_empty() {
+ s.execute("text.insert", &json!({"text": text, "raw": true})).unwrap();
+ }
+ r["story"].as_u64().unwrap()
+}
+
+fn text(s: &mut Session, sid: u64) -> String {
+ s.execute("story.get", &json!({"story": sid})).unwrap()["text"].as_str().unwrap().to_string()
+}
+
+fn steps(s: &Session) -> usize {
+ s.doc().unwrap().history.undo.len()
+}
+
+#[test]
+fn ime_composition_shows_in_place_and_commits_as_one_step() {
+ // Romaji → kana → conversion → commit, as the macOS Japanese IME sends it.
+ let mut s = session();
+ let v = ViewInfo::default();
+ let sid = typing_into(&mut s, "曲:");
+ let (before, journal) = (steps(&s), s.journal.len());
+ let mut carets = Vec::new();
+ for (t, r) in [("g", 1..1), ("が", 1..1), ("がg", 2..2), ("ががく", 3..3)] {
+ s.tool_preedit(t, Some(r), v).unwrap();
+ assert_eq!(text(&mut s, sid), format!("曲:{t}"), "marked text lays out in place");
+ assert!(s.tool_composing());
+ carets.push(s.tool_ime_caret(v).expect("the candidate window has a place"));
+ }
+ // The candidate window follows the IME's cursor along the line.
+ assert!(carets.windows(2).all(|w| w[1].0.x >= w[0].0.x), "{carets:?}");
+ let (top, bottom) = carets[3];
+ assert!((top.x - bottom.x).abs() < 1e-6 && top.y < bottom.y, "an upright caret line in horizontal text: {top:?} {bottom:?}");
+ // Conversion: the window goes to the clause being converted, here the whole marked text.
+ s.tool_preedit("雅楽", Some(0..2), v).unwrap();
+ assert_eq!(text(&mut s, sid), "曲:雅楽");
+ assert!(s.tool_ime_caret(v).unwrap().0.x < carets[3].0.x);
+ let marks = s.overlays(v);
+ assert!(marks.len() >= 2, "the marked text is underlined, the clause twice: {marks:?}");
+ assert_eq!((steps(&s), s.journal.len()), (before, journal), "composing records nothing");
+ // The commit replaces the marked text: one undo step, one command.
+ s.tool_ime_commit("雅楽", v).unwrap();
+ assert!(!s.tool_composing());
+ assert_eq!(text(&mut s, sid), "曲:雅楽");
+ assert_eq!(steps(&s), before + 1);
+ assert_eq!(s.journal[journal..], [("text.insert".to_string(), json!({"text": "雅楽"}))]);
+ assert_eq!(s.doc().unwrap().selection.text.map(|t| t.focus), Some("曲:雅楽".len()));
+ assert!(s.overlays(v).is_empty(), "no underline once typed");
+ // macOS and Windows clear the marked text before they commit: the same result.
+ s.tool_preedit("おと", Some(2..2), v).unwrap();
+ s.tool_preedit("", None, v).unwrap();
+ s.tool_ime_commit("音", v).unwrap();
+ assert_eq!(text(&mut s, sid), "曲:雅楽音");
+ assert_eq!(steps(&s), before + 2);
+ s.execute("edit.undo", &json!({})).unwrap();
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(text(&mut s, sid), "曲:");
+}
+
+#[test]
+fn ime_cancel_leaves_no_undo_step_and_keeps_the_text() {
+ let mut s = session();
+ let v = ViewInfo::default();
+ let sid = typing_into(&mut s, "雅楽");
+ let before = steps(&s);
+ let doc = s.doc().unwrap().doc.clone();
+ s.tool_preedit("えんそう", Some(4..4), v).unwrap();
+ assert_eq!(text(&mut s, sid), "雅楽えんそう");
+ // Escape in the IME: the marked text goes and the document is the one before.
+ s.tool_preedit("", None, v).unwrap();
+ assert!(!s.tool_composing());
+ assert!(s.doc().unwrap().interaction.is_none(), "no gesture left open");
+ assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &doc));
+ assert_eq!(steps(&s), before);
+ // A stray clear (no composition) never deletes the selected text.
+ s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 3})).unwrap();
+ s.tool_preedit("", None, v).unwrap();
+ assert_eq!(text(&mut s, sid), "雅楽");
+ // A composition over a selection replaces it; cancelling it brings the selection back.
+ s.tool_preedit("が", Some(1..1), v).unwrap();
+ assert_eq!(text(&mut s, sid), "が楽");
+ s.tool_preedit("", None, v).unwrap();
+ assert_eq!(text(&mut s, sid), "雅楽");
+ assert_eq!(s.doc().unwrap().selection.text.map(|t| t.range()), Some(0..3));
+ s.tool_preedit("が", Some(1..1), v).unwrap();
+ s.tool_ime_commit("我", v).unwrap();
+ assert_eq!(text(&mut s, sid), "我楽");
+ assert_eq!(steps(&s), before + 1);
+}
+
+#[test]
+fn keys_belong_to_the_ime_while_it_composes() {
+ let mut s = session();
+ let v = ViewInfo::default();
+ let sid = typing_into(&mut s, "");
+ s.tool_preedit("しょうこ", Some(4..4), v).unwrap();
+ // Backspace, arrows, Enter and Escape edit the composition in the IME, not the story.
+ for k in [ToolKey::Backspace, ToolKey::Left, ToolKey::Enter, ToolKey::Escape] {
+ assert!(s.tool_key(k, Mods::default(), v).unwrap(), "{k:?} is the IME's");
+ }
+ assert_eq!(text(&mut s, sid), "しょうこ");
+ assert!(s.tool_composing());
+ assert_eq!(s.tool_id(), "type", "Escape didn't leave the Type tool");
+ // An Undo that comes anyway (a script, the control channel) ends the composition first: the
+ // marked text becomes typing, which Undo takes back.
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert!(!s.tool_composing());
+ assert_eq!(text(&mut s, sid), "");
+ s.execute("edit.redo", &json!({})).unwrap();
+ assert_eq!(text(&mut s, sid), "しょうこ");
+ // A click keeps the marked text as typed.
+ s.tool_preedit("ひちりき", Some(4..4), v).unwrap();
+ let at = s.layout().to_canvas(designcraft_doc::SpreadRef::Doc(0), designcraft_geom::Point::new(100.0, 150.0));
+ s.pointer(&PointerEvent::new(PointerKind::Down, at.x, at.y), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, at.x, at.y), v).unwrap();
+ assert!(!s.tool_composing());
+ assert_eq!(text(&mut s, sid), "しょうこひちりき");
+}
+
+#[test]
+fn an_ime_commit_takes_the_format_chosen_at_the_caret() {
+ let mut s = session();
+ let v = ViewInfo::default();
+ let sid = typing_into(&mut s, "A");
+ s.execute("type.char", &json!({"attrs": {"size": 24}})).unwrap();
+ s.tool_preedit("にほん", Some(3..3), v).unwrap();
+ s.tool_ime_commit("日本", v).unwrap();
+ s.execute("text.select", &json!({"story": sid, "anchor": 1, "focus": 7})).unwrap();
+ assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], 24.0);
+}
diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs
index 1bf8db930..a294aedd4 100644
--- a/crates/engine/src/tooling.rs
+++ b/crates/engine/src/tooling.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
use designcraft_geom::Unit;
pub use designcraft_tools::SnapView;
-use designcraft_tools::{Action, CanvasLayout, Cursor, Mods, Overlay, PointerEvent, ToolContext, ToolKey};
+use designcraft_tools::{Action, CanvasLayout, Cursor, Mods, Overlay, PointerEvent, PointerKind, ToolContext, ToolKey};
use serde::Serialize;
use serde_json::Value;
@@ -58,6 +58,10 @@ impl Session {
if self.tool.id() == id {
return;
}
+ // A composition ends with the tool that shows it: its marked text stays as typed.
+ if let Err(e) = self.end_composition() {
+ log::warn!("ending the IME composition: {e}");
+ }
self.tool = designcraft_tools::create(id);
// Leaving the Type tool keeps the text frame selected.
if id != "type"
@@ -98,11 +102,20 @@ impl Session {
}
pub fn pointer(&mut self, ev: &PointerEvent, view: ViewInfo) -> Result<()> {
+ // A press ends a composition, keeping its marked text as typed (like clicking away from
+ // one in a native text view); the UI tells the system IME to drop it.
+ if matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick) {
+ self.end_composition()?;
+ }
let actions = self.with_ctx(view, |t, cx| t.pointer(cx, ev)).unwrap_or_default();
self.run_actions(actions)
}
pub fn tool_key(&mut self, key: ToolKey, mods: Mods, view: ViewInfo) -> Result<bool> {
+ // Keys belong to the IME while it composes: nothing else (Escape's deselect) takes them.
+ if self.tool.composing() {
+ return Ok(true);
+ }
let actions = self.with_ctx(view, |t, cx| t.key(cx, key, mods)).unwrap_or_default();
let handled = !actions.is_empty();
self.run_actions(actions)?;
@@ -125,6 +138,55 @@ impl Session {
self.with_ctx(ViewInfo::at_zoom(1.0), |t, cx| t.wants_text(cx)).unwrap_or(false)
}
+ /// IME marked text at the text caret (see `Tool::ime_preedit`; `active_chars` counts
+ /// characters of `text`). It shows in place as a preview: no undo step, nothing journaled.
+ pub fn tool_preedit(&mut self, text: &str, active_chars: Option<std::ops::Range<usize>>, view: ViewInfo) -> Result<()> {
+ let actions = self.with_ctx(view, |t, cx| t.ime_preedit(cx, text, active_chars)).unwrap_or_default();
+ let r = self.run_actions(actions);
+ if r.is_err() && self.tool.composing() {
+ // The marked text can't show (its story went away): the composition ends with nothing
+ // typed, so no gesture stays open.
+ match self.with_ctx(view, |t, cx| t.ime_preedit(cx, "", None)) {
+ Some(cancel) => self.run_actions(cancel)?,
+ None => self.tool = designcraft_tools::create(self.tool.id()),
+ }
+ }
+ r
+ }
+
+ /// The IME committed `text`: it replaces the marked text as one `text.insert` (one undo step,
+ /// one journaled command).
+ pub fn tool_ime_commit(&mut self, text: &str, view: ViewInfo) -> Result<()> {
+ let actions = self.with_ctx(view, |t, cx| t.ime_commit(cx, text)).unwrap_or_default();
+ self.run_actions(actions)
+ }
+
+ /// Is the active tool showing uncommitted IME text?
+ pub fn tool_composing(&self) -> bool {
+ self.tool.composing()
+ }
+
+ /// End an IME composition from outside the IME (a click, a tool switch, another command): the
+ /// marked text stays as typed, as one undo step.
+ pub fn end_composition(&mut self) -> Result<()> {
+ if !self.tool.composing() {
+ return Ok(());
+ }
+ match self.with_ctx(ViewInfo::default(), |t, cx| t.ime_end(cx)) {
+ Some(actions) => self.run_actions(actions),
+ None => {
+ // No document to type into: only the tool's state goes.
+ self.tool = designcraft_tools::create(self.tool.id());
+ Ok(())
+ }
+ }
+ }
+
+ /// The caret line (canvas space) the IME candidate window follows.
+ pub fn tool_ime_caret(&mut self, view: ViewInfo) -> Option<(designcraft_geom::Point, designcraft_geom::Point)> {
+ self.with_ctx(view, |t, cx| t.ime_caret(cx)).flatten()
+ }
+
pub fn run_actions(&mut self, actions: Vec<Action>) -> Result<()> {
for a in actions {
match a {
@@ -155,7 +217,9 @@ impl Session {
let Some(it) = st.interaction.clone() else { return Err(EngineError::Other("no interaction".into())) };
st.doc = it.doc.clone();
st.selection = it.selection.clone();
- let r = self.execute(cmd, &params);
+ // Not through `execute`: a preview is part of the gesture (an IME composition included),
+ // not a command that ends it.
+ let r = self.guarded(cmd, |s| s.execute_unguarded(cmd, &params));
if let Some(st) = self.active_mut()
&& let Some(i) = st.interaction.as_mut()
{
diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs
index 0bd4a5930..6cfdf02c1 100644
--- a/crates/mcp/src/headless.rs
+++ b/crates/mcp/src/headless.rs
@@ -190,6 +190,32 @@ impl Headless {
Ok(json!({"inserted": t.chars().count()}))
}
+ /// `ui.ime {preedit?, active?: [start, end], commit?}`: input method events at the Type tool's
+ /// caret, as the app's `ui.ime` sends them: marked text shows in place with nothing recorded,
+ /// the committed text is typed as one `text.insert`, an empty `preedit` cancels.
+ fn ime(&mut self, p: &Value) -> Result<Value, String> {
+ let caret = self.session.active().is_some_and(|st| st.selection.text.is_some_and(|t| st.doc.text_story(t.story, t.cell).is_some()));
+ if !caret || !self.session.wants_text() {
+ return Err("no text caret for the input method: click into a text frame with the type tool first (pointer with tool \"type\")".into());
+ }
+ let n = |v: &Value| v.as_u64().and_then(|n| usize::try_from(n).ok());
+ let r = (|| {
+ if let Some(text) = s(p, "preedit") {
+ let active = p.get("active").and_then(Value::as_array).and_then(|a| Some(n(a.first()?)?..n(a.get(1)?)?));
+ self.session.tool_preedit(text, active, VIEW)?;
+ }
+ match s(p, "commit") {
+ // A bare line break confirms the marked text, as Enter does in some IMEs.
+ Some("\n" | "\r") => self.session.end_composition(),
+ Some(text) => self.session.tool_ime_commit(text, VIEW),
+ None => Ok(()),
+ }
+ })();
+ self.session.ui_requests.clear();
+ r.map_err(|e| e.to_string())?;
+ Ok(json!({"composing": self.session.tool_composing(), "selection": self.selection()}))
+ }
+
fn render(&mut self, p: &Value) -> Result<Value, String> {
let st = self.session.active().ok_or("no document open")?;
let page = page_index(p, st.doc.page_count())?;
@@ -246,6 +272,7 @@ impl Backend for Headless {
"ui.pointer" => self.pointer(p),
"ui.key" => self.key(p),
"ui.text" => self.text(p),
+ "ui.ime" => self.ime(p),
"ui.render" => self.render(p),
"app.open" => self.exec("file.open", &json!({"path": s(p, "path")})),
"app.save" => self.exec("file.save", &json!({"path": s(p, "path")})),
diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rs
index 3051a5ad4..420d65253 100644
--- a/crates/mcp/src/tests.rs
+++ b/crates/mcp/src/tests.rs
@@ -147,6 +147,7 @@ fn tool_definitions_are_well_formed() {
"pointer",
"key",
"type_text",
+ "ime",
"click",
"drag",
"select_tool",
@@ -225,6 +226,34 @@ fn type_tool_gesture_then_typing() {
assert_eq!(ok(&mut s, "key", json!({"key": "V"}))["tool"], "selection");
}
+#[test]
+fn ime_composes_at_the_type_tool_caret() {
+ let mut s = server();
+ // No caret yet: the error says how to get one.
+ assert_eq!(call(&mut s, "ime", json!({"preedit": "に"}))["isError"], true);
+ ok(
+ &mut s,
+ "pointer",
+ json!({"tool": "type", "events": [{"kind": "down", "x": 72, "y": 72}, {"kind": "drag", "x": 300, "y": 200}, {"kind": "up", "x": 300, "y": 200}]}),
+ );
+ let story = |s: &mut Server| {
+ let doc = ok(s, "inspect_document", json!({}));
+ let id = doc["stories"][0]["id"].as_u64().unwrap();
+ ok(s, "get_story", json!({"story": id}))["text"].as_str().unwrap().to_string()
+ };
+ assert_eq!(ok(&mut s, "ime", json!({"preedit": "にほん", "active": [3, 3]}))["composing"], true);
+ ok(&mut s, "ime", json!({"preedit": "日本", "active": [0, 2]}));
+ assert_eq!(story(&mut s), "日本", "marked text shows in place");
+ assert_eq!(ok(&mut s, "ime", json!({"commit": "日本"}))["composing"], false);
+ assert_eq!(story(&mut s), "日本");
+ // A cancelled composition leaves nothing; the commit is one undo step.
+ ok(&mut s, "ime", json!({"preedit": "ご"}));
+ ok(&mut s, "ime", json!({"preedit": ""}));
+ assert_eq!(story(&mut s), "日本");
+ ok(&mut s, "execute", json!({"command": "edit.undo"}));
+ assert_eq!(story(&mut s), "");
+}
+
#[test]
fn batch_stops_on_first_error() {
let mut s = server();
diff --git a/crates/mcp/src/tools.rs b/crates/mcp/src/tools.rs
index 1be5bba8a..c6cf082bd 100644
--- a/crates/mcp/src/tools.rs
+++ b/crates/mcp/src/tools.rs
@@ -311,6 +311,25 @@ pub fn tool_definitions() -> Vec<Value> {
obj(json!({"text": string("Text to type (\\n = new paragraph)")}), &["text"]),
false,
),
+ tool(
+ "ime",
+ "Input method",
+ "Input method (IME) events at the Type tool's caret, as a system IME sends them: `preedit` is the marked text \
+ (shown in place, no undo step; an empty string cancels the composition), `active` the clause being converted \
+ in characters of it, `commit` the text the IME types in its place (one undo step). Example, Japanese: \
+ {preedit:\"にほん\", active:[3,3]} then {preedit:\"日本\", active:[0,2]} then {commit:\"日本\"}. Returns \
+ whether a composition is still open.",
+ obj(
+ json!({
+ "preedit": string("Marked text (replaces the previous marked text; empty = cancel)"),
+ "active": {"type": "array", "items": {"type": "integer", "minimum": 0}, "minItems": 2, "maxItems": 2,
+ "description": "[start, end] of the clause being converted, in characters of preedit (start = end: the IME's cursor)"},
+ "commit": string("Committed text, typed in place of the marked text"),
+ }),
+ &[],
+ ),
+ false,
+ ),
tool(
"click",
"Click",
@@ -572,6 +591,7 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<ToolResult, Str
j(b.call("ui.key", pick(a, &["key", "shift", "alt", "cmd", "ctrl"]))?)
}
"type_text" => j(b.call("ui.text", json!({"text": req_str(a, "text")?}))?),
+ "ime" => j(b.call("ui.ime", pick(a, &["preedit", "active", "commit"]))?),
"click" | "drag" => {
need_ui(b, name)?;
let keys: &[&str] = &["x", "y", "toX", "toY", "steps", "button", "count", "shift", "alt", "cmd", "ctrl"];
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index cdeaee792..185d12b62 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -382,6 +382,30 @@ pub trait Tool: Send {
fn wants_text(&self, _cx: &ToolContext) -> bool {
false
}
+ /// IME composition (marked text) at the text caret: `text` replaces the previous marked text
+ /// (or the selected text when a composition starts); an empty `text` ends the composition with
+ /// nothing typed. `active_chars` is the clause being converted (empty: the IME's cursor), in
+ /// characters of `text`. The result arrives through [`Tool::ime_commit`].
+ fn ime_preedit(&mut self, _cx: &ToolContext, _text: &str, _active_chars: Option<std::ops::Range<usize>>) -> Vec<Action> {
+ vec![]
+ }
+ /// The IME committed `text`: it replaces the marked text and is typed as one step.
+ fn ime_commit(&mut self, _cx: &ToolContext, _text: &str) -> Vec<Action> {
+ vec![]
+ }
+ /// End a composition from outside the IME (a click, another command): the marked text stays
+ /// as typed.
+ fn ime_end(&mut self, _cx: &ToolContext) -> Vec<Action> {
+ vec![]
+ }
+ /// Is uncommitted IME text being shown? Keys, shortcuts and Undo wait while it is.
+ fn composing(&self) -> bool {
+ false
+ }
+ /// Where the IME candidate window goes: a caret line (top, bottom) in canvas space.
+ fn ime_caret(&self, _cx: &ToolContext) -> Option<(Point, Point)> {
+ None
+ }
}
pub fn create(id: &str) -> Box<dyn Tool> {
diff --git a/crates/tools/src/text.rs b/crates/tools/src/text.rs
index e6ef1a5fe..625d998f3 100644
--- a/crates/tools/src/text.rs
+++ b/crates/tools/src/text.rs
@@ -1,12 +1,28 @@
//! Type tool (T): click in a frame to place the caret, drag in a frame to select text, drag on
//! empty canvas to draw a new text frame. Keys move the caret / delete; typing goes through
//! `text.insert` (sent by the UI as text input).
+//!
+//! IME: the marked text of a composition is a preview of `text.insert` at the caret (so it lays
+//! out in place like typed text), underlined, until the IME commits it (one `text.insert`, one
+//! undo step) or clears it (the preview goes, leaving no undo step).
-use designcraft_geom::Point;
+use std::ops::Range;
+
+use designcraft_compose as compose;
+use designcraft_doc::{ItemId, TextSel};
+use designcraft_geom::{Affine, Point, Vec2};
use serde_json::json;
use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, ToolContext, ToolKey, frame::snapped_drag_rect, rect_json, spread_json};
+/// IME marked text shown at the caret through a preview of `text.insert`, not yet typed.
+#[derive(Clone, Debug, PartialEq)]
+struct Preedit {
+ text: String,
+ /// The clause being converted (empty: the IME's cursor), in bytes of `text`.
+ active: Option<Range<usize>>,
+}
+
#[derive(Default)]
pub struct TypeTool {
/// Vertical Type Tool: frames it draws set text vertically.
@@ -21,6 +37,7 @@ pub struct TypeTool {
guides: Vec<Overlay>,
/// The press selected a word, line, paragraph or story (several clicks): drags keep it.
unit_selected: bool,
+ preedit: Option<Preedit>,
}
/// Are whole rows or whole columns of a table selected?
@@ -37,6 +54,78 @@ impl TypeTool {
}
}
+/// Byte range in `s` of the characters `r` (IME ranges count characters; carets count bytes).
+/// `None` when `r` runs past the end of `s` or backwards.
+fn char_range_to_bytes(s: &str, r: Range<usize>) -> Option<Range<usize>> {
+ let n = s.chars().count();
+ let byte = |c: usize| if c == n { Some(s.len()) } else { s.char_indices().nth(c).map(|(i, _)| i) };
+ let (a, b) = (byte(r.start)?, byte(r.end)?);
+ (a <= b).then_some(a..b)
+}
+
+/// Text space → canvas for frame `fid` (with the quarter turn of a vertical frame).
+fn text_canvas_xf(cx: &ToolContext, fid: ItemId) -> Option<Affine> {
+ Some(cx.item_canvas_xf(fid)? * cx.doc.text_xf(cx.doc.item(fid)?))
+}
+
+/// Where the marked text starts: the preview typed it just before the caret.
+fn marked_start(cx: &ToolContext, ts: TextSel, p: &Preedit) -> usize {
+ let text = cx.doc.text_story(ts.story, ts.cell).map_or("", |s| s.text.as_str());
+ designcraft_doc::story::floor_char_boundary(text, ts.focus.saturating_sub(p.text.len()))
+}
+
+/// The caret line (top, bottom) at byte `pos` of the selection's text, in canvas space; `None`
+/// when `pos` isn't composed (overset).
+fn caret_line(cx: &ToolContext, ts: TextSel, pos: usize) -> Option<(Point, Point)> {
+ let cs = cx.cache.get(cx.doc, ts.story, None);
+ let (fi, x, bl, asc, desc) = match ts.cell {
+ Some(c) => match c.footnote_id() {
+ Some(id) => compose::note_caret(&cs, id, pos)?,
+ None => compose::cell_caret(&cs, c.table, c.row, c.col, pos)?,
+ },
+ None => compose::caret(&cs, pos)?,
+ };
+ let m = text_canvas_xf(cx, cs.frames.get(fi)?.frame)?;
+ Some((m * Point::new(x, bl - asc), m * Point::new(x, bl + desc)))
+}
+
+/// Underlines (start, end in canvas space) under bytes `r` of the selection's text.
+fn underlines(cx: &ToolContext, ts: TextSel, r: Range<usize>) -> Vec<(Point, Point)> {
+ let mut out = Vec::new();
+ let mut add = |lines: &[compose::Line], m: Affine| {
+ for l in lines {
+ let (s, e) = (r.start.max(l.range.start), r.end.min(l.range.end));
+ if s < e {
+ out.extend(compose::highlight_quads(l, s, e, false).into_iter().map(|q| (m * q[3], m * q[2])));
+ }
+ }
+ };
+ let cs = cx.cache.get(cx.doc, ts.story, None);
+ match ts.cell {
+ // A cell's or footnote's text is composed on its own and placed in the frame.
+ Some(c) => {
+ let found = match c.footnote_id() {
+ Some(id) => compose::find_note(&cs, id).map(|(fi, n)| (fi, n.origin, n.text.clone())),
+ None => compose::find_cell(&cs, c.table, c.row, c.col).map(|(fi, _, pc)| (fi, pc.origin, pc.text.clone())),
+ };
+ if let Some((fi, origin, text)) = found
+ && let Some(m) = cs.frames.get(fi).and_then(|ft| text_canvas_xf(cx, ft.frame))
+ && let Some(ft) = text.frames.first()
+ {
+ add(&ft.lines, m * Affine::translate(origin.to_vec2()));
+ }
+ }
+ None => {
+ for ft in &cs.frames {
+ if let Some(m) = text_canvas_xf(cx, ft.frame) {
+ add(&ft.lines, m);
+ }
+ }
+ }
+ }
+ out
+}
+
impl Tool for TypeTool {
fn id(&self) -> &'static str {
if self.vertical { "verticalType" } else { "type" }
@@ -139,7 +228,8 @@ impl Tool for TypeTool {
}
fn key(&mut self, cx: &ToolContext, key: ToolKey, mods: Mods) -> Vec<Action> {
- if cx.selection.text.is_none() {
+ // The IME owns the keys while it composes (the UI holds them back; this is a guard).
+ if cx.selection.text.is_none() || self.preedit.is_some() {
return vec![];
}
let mv = |dir: &str| {
@@ -161,8 +251,29 @@ impl Tool for TypeTool {
}
}
- fn overlays(&self, _cx: &ToolContext) -> Vec<Overlay> {
- if self.drawing { self.guides.clone() } else { vec![] }
+ fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
+ if self.drawing {
+ return self.guides.clone();
+ }
+ let (Some(p), Some(ts)) = (&self.preedit, cx.selection.text) else { return vec![] };
+ // Marked text: a thin underline, a thick one under the clause being converted.
+ let start = marked_start(cx, ts, p);
+ let line = |(a, b): (Point, Point)| Overlay::Line { a, b, color: [0, 0, 0], dashed: false };
+ let mut o: Vec<Overlay> = underlines(cx, ts, start..start + p.text.len()).into_iter().map(line).collect();
+ match &p.active {
+ Some(r) if !r.is_empty() => {
+ for (a, b) in underlines(cx, ts, start + r.start..start + r.end) {
+ // A second line a pixel further from the text.
+ let d = b - a;
+ let n = Vec2::new(-d.y, d.x) * (cx.tol(1.0) / d.hypot().max(1e-9));
+ o.push(line((a + n, b + n)));
+ }
+ }
+ // The IME's cursor inside the marked text (the text caret stays at its end).
+ Some(r) if r.start < p.text.len() => o.extend(caret_line(cx, ts, start + r.start).map(line)),
+ _ => {}
+ }
+ o
}
fn cursor(&self, cx: &ToolContext, p: Point, _m: Mods) -> Cursor {
@@ -179,4 +290,81 @@ impl Tool for TypeTool {
fn wants_text(&self, cx: &ToolContext) -> bool {
cx.selection.text.is_some()
}
+
+ fn ime_preedit(&mut self, cx: &ToolContext, text: &str, active_chars: Option<Range<usize>>) -> Vec<Action> {
+ let text: String = text.chars().filter(|c| !c.is_control()).collect();
+ // A cancelled composition: the preview goes, leaving no undo step. A clear with nothing
+ // marked (some integrations send one when the IME is enabled) changes nothing, so it never
+ // deletes the selected text.
+ if text.is_empty() || cx.selection.text.is_none() {
+ return if self.preedit.take().is_some() { vec![Action::Cancel] } else { vec![] };
+ }
+ let mut out = Vec::new();
+ if self.preedit.is_none() {
+ out.push(Action::Begin("Type".into()));
+ }
+ // Each update replaces the previous marked text (or the selected text). Marked text shows
+ // as it is: typographer's quotes and autocorrect wait for the commit.
+ out.push(Action::Preview("text.insert".into(), json!({"text": text, "raw": true})));
+ self.preedit = Some(Preedit { active: active_chars.and_then(|r| char_range_to_bytes(&text, r)), text });
+ out
+ }
+
+ fn ime_commit(&mut self, cx: &ToolContext, text: &str) -> Vec<Action> {
+ let text: String = text.chars().map(|c| if c == '\r' { '\n' } else { c }).filter(|c| !c.is_control() || matches!(c, '\n' | '\t')).collect();
+ // The marked text goes; the result is typed in its place like any typing: one command,
+ // one undo step.
+ let mut out = if self.preedit.take().is_some() { vec![Action::Cancel] } else { vec![] };
+ if !text.is_empty() && cx.selection.text.is_some() {
+ out.push(Action::Exec("text.insert".into(), json!({"text": text})));
+ }
+ out
+ }
+
+ fn ime_end(&mut self, cx: &ToolContext) -> Vec<Action> {
+ match self.preedit.as_ref().map(|p| p.text.clone()) {
+ Some(text) => self.ime_commit(cx, &text),
+ None => vec![],
+ }
+ }
+
+ fn composing(&self) -> bool {
+ self.preedit.is_some()
+ }
+
+ fn ime_caret(&self, cx: &ToolContext) -> Option<(Point, Point)> {
+ let ts = cx.selection.text?;
+ // The candidate window follows the clause being converted (or the IME's cursor), else the
+ // caret.
+ let at = match &self.preedit {
+ Some(p) => marked_start(cx, ts, p) + p.active.as_ref().map_or(0, |r| r.start),
+ None => ts.range().start,
+ };
+ caret_line(cx, ts, at).or_else(|| {
+ // Overset text has no caret on a page: the window goes to the out port of the story's
+ // last frame.
+ let fid = cx.doc.story(ts.story).and_then(|s| s.frames.last().copied()).or(ts.frame)?;
+ let it = cx.doc.item(fid)?;
+ let m = cx.item_canvas_xf(fid)? * it.xf;
+ let r = it.inner_bounds();
+ Some((m * Point::new(r.x1, r.y1 - 12.0), m * Point::new(r.x1, r.y1)))
+ })
+ }
+}
+
+#[cfg(test)]
+mod ime_tests {
+ use super::char_range_to_bytes;
+
+ #[test]
+ fn ime_character_ranges_become_byte_ranges() {
+ assert_eq!(char_range_to_bytes("雅楽", 0..2), Some(0..6));
+ assert_eq!(char_range_to_bytes("がg", 1..2), Some(3..4));
+ assert_eq!(char_range_to_bytes("がg", 2..2), Some(4..4));
+ // Past the end or backwards: no clause.
+ assert_eq!(char_range_to_bytes("が", 1..3), None);
+ #[allow(clippy::reversed_empty_ranges)]
+ let backwards = 1..0;
+ assert_eq!(char_range_to_bytes("がg", backwards), None);
+ }
}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 246061a5f..9d5c5df4a 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -316,6 +316,7 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
}
let sel_rect = draw_selection(app, &painter, &xf, &doc, &layout);
draw_tool_overlays(app, &painter, &xf);
+ ime_output(app, ui.ctx(), &xf);
if rulers {
draw_rulers(app, ui, full, rect, &xf, &doc, &layout, &t);
ruler_units_menus(app, &resp, full, rect, &doc);
@@ -1485,15 +1486,23 @@ fn handle_input(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, r
resp.request_focus();
}
// Keyboard for the active tool.
- if ui.ctx().text_edit_focused() && !resp.has_focus() {
+ if (ui.ctx().text_edit_focused() && !resp.has_focus()) || !keyboard_pane(app) {
return;
}
+ // Keys of a frame that began composing are the IME's too (a Backspace that empties the marked
+ // text must not delete the character before it).
+ let composing_at_start = app.session.tool_composing();
let evs = ui.input(|i| i.events.clone());
for e in evs {
+ let composing = composing_at_start || app.session.tool_composing();
match e {
- egui::Event::Text(t) if wants_text => {
+ egui::Event::Ime(ime) if wants_text => ime_event(app, ime, vi),
+ // Keys the IME passes through come as text only while nothing is marked.
+ egui::Event::Text(t) if wants_text && !composing => {
let _ = app.run("text.insert", json!({"text": t}));
}
+ // The IME owns the keyboard until it commits: no typing, clipboard or editing keys.
+ egui::Event::Text(_) | egui::Event::Paste(_) | egui::Event::Copy | egui::Event::Cut | egui::Event::Key { .. } if composing => {}
egui::Event::Paste(t) if wants_text => {
// Formatted when the system clipboard still holds what was copied here;
// Shift (⇧⌘V) pastes without formatting.
@@ -1547,6 +1556,95 @@ fn handle_input(app: &mut DesignApp, ui: &mut egui::Ui, resp: &egui::Response, r
let _ = DVec2::ZERO;
}
+/// Does the current pane take the keyboard? Both panes of a split window see the same events, so
+/// typing, tool keys, the clipboard and the input method serve only the pane last clicked.
+fn keyboard_pane(app: &DesignApp) -> bool {
+ !app.split || app.pane == app.focus_pane
+}
+
+/// One input method event for the text caret: marked text previews in the story, a commit types.
+fn ime_event(app: &mut DesignApp, e: egui::ImeEvent, vi: designcraft_engine::ViewInfo) {
+ let r = match e {
+ egui::ImeEvent::Preedit { text, active_range_chars } => {
+ app.ime_marked = Some(text.clone()).filter(|t| !t.is_empty());
+ app.session.tool_preedit(&text, active_range_chars, vi).map_err(|e| e.to_string())
+ }
+ // A bare line break confirms the composition (Enter with some IMEs): the marked text, not
+ // a new paragraph.
+ egui::ImeEvent::Commit(t) if t == "\n" || t == "\r" => {
+ keep_marked_text(app);
+ Ok(())
+ }
+ egui::ImeEvent::Commit(t) => {
+ app.ime_marked = None;
+ app.session.tool_ime_commit(&t, vi).map_err(|e| e.to_string())
+ }
+ // The web text agent's corrections and mobile Backspace: characters around the caret go.
+ egui::ImeEvent::DeleteSurrounding { before_chars, after_chars } if !app.session.tool_composing() => {
+ delete_surrounding(app, before_chars, after_chars)
+ }
+ // `Enabled` and `Disabled` carry nothing: the IME is on while `ime_output` sets `ime`.
+ _ => Ok(()),
+ };
+ if let Err(e) = r {
+ app.status(e);
+ }
+ app.after_engine();
+}
+
+/// Delete `before` characters before the caret and `after` after it (sizes come from the browser:
+/// a correction never deletes more than a word's worth).
+fn delete_surrounding(app: &mut DesignApp, before: usize, after: usize) -> Result<(), String> {
+ for (n, forward) in [(before, false), (after, true)] {
+ for _ in 0..n.min(64) {
+ app.run("text.delete", json!({"forward": forward}))?;
+ }
+ }
+ Ok(())
+}
+
+/// End a composition outside the IME (a click away, the canvas losing the keyboard): the marked
+/// text stays as typed, and the system IME is told to drop its copy.
+pub(crate) fn keep_marked_text(app: &mut DesignApp) {
+ if app.ime_marked.take().is_some() {
+ app.ime_discard = true;
+ }
+ if app.session.tool_composing() {
+ if let Err(e) = app.session.end_composition() {
+ app.status(e.to_string());
+ }
+ app.after_engine();
+ }
+}
+
+/// While the text caret is in the canvas, let the system IME compose (egui-winit allows it only in
+/// frames that set `ime`; the web build focuses its hidden text input then) and keep the candidate
+/// window at the caret, through zoom, scrolling, view rotation and vertical text.
+fn ime_output(app: &mut DesignApp, ctx: &egui::Context, xf: &Xf) {
+ if !keyboard_pane(app) {
+ return;
+ }
+ // A text field, dialog or Quick Apply has the keyboard: egui's own text editing serves the IME.
+ let ours = app.session.wants_text() && !ctx.text_edit_focused() && app.ui.dialog.is_none() && app.ui.palette.is_none();
+ let caret = if ours { app.session.tool_ime_caret(app.view_info()) } else { None };
+ let Some((a, b)) = caret else {
+ // The IME goes away with its marked text: keep that text as typed, like a click away.
+ keep_marked_text(app);
+ return;
+ };
+ let r = Rect::from_two_pos(xf.to_screen(a), xf.to_screen(b)).expand(1.0);
+ // The composition ended outside the IME (a click, a command): the IME drops what it has marked.
+ let interrupt = app.ime_marked.is_some() && !app.session.tool_composing();
+ if interrupt {
+ app.ime_marked = None;
+ app.ime_discard = true;
+ }
+ app.canvas_ime = true;
+ ctx.output_mut(|o| {
+ o.ime = Some(egui::output::IMEOutput { purpose: egui::IMEPurpose::Normal, rect: r, cursor_rect: r, should_interrupt_composition: interrupt });
+ });
+}
+
/// View → Show Hidden Characters: ¶ paragraph ends, » tabs, · spaces, ¬ forced line breaks, # end of story.
fn draw_hidden_characters(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout) {
for story in doc.stories.values() {
@@ -1854,4 +1952,153 @@ mod tests {
right_click(&mut h, pos2(rect.center().x, rect.min.y - RULER / 2.0));
assert!(h.query_by_label(" Points").is_none());
}
+
+ /// One frame of the whole app with `events`; returns what it asks of the system IME.
+ fn ime_frame(app: &mut DesignApp, ctx: &egui::Context, events: Vec<egui::Event>) -> Option<egui::output::IMEOutput> {
+ let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1400.0, 900.0))), events, ..Default::default() };
+ let mut out = ctx.run_ui(raw, |ui| {
+ app.logic(ui.ctx());
+ app.ui(ui);
+ });
+ out.textures_delta.clear();
+ out.platform_output.ime
+ }
+
+ /// The Type tool's caret at the end of a new text frame holding `text`, the app's first frames
+ /// drawn; returns the story id.
+ fn typing_app(app: &mut DesignApp, ctx: &egui::Context, text: &str) -> Value {
+ app.run("file.new", json!({})).unwrap();
+ let f = app.session.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text", "text": text})).unwrap();
+ let sid = f["story"].clone();
+ app.session.execute("text.select", &json!({"story": sid, "anchor": text.len(), "focus": text.len()})).unwrap();
+ app.select_tool("type");
+ for _ in 0..4 {
+ ime_frame(app, ctx, vec![]);
+ }
+ sid
+ }
+
+ fn story_text(app: &mut DesignApp, sid: &Value) -> String {
+ app.session.execute("story.get", &json!({"story": sid})).unwrap()["text"].as_str().unwrap().to_string()
+ }
+
+ /// Spread point `p` on screen.
+ fn on_screen(app: &DesignApp, p: Point) -> Pos2 {
+ let st = app.session.active().unwrap();
+ let c = CanvasLayout::new(&st.doc, st.editing_parents).to_canvas(SpreadRef::Doc(0), p);
+ Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()).to_screen(c)
+ }
+
+ fn preedit(t: &str, c: usize) -> egui::Event {
+ egui::Event::Ime(egui::ImeEvent::Preedit { text: t.into(), active_range_chars: Some(c..c) })
+ }
+
+ #[test]
+ #[allow(deprecated)] // `ImeEvent::Enabled` / `Disabled`: egui no longer sends them, other integrations may.
+ fn ime_events_compose_in_the_story_with_the_candidate_window_at_the_caret() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ let sid = typing_app(&mut app, &ctx, "曲:");
+ // At a text caret the IME is allowed, its window on the caret as the canvas draws it.
+ let ime = ime_frame(&mut app, &ctx, vec![]).expect("IME allowed at a text caret");
+ let (top, bottom) = {
+ let st = app.session.active().unwrap();
+ let cs = app.session.cache.get(&st.doc, designcraft_doc::StoryId(sid.as_u64().unwrap()), None);
+ let (fi, x, bl, asc, desc) = compose::caret(&cs, "曲:".len()).unwrap();
+ let fid = cs.frames[fi].frame;
+ let (a, _) = item_canvas_xf(&st.doc, &CanvasLayout::new(&st.doc, st.editing_parents), fid).unwrap();
+ let m = a * st.doc.text_xf(st.doc.item(fid).unwrap());
+ let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());
+ (xf.to_screen(m * Point::new(x, bl - asc)), xf.to_screen(m * Point::new(x, bl + desc)))
+ };
+ assert!(ime.rect.expand(1.0).contains(top) && ime.rect.expand(1.0).contains(bottom), "{:?} vs {top:?}–{bottom:?}", ime.rect);
+ assert_eq!(ime.cursor_rect, ime.rect);
+ assert!(!ime.should_interrupt_composition);
+ assert!(app.canvas_ime(), "the web build keeps whole words typed at the caret");
+ // "ni" → に → 日, with the events winit sends.
+ ime_frame(&mut app, &ctx, vec![egui::Event::Ime(egui::ImeEvent::Enabled), preedit("ni", 2)]);
+ assert_eq!(story_text(&mut app, &sid), "曲:ni", "marked text shows in place");
+ assert!(app.session.tool_composing());
+ // Keys, plain text, the clipboard and Undo (the native menu's ⌘Z) wait for the IME.
+ let steps = app.session.doc().unwrap().history.undo.len();
+ let key = |key, pressed| egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
+ ime_frame(
+ &mut app,
+ &ctx,
+ vec![key(egui::Key::Backspace, true), key(egui::Key::Backspace, false), egui::Event::Text("x".into()), egui::Event::Paste("y".into())],
+ );
+ crate::menus::activate(&mut app, "edit.undo", &Value::Null);
+ assert!(!crate::menus::enabled(&app, "edit.undo"));
+ assert_eq!(story_text(&mut app, &sid), "曲:ni");
+ let ime2 = ime_frame(&mut app, &ctx, vec![preedit("に", 1)]).expect("IME allowed while composing");
+ assert_eq!(story_text(&mut app, &sid), "曲:に");
+ assert!(ime2.rect.center().x > ime.rect.center().x, "the window follows the IME's cursor: {:?} {:?}", ime2.rect, ime.rect);
+ ime_frame(&mut app, &ctx, vec![egui::Event::Ime(egui::ImeEvent::Commit("日".into())), egui::Event::Ime(egui::ImeEvent::Disabled)]);
+ assert_eq!(story_text(&mut app, &sid), "曲:日");
+ assert!(!app.session.tool_composing());
+ assert_eq!(app.session.doc().unwrap().history.undo.len(), steps + 1, "the commit is one undo step");
+ assert_eq!(app.session.journal.last(), Some(&("text.insert".to_string(), json!({"text": "日"}))));
+ // After the commit, typing and Undo work again.
+ ime_frame(&mut app, &ctx, vec![egui::Event::Text("!".into())]);
+ assert_eq!(story_text(&mut app, &sid), "曲:日!");
+ crate::menus::activate(&mut app, "edit.undo", &Value::Null);
+ crate::menus::activate(&mut app, "edit.undo", &Value::Null);
+ assert_eq!(story_text(&mut app, &sid), "曲:");
+ // Without a text caret the IME is off: single-key tool shortcuts keep working.
+ app.select_tool("selection");
+ assert!(ime_frame(&mut app, &ctx, vec![]).is_none());
+ assert!(!app.canvas_ime());
+ }
+
+ /// Both panes of a split window see the same keyboard events: only the pane last clicked
+ /// types them.
+ #[test]
+ fn a_split_window_types_once() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ let sid = typing_app(&mut app, &ctx, "ab");
+ app.run("window.split", json!({"on": true})).unwrap();
+ for _ in 0..3 {
+ ime_frame(&mut app, &ctx, vec![]);
+ }
+ assert!(app.other_pane.as_ref().and_then(|o| o.1).is_some(), "both panes are drawn");
+ ime_frame(&mut app, &ctx, vec![egui::Event::Text("c".into())]);
+ assert_eq!(story_text(&mut app, &sid), "abc");
+ let key = |pressed| egui::Event::Key { key: egui::Key::Backspace, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
+ ime_frame(&mut app, &ctx, vec![key(true), key(false)]);
+ assert_eq!(story_text(&mut app, &sid), "ab");
+ ime_frame(&mut app, &ctx, vec![egui::Event::Paste("de".into())]);
+ assert_eq!(story_text(&mut app, &sid), "abde");
+ ime_frame(&mut app, &ctx, vec![preedit("に", 1)]);
+ assert_eq!(story_text(&mut app, &sid), "abdeに");
+ ime_frame(&mut app, &ctx, vec![egui::Event::Ime(egui::ImeEvent::Commit("日".into()))]);
+ assert_eq!(story_text(&mut app, &sid), "abde日");
+ }
+
+ #[test]
+ fn clicking_away_mid_composition_keeps_the_text_and_interrupts_the_ime() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ let sid = typing_app(&mut app, &ctx, "");
+ ime_frame(&mut app, &ctx, vec![preedit("しょうこ", 4)]);
+ assert!(app.session.tool_composing());
+ // A click in the frame (macOS and Windows send nothing for it): the marked text stays as
+ // typed and the IME is told to drop its composition, once.
+ let p = on_screen(&app, Point::new(150.0, 120.0));
+ let press = |pressed| egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
+ let ime = ime_frame(&mut app, &ctx, vec![egui::Event::PointerMoved(p), press(true)]).expect("still at a text caret");
+ assert!(ime.should_interrupt_composition);
+ ime_frame(&mut app, &ctx, vec![press(false)]);
+ assert!(!app.session.tool_composing());
+ assert_eq!(story_text(&mut app, &sid), "しょうこ");
+ assert!(app.take_ime_discard(), "the host tells the system IME to drop its marked text");
+ assert!(!ime_frame(&mut app, &ctx, vec![]).expect("still at a text caret").should_interrupt_composition);
+ assert!(!app.take_ime_discard());
+ // A bare line break from the IME confirms a composition: the marked text, no new paragraph.
+ ime_frame(&mut app, &ctx, vec![preedit("がく", 2)]);
+ ime_frame(&mut app, &ctx, vec![egui::Event::Ime(egui::ImeEvent::Commit("\n".into()))]);
+ assert!(!app.session.tool_composing());
+ let typed = story_text(&mut app, &sid);
+ assert!(typed.contains("がく") && typed.contains("しょうこ") && !typed.contains('\n'), "{typed:?}");
+ }
}
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index b8cca617d..c0b8353a4 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -10,6 +10,8 @@
//! drive the active tool through the same path as the mouse
//! - `ui.key {key, shift?, alt?, cmd?}`: synthetic keyboard input
//! - `ui.text {text}`: insert at the explicit caret, or type into a focused UI field
+//! - `ui.ime {preedit?, active?: [start, end], commit?}`: input method events, as the system IME
+//! sends them (marked text with its converting clause in characters, then the committed text)
//! - `ui.move {x, y}` / `ui.click {x, y, button?, count?, shift?…}` / `ui.drag {x, y, toX, toY, steps?}`:
//! real egui pointer input in screen points (reaches every widget: panels, flyouts, dialogs)
//! - `ui.set {brightness?, panel?, dockTab?, rulers?, outline?, …}`
@@ -246,6 +248,18 @@ fn handle_request(app: &mut DesignApp, ctx: &egui::Context, req: &ControlRequest
wrap(app.run("text.insert", json!({"text": text})))
}
}
+ "ui.ime" => {
+ let n = |v: &Value| v.as_u64().and_then(|n| usize::try_from(n).ok());
+ if let Some(text) = s("preedit") {
+ let active = p.get("active").and_then(Value::as_array).and_then(|a| Some(n(a.first()?)?..n(a.get(1)?)?));
+ app.synthetic.push(egui::Event::Ime(egui::ImeEvent::Preedit { text: text.to_string(), active_range_chars: active }));
+ }
+ if let Some(text) = s("commit") {
+ app.synthetic.push(egui::Event::Ime(egui::ImeEvent::Commit(text.to_string())));
+ }
+ ctx.request_repaint();
+ ok(Value::Null)
+ }
"ui.set" => {
let mut r = Ok(Value::Null);
if let Some(b) = s("brightness") {
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index dd75c94fc..de48d792c 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -500,6 +500,14 @@ pub struct DesignApp {
/// What the last object copy put on the system clipboard (no native menu): without it the
/// paste keys produce no paste event. It is never pasted as text.
object_clip_text: Option<String>,
+ /// The marked text the system IME last sent (`None` once it commits or clears). When the
+ /// composition ends some other way (a click, a tool switch), the IME is told to drop it.
+ pub(crate) ime_marked: Option<String>,
+ /// The system IME must drop its marked text (see [`Self::take_ime_discard`]).
+ pub(crate) ime_discard: bool,
+ /// The canvas asked for the system IME in the last frame (the web build's text input has the
+ /// keyboard then).
+ pub(crate) canvas_ime: bool,
}
impl DesignApp {
@@ -540,6 +548,9 @@ impl DesignApp {
egui_ctx: None,
in_control: false,
object_clip_text: None,
+ ime_marked: None,
+ ime_discard: false,
+ canvas_ime: false,
}
}
@@ -578,6 +589,21 @@ impl DesignApp {
self.ui.status = s.into();
}
+ /// Did an IME composition end outside the IME this frame (a click, a tool switch)? egui's
+ /// `should_interrupt_composition` toggles winit's IME, which doesn't reach the macOS input
+ /// context: it keeps the marked text and types it again into the next composition, so the
+ /// host discards it there.
+ pub fn take_ime_discard(&mut self) -> bool {
+ std::mem::take(&mut self.ime_discard)
+ }
+
+ /// Did the canvas ask for the system IME in the last frame (a Type tool caret)? The web
+ /// build's hidden text input has the keyboard then, so text events hold what the browser
+ /// typed (see [`drop_key_name_text`]).
+ pub fn canvas_ime(&self) -> bool {
+ self.canvas_ime
+ }
+
/// THE entry point for every action (menus, shortcuts, palette, panels, control channel).
pub fn run(&mut self, id: &str, params: Value) -> Result<Value, String> {
if let Some(r) = menus::run_ui(self, id, &params) {
@@ -913,6 +939,7 @@ impl DesignApp {
/// Lay out the whole window.
pub fn ui(&mut self, ui: &mut egui::Ui) {
let ctx = ui.ctx().clone();
+ self.canvas_ime = false;
if !self.fonts_ready {
ctx.request_repaint();
return;
@@ -1122,19 +1149,50 @@ pub fn now_ms() -> f64 {
}
}
-/// Is `text` the name of a key ("AltGraph", "Dead", "AudioVolumeUp") instead of typed text?
-/// Browsers report a key press as a string: the character it types, or a name made of ASCII
-/// letters and digits that starts with a capital. One key press never types such a word.
+/// Named `KeyboardEvent.key` values (W3C UI Events) that a browser reports for a key that types
+/// nothing, separated by spaces. eframe's web backend sends the ones it doesn't know as text.
+const KEY_NAMES: &str = "\
+ Unidentified Alt AltGraph CapsLock Control Fn FnLock Hyper Meta NumLock ScrollLock Shift Super Symbol SymbolLock OS Win Enter \
+ Tab ArrowDown ArrowLeft ArrowRight ArrowUp End Home PageDown PageUp Backspace Clear Copy CrSel Cut Delete EraseEof ExSel \
+ Insert Paste Redo Undo Accept Again Attn Cancel ContextMenu Esc Escape Execute Find Finish Help Pause Play Props Select \
+ ZoomIn ZoomOut BrightnessDown BrightnessUp Eject LogOff Power PowerOff PrintScreen Hibernate Standby WakeUp AllCandidates \
+ Alphanumeric CodeInput Compose Convert Dead FinalMode GroupFirst GroupLast GroupNext GroupPrevious ModeChange NextCandidate \
+ NonConvert PreviousCandidate Process SingleCandidate HangulMode HanjaMode JunjaMode Eisu Hankaku Hiragana HiraganaKatakana \
+ KanaMode KanjiMode Katakana Romaji Zenkaku ZenkakuHankaku Soft1 Soft2 Soft3 Soft4 ChannelDown ChannelUp Close MailForward \
+ MailReply MailSend MediaClose MediaFastForward MediaPause MediaPlay MediaPlayPause MediaRecord MediaRewind MediaStop \
+ MediaTrackNext MediaTrackPrevious New Open Print Save SpellCheck Key11 Key12 AudioBalanceLeft AudioBalanceRight \
+ AudioBassBoostDown AudioBassBoostToggle AudioBassBoostUp AudioFaderFront AudioFaderRear AudioSurroundModeNext AudioTrebleDown \
+ AudioTrebleUp AudioVolumeDown AudioVolumeUp AudioVolumeMute MicrophoneToggle MicrophoneVolumeDown MicrophoneVolumeUp \
+ MicrophoneVolumeMute SpeechCorrectionList SpeechInputToggle LaunchApplication1 LaunchApplication2 LaunchCalendar \
+ LaunchContacts LaunchMail LaunchMediaPlayer LaunchMusicPlayer LaunchPhone LaunchScreenSaver LaunchSpreadsheet \
+ LaunchWebBrowser LaunchWebCam LaunchWordProcessor BrowserBack BrowserFavorites BrowserForward BrowserHome BrowserRefresh \
+ BrowserSearch BrowserStop AppSwitch Call Camera CameraFocus EndCall GoBack GoHome HeadsetHook LastNumberRedial Notification \
+ MannerMode VoiceDial TV TV3DMode TVAntennaCable TVAudioDescription TVAudioDescriptionMixDown TVAudioDescriptionMixUp \
+ TVContentsMenu TVDataService TVInput TVInputComponent1 TVInputComponent2 TVInputComposite1 TVInputComposite2 TVInputHDMI1 \
+ TVInputHDMI2 TVInputHDMI3 TVInputHDMI4 TVInputVGA1 TVMediaContext TVNetwork TVNumberEntry TVPower TVRadioService TVSatellite \
+ TVSatelliteBS TVSatelliteCS TVSatelliteToggle TVTerrestrialAnalog TVTerrestrialDigital TVTimer AVRInput AVRPower ColorF0Red \
+ ColorF1Green ColorF2Yellow ColorF3Blue ColorF4Grey ColorF5Brown ClosedCaptionToggle Dimmer DisplaySwap DVR Exit \
+ FavoriteClear0 FavoriteClear1 FavoriteClear2 FavoriteClear3 FavoriteRecall0 FavoriteRecall1 FavoriteRecall2 FavoriteRecall3 \
+ FavoriteStore0 FavoriteStore1 FavoriteStore2 FavoriteStore3 Guide GuideNextDay GuidePreviousDay Info InstantReplay Link \
+ ListProgram LiveContent Lock MediaApps MediaAudioTrack MediaLast MediaSkipBackward MediaSkipForward MediaStepBackward \
+ MediaStepForward MediaTopMenu NavigateIn NavigateNext NavigateOut NavigatePrevious NextFavoriteChannel NextUserProfile \
+ OnDemand Pairing PinPDown PinPMove PinPToggle PinPUp PlaySpeedDown PlaySpeedReset PlaySpeedUp RandomToggle RcLowBattery \
+ RecordSpeedNext RfBypass ScanChannelsToggle ScreenModeNext Settings SplitScreenToggle STBInput STBPower Subtitle Teletext \
+ VideoModeNext Wink ZoomToggle";
+
+/// Is `text` the name of a key ("AltGraph", "Dead", "F13") instead of typed text?
pub fn is_key_name(text: &str) -> bool {
- text.len() > 1 && text.starts_with(|c: char| c.is_ascii_uppercase()) && text.chars().all(|c| c.is_ascii_alphanumeric())
+ let function_key = text.strip_prefix('F').is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()));
+ function_key || KEY_NAMES.split_ascii_whitespace().any(|k| k == text)
}
-/// Drop text events that are key names. eframe's web backend sends the name of every key it
-/// doesn't know as text, so AltGr typed "AltGraph" into the story. `text_field_focused`: an
-/// egui text field has the keyboard; its text comes from the browser's input events, which can
-/// hold whole words, and is left alone.
-pub fn drop_key_name_text(raw: &mut egui::RawInput, text_field_focused: bool) {
- if !text_field_focused {
+/// Drop text events that are key names: eframe's web backend sends the name of every key it
+/// doesn't know as text, so AltGr typed "AltGraph" into the story. `text_input_focused`: an egui
+/// text field or the canvas's text caret has the keyboard, so the browser's hidden text input
+/// sends the text, which holds only typed text (whole words from autocompletion too) and is left
+/// alone.
+pub fn drop_key_name_text(raw: &mut egui::RawInput, text_input_focused: bool) {
+ if !text_input_focused {
raw.events.retain(|e| !matches!(e, egui::Event::Text(t) if is_key_name(t)));
}
}
@@ -1248,7 +1306,14 @@ mod tests {
let mut raw = egui::RawInput { events: events(), ..Default::default() };
drop_key_name_text(&mut raw, false);
assert_eq!(raw.events, vec![text("é"), text("A"), text("ß"), text("ab"), text("Æ")]);
- // A focused text field gets its text from input events, which can be whole words.
+ assert!(is_key_name("F13") && is_key_name("Escape") && is_key_name("ZoomToggle") && !is_key_name("F") && !is_key_name("Fx"));
+ // Capitalised words (mobile autocompletion) are text.
+ let words = vec![text("Hello"), text("Tokyo"), text("OK"), text("Hi there")];
+ let mut raw = egui::RawInput { events: words.clone(), ..Default::default() };
+ drop_key_name_text(&mut raw, false);
+ assert_eq!(raw.events, words);
+ // A focused text field or a canvas text caret gets its text from the browser's input
+ // events, which hold whole words, key-name-like ones too ("Help").
let mut raw = egui::RawInput { events: events(), ..Default::default() };
drop_key_name_text(&mut raw, true);
assert_eq!(raw.events, events());
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index fab03ddf3..b9c4439d9 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1575,8 +1575,22 @@ pub(crate) fn export_pdf(app: &mut DesignApp, p: &Value) -> Result<Value, String
}
}
+/// Commands that act on the text being typed wait while an IME composes at the caret (its marked
+/// text isn't typed yet): Undo and Redo (the native menu takes ⌘Z ahead of the IME), the clipboard
+/// and the selection.
+pub(crate) fn waits_for_ime(app: &DesignApp, id: &str) -> bool {
+ app.session.tool_composing()
+ && (matches!(
+ id,
+ "edit.undo" | "edit.redo" | "edit.cut" | "edit.copy" | "edit.clear" | "edit.duplicate" | "edit.selectAll" | "edit.deselectAll"
+ ) || id.starts_with("edit.paste"))
+}
+
/// Is a command enabled (for menu greying)?
pub fn enabled(app: &DesignApp, id: &str) -> bool {
+ if waits_for_ime(app, id) {
+ return false;
+ }
match designcraft_engine::find_command(id) {
Some(c) => (c.enabled)(&app.session).is_ok(),
None => true,
@@ -1787,6 +1801,9 @@ pub fn checked(app: &DesignApp, id: &str, params: &Value) -> Option<bool> {
/// Enablement for menu display (UI commands need a document unless they're app/window-level).
pub fn menu_enabled(app: &DesignApp, id: &str) -> bool {
+ if waits_for_ime(app, id) {
+ return false;
+ }
if ui_label(id).is_some() {
return app.session.active().is_some() || id.starts_with("app.") || id.starts_with("window.");
}
@@ -1811,6 +1828,9 @@ pub fn close_document(app: &mut DesignApp, index: Option<usize>) {
/// A menu item was chosen. An engine command whose label ends in "…" and that takes parameters
/// opens a dialog built from its parameter documentation (see [`crate::dialogs::command_fields`]).
pub fn activate(app: &mut DesignApp, id: &str, params: &Value) {
+ if waits_for_ime(app, id) {
+ return;
+ }
if params.is_null() && id == "file.close" {
close_document(app, None);
return;
@@ -2090,7 +2110,8 @@ fn parse_shortcut(sc: &str) -> Option<(egui::Modifiers, egui::Key)> {
pub fn shortcuts(app: &mut DesignApp, ctx: &egui::Context) {
// Only a focused text field takes the keys: the canvas (or a button) having focus after a click
// must not swallow tool shortcuts until Esc clears it.
- if ctx.text_edit_focused() || app.ui.dialog.is_some() || app.ui.palette.is_some() {
+ // While an IME composes at the text caret, the keyboard is its own.
+ if ctx.text_edit_focused() || app.ui.dialog.is_some() || app.ui.palette.is_some() || app.session.tool_composing() {
return;
}
let typing = app.session.wants_text();
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 52c96586c..263a28f62 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -28,6 +28,7 @@ so only enable it while you use it. Transport: `apps/designcraft/src/control_ser
| `ui.tool.select` | `{tool}` | Select a tool (`selection`, `type`, `rectangleFrame`, …) |
| `ui.pointer` | `{events:[{kind: down\|drag\|up\|move\|doubleclick, x, y, space?: "screen"\|"canvas", clicks?}], mods?}` | Drive the active tool through the same code path as the mouse; `clicks` on a `down` is the press count (3 = triple click); returns the selection, the tool and its cursor at the last event (e.g. `LoadedText`, `ThreadLink`, `Unthread`) |
| `ui.key` / `ui.text` | `{key, shift?, alt?, cmd?}` / `{text}` | Synthetic keyboard input (typing into a text frame) |
+| `ui.ime` | `{preedit?, active?: [start, end], commit?}` | Input method events as the system IME sends them: marked text (shown in place, `active` is the clause being converted, in characters) and the committed text (typed as one `text.insert`) |
| `ui.move` / `ui.click` / `ui.drag` | screen points, `button?: left\|right\|middle` | Real egui pointer input — reaches every widget, menu and panel |
| `ui.set` | `{brightness?, panel?, rulers?, guides?, frameEdges?, baselineGrid?, textThreads?, screenMode?, zoom?, page?, fit?}` | UI state |
| `ui.dialog.open` | `{id, fields?}` | Open a dialog by id (e.g. `paragraphStyleOptions` or `characterStyleOptions` with `{name, section}`, or `{new: true}` for a new style) |
diff --git a/docs/mcp.md b/docs/mcp.md
index 772f8a91f..5f8e000c3 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -88,6 +88,7 @@ and its second character starts at 3. An offset inside a character snaps back to
| `pointer` | Mouse gesture events (`down`/`drag`/`up`/`move`/`doubleclick`) for the active tool, optional `tool` |
| `key` | Key press with modifiers (headless: active tool → command shortcuts → tool shortcuts) |
| `type_text` | Type at the text insertion point (or into the focused dialog field in the app) |
+| `ime` | Input method events at the Type tool's caret: `preedit` (marked text in place, `active` = converting clause in characters), `commit` (typed as one step) |
| `click` / `drag` | Real pointer input at screen points (connected only) |
| `menu_list`, `ui_inspect`, `ui_set` | Menu tree, UI state, change UI state (connected only) |
| `dialog_set` / `dialog_confirm` / `dialog_cancel` | Fill in and close the open dialog (connected only) |