Skip to content

Add public Window API to drive copy/cut/paste of the focused text input; adopt it in the winit web backend - #12483

Draft
ruffsl wants to merge 1 commit into
slint-ui:masterfrom
ruffsl:patch/wasm-clipboard-seam
Draft

Add public Window API to drive copy/cut/paste of the focused text input; adopt it in the winit web backend#12483
ruffsl wants to merge 1 commit into
slint-ui:masterfrom
ruffsl:patch/wasm-clipboard-seam

Conversation

@ruffsl

@ruffsl ruffsl commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Disclosure: this change was developed and verified with the aid of an LLM.


Fixes #12482.

Adds three methods to Window: copy_focused_text_selection, cut_focused_text_selection (both returning the selected text), and paste_into_focused_text. Cut and paste honor the same read_only()/enabled() gates as the keyboard shortcuts; copy is deliberately ungated, matching the Copy shortcut. TextInput::insert_text is extracted from paste_clipboard (behavior-preserving; undo entries unchanged) so paste can insert caller-supplied text.

The winit wasm backend's copy/cut/paste DOM handlers now call the new API instead of reaching into WindowInner/TextInput privates. This also removes the CURRENT_WASM_CLIPBOARD_DATA thread-local shuttle (now unreferenced) and its scoped-tls-hkt dependency, and fixes a latent hazard: the old handler held the focus_item RefCell borrow across TextInput calls that can run edited/accepted callbacks into app code; the new helper drops the borrow before dispatch.

Tests: api/rs/slint/tests/window_focused_text_clipboard.rs — copy/cut/paste semantics, selection replacement, no-focus and empty-selection refusal, read-only (copy allowed, cut/paste refused), disabled, and multi-byte selection boundaries. CHANGELOG entry included (in an Unreleased section; will fold into master's on rebase).

One disclosed question: copy_focused_text_selection returns the real text of input-type: password fields, because the existing copy path has no password guard — this PR matches that behavior rather than silently diverging. Happy to add an InputType::Password refusal here (or in both places) if that's preferred.

@ruffsl
ruffsl force-pushed the patch/wasm-clipboard-seam branch from 41ea9c2 to 505709a Compare July 14, 2026 18:21
`slint::Window` gains four methods that operate on the currently
focused `TextInput`:

- `copy_focused_text_selection() -> Option<SharedString>`
- `cut_focused_text_selection() -> Option<SharedString>`
- `paste_into_focused_text(&str) -> bool`
- `has_focused_text_input() -> bool`

Copy and cut return the selected text to the caller instead of writing
it through `Platform::set_clipboard_text`, and paste takes the text as
an argument instead of reading `Platform::clipboard_text`. This lets a
custom backend service a clipboard operation from inside its own event
handler. The motivating case is the web platform: the browser clipboard
is asynchronous and reachable only from a `copy`/`cut`/`paste`
`ClipboardEvent` fired during a user gesture, which the synchronous
`Platform` clipboard hooks cannot satisfy — so the data has to flow
through the event, not the `Platform` trait.

`has_focused_text_input` reports whether a text input currently holds
the focus, letting such a backend keep its clipboard-event target in
sync with Slint — on the web, that means keeping a hidden editable
element focused exactly while a text input is edited, since browsers
only deliver clipboard (and IME) events to an editable context.

Cut and paste honor the same read-only/disabled gate as the Cut/Paste
keyboard shortcuts; copy stays ungated like the Copy shortcut.

`TextInput::insert_text` is extracted from `paste_clipboard` so the
paste path can insert externally-supplied text, and the winit web
backend's `copy`/`cut`/`paste` handlers now drive these `Window`
methods. That retires the `CURRENT_WASM_CLIPBOARD_DATA` thread-local
shuttle: the wasm `Platform` clipboard hooks fall back to their default
no-op implementations (nothing could reach them outside a clipboard
event anyway) and the winit backend's `scoped-tls-hkt` dependency is
dropped.
@ogoffart

Copy link
Copy Markdown
Member

Just a thought: maybe these could be done with event such as WindowEvent::ClipboardPaste(SharedString), WindowEvent::ClipboardCopy (and then we could even return a string with the help of #12116 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No way for a custom Platform to implement clipboard on wasm — TextInput copy/cut/paste is unreachable outside the winit backend

2 participants