terminal: Fix misaligned mouse selection when inline assist is active (#26112)

This PR fixes an issue where mouse selection in the terminal would be
offset when the Terminal Inline Assistant was active. The problem was
caused by incorrect coordinate translation when handling mouse events
with an active inline assistant.

The fix adjusts mouse event coordinates by properly accounting for the
terminal view's `scroll_top` value when the inline assistant is present,
ensuring that text selection precisely follows the mouse cursor
position.

Closes #26111 

Release Notes:

- Fixed text selection misalignment in terminal when the inline
assistant is active

Co-authored-by: Peter Tripp <peter@zed.dev>
This commit is contained in:
Hourann 2025-04-08 04:10:14 +08:00 committed by GitHub
parent 5996c58452
commit e7a0f0e876
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -425,14 +425,23 @@ impl TerminalElement {
fn register_mouse_listeners(&mut self, mode: TermMode, hitbox: &Hitbox, window: &mut Window) { fn register_mouse_listeners(&mut self, mode: TermMode, hitbox: &Hitbox, window: &mut Window) {
let focus = self.focus.clone(); let focus = self.focus.clone();
let terminal = self.terminal.clone(); let terminal = self.terminal.clone();
let terminal_view = self.terminal_view.clone();
self.interactivity.on_mouse_down(MouseButton::Left, { self.interactivity.on_mouse_down(MouseButton::Left, {
let terminal = terminal.clone(); let terminal = terminal.clone();
let focus = focus.clone(); let focus = focus.clone();
let terminal_view = terminal_view.clone();
move |e, window, cx| { move |e, window, cx| {
window.focus(&focus); window.focus(&focus);
let scroll_top = terminal_view.read(cx).scroll_top;
terminal.update(cx, |terminal, cx| { terminal.update(cx, |terminal, cx| {
terminal.mouse_down(e, cx); let mut adjusted_event = e.clone();
if scroll_top > Pixels::ZERO {
adjusted_event.position.y += scroll_top;
}
terminal.mouse_down(&adjusted_event, cx);
cx.notify(); cx.notify();
}) })
} }
@ -442,6 +451,7 @@ impl TerminalElement {
let terminal = self.terminal.clone(); let terminal = self.terminal.clone();
let hitbox = hitbox.clone(); let hitbox = hitbox.clone();
let focus = focus.clone(); let focus = focus.clone();
let terminal_view = terminal_view.clone();
move |e: &MouseMoveEvent, phase, window, cx| { move |e: &MouseMoveEvent, phase, window, cx| {
if phase != DispatchPhase::Bubble { if phase != DispatchPhase::Bubble {
return; return;
@ -449,9 +459,15 @@ impl TerminalElement {
if e.pressed_button.is_some() && !cx.has_active_drag() && focus.is_focused(window) { if e.pressed_button.is_some() && !cx.has_active_drag() && focus.is_focused(window) {
let hovered = hitbox.is_hovered(window); let hovered = hitbox.is_hovered(window);
let scroll_top = terminal_view.read(cx).scroll_top;
terminal.update(cx, |terminal, cx| { terminal.update(cx, |terminal, cx| {
if terminal.selection_started() || hovered { if terminal.selection_started() || hovered {
terminal.mouse_drag(e, hitbox.bounds, cx); let mut adjusted_event = e.clone();
if scroll_top > Pixels::ZERO {
adjusted_event.position.y += scroll_top;
}
terminal.mouse_drag(&adjusted_event, hitbox.bounds, cx);
cx.notify(); cx.notify();
} }
}) })