diff --git a/docs/changelog.rst b/docs/changelog.rst index 5373eaf41..bfc643d1b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -234,6 +234,9 @@ Detailed list of changes - Fix a space immediately after a tab stop being dropped when the screen contents are copied or sent to the scrollback pager (:iss:`10609`) +- diff kitten: Fix selecting text with the mouse lagging badly when using a + mouse with a high polling rate (:iss:`10634`) + - Fix closing an unfocused window that comes before the focused window in the layout sometimes moving focus to a different window diff --git a/kittens/diff/mouse.go b/kittens/diff/mouse.go index 89bcaf1ce..a4cc77f3d 100644 --- a/kittens/diff/mouse.go +++ b/kittens/diff/mouse.go @@ -125,17 +125,18 @@ func (self *Handler) update_mouse_selection(ev *loop.MouseEvent) { self.mouse_selection.DragScroll(ev, self.lp, self.drag_scroll_tick) return } - self.do_update_mouse_selection(ev) + if self.do_update_mouse_selection(ev) { + self.draw_screen() + } } -func (self *Handler) do_update_mouse_selection(ev *loop.MouseEvent) { +func (self *Handler) do_update_mouse_selection(ev *loop.MouseEvent) (changed bool) { pos := self.scroll_pos y := ev.Cell.Y y = utils.Max(0, utils.Min(y, self.screen_size.num_lines-1)) self.logical_lines.IncrementScrollPosBy(&pos, y) x := self.mouse_selection.StartLine().MinX() - self.mouse_selection.Update(ev, self.line_pos_from_pos(x, pos)) - self.draw_screen() + return self.mouse_selection.Update(ev, self.line_pos_from_pos(x, pos)) } func (self *Handler) clear_mouse_selection() { diff --git a/tools/tui/loop/write.go b/tools/tui/loop/write.go index a279e2e16..bf1747fb8 100644 --- a/tools/tui/loop/write.go +++ b/tools/tui/loop/write.go @@ -53,10 +53,24 @@ func writestring_ignoring_temporary_errors(f *tty.Term, buf string) (int, error) return n, err } +// Remove the first n entries from pending_writes. This must not copy the +// remaining entries, as when there are lots of small writes queued up (for +// example when redrawing the screen in response to a flood of mouse events) that +// becomes quadratic, see https://github.com/kovidgoyal/kitty/issues/10634 +func (self *Loop) discard_sent_pending_writes(n int) { + clear(self.pending_writes[:n]) + if n >= len(self.pending_writes) { + // reuse the underlying array + self.pending_writes = self.pending_writes[:0] + } else { + self.pending_writes = self.pending_writes[n:] + } +} + func (self *Loop) flush_pending_writes(tty_write_channel chan<- write_msg) (num_sent int) { defer func() { if num_sent > 0 { - self.pending_writes = utils.ShiftLeft(self.pending_writes, num_sent) + self.discard_sent_pending_writes(num_sent) } }() for len(self.pending_writes) > num_sent { @@ -74,7 +88,7 @@ func (self *Loop) wait_for_write_to_complete(sentinel IdType, tty_write_channel num_sent := 0 defer func() { if num_sent > 0 { - self.pending_writes = utils.ShiftLeft(self.pending_writes, num_sent) + self.discard_sent_pending_writes(num_sent) } }() diff --git a/tools/tui/mouse.go b/tools/tui/mouse.go index 501dca8b5..16c08a4b4 100644 --- a/tools/tui/mouse.go +++ b/tools/tui/mouse.go @@ -78,14 +78,18 @@ func (ms *MouseSelection) StartNewSelection(ev *loop.MouseEvent, line LinePos, m ms.is_active = true } -func (ms *MouseSelection) Update(ev *loop.MouseEvent, line LinePos) { +// Update the end of the selection, returning true if it changed +func (ms *MouseSelection) Update(ev *loop.MouseEvent, line LinePos) (changed bool) { ms.drag_scroll.timer_id = 0 if ms.is_active { + prev := ms.end ms.end.x = max(line.MinX(), min(ev.Cell.X, line.MaxX())) cell_start := ms.cell_width * ms.end.x ms.end.in_first_half_of_cell = ev.Pixel.X <= cell_start+ms.cell_width/2 ms.end.line = line + changed = !prev.Equal(ms.end) } + return } func (ms *MouseSelection) LineBounds(line_pos LinePos) (start_x, end_x int) {