The clipboard
egui-pop copies and pastes typed content, not only text: a colour, a
set of nodes, a theme token, an application’s own types. The decision
and its reasons are ADR 0015; this page is how
to use it. Everything below is egui_pop::api::clipboard (the
contract, the plugin) and egui_pop::model::clipboard (formats, items,
negotiation, no egui).
The model
- The owner keeps the items: one per slot, the user’s and each
agent session’s own. It is the application’s kernel when the host
says so (
Host::clipboard), else egui-pop’sLocalClipboard, kept in memory byClipboardPluginand shared by the application’s windows. Eachputraises its revision and publishesclipboard.changed(the slot, the type, the revision, never the payload). - An item (
ClipItem) has a type (a private format,application/x-<app>.<type>+json), a version, a JSON payload, a title (“a colour”, “3 nodes”), readable fallbacks (text always, HTML when the type has one), where it came from, and what sanitisers left out. - The system clipboard receives the user’s copies’ fallbacks
(text), so a colour lands in a chat as
#d9a441. A paste whose text is still what the owner wrote there pastes the owner’s item; other text is external and pasted as text. A type declared internal (ClipType::internal) never reaches the system clipboard. How it is written and read is the system backend’s business. - A target accepts formats, richest first. A paste takes the first the item offers, or one a declared transform makes from what it offers (one transform, never a chain); nothing fits: the paste is refused with the reason, and Edit > Paste is greyed.
A component: a source and a target
A component registers an endpoint by name with a ClipSource
(what a copy takes; a cut’s delete) and a ClipTarget (the formats it
accepts; how it applies a paste), then says each pass which widget
draws it. While that widget has the focus, the Edit commands use the
endpoint.
use egui_pop::api::clipboard::{self, ClipItem, ClipSource, ClipTarget, Cut, Paste, TEXT};
impl ClipSource for Swatches {
fn copy(&self, _: &AppHandle) -> Option<ClipItem> {
let c = self.selected()?;
Some(ClipItem::new(COLOR, 1, json!({ "hex": c }), "a colour")
.fallback(TEXT, json!(c)))
}
fn can_cut(&self, _: &AppHandle) -> bool { !self.locked() }
fn delete(&self, _: &AppHandle, cut: &Cut<'_>) -> Result<(), String> {
self.remove_selected(cut.group) // its writes carry the group
}
}
impl ClipTarget for Swatches {
fn accepts(&self, _: &AppHandle) -> Vec<FormatId> { vec![COLOR, TEXT] }
fn paste(&self, _: &AppHandle, p: &Paste<'_>) -> Result<Json, String> {
let hex = p.repr.data["hex"].as_str().unwrap_or_default();
if !p.dry_run {
self.add(hex, p.group);
}
Ok(json!(format!("adds {hex}"))) // paste special's preview
}
}
// In `connect`: owned by the plugin, gone with it.
ctx.clip_endpoint("demo.swatches", Some(swatches.clone()), Some(swatches.clone()));
// Each pass, where it is drawn:
let response = draw_swatches(ui);
clipboard::bind(app, "demo.swatches", response.id);
In a node graph, a tree or a list of sliders, the focus sits on each node, row or slider, not on the panel. Such a component binds its elements, or an area, instead of one widget:
// Each element, with its key: the source and the target receive it.
for node in &graph.nodes {
let response = draw_node(ui, node);
clipboard::bind_element(app, "demo.graph", response.id, &node.key);
}
// Or any focused widget whose centre lies in the canvas.
clipboard::bind_area(app, "demo.graph", canvas.rect);
ClipSource::copy_element, Cut::element and Paste::element carry
the focused element’s key (clipboard::element says it); an agent
names one with element in clipboard.copy and clipboard.paste. A
widget bound alone wins over an area, and of two areas holding the
focused widget the smaller one. The focused widget’s rectangle is read
from egui’s lists of widgets: Context::read_response is never used
for it, as building a response for a widget that is not hovered during
a press makes egui surrender that widget’s focus (a host finding the
focused row that way steals the tree’s focus on the click that gave
it).
A paste is one undo step (“Paste a colour”): egui-pop opens a
history group (Host::begin_group, the store’s by default) and the
target’s writes carry it (Set::gesture(paste.group)). A cut is the
copy and the delete in one step, and copies only once its delete is
accepted: a refused delete copies nothing. A dry run (p.dry_run)
says what would change and changes nothing: paste special shows it as
the preview, an agent reads it as the plan.
Writes a source or a target makes are its plugin’s own (the scope of the plugin that registered the endpoint).
A target whose edit goes through the application’s own loop (a
network edit, a command the viewport applies on the next frame)
implements ClipTarget::apply and answers Pasted::Later with a
result to come (host::pending, the same as a host’s
Reply::Later). Its writes still carry paste.group, which stays
open until the result arrives, so the paste stays one undo step. The
clipboard.paste step answers at once with "pending": true (and no
changes); when the result arrives the group closes (kept only on
success), clipboard.pasted is published with the same summary and
the target’s changes or its error, and the user sees a failure or
a loss in a toast. An agent watches clipboard.pasted for the
outcome; clipboard::pending_pastes and the collector say how many
wait. A dry run (paste special’s preview, an agent’s plan) always goes
through ClipTarget::paste, now.
Types, transforms and sanitisers
Plugins declare them in connect, and they go with the plugin:
ctx.clip_type(
ClipType::new(COLOR, 2, "a colour")
.fallbacks(|p| vec![Repr::new(TEXT, p["hex"].clone())])
.parts(|_| vec![Part::new("alpha", "the opacity")])
.migrate(|from, old| match from {
1 => Ok(json!({ "hex": old["rgb"] })),
v => Err(format!("no colour of version {v}")),
}),
)?
.clip_transform(Transform::new(
TransformSpec::new("demo.parse-colour", TEXT, COLOR),
|r| parse(r.text().unwrap_or_default()).map(Transformed::exact),
))?
.clip_sanitiser(
Sanitiser::new("demo.paths", Boundary::System, |data| strip_paths(data))
.format(TEXT),
)?;
- A type’s title (“a colour”) is a declaration, translated under
clip-<format>.title(clipboard::type_title_key) and listed by the key extractor; an item’s own title (“3 nodes”) is translated when its source gives it as a message,ClipItem::title_key("demo-nodes", &[("count", "3")]), its Englishtitlethe fallback. The undo step’s label (“Paste a colour”) and paste special’s formats use them. - A type renders its fallbacks for items whose source gave none, lists its parts (paste special offers each alone), and migrates a payload made at an older version before it is pasted.
- A transform makes one format from another;
TransformSpec::lossysays it loses information: it is applied, and a toast names the loss (Transformed::lossy(data, "the hue")); paste special shows it beforehand.Transform::whenlimits it to some representations or targets.Transform::for_targetadapts to the target: it receives the target’s name, the element pasted into and what the target says of itself (ClipTarget::context: the image a mask lands on, the range a value is clamped to, the names already taken), so a mask scaled to the image, a value clamped or a name made unique is a transform, its loss shown in the toast and paste special’s preview, rather than an adaptation hidden inClipTarget::paste. - A sanitiser runs on each representation crossing its boundary:
Boundary::System(what leaves for other applications: local paths, secrets, owner-local ids),Boundary::External(what comes in),Boundary::Owner(inside, nothing by default). It returns what it removed; the item records it and the copy’s toast says it (“Copied; left out: a local path”).
egui-pop declares two of its own: egui_pop.controls removes control
characters and escape sequences from external text, and
egui_pop.html-to-text (lossy) reduces HTML to text for a target that
does not take HTML.
The system backend
A SystemBackend writes one copy’s representations to the system
clipboard at once and reads back what other applications left there:
| Backend | Writes | Reads |
|---|---|---|
EguiText (the default) | text, through egui | nothing: the paste key’s text only |
platform::clipboard::Arboard (feature clipboard) | text, HTML (with its text), an image, files | text, HTML, an image, files; the primary selection |
the host’s (Clipboard::system) | what the kernel decides (its private types too) | idem |
A copy gives the backend every representation it writes
(SystemBackend::writes), sanitised for the system boundary: the
fallbacks, and the private type itself where the backend writes it. A
paste reads every format the backend reads, as one external item
with several representations (ClipItem::external: files as
text/uri-list, an image as image/png, HTML, text), so negotiation
picks the richest the target accepts; it is sanitised at the external
boundary first. With a backend that reads, Edit > Paste from the menu
pastes what the system clipboard holds too, and the paste command is
enabled for a target taking only what the backend may read
(SystemBackend::readable). The system clipboard is read once per
pass.
The representations’ data: text, HTML and a URI list (one file://
URI per line, CRLF between) are JSON strings; an image is a string,
the PNG file’s bytes in standard base64; a private type is its JSON
payload.
// The application's own backend (or the platform crate's):
clipboard::set_backend(app, Box::new(MyBackend::new()));
// A kernel's: its `Clipboard` implementation returns it.
fn system(&mut self) -> Option<&mut dyn SystemBackend> { Some(&mut self.system) }
The clipboard collector names the backend in use and what it says of
itself (SystemBackend::describe: on Wayland, its path).
arboard (egui-pop-platform)
use egui_pop::platform::clipboard::SystemClipboardPlugin;
App::builder()
.plugin(ClipboardPlugin)
.plugin(SystemClipboardPlugin::new()) // feature `platform-clipboard`
SystemClipboardPlugin installs Arboard, which opens the clipboard
on first use. arboard writes one format per copy: files (a
text/uri-list of file:// URIs), else an image, else HTML with its
text alternative, else text; it reads all four. Private types stay in
the owner. Its path to the system clipboard:
| Platform | Path |
|---|---|
| Windows, macOS | the Win32 clipboard, NSPasteboard |
| X11 | selections, through x11rb |
| Wayland | XWayland by default: a copy reaches native Wayland applications only when the compositor hands XWayland’s selection over (some do only while an X11 window has the focus). With platform-clipboard-wayland (arboard’s wayland-data-control, through wl-clipboard-rs), the data-control protocol where the compositor offers it (wlroots compositors, KDE Plasma), XWayland elsewhere. The core protocol, which needs the window’s input serial, is what egui-winit’s text clipboard uses (smithay-clipboard), not arboard. |
| the web | none: the module is not built for wasm32 |
The primary selection (select to copy, middle-click to paste) is
read and written on X11 and Wayland through the backend
(clipboard::primary_text, clipboard::set_primary_text); egui-pop’s
text inputs and code editor do not use it yet (opt-in, ADR 0015).
Steps
Copy and paste are steps (ADR 0011), observed, vetoed, redirected or replaced like any other:
| Step | Arguments | A before-hook may |
|---|---|---|
clipboard.copy | source, slot, cut, dry_run, origin | veto (a secret field), redirect (another selection, another slot) |
clipboard.paste | target, slot, format, transform, parts, if_rev, dry_run, text, origin | veto, redirect (another target, another format) |
Their results say the type, the plan (format, to, transform,
lossy), what was lost and stripped, and the target’s changes.
clipboard::copy and clipboard::paste run them from code; a refusal
or a veto asked by the user shows in a toast.
Commands and keys
| Command | Keys | What |
|---|---|---|
edit.cut | Ctrl+X, Shift+Delete | cut the focused endpoint’s selection |
edit.copy | Ctrl+C, Ctrl+Insert | copy it |
edit.paste | Ctrl+V, Shift+Insert | paste in the richest format it accepts |
edit.pasteSpecial | Ctrl+Shift+V | choose the format, transform or part, each with its preview |
They sit in the Edit menu under undo (group 1_clipboard), with
when !textInputFocus and the keys canCut, canCopy, canPaste:
greyed when nothing focused answers. Ctrl is Command on macOS. Text
fields keep egui’s own copy and paste, as they keep their own undo; the
code editor and the terminal keep theirs too.
egui-winit turns the platform’s copy, cut and paste chords into events
and swallows the keys (see upstream); while an endpoint
has the focus, those events run the commands (Shift held on a paste:
paste special), so they work under winit as under the keymap. When the
system clipboard holds no text (files, an image), egui-winit makes no
paste event at all; egui-pop recognises the chord by its release and
pastes with no_text: the system backend reads what the clipboard
holds. With the default backend, which reads nothing, such a paste is
refused when the owner’s text is no longer on the system clipboard.
The chords in the keymap
With a backend that reads the system clipboard (SystemClipboardPlugin
added), AppBuilder::run_native turns eframe’s
NativeOptions::clipboard_shortcuts off: egui-winit sends the chords
as plain keys, and the keymap runs them everywhere. While a text field
has the focus, edit.copy, edit.cut and edit.paste (their when
also holds for textInputFocus && clipboardKeymap) give it egui’s own
Event::Copy, Event::Cut and Event::Paste, the pasted text read
through the backend, so the field edits as before. A remapped chord
(Ctrl+Shift+C for edit.copy) then copies from text fields too, and
the paste key is seen on its press whatever the clipboard holds.
An application whose NativeOptions already have the shortcuts off
gets the same; it then needs a backend that reads, or a text field
pastes nothing. Elsewhere nothing changes: on the web, with the default
backend (which reads nothing) and on eframe’s own runner, text fields
keep egui’s events and the events of an endpoint go to the commands as
above. clipboard::set_keymap_shortcuts says which applies (a host on
its own loop calls it); the clipboardKeymap context key shows it.
Agents
Agents go through the same steps, with the user’s refusals:
clipboard.copy {source, cut, dry_run};clipboard.paste {target, format, transform, parts, if_rev, dry_run}: a dry run answers the plan, the transform, what would be stripped and lost, and the target’s changes, without making them;clipboard.offers {target}: what the slot holds and every way it fits the target, each with its preview;clipboard.read,clipboard.write {format, data, title}.
Each works in the session’s own slot (agent:<name>): an agent neither
reads nor overwrites what the user copied, unless the owner allows it
(Clipboard::may_use; with LocalClipboard,
clipboard::allow_session). The clipboard collector of the debug
bridge shows the owner, its revision, each slot’s item (without its
payload), the endpoints and what plugins declared.
Not yet
- With the default backend, what the system clipboard holds is read only when the paste key brings it (egui reads it as text, on the key): Edit > Paste from the menu pastes the owner’s item.
- Private types across processes without a shared owner need a backend that writes them (a host’s); arboard writes only standard formats, one per copy.
- egui-pop’s text inputs and code editor do not use the primary selection yet, though the arboard backend reads and writes it.
- On the web no backend reads the system clipboard: a paste knows the browser’s paste text only.
- The first types (colours, graph nodes, theme tokens, table rows) and their sanitisers come next (ADR 0015, step 4).