Skip to content

Use the most recent serial of kinds KeyPress or MousePress when copying#52053

Merged
kubkon merged 2 commits intozed-industries:mainfrom
mchisolm0:fix-linux-clipboard
Mar 25, 2026
Merged

Use the most recent serial of kinds KeyPress or MousePress when copying#52053
kubkon merged 2 commits intozed-industries:mainfrom
mchisolm0:fix-linux-clipboard

Conversation

@mchisolm0
Copy link
Copy Markdown
Collaborator

@mchisolm0 mchisolm0 commented Mar 20, 2026

Context

Wayland's write_to_clipboard and write_to_primary exclusively use SerialKind::KeyPress as the serial. When Wayland users try to copy using the mouse without having pressed a key on the keyboard, state.serial_tracker.get(SerialKind::KeyPress) could return 0, causing users to experience failed writes to the clipboard or primary with no visibility of what failed or why.

I have added a latest_of(...) helper in crates/gpui_linux/src/linux/wayland/serial.rs which returns the most recent tracked serial of the SerialKinds provided. In crates/gpui_linux/src/linux/wayland/client.rs, I call this new helper so serial is assigned the latest tracked serial of kind KeyPress or MousePress.

I, personally, only reproduced the bug on KDE Plasma (Wayland). All the reports with confirmed desktop environments have been KDE Plasma, though it may also affect other desktop environments. I have tested this fix on KDE Plasma and Niri.

Old context for reference WIP This change fixed mouse copying on KDE Plasma but broke it on others. I will review the new logs I gathered with actual values for `serial` using `SerialKind::KeyPress` and `SerialKind::MousePress`. Once I have, I will update the approach.

Original Context

Wayland's write_to_clipboard and write_to_primary use SerialKind::KeyPress, exclusively. When Wayland users tried to copy using the mouse without having pressed a key on the keyboard, state.serial_tracker.get(SerialKind::KeyPress) could return 0, causing users to experience failed writes to the clipboard or primary with no visibility of what failed or why.

This PR adds a fallback to SerialKind::MousePress when state.serial_tracker.get(SerialKind::KeyPress) returns 0 for both write_to_clipboard and write_to_primary. In my testing on KDE Plasma, this fixes the bug when users copy with the mouse from the context menu and when users use the middle mouse click for primary copy/pasting. I, personally, only reproduced the bug on KDE Plasma (Wayland). All the reports with confirmed desktop environments have been KDE Plasma, though it may also affect other desktop environments.

Closes #51636, #45482, and #44695

What I have manually tested

  • Copy with mouse, paste with mouse (no prior KeyPress)
  • Copy with mouse, paste with keyboard (no prior KeyPress)
  • Select with mouse, paste with middle mouse button (no prior KeyPress)
  • Copy with keyboard, paste with keyboard
  • Copy with keyboard, paste with mouse

Still need help

  • Is this the preferred approach?
  • The first time I tested, the 3rd or 4th paste with the mouse pasted a stale clipboard entry (top two lines of buffer in video), but I have been unable to reproduce it.

How to Review

May need to be on KDE Plasma (see note in context section)

  • cargo run
  • Double click to select text (must select using the mouse)
  • Middle mouse click to paste primary using write_to_primary and observe content is pasted correctly
  • Click and drag to select different text
  • Right click on text
  • Select "copy" (must use mouse since keyboard keypresses sometimes avoided the bug)
  • Right click and select "paste"
  • Observe pasted content is correct
Recording of fix on KDE Plasma https://github.com/user-attachments/assets/479a7167-d19d-4d4e-a53a-b67c29d49b9f
Recording of fix on Niri https://github.com/user-attachments/assets/dbb2e2ea-d886-4ea2-955b-13b0e797974b

Self-Review Checklist

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Release Notes:

  • Fix copy for some Wayland users

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Mar 20, 2026
@zed-community-bot zed-community-bot bot added the guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions label Mar 20, 2026
@mchisolm0

This comment was marked as outdated.

@mchisolm0 mchisolm0 marked this pull request as ready for review March 25, 2026 04:01
@zed-codeowner-coordinator zed-codeowner-coordinator bot requested review from a team, Veykril and kubkon and removed request for a team March 25, 2026 04:02
@mchisolm0 mchisolm0 changed the title Use mouse serial when KeyPress is zero Use the most recent serial of kinds KeyPress or MousePress when copying Mar 25, 2026
Copy link
Copy Markdown
Member

@kubkon kubkon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks!

@kubkon kubkon merged commit 103fa37 into zed-industries:main Mar 25, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent issues with copying text to clipboard (Linux, KDE Plasma Wayland session)

2 participants