@@ -400,8 +400,8 @@ export function isTerminalPasteShortcut(
400400 * primary selection and use the button for autoscroll, so only desktops that
401401 * expect the gesture get it.
402402 */
403- export function isTerminalMiddleClickPastePlatform ( platform = navigator . platform ) : boolean {
404- return / l i n u x | b s d | x 1 1 / i. test ( platform ) ;
403+ function isMiddleClickPastePlatform ( ) : boolean {
404+ return / l i n u x | b s d / i. test ( navigator . platform ) ;
405405}
406406
407407export function isTerminalCompositionCommitInput ( event : Pick < InputEvent , "inputType" > ) : boolean {
@@ -948,23 +948,17 @@ export class GhosttyTerminalSurface {
948948 }
949949
950950 /**
951- * The middle-click paste source. A browser cannot read the X11/Wayland
952- * PRIMARY buffer, so the terminal's own selection stands in for it when the
953- * user highlighted here, and the system clipboard covers the rest. Both go
954- * through pasteFromClipboard, so a middle click joins the same paste race as
955- * every other paste path.
951+ * Middle-click pastes the terminal's own selection, which is the only
952+ * primary-selection-like buffer a browser can read. It goes through
953+ * pasteFromClipboard so it joins the same paste race as every other path.
954+ * With nothing selected here there is no buffer to paste, and CLIPBOARD is
955+ * deliberately not substituted: middle-click must never emit text the user
956+ * only ever copied.
956957 */
957- private pasteFromSelectionBuffer ( ) : void {
958+ private pasteTerminalSelection ( ) : void {
958959 const selection = this . getSelection ( ) ;
959- if ( selection . length > 0 ) {
960- void this . pasteFromClipboard ( ( ) => Promise . resolve ( selection ) ) ;
961- return ;
962- }
963- const clipboard = navigator . clipboard ;
964- if ( typeof clipboard ?. readText !== "function" ) return ;
965- void this . pasteFromClipboard ( ( ) => clipboard . readText ( ) ) . catch ( ( ) => {
966- // Clipboard read denied; middle-click has no other source to fall back to.
967- } ) ;
960+ if ( selection . length === 0 ) return ;
961+ void this . pasteFromClipboard ( ( ) => Promise . resolve ( selection ) ) ;
968962 }
969963
970964 hasSelection ( ) : boolean {
@@ -1301,12 +1295,10 @@ export class GhosttyTerminalSurface {
13011295 this . canvas . setPointerCapture ( event . pointerId ) ;
13021296 return ;
13031297 }
1304- if ( event . button === 1 && isTerminalMiddleClickPastePlatform ( ) ) {
1305- // Cancelling the pointer event also suppresses the compatibility
1306- // mousedown, and with it Chromium's middle-click autoscroll.
1307- event . preventDefault ( ) ;
1308- event . stopPropagation ( ) ;
1309- this . pasteFromSelectionBuffer ( ) ;
1298+ if ( event . button === 1 && isMiddleClickPastePlatform ( ) ) {
1299+ // Left uncancelled on purpose: cancelling pointerdown drops the
1300+ // compatibility mousedown, which is what activates a split pane.
1301+ this . pasteTerminalSelection ( ) ;
13101302 return ;
13111303 }
13121304 if ( event . button !== 0 ) return ;
@@ -1552,6 +1544,10 @@ export class GhosttyTerminalSurface {
15521544 if ( this . canvas . hasPointerCapture ( event . pointerId ) ) {
15531545 this . canvas . releasePointerCapture ( event . pointerId ) ;
15541546 }
1547+ if ( event . button === 1 && isMiddleClickPastePlatform ( ) ) {
1548+ event . preventDefault ( ) ;
1549+ return ;
1550+ }
15551551 if ( event . button !== 0 ) return ;
15561552 if ( ! this . selectionMoved && this . selectionMode === "cell" ) {
15571553 this . clearSelection ( ) ;
@@ -1588,15 +1584,23 @@ export class GhosttyTerminalSurface {
15881584 } ;
15891585
15901586 private readonly onMouseDown = ( event : MouseEvent ) => {
1591- // onPointerDown answers the middle button; cancelling it here too keeps
1592- // Chromium's autoscroll from starting in browsers that still deliver the
1593- // compatibility mousedown after a cancelled pointerdown.
1594- if ( event . button === 0 || ( event . button === 1 && isTerminalMiddleClickPastePlatform ( ) ) ) {
1587+ // Cancelling the middle button here stops autoscroll while still letting
1588+ // the event bubble to the drawer handler that activates a split pane.
1589+ if ( event . button === 0 || ( event . button === 1 && isMiddleClickPastePlatform ( ) ) ) {
15951590 event . preventDefault ( ) ;
15961591 }
15971592 this . focus ( ) ;
15981593 } ;
15991594
1595+ /**
1596+ * Chromium pastes PRIMARY into the focused editable on a middle mouseup, and
1597+ * the hidden textarea is focused, so leaving the default alive would deliver
1598+ * a second paste through onPaste on top of the one onPointerDown sent.
1599+ */
1600+ private readonly onMouseUp = ( event : MouseEvent ) => {
1601+ if ( event . button === 1 && isMiddleClickPastePlatform ( ) ) event . preventDefault ( ) ;
1602+ } ;
1603+
16001604 private readonly onContextMenu = ( event : MouseEvent ) => {
16011605 if ( shouldReportTerminalMouse ( this . core . isMouseTracking ( ) , event ) ) {
16021606 event . preventDefault ( ) ;
@@ -1686,6 +1690,7 @@ export class GhosttyTerminalSurface {
16861690 this . canvas . addEventListener ( "pointercancel" , this . onPointerUp ) ;
16871691 this . canvas . addEventListener ( "wheel" , this . onWheel , { passive : false } ) ;
16881692 this . canvas . addEventListener ( "mousedown" , this . onMouseDown ) ;
1693+ this . canvas . addEventListener ( "mouseup" , this . onMouseUp ) ;
16891694 this . canvas . addEventListener ( "contextmenu" , this . onContextMenu ) ;
16901695 this . scrollbar . addEventListener ( "pointerdown" , this . onScrollbarPointerDown ) ;
16911696 this . scrollbar . addEventListener ( "pointermove" , this . onScrollbarPointerMove ) ;
@@ -1711,6 +1716,7 @@ export class GhosttyTerminalSurface {
17111716 this . canvas . removeEventListener ( "pointercancel" , this . onPointerUp ) ;
17121717 this . canvas . removeEventListener ( "wheel" , this . onWheel ) ;
17131718 this . canvas . removeEventListener ( "mousedown" , this . onMouseDown ) ;
1719+ this . canvas . removeEventListener ( "mouseup" , this . onMouseUp ) ;
17141720 this . canvas . removeEventListener ( "contextmenu" , this . onContextMenu ) ;
17151721 this . scrollbar . removeEventListener ( "pointerdown" , this . onScrollbarPointerDown ) ;
17161722 this . scrollbar . removeEventListener ( "pointermove" , this . onScrollbarPointerMove ) ;
0 commit comments