SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Agent chat: a text selection dragged past the edge scrolls the conversation (#11527) (#11533)
Mateusz Sych28b47059bb07
diff --git a/crates/ui-egui/src/agent_chat/markdown_view.rs b/crates/ui-egui/src/agent_chat/markdown_view.rsindex c9f8473426..a2174ea831 100644--- a/crates/ui-egui/src/agent_chat/markdown_view.rs+++ b/crates/ui-egui/src/agent_chat/markdown_view.rs@@ -60,10 +60,33 @@ fn host(url: &str) -> &str {rest.split(['/', '?', '#']).next().unwrap_or(rest)}+/// A selectable label of the conversation. egui leaves a label scrolled out of view out of the+/// text selection, so a selection whose start had scrolled away was dropped, and Copy missed the+/// hidden text (#11527). Out of view this one still takes part: it keeps its end of the selection+/// and gives its text to Copy, clipped to nothing so it is not drawn and never moves the selection.+pub(super) fn selectable(ui: &mut egui::Ui, label: egui::Label) -> egui::Response {+ use egui::text_selection::LabelSelectionState;+ let (pos, galley, response) = label.selectable(true).layout_in_ui(ui);+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Label, ui.is_enabled(), galley.text()));+ // As `egui::Label` draws a label that is not a button.+ let colour = ui.visuals().text_color();+ if ui.is_rect_visible(response.rect) {+ let underline = if response.has_focus() || response.highlighted() { Stroke::new(1.0, colour) } else { Stroke::NONE };+ LabelSelectionState::label_text_selection(ui, &response, pos, galley, colour, underline);+ } else if ui.ctx().plugin::<LabelSelectionState>().lock().has_selection() {+ let clip = ui.clip_rect();+ ui.set_clip_rect(egui::Rect::NOTHING);+ LabelSelectionState::label_text_selection(ui, &response, pos, galley, colour, Stroke::NONE);+ ui.set_clip_rect(clip);+ }+ response+}+/// A paragraph's text, then its links as buttons (a laid-out text cannot be clicked in parts).fn spans(ui: &mut egui::Ui, spans: &[Span], size: f32, color: Color32) {let t = Tokens::get();- ui.add(egui::Label::new(job(ui, spans, size, color)).selectable(true).wrap());+ let text = job(ui, spans, size, color);+ selectable(ui, egui::Label::new(text).wrap());let links: Vec<(&str, &str)> =spans.iter().filter_map(|s| if let Span::Link { label, url } = s { Some((label.as_str(), url.as_str())) } else { None }).collect();if !links.is_empty() {@@ -92,7 +115,7 @@ pub fn show(ui: &mut egui::Ui, slot: usize, src: &str) {_ => t.font_heading,};ui.add_space(4.0);- ui.add(egui::Label::new(RichText::new(markdown::plain(s)).size(size).semibold().color(t.text)).selectable(true).wrap());+ selectable(ui, egui::Label::new(RichText::new(markdown::plain(s)).size(size).semibold().color(t.text)).wrap());}Block::Code { text, .. } => {crate::ui_kit::Container::show_backing(@@ -100,10 +123,9 @@ pub fn show(ui: &mut egui::Ui, slot: usize, src: &str) {egui::Frame::NONE.fill(t.field).corner_radius(t.radius_sm).inner_margin(Margin::same(6)),|ui| {ui.set_width(ui.available_width());- ui.add(- egui::Label::new(RichText::new(text).monospace().size(size - (t.font_button - t.font_body)).color(t.text))- .selectable(true)- .wrap(),+ selectable(+ ui,+ egui::Label::new(RichText::new(text).monospace().size(size - (t.font_button - t.font_body)).color(t.text)).wrap(),);if crate::ui_kit::button(ui, tr("ui.agent_chat_copy"), crate::ui_kit::Button::Small).clicked() {ui.ctx().copy_text(text.clone());@@ -129,7 +151,7 @@ pub fn show(ui: &mut egui::Ui, slot: usize, src: &str) {}Block::Pre(text) => {egui::ScrollArea::horizontal().id_salt(("agent_chat_pre", slot, text.len())).show(ui, |ui| {- ui.add(egui::Label::new(RichText::new(text).monospace().size(t.font_small).color(t.text)).selectable(true));+ selectable(ui, egui::Label::new(RichText::new(text).monospace().size(t.font_small).color(t.text)));});}Block::Rule => {diff --git a/crates/ui-egui/src/agent_chat/panel.rs b/crates/ui-egui/src/agent_chat/panel.rsindex 6276755f81..30002a73cd 100644--- a/crates/ui-egui/src/agent_chat/panel.rs+++ b/crates/ui-egui/src/agent_chat/panel.rs@@ -1056,6 +1056,8 @@ fn desktop_body(app: &mut SolveApp, ui: &mut egui::Ui) {ui.spacing_mut().item_spacing.y = Tokens::get().space_10;conversation(app, ui);});+ // Last, so the scroll goes to this area and not to one nested in the conversation.+ scroll_while_selecting(ui);});if esc || stop_clicked {cancel(app);@@ -1678,6 +1680,40 @@ fn copy_message_menu(ui: &egui::Ui, response: &egui::Response, n: usize, text: &}}+/// A text selection dragged near or past the top or bottom of the conversation scrolls it, faster+/// the further out, so a long answer can be selected in one drag (#11527). Only a drag towards+/// that edge does: a click, a double click or a held press on a line by the edge, or a drag from+/// there into the conversation, scrolls nothing. `ui`: the conversation's scroll area, once its+/// content is drawn.+#[cfg(not(target_arch = "wasm32"))]+fn scroll_while_selecting(ui: &egui::Ui) {+ // Scrolling starts this far inside the edge, and goes this many points a second for each+ // point further out, up to four times as far out; once the pointer has moved more than+ // `TOWARDS` that way since the press.+ const EDGE: f32 = 32.0;+ const SPEED: f32 = 16.0;+ const TOWARDS: f32 = 8.0;+ let view = ui.clip_rect();+ let pointer = ui.input(|i| i.pointer.press_origin().filter(|p| i.pointer.primary_down() && view.contains(*p)).zip(i.pointer.latest_pos()));+ let Some((from, at)) = pointer else {+ return;+ };+ if !ui.ctx().plugin::<egui::text_selection::LabelSelectionState>().lock().has_selection() {+ return;+ }+ let content = ui.min_rect();+ let out = if at.y < view.top() + EDGE && from.y - at.y > TOWARDS && content.top() < view.top() - 0.5 {+ at.y - (view.top() + EDGE)+ } else if at.y > view.bottom() - EDGE && at.y - from.y > TOWARDS && content.bottom() > view.bottom() + 0.5 {+ at.y - (view.bottom() - EDGE)+ } else {+ return;+ };+ let step = out.clamp(-4.0 * EDGE, 4.0 * EDGE) * SPEED * ui.input(|i| i.stable_dt).min(0.1);+ // A positive delta moves the content down, showing what is above.+ ui.scroll_with_delta_animation(vec2(0.0, -step), egui::style::ScrollAnimation::none());+}+#[cfg(not(target_arch = "wasm32"))]fn conversation(app: &mut SolveApp, ui: &mut egui::Ui) {let t = Tokens::get();@@ -1696,7 +1732,7 @@ fn conversation(app: &mut SolveApp, ui: &mut egui::Ui) {let before = ui.cursor();ui_kit::assistant_bubble(ui, |ui| {if !text.is_empty() {- ui.add(egui::Label::new(RichText::new(text).color(t.text)).selectable(true).wrap());+ super::markdown_view::selectable(ui, egui::Label::new(RichText::new(text).color(t.text)).wrap());}if !pictures.is_empty() {if let Some(at) = picture_strip(ui, n, pictures) {@@ -1720,7 +1756,7 @@ fn conversation(app: &mut SolveApp, ui: &mut egui::Ui) {}Item::Agent { thought, text } => {if *thought {- ui.add(egui::Label::new(RichText::new(text).italics().color(t.text_dim)).selectable(true).wrap());+ super::markdown_view::selectable(ui, egui::Label::new(RichText::new(text).italics().color(t.text_dim)).wrap());} else {let before = ui.cursor();ui_kit::assistant_prose(ui, |ui| super::markdown_view::show(ui, n, text));@@ -1801,7 +1837,7 @@ fn conversation(app: &mut SolveApp, ui: &mut egui::Ui) {Item::Approval { id, title, tool, answer: given } => {ui_kit::assistant_approval(ui, tr("ui.agent_chat_approval"), |ui| {ui_kit::assistant_tool_detail(ui, |ui| {- ui.add(egui::Label::new(RichText::new(title).monospace().size(t.font_caption)).selectable(true).wrap());+ super::markdown_view::selectable(ui, egui::Label::new(RichText::new(title).monospace().size(t.font_caption)).wrap());});match given {Some(true) => {@@ -1910,7 +1946,7 @@ fn conversation(app: &mut SolveApp, ui: &mut egui::Ui) {}_ => {if !p.text.is_empty() {- ui.add(egui::Label::new(RichText::new(&p.text).color(t.text)).selectable(true).wrap());+ super::markdown_view::selectable(ui, egui::Label::new(RichText::new(&p.text).color(t.text)).wrap());}if p.image {ui.label(RichText::new(tr("ui.agent_chat_view_attached")).color(t.text_dim).size(crate::theme::Tokens::get().font_small));diff --git a/crates/ui-egui/src/agent_chat/tests.rs b/crates/ui-egui/src/agent_chat/tests.rsindex d1068ea56e..deeed0abfd 100644--- a/crates/ui-egui/src/agent_chat/tests.rs+++ b/crates/ui-egui/src/agent_chat/tests.rs@@ -2905,6 +2905,133 @@ fn narrow_chat() -> crate::scenario::Harness {h}+/// The rows `a_selection_held_at_the_edge_scrolls_and_keeps_its_start` draws that are in view:+/// each row's number, where its text is, and the conversation's view.+fn rows_in_view(h: &crate::scenario::Harness) -> Vec<(usize, egui::Rect, egui::Rect)> {+ let mut rows = Vec::new();+ for clipped in &h.shapes {+ crate::test_support::each_shape(&clipped.shape, &mut |shape| {+ if let egui::Shape::Text(text) = shape+ && let Some(n) = text.galley.text().strip_prefix("Parts list row ").and_then(|n| n.parse::<usize>().ok())+ {+ let at = egui::Rect::from_min_size(text.pos, text.galley.size());+ if clipped.clip_rect.intersects(at) {+ rows.push((n, at, clipped.clip_rect));+ }+ }+ });+ }+ rows.sort_by_key(|r| r.0);+ rows+}++/// #11527: a text selection held at the conversation's edge scrolls it, and keeps its start once+/// that has scrolled out of view, so Copy takes the hidden rows too. Resting there with no button+/// down, or after letting go, scrolls nothing.+#[test]+fn a_selection_held_at_the_edge_scrolls_and_keeps_its_start() {+ let mut h = narrow_chat();+ for n in 0..80 {+ h.app.agent_chat.items.push(Item::Agent { thought: false, text: format!("Parts list row {n}") });+ }+ h.frames(4);+ let rows = rows_in_view(&h);+ let (&(top, _, view), &(last, last_at, _)) = (rows.first().expect("rows in view"), rows.last().expect("rows in view"));+ assert_eq!(last, 79, "the conversation shows its end");+ let edge = egui::pos2(last_at.left() + 10.0, view.top() + 4.0);+ let press = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };+ h.frame_with(vec![egui::Event::PointerMoved(edge)]);+ h.frames(20);+ assert_eq!(rows_in_view(&h)[0].0, top, "no button down: the pointer at the edge scrolls nothing");++ // Start the selection at the end of the last row and hold it at the top edge.+ let start = last_at.right_center() - egui::vec2(1.0, 0.0);+ h.frame_with(vec![egui::Event::PointerMoved(start), press(start, true)]);+ h.frame_with(vec![egui::Event::PointerMoved(edge)]);+ for _ in 0..600 {+ if !rows_in_view(&h).iter().any(|r| r.0 == last) {+ break;+ }+ h.frame();+ }+ let rows = rows_in_view(&h);+ assert!(!rows.iter().any(|r| r.0 == last), "holding the selection at the top scrolls the last row out of view");+ let reached = rows[0].0;+ assert!(reached < top, "row {reached} is in view, it was {top}");+ h.frame_with(vec![press(edge, false)]);+ let after = rows_in_view(&h)[0].0;+ h.frames(20);+ assert_eq!(rows_in_view(&h)[0].0, after, "letting go stops the scrolling");+ assert!(h.ctx.plugin::<egui::text_selection::LabelSelectionState>().lock().has_selection(), "the selection keeps its start out of view");++ h.frame_with(vec![egui::Event::Copy]);+ let copied = h+ .output+ .commands+ .iter()+ .find_map(|c| if let egui::OutputCommand::CopyText(text) = c { Some(text.clone()) } else { None })+ .expect("Copy takes the selection");+ // The selection's moving end is under the pointer, in the first row or two in view.+ for n in [reached + 2, 78] {+ assert!(copied.contains(&format!("Parts list row {n}\n")), "row {n} is copied whole: {copied:?}");+ }+ assert!(copied.trim_end().ends_with("Parts list row 79"), "the selection ends where it started, out of view: {copied:?}");+}++/// #11527: only a drag towards the edge scrolls. Holding a press on the line at the top edge, a+/// double click there, or a drag from there into the conversation leaves it where it is.+#[test]+fn a_press_at_the_edge_without_a_drag_towards_it_scrolls_nothing() {+ let mut h = narrow_chat();+ // One paragraph taller than the conversation: every point in view is on its text.+ h.app.agent_chat.items.push(Item::Agent { thought: false, text: "Bill of materials ".repeat(400) });+ h.frames(4);+ // Where the answer's text starts, the conversation's view, and the first line wholly in view+ // (egui itself scrolls a pressed line that is cut off at the edge into view).+ let answer = |h: &crate::scenario::Harness| {+ let mut found = 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().starts_with("Bill of materials")+ && clipped.clip_rect.intersects(egui::Rect::from_min_size(text.pos, text.galley.size()))+ {+ let line = text.galley.rows.iter().map(|r| r.rect().translate(text.pos.to_vec2())).find(|r| r.top() >= clipped.clip_rect.top());+ found = Some((text.pos.y, clipped.clip_rect, line.expect("a line wholly in view")));+ }+ });+ }+ found.expect("the answer is in view")+ };+ let (rest, view, line) = answer(&h);+ let press = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };+ let edge = egui::pos2(view.left() + 40.0, line.center().y);+ assert!(edge.y < view.top() + 32.0, "the first line in view is by the edge");+ h.frame_with(vec![egui::Event::PointerMoved(edge), press(edge, true)]);+ h.frames(30);+ assert_eq!(answer(&h).0, rest, "a press held at the edge scrolls nothing");+ h.frame_with(vec![press(edge, false)]);+ for _ in 0..2 {+ h.frame_with(vec![press(edge, true)]);+ h.frame_with(vec![press(edge, false)]);+ }+ h.frames(10);+ assert_eq!(answer(&h).0, rest, "a double click at the edge scrolls nothing");+ let inside = edge + egui::vec2(0.0, 20.0);+ h.frame_with(vec![egui::Event::PointerMoved(edge), press(edge, true)]);+ h.frame_with(vec![egui::Event::PointerMoved(inside)]);+ h.frames(30);+ assert_eq!(answer(&h).0, rest, "a drag from the edge into the conversation scrolls nothing");+ h.frame_with(vec![press(inside, false)]);+ // Towards the edge it does.+ let middle = view.center();+ h.frame_with(vec![egui::Event::PointerMoved(middle), press(middle, true)]);+ h.frame_with(vec![egui::Event::PointerMoved(edge)]);+ h.frames(30);+ assert!(answer(&h).0 > rest, "a drag to the top edge scrolls the answer down");+ h.frame_with(vec![press(edge, false)]);+}+/// #10172: before a connection, what the panel draws under the agent switcher (the Agent row/// with Add agent and Refresh) keeps the panel's side margin, docked and floating: nothing starts/// at the panel's edge or is cut off at the other one.diff --git a/docs/agent-chat.md b/docs/agent-chat.mdindex e65e28f636..c6dc94293b 100644--- a/docs/agent-chat.md+++ b/docs/agent-chat.md@@ -53,6 +53,11 @@ in an answer. Copy, Cut and Paste do not also operate on selected CAD geometry.pointer back over the model does not change this: click outside Assistant to return thekeyboard to CAD. Closing or minimising the panel also releases it.+To select more of the conversation than fits in the panel, drag the selection to its top or+bottom edge and hold it there: the conversation scrolls, faster the further past the edge you+go, and the selection grows with it. It stays when its start scrolls out of view, and Copy takes+all of 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 thewindow and let go. The side it would land on lights up while you drag. You can also pick