Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Agent chat: keep transcript shortcuts out of CAD (#10576)

MarkusRe6e67ae34b060
diff --git a/crates/ui-egui/src/agent_chat/mod.rs b/crates/ui-egui/src/agent_chat/mod.rs
index 8a9a7b6848..c2f4b7261b 100644
--- a/crates/ui-egui/src/agent_chat/mod.rs
+++ b/crates/ui-egui/src/agent_chat/mod.rs
@@ -49,7 +49,7 @@ use crate::i18n::{format as trf, tr};
use crate::{ControlRequest, SolveApp};
use backend::{ChatBackend, Event, PermissionOption, PromptPart, ToolStatus};
-pub(crate) use panel::{left_inset, tool_space};
+pub(crate) use panel::{left_inset, owns_keyboard_input, tool_space};
pub use panel::{show, tip as panel_tip, toggle, toggle_shortcut};
/// Most entries kept in the conversation; older ones are dropped.
@@ -375,6 +375,8 @@ pub struct AgentChat {
pub confirm_new: bool,
/// Where the panel was drawn last (for its shortcuts).
pub panel_rect: Option<egui::Rect>,
+ /// A pointer press in the transcript owns keys just like a focused text editor.
+ keyboard_owned: bool,
/// Unreserved sheet space, shared by the actual dock and its drag preview.
pub(crate) dock_space: Option<egui::Rect>,
/// The last message sent, kept to send again after a sign-in (its files too).
@@ -477,6 +479,7 @@ impl AgentChat {
}
// Hit regions must be measured again for the incoming conversation.
self.panel_rect = None;
+ self.keyboard_owned = false;
self.dock_space = None;
self.drop_rect = None;
}
diff --git a/crates/ui-egui/src/agent_chat/panel.rs b/crates/ui-egui/src/agent_chat/panel.rs
index 201b2f8b8f..b7388ca0cf 100644
--- a/crates/ui-egui/src/agent_chat/panel.rs
+++ b/crates/ui-egui/src/agent_chat/panel.rs
@@ -83,6 +83,26 @@ pub fn tip(app: &SolveApp) -> String {
}
}
+/// Selectable transcript labels do not take egui keyboard focus. Keep their keys out of
+/// CAD until a pointer press outside the chat, not merely a move of the pointer. Use the
+/// panel in this window, never label selections belonging to another native viewport.
+/// Called before layout so even a click and a shortcut in the same frame are routed here.
+pub(crate) fn owns_keyboard_input(app: &mut SolveApp, ctx: &egui::Context) -> bool {
+ let rect = app.agent_chat.panel_rect.filter(|_| shown(app) && app.agent_chat.open && !app.preferences.agent_chat_minimized);
+ let Some(rect) = rect else {
+ app.agent_chat.keyboard_owned = false;
+ return false;
+ };
+ ctx.input(|input| {
+ for event in &input.events {
+ if let egui::Event::PointerButton { pos, pressed: true, .. } = event {
+ app.agent_chat.keyboard_owned = rect.contains(*pos);
+ }
+ }
+ });
+ app.agent_chat.keyboard_owned
+}
+
/// The panel, when the chat is turned on and open. Call before the central viewport is laid out.
pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
let ctx = ui.ctx().clone();
diff --git a/crates/ui-egui/src/agent_chat/tests.rs b/crates/ui-egui/src/agent_chat/tests.rs
index 18aa273c09..64b0940e82 100644
--- a/crates/ui-egui/src/agent_chat/tests.rs
+++ b/crates/ui-egui/src/agent_chat/tests.rs
@@ -1,6 +1,126 @@
use super::*;
use std::sync::mpsc::channel;
+fn transcript_clipboard_harness(layout: crate::shell::LayoutMode) -> crate::scenario::Harness {
+ let mut h = narrow_chat();
+ h.app.ui.layout = layout;
+ h.app.preferences.agent_chat_width = 500.0;
+ h.app.agent_chat.items.push(Item::Agent { thought: false, text: "Transcript clipboard sentinel".into() });
+ h.app.run("solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"})).unwrap();
+ h.app.run("select.set", json!({"items": [{"type": "body", "name": "Cube"}]})).unwrap();
+ h.frames(4);
+ h
+}
+
+fn transcript_text_position(h: &crate::scenario::Harness) -> egui::Pos2 {
+ let mut at = None;
+ for clipped in &h.shapes {
+ crate::test_support::each_shape(&clipped.shape, &mut |shape| {
+ if let egui::Shape::Text(text) = shape
+ && text.galley.text() == "Transcript clipboard sentinel"
+ {
+ at = Some(text.pos + egui::vec2(15.0, 7.0));
+ }
+ });
+ }
+ at.expect("the assistant message is visible")
+}
+
+fn transcript_click(h: &mut crate::scenario::Harness, pos: egui::Pos2) {
+ h.frame_with(vec![
+ egui::Event::PointerMoved(pos),
+ egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE },
+ ]);
+ h.frame_with(vec![egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }]);
+}
+
+#[test]
+fn transcript_copy_and_cut_do_not_touch_the_cad_clipboard() {
+ for layout in [crate::shell::LayoutMode::Docked, crate::shell::LayoutMode::Floating] {
+ let mut h = transcript_clipboard_harness(layout);
+ let at = transcript_text_position(&h);
+ transcript_click(&mut h, at);
+ transcript_click(&mut h, at);
+ assert!(h.ctx.plugin::<egui::text_selection::LabelSelectionState>().lock().has_selection(), "select real label text");
+ assert!(!h.ctx.egui_wants_keyboard_input(), "a selectable label is not a text editor");
+ // Moving the mouse away does not transfer the keyboard back to the model.
+ h.frame_with(vec![egui::Event::PointerMoved(egui::pos2(500.0, 500.0))]);
+ let undo = h.app.session.undo.len();
+ h.frame_with(vec![egui::Event::Copy]);
+ assert!(h.output.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(text) if text == "Transcript")), "copy selected text");
+ assert!(h.app.copied_bodies.is_empty(), "{layout:?}: copying text must not copy a CAD body");
+ h.frame_with(vec![egui::Event::Cut]);
+ assert!(h.app.cut.is_empty(), "{layout:?}: cutting read-only text must not cut a CAD body");
+ assert_eq!(h.app.session.undo.len(), undo);
+ }
+}
+
+#[test]
+fn transcript_paste_does_not_insert_a_previously_copied_body() {
+ for layout in [crate::shell::LayoutMode::Docked, crate::shell::LayoutMode::Floating] {
+ let mut h = transcript_clipboard_harness(layout);
+ h.frame_with(vec![egui::Event::Copy]);
+ assert!(!h.app.copied_bodies.is_empty(), "prime the real CAD clipboard");
+ let at = transcript_text_position(&h);
+ transcript_click(&mut h, at);
+ transcript_click(&mut h, at);
+ let undo = h.app.session.undo.len();
+ h.frame_with(vec![egui::Event::Paste("text from another app".into())]);
+ assert_eq!(h.app.session.model.state().bodies.len(), 1, "{layout:?}: text paste must not paste CAD geometry");
+ assert_eq!(h.app.session.undo.len(), undo);
+ // A deliberate click outside the panel returns ownership to CAD.
+ transcript_click(&mut h, egui::pos2(700.0, 500.0));
+ h.frame_with(vec![egui::Event::Paste("SolidCraft bodies".into())]);
+ assert_eq!(h.app.session.model.state().bodies.len(), 2, "{layout:?}: CAD paste still works after returning to the model");
+ }
+}
+
+#[test]
+fn transcript_key_events_do_not_run_cad_shortcuts() {
+ for layout in [crate::shell::LayoutMode::Docked, crate::shell::LayoutMode::Floating] {
+ let mut h = transcript_clipboard_harness(layout);
+ let at = transcript_text_position(&h);
+ transcript_click(&mut h, at);
+ transcript_click(&mut h, at);
+ let key = |key| egui::Event::Key {
+ key,
+ physical_key: None,
+ pressed: true,
+ repeat: false,
+ modifiers: egui::Modifiers::CTRL | egui::Modifiers::COMMAND,
+ };
+ h.frame_with(vec![key(egui::Key::C)]);
+ assert!(h.app.copied_bodies.is_empty(), "{layout:?}: key-event copy also belongs to the chat");
+ h.frame_with(vec![key(egui::Key::X)]);
+ assert!(h.app.cut.is_empty(), "{layout:?}: key-event cut also belongs to the chat");
+ assert!(crate::edit_clipboard::copy(&mut h.app));
+ h.frame_with(vec![key(egui::Key::V)]);
+ assert_eq!(h.app.session.model.state().bodies.len(), 1, "{layout:?}: key-event paste stays out of CAD");
+ // A quick tap can have no modifiers held by the end of the frame. It must not
+ // fall through to the plain A (Appearance) shortcut while selecting chat text.
+ h.frame_with(vec![key(egui::Key::A)]);
+ assert!(h.app.dialog.is_none(), "{layout:?}: selecting text must not open Appearance");
+ }
+}
+
+#[test]
+fn transcript_shortcut_ownership_ends_when_the_panel_is_hidden() {
+ for hidden in ["closed", "minimized", "disabled"] {
+ let mut h = transcript_clipboard_harness(crate::shell::LayoutMode::Floating);
+ h.frame_with(vec![egui::Event::Copy]);
+ let at = transcript_text_position(&h);
+ transcript_click(&mut h, at);
+ transcript_click(&mut h, at);
+ match hidden {
+ "closed" => h.app.agent_chat.open = false,
+ "minimized" => h.app.preferences.agent_chat_minimized = true,
+ _ => h.app.preferences.ai_features = false,
+ }
+ h.frame_with(vec![egui::Event::Paste("SolidCraft bodies".into())]);
+ assert_eq!(h.app.session.model.state().bodies.len(), 2, "{hidden}: the invisible panel must not retain CAD keys");
+ }
+}
+
fn chat() -> AgentChat {
let mut c = AgentChat::default();
c.state = Some(State::Ready);
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index 6618df3637..fc96161978 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -3125,7 +3125,8 @@ pub(crate) fn cancel_unfinished(app: &mut SolveApp) -> bool {
pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
crate::viewport_status::reconcile(app, ctx);
crate::browser::update_folder_pick(app, ctx);
- if design_shown(app) {
+ let chat_owner = crate::agent_chat::owns_keyboard_input(app, ctx);
+ if design_shown(app) && !chat_owner {
crate::navigation::step_key_hold(app, ctx);
} else {
app.viewport.key_hold = None;
@@ -3201,6 +3202,7 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
}
if popup_owner
|| inline_editor
+ || chat_owner
|| (crate::viewport_status::enabled(app) && app.ui.palette_open)
|| (ctx.egui_wants_keyboard_input() && !(crate::viewport_status::enabled(app) && app.viewport_status.owns_focus(ctx)))
{
diff --git a/docs/agent-chat.md b/docs/agent-chat.md
index ab6ea43d00..27c3b01c4f 100644
--- a/docs/agent-chat.md
+++ b/docs/agent-chat.md
@@ -48,6 +48,11 @@ In the floating layout, Assistant fills the viewport height in the shared rounde
space around its edges. The compass and navigation bar move into the remaining space beside it. It overlays the model without changing the viewport projection;
clicks and scrolling over the panel stay in the chat. The docked layout retains its side panel.
+After clicking in Assistant, keyboard input stays in the chat, including when selecting text
+in an answer. Copy, Cut and Paste do not also operate on selected CAD geometry. Moving the
+pointer back over the model does not change this: click outside Assistant to return the
+keyboard to CAD. Closing or minimising the panel also releases it.
+
- **Docking:** on the left, Assistant is the first panel, before vertical history and the browser.
The drag highlight shows the actual destination bounds. Drag the panel's header (the dotted grip and the title) to the other half of the
window and let go. The side it would land on lights up while you drag. You can also pick