From 32e2ae92c352b0e7cca9e2ef688a461dd001571c Mon Sep 17 00:00:00 2001 From: David Mo Date: Mon, 24 Feb 2025 12:22:32 -0500 Subject: [PATCH] fix clipboard spam when dragging mouse --- src/Surface.zig | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 98c344927..39469624c 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -2892,6 +2892,24 @@ pub fn mouseButtonCallback( } } + // The selection clipboard is only updated when the left button is released + // when copy on select is enabled. + if (button == .left and + action == .release and + self.config.copy_on_select != .false) + { + self.renderer_state.mutex.lock(); + defer self.renderer_state.mutex.unlock(); + const prev_ = self.io.terminal.screen.selection; + if (prev_) |prev| { + try self.setSelection(terminal.Selection.init( + prev.start(), + prev.end(), + false, + )); + } + } + // Report mouse events if enabled { self.renderer_state.mutex.lock(); @@ -3029,7 +3047,7 @@ pub fn mouseButtonCallback( 1 => { // If we have a selection, clear it. This always happens. if (self.io.terminal.screen.selection != null) { - try self.setSelection(null); + try self.io.terminal.screen.select(null); try self.queueRender(); } }, @@ -3038,7 +3056,7 @@ pub fn mouseButtonCallback( 2 => { const sel_ = self.io.terminal.screen.selectWord(pin.*); if (sel_) |sel| { - try self.setSelection(sel); + try self.io.terminal.screen.select(sel); try self.queueRender(); } }, @@ -3050,7 +3068,7 @@ pub fn mouseButtonCallback( else self.io.terminal.screen.selectLine(.{ .pin = pin.* }); if (sel_) |sel| { - try self.setSelection(sel); + try self.io.terminal.screen.select(sel); try self.queueRender(); } }, @@ -3335,7 +3353,7 @@ pub fn mousePressureCallback( // to handle state inconsistency here. const pin = self.mouse.left_click_pin orelse break :select; const sel = self.io.terminal.screen.selectWord(pin.*) orelse break :select; - try self.setSelection(sel); + try self.io.terminal.screen.select(sel); try self.queueRender(); } } @@ -3538,7 +3556,7 @@ fn dragLeftClickDouble( // Get the word closest to our starting click. const word_start = screen.selectWordBetween(click_pin, drag_pin) orelse { - try self.setSelection(null); + try self.io.terminal.screen.select(null); return; }; @@ -3547,20 +3565,20 @@ fn dragLeftClickDouble( drag_pin, click_pin, ) orelse { - try self.setSelection(null); + try self.io.terminal.screen.select(null); return; }; // If our current mouse position is before the starting position, // then the selection start is the word nearest our current position. if (drag_pin.before(click_pin)) { - try self.setSelection(terminal.Selection.init( + try self.io.terminal.screen.select(terminal.Selection.init( word_current.start(), word_start.end(), false, )); } else { - try self.setSelection(terminal.Selection.init( + try self.io.terminal.screen.select(terminal.Selection.init( word_start.start(), word_current.end(), false, @@ -3592,7 +3610,7 @@ fn dragLeftClickTriple( } else { sel.endPtr().* = line.end(); } - try self.setSelection(sel); + try self.io.terminal.screen.select(sel); } fn dragLeftClickSingle( @@ -3646,7 +3664,7 @@ fn dragLeftClickSingle( else cell_xpos < cell_xboundary; - try self.setSelection(if (selected) terminal.Selection.init( + try self.io.terminal.screen.select(if (selected) terminal.Selection.init( drag_pin, drag_pin, SurfaceMouse.isRectangleSelectState(self.mouse.mods), @@ -3681,7 +3699,7 @@ fn dragLeftClickSingle( break :start start; }; - try self.setSelection(terminal.Selection.init( + try self.io.terminal.screen.select(terminal.Selection.init( start, drag_pin, SurfaceMouse.isRectangleSelectState(self.mouse.mods), @@ -3696,7 +3714,7 @@ fn dragLeftClickSingle( // set earlier. assert(self.io.terminal.screen.selection != null); const sel = self.io.terminal.screen.selection.?; - try self.setSelection(terminal.Selection.init( + try self.io.terminal.screen.select(terminal.Selection.init( sel.start(), drag_pin, sel.rectangle, @@ -3740,7 +3758,7 @@ fn checkResetSelSwitch( } // Nullifying a selection can't fail. - if (reset) self.setSelection(null) catch unreachable; + if (reset) self.io.terminal.screen.select(null) catch unreachable; } // Handles how whether or not the drag screen point is before the click point.