Skip to content

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

Merge PR #1002: Tools: Cmd held with another tool borrows the Selection tool for one gesture

aiongg6a22e38b1e23
diff --cc crates/engine/src/tooling.rs
index b73d9a6aa,964fd9c1c..47f1ff6c0
--- a/crates/engine/src/tooling.rs
+++ b/crates/engine/src/tooling.rs
@@@ -60,11 -61,9 +60,12 @@@ impl Session
if self.tool.id() == id {
return;
}
- self.end_composition();
+ // 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);
+ self.temp_tool = None;
// Any other tool unloads the place cursor.
if id != "placeGun" {
self.loaded = None;
@@@ -105,52 -104,64 +106,96 @@@
unit: view.unit,
stroke_in_front: view.stroke_in_front,
};
- // A gesture a Cmd-held press started runs on the borrowed selection tool.
- let tool = self.temp_tool.as_deref_mut().unwrap_or(self.tool.as_mut());
- Some(f(tool, &cx))
+ Some(f(self, &cx))
+ }
+
++ /// The tool pointer, key, overlay and cursor events go to: the selection tool a Cmd-held press
++ /// borrowed, for the rest of that gesture, or else the chosen tool.
++ fn gesture_tool(&mut self) -> &mut dyn designcraft_tools::Tool {
++ match self.temp_tool.as_deref_mut() {
++ Some(t) => t,
++ None => self.tool.as_mut(),
++ }
+ }
+
+ /// Which selection tool a Cmd-held press borrows while `tool` is chosen, as InDesign's
+ /// Cmd/Ctrl does: the Direct Selection tool for the Pen family and Pencil, the Selection
+ /// tool for the drawing, type and other tools; none for the selection tools themselves,
+ /// Hand, Zoom and a loaded place cursor, where Cmd means something else or nothing.
+ fn borrowed_selection_tool(tool: &str) -> Option<&'static str> {
+ match tool {
+ "selection" | "directSelection" | "hand" | "zoom" | "placeGun" => None,
+ "pen" | "addAnchor" | "deleteAnchor" | "convertDirection" | "pencil" | "smooth" | "erase" | "scissors" => Some("directSelection"),
+ _ => Some("selection"),
+ }
+ }
+
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, |s, cx| s.tool.pointer(cx, ev)).unwrap_or_default();
- self.run_actions(actions)
+ // Cmd held at a press: the gesture (press, drags, release) goes to the selection tool the
+ // chosen tool borrows, which selects, moves or resizes without a tool change (#919).
+ if matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)
+ && ev.mods.cmd
+ && self.temp_tool.is_none()
+ && !self.tool.busy()
+ && let Some(id) = Self::borrowed_selection_tool(self.tool.id())
+ {
+ self.temp_tool = Some(designcraft_tools::create(id));
+ }
- let actions = self.with_ctx(view, |t, cx| t.pointer(cx, ev)).unwrap_or_default();
++ let actions = self.with_ctx(view, |s, cx| s.gesture_tool().pointer(cx, ev)).unwrap_or_default();
+ let r = self.run_actions(actions);
+ if ev.kind == PointerKind::Up && self.temp_tool.as_ref().is_some_and(|t| !t.busy()) {
+ self.temp_tool = None;
+ }
+ r
}
+ /// Dispatch a temporary view tool without replacing the active tool or its state.
+ /// Its actions use the same execution path as the selected tool's gestures.
+ pub fn pointer_with_tool(&mut self, tool: &mut dyn designcraft_tools::Tool, ev: &PointerEvent, view: ViewInfo) -> Result<()> {
+ let actions = self.with_ctx(view, |_, cx| tool.pointer(cx, ev)).unwrap_or_default();
+ self.run_actions(actions)
+ }
+
pub fn tool_key(&mut self, key: ToolKey, mods: Mods, view: ViewInfo) -> Result<bool> {
- let actions = self.with_ctx(view, |t, cx| t.key(cx, key, mods)).unwrap_or_default();
+ // 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, |s, cx| s.tool.key(cx, key, mods)).unwrap_or_default();
++ let actions = self.with_ctx(view, |s, cx| s.gesture_tool().key(cx, key, mods)).unwrap_or_default();
let handled = !actions.is_empty();
self.run_actions(actions)?;
Ok(handled)
}
pub fn overlays(&mut self, view: ViewInfo) -> Vec<Overlay> {
- self.with_ctx(view, |s, cx| s.tool.overlays(cx)).unwrap_or_default()
- self.with_ctx(view, |t, cx| t.overlays(cx)).unwrap_or_default()
++ self.with_ctx(view, |s, cx| s.gesture_tool().overlays(cx)).unwrap_or_default()
}
pub fn cursor(&mut self, p: designcraft_geom::Point, mods: Mods, view: ViewInfo) -> Cursor {
- self.with_ctx(view, |s, cx| s.tool.cursor(cx, p, mods)).unwrap_or_default()
+ // Cmd held over the canvas shows the selection tool a press would borrow.
+ if mods.cmd
+ && self.temp_tool.is_none()
+ && !self.tool.busy()
+ && let Some(id) = Self::borrowed_selection_tool(self.tool.id())
+ {
+ let borrowed = designcraft_tools::create(id);
+ return self.with_ctx(view, |_, cx| borrowed.cursor(cx, p, mods)).unwrap_or_default();
+ }
- self.with_ctx(view, |t, cx| t.cursor(cx, p, mods)).unwrap_or_default()
++ self.with_ctx(view, |s, cx| s.gesture_tool().cursor(cx, p, mods)).unwrap_or_default()
+ }
+
+ /// Canvas centres of the content grabbers the Selection tool shows: the selected frame's and
+ /// the one under `hover`.
+ pub fn content_grabbers(&mut self, hover: Option<designcraft_geom::Point>, view: ViewInfo) -> Vec<designcraft_geom::Point> {
+ if self.tool.id() != "selection" {
+ return vec![];
+ }
+ self.with_ctx(view, |_, cx| designcraft_tools::select::grabbers(cx, hover).into_iter().map(|(_, c)| c).collect()).unwrap_or_default()
}
pub fn tool_busy(&self) -> bool {
@@@ -158,56 -169,7 +203,56 @@@
}
pub fn wants_text(&mut self) -> bool {
- self.with_ctx(ViewInfo::at_zoom(1.0), |s, cx| s.tool.wants_text(cx)).unwrap_or(false)
- self.with_ctx(ViewInfo::at_zoom(1.0), |t, cx| t.wants_text(cx)).unwrap_or(false)
++ self.with_ctx(ViewInfo::at_zoom(1.0), |s, cx| s.gesture_tool().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, |s, cx| s.tool.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, |s, cx| s.tool.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, |s, cx| s.tool.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(), |s, cx| s.tool.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, |s, cx| s.tool.ime_caret(cx)).flatten()
}
pub fn run_actions(&mut self, actions: Vec<Action>) -> Result<()> {
diff --cc docs/ui-parity.md
index 7454e1369,541d432a4..fe276cfde
--- a/docs/ui-parity.md
+++ b/docs/ui-parity.md
@@@ -28,9 -28,9 +28,9 @@@ basics of editing fail for too many use
|---|---|---|---|---|
| Chrome: app bar, Control panel, Tools panel, dock, doc tabs, status bar | Medium Dark, measured sizes | measured look | ✓ | 11-observed-ui.md |
| Properties panel and Contextual Task Bar | per selection | ✓, task bar movable (#143) | ✓ | |
- | Selection tool: click, marquee, move, Alt-duplicate, resize handles, rotate from corners | ✓ | ✓ | ✓ | |
+ | Selection tool: click, marquee, move, Alt-duplicate, resize handles, rotate from corners | ✓ | ✓ | ✓ | Cmd/Ctrl held with another tool borrows it for one gesture (Direct Selection with the Pen family), #919 |
| Direct Selection, Pen family, Pencil, Scissors | ✓ | ✓ | ~ | moving reference point "0" fails (#337) |
-| Live corners (yellow handle) | ✓ | ✓ | ~ | not working for some users (#186) |
+| Live corners (yellow handle) | ✓ | ✓ widget on a selected rectangle frame; diamonds drag the corner size (Shift: one corner), Alt-click steps the shape (Shift+Alt: all) | ✓ | #186 |
| Content grabber, frame fitting by double-click | ✓ | ✓ | ✓ | |
| Smart guides, snapping to guides/grid/baseline | ✓ | ✓ (2,049-line `snap.rs`) | ~ | vertical smart spacing between two rectangles (#188) |
| Nudging with arrows, Shift ×10 | ✓ | ✓ | ✓ | |