tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
82.21k stars 2.47k forks source link

[bug] Issues and inconsistencies when running `clipboard.writeText()` #7345

Open vemonet opened 1 year ago

vemonet commented 1 year ago

Describe the bug

I am building a small emoji picker desktop app with tauri and svelte. Thanks a lot for tauri and its documentation, it was quick and pleasant to get the project up and running!

I am facing 2 issues when writing to the clipboard though, they occur in different context but seem to be related

When the user clicks on an emoji it is added to the clipboard using clipboard.writeText(emoji) in javascript, the window is hidden, then we invoke() a rust command which aims to automatically paste the copied emoji. In the rust invocation we have 2 scenarios:

  1. On x11 we use xdotool key ctrl+v, the emoji is properly pasted, but the clipboard is emptied, and we get the same behavior when using xdotool type 😅

If we try to write again the emoji to the clipboard with app_handle.clipboard_manager().write_text(emoji).unwrap(); we actually end up with what was in the clipboard previously (which is a nice feature, but not what we expected)

On an other hand triggering xdotool directly from the terminal does not empty the clipboard, which makes me think something could be happening on the tauri side

  1. On wayland we use ydotool key, the emoji is properly pasted, and it stays in the clipboard. But if I try to add back the item that was in the clipboard before copying the emoji, this time the emoji stays...

I also tried to write back the previous clipboard item through the javascript, after having await the invoke, but the emoji stays in the clipboard. I also tried to do all the write_text and hide window from the rust command, but still the same issues

This happens when I just hide the window, and when I completely close the app, no changes

Reproduction

Feel free to check the code, it is quite straight to the point and has comments:

To reproduce:

If you are using linux x11 you will need to install an additional dependency for xdotool, on fedora it goes like sudo dnf install libX11-devel libxdo-devel

Then install and run the app:

git clone https://github.com/vemonet/EmojiMart -b clipboard-issue
cd EmojiMart
make install
make dev

When the picker pops up click on an emoji:

Sorry if it is a bit complex but those 2 errors are so connected it did not feel right to create 2 different issues yet

You can change a bit where the clipboard.writeText() are called to see how weird the whole clipboard behavior is

Expected behavior

When we call clipboard.writeText("😅") (or its rust counterpart) it should add 😅 to the keyboard, instead of adding the previous clipboard item or clearing the clipboard

Platform and versions

[✔] Environment
    - OS: Fedora 38.0.0 X64
    ✔ webkit2gtk-4.0: 2.40.3
    ✔ rsvg2: 2.56.2
    ✔ rustc: 1.67.1 (d5a82bbd2 2023-02-07)
    ✔ Cargo: 1.67.1 (8ecd4f20a 2023-01-10)
    ✔ rustup: 1.25.2 (17db695f1 2023-02-01)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (default)
    - node: 18.15.0
    - pnpm: 8.3.0
    - yarn: 1.22.19
    - npm: 9.5.0

[-] Packages
    - tauri [RUST]: 1.4.1
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: 1.4.0
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../build
    - devPath: http://localhost:5173/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

I found an issue that could be related here: https://github.com/tauri-apps/tauri/issues/3666

I also tried to run the clipboard_manager().write_text in a separate thread (using thread::spawn), but the results were the same

Any idea what could cause these issues? Are they caused by the same underlying problem? Or are they 2 completely separate problems dependent on the windowing system?

Anyone ever faced this weird cleaning of the clipboard by xdotool only when called from tauri?

I must admit it's quite puzzling, so any tips on how to better handle the clipboard are welcomed! Maybe there are things I am not aware of (different types of clipboard exists?)

amrbashir commented 1 year ago

I don't think tauri has any effect since you're using Command which is from rust std lib, but maybe try using arboard crate which will be used under the hood for tauri-plugin-clipboard for v2