Expand description
The clipboard (ADR 0015): typed items kept by the application’s owner, readable fallbacks on the system clipboard, a paste negotiated between what a copy offers and what the target accepts, and hooks at each boundary.
- The owner implements
Clipboard: one item per slot (the user’s, and each agent session’s own),put,get, a revision. The host gives it (Host::clipboard); without one,ClipboardPluginkeeps aLocalClipboardin memory, shared by the application’s windows. It publishesclipboard.changed(the slot, the type, the revision; never the payload). - Sources and targets are the components’: a component registers
an endpoint by name (
register, orCtx::clip_endpoint) with aClipSource(what a copy takes, what a cut deletes) and aClipTarget(the formats it accepts, how it applies a paste), and says each pass which widget draws it (bind), which widgets draw its elements (bind_element: a graph’s nodes, a tree’s rows, each with its key) or which area it covers (bind_area): the endpoint of the focused widget is the one the Edit commands use, and its source and target receive the element’s key. - Types, transforms and sanitisers are declared by plugins and go
with them (
Ctx::clip_type,Ctx::clip_transform,Ctx::clip_sanitiser): a type renders its fallbacks, lists its parts and migrates its older versions; a transform makes one format from another (lossy or not); a sanitiser removes what must not cross a boundary and says what it removed. - Steps (ADR 0011):
clipboard.copyandclipboard.paste, which a before-hook vetoes (a secret field) or redirects (another selection, another target, another format). A copy is never an undo step; a cut deletes and copies as one undo step and copies only once its delete is accepted; a paste is one undo step (“Paste a colour”), through the host’s history group (Host::begin_group). - Commands:
edit.cut(Ctrl+X, Shift+Delete),edit.copy(Ctrl+C, Ctrl+Insert),edit.paste(Ctrl+V, Shift+Insert) andedit.pasteSpecial(Ctrl+Shift+V, a choice of every format, transform and part that fits, each with the target’s preview) in the Edit menu,when!textInputFocusand the keyscanCut,canCopy,canPaste: greyed when nothing focused answers, and text fields keep egui’s own copy and paste. egui-winit turns the platform’s copy, cut and paste chords into events and swallows the keys; those events are dispatched to the commands when an endpoint has the focus (Shift held on a paste: paste special). Where a backend reads the system clipboard (egui-pop-platform’sSystemClipboardPlugin),run_nativeturns eframe’s clipboard shortcuts off instead: the chords arrive as keys, and the keymap runs them in text fields too (set_keymap_shortcuts), so a remapped chord edits text as well. For agents:clipboard.copy,clipboard.paste(withformat,transform,parts,if_revanddry_run),clipboard.offers,clipboard.read,clipboard.write, each in the session’s own slot unless the owner allows otherwise; theclipboardcollector.
The system clipboard is reached through a SystemBackend: a
copy gives it every representation it writes, at once; a paste reads
every format it can (files, images, HTML, text) as one external item.
The default, EguiText, writes text through egui and reads
nothing: a paste then knows the system clipboard only by the text the
paste key brought, and a paste through the menu or a command uses the
owner’s item. A host gives its own backend (Clipboard::system),
an application installs one (set_backend; egui-pop-platform’s
clipboard feature has one over arboard).
use std::rc::Rc;
use std::cell::RefCell;
use egui_pop_api::clipboard::{self, ClipSource, ClipTarget, ClipboardPlugin, Paste};
use egui_pop_api::{App, AppHandle};
use egui_pop_model::clipboard::{ClipItem, FormatId, TEXT};
use egui_pop_model::commands::Origin;
const NOTE: FormatId = FormatId::from_static("application/x-demo.note+json");
/// A note: copied as a typed item, pasted from one or from text.
#[derive(Default)]
struct Note(RefCell<String>);
impl ClipSource for Note {
fn copy(&self, _: &AppHandle) -> Option<ClipItem> {
let text = self.0.borrow().clone();
Some(ClipItem::new(NOTE, 1, serde_json::json!({ "text": text }), "a note")
.fallback(TEXT, serde_json::json!(text)))
}
}
impl ClipTarget for Note {
fn accepts(&self, _: &AppHandle) -> Vec<FormatId> { vec![NOTE, TEXT] }
fn paste(&self, _: &AppHandle, p: &Paste<'_>) -> Result<serde_json::Value, String> {
let text = p.repr.data["text"].as_str().or(p.repr.text()).unwrap_or("").to_string();
if !p.dry_run { *self.0.borrow_mut() = text.clone(); }
Ok(serde_json::json!({ "set": text }))
}
}
let app = App::builder().plugin(ClipboardPlugin::default())
.build(&egui::Context::default()).unwrap();
let h = app.handle();
let (a, b) = (Rc::new(Note::default()), Rc::new(Note::default()));
*a.0.borrow_mut() = "hello".into();
clipboard::register(h, "a", Some(a.clone()), Some(a.clone()));
clipboard::register(h, "b", Some(b.clone()), Some(b.clone()));
assert!(clipboard::copy(h, clipboard::CopyRequest::new("a"), &Origin::User).ok());
assert!(clipboard::paste(h, clipboard::PasteRequest::new("b"), &Origin::User).ok());
assert_eq!(*b.0.borrow(), "hello");Structs§
- Clip
Item - What one copy made: a typed item with its readable fallbacks.
- Clip
Origin - Where an item came from.
- Clip
Type - A type of item a plugin declares and owns.
- Clipboard
Plugin - The clipboard as a plugin: the default owner (a
LocalClipboard, unless the host owns the clipboard), the steps, the Edit commands and keys, the agent commands, theclipboardcollector, egui-pop’s own sanitiser of external text and its HTML to text transform. - Copy
Request - A copy, as the
clipboard.copystep’s arguments. - Cut
- A cut’s delete: the item copied, who cuts, and the history group its
writes carry (
Set::gesture) so that the delete is one undo step. - Destination
- The target a transform adapts a paste to.
- Egui
Text - The default backend: text through egui (
Context::copy_text, which eframe hands to its own clipboard at the end of the pass), nothing read. - Format
Id - A format: a MIME-like type,
type/subtypewith optional parameters. - Local
Clipboard - The default owner: one item per slot, in memory.
- Part
- A part of an item a paste special may take alone (a parameter set’s modules).
- Paste
- A paste as a target receives it.
- Paste
Request - A paste, as the
clipboard.pastestep’s arguments. - Plan
- How a paste goes: the representation taken from the offer, what the target receives, and the transform between them, if any.
- Refusal
- Why nothing on the clipboard fits a target.
- Repr
- One representation of an item: a format and its data (a JSON string for a text format).
- Sanitiser
- A sanitiser a plugin declares and owns: run on each representation crossing its boundary (of its format, or all), it removes what must not cross and says what it removed.
- Stripped
- What a sanitiser removed, and why.
- Title
Key - A title as a catalogue message: its key and its arguments.
- Transform
- A transform a plugin declares and owns.
- Transform
Spec - A transform a plugin declares: it makes
tofromfrom. - Transformed
- What a transform made: the data, and what it lost if anything.
- Way
- Every way the clipboard fits a target, as paste special lists them: the plan, its label, the target’s preview (a dry run), and the parts.
Enums§
- Answer
- What the owner answers a
put. - Boundary
- The boundary a representation crosses, which sanitisers run on.
- Pasted
- What a target answers a paste it applies (
ClipTarget::apply). - Slot
- Where an item is kept in the owner: the user’s slot, or a session’s own (an agent’s, a script’s), so that an agent neither reads nor overwrites what the user copied unless the owner allows it.
Constants§
- CMD_
CLIP_ COPY - Copies from an endpoint by name (
source,slot,cut,dry_run). - CMD_
CLIP_ PASTE - Pastes into an endpoint by name (
target,slot,format,transform,parts,if_rev,dry_run). - CMD_
COPY - Copies the focused endpoint’s selection.
- CMD_CUT
- Cuts the focused endpoint’s selection: copied once its delete is accepted, one undo step.
- CMD_
OFFERS - What a slot holds and the ways it fits a target (
slot,target). - CMD_
PASTE - Pastes into the focused endpoint, in the richest format it accepts.
- CMD_
PASTE_ SPECIAL - Pastes into the focused endpoint in a format, through a transform or only some parts, chosen in a dialog showing each one’s preview.
- CMD_
READ - A slot’s item, payload included (
slot). - CMD_
WRITE - Puts an item in a slot (
slot,format,data,title). - CONTEXT_
KEYMAP_ SHORTCUTS - The context key true while the keymap runs the copy, cut and paste
chords in text fields too (
set_keymap_shortcuts). - HTML
- HTML, with a text alternative.
- HTML_
TO_ TEXT - A transform of egui-pop’s own: HTML reduced to its text (lossy).
- MENU_
GROUP - The Edit menu’s group of the clipboard commands, under undo.
- PLUGIN
- The plugin’s id.
- PNG
- An image.
- SANITISE_
CONTROLS - A sanitiser of egui-pop’s own: control characters and escape sequences removed from external text.
- STEP_
COPY - A copy (
{ source, slot, cut, dry_run, origin }): a before-hook vetoes it or redirects it (another source, another slot); its default asks the source for the item, sanitises what goes to the system clipboard, deletes for a cut, and gives the item to the owner. Its result: the type, the title, the revision, what was left out. - STEP_
PASTE - A paste (
{ target, slot, format, transform, parts, if_rev, dry_run, text, origin }): a before-hook vetoes it or redirects it (another target, another format); its default takes the owner’s item (or the external text), sanitises external content, negotiates the format, runs the transform and has the target apply it as one undo step. Its result: the plan, what was lost, the target’s changes. - SYSTEM_
PLUGIN - The id of
egui-pop-platform’sSystemClipboardPlugin, whose backend reads the system clipboard: with it added,run_nativeturns eframe’s clipboard shortcuts off and the keymap runs the chords (set_keymap_shortcuts). - TEXT
- Plain text: every item renders one, and external text arrives as it.
- URI_
LIST - A list of files or links.
Traits§
- Clip
Source - What a copy takes from a component.
- Clip
Target - Where a paste goes in a component.
- Clipboard
- The owner of the clipboard, as the interface reaches it.
- System
Backend - The system clipboard as egui-pop writes and reads it: what one copy leaves there for other applications, what a paste finds there.
Functions§
- allow_
session - Lets a session (
agent:<name>) use the user’s slot, when the owner is egui-pop’sLocalClipboard. - bind
- Says which widget draws an endpoint, each pass it is drawn: while that widget has the focus, the Edit commands use the endpoint.
- bind_
area - Says that an endpoint covers an area, each pass it is drawn: any
focused widget whose centre lies inside it (a node of a graph, a row
of a tree, a slider of a list) uses the endpoint. A widget bound
alone (
bind,bind_element) wins over an area. - bind_
element - Says which widget draws one element of an endpoint (a node, a row)
and its key, each pass it is drawn: while that widget has the focus,
the Edit commands use the endpoint, and its source and target receive
the key (
ClipSource::copy_element,Cut::element,Paste::element). - clip_
type - The type declared for a format.
- copy
- Copies through the
STEP_COPYstep. A refusal or a veto asked by the user shows in a toast, and so does what was left out. - default_
access - The default rule: the user may use any slot, a session (an agent, a script) its own only.
- element
- The key of an endpoint’s element whose widget has the focus, if one
was bound (
bind_element). - endpoints
- The names of the endpoints registered.
- focused
- The endpoint whose widget has the focus: one bound to it alone or as an element, else one whose area holds it.
- format_
title - What a format is called, in the interface’s language: plain text, a declared type’s title, else the format itself.
- keymap_
shortcuts - Whether the keymap runs the copy, cut and paste chords in text fields
too (
set_keymap_shortcuts). - paste
- Pastes through the
STEP_PASTEstep. A refusal or a veto asked by the user shows in a toast, and so does what a lossy transform lost. - paste_
special - Opens paste special for a target: a choice of every way, each with its preview; choosing pastes it.
- paste_
special_ for - Opens paste special for a request (its
target,textandelement). - pending_
pastes - How many pastes wait for their result (
Pasted::Later). - primary_
text - The primary selection’s text, through the system backend (opt-in: egui-pop’s own widgets do not use it yet).
- register
- Registers a component’s endpoint by name (replacing one of that name): its source, its target, or both.
- set_
backend - Installs the system clipboard’s backend (unless the owner gives its
own,
Clipboard::system); before, egui’s text (EguiText). - set_
keymap_ shortcuts - Says that the copy, cut and paste chords arrive as plain keys
(eframe’s
NativeOptions::clipboard_shortcutsoff), so the keymap runs them everywhere:edit.copy,edit.cutandedit.pastethen also run while a text field has the focus, and give it egui’s ownEvent::Copy,Event::CutandEvent::Paste(the text read through the system backend), as egui-winit would have. A remapped chord then edits text fields too. - set_
primary_ text - Sets the primary selection’s text, through the system backend.
- title_
of - What an item is called, in the interface’s language: its own message
(
ClipItem::title_key), else its type’s declared title when the item kept it, else its English title, else its format. - transforms
- The transforms declared.
- type_
title_ key - The catalogue key of a type’s title:
clip-<slug(format)>.title. - types
- The types declared, with the plugin owning each (for catalogues:
each type’s title is a declaration, keyed by
type_title_key). - unregister
- Removes an endpoint.
- ways
- The ways a slot’s item fits a target (
text: what the paste key brought): each format and transform, then each part alone. - ways_
for - The ways a request’s item fits its target (its
target,slot,textandelement; its format, transform and parts ignored). - with_
backend - Runs
fon the system backend: the owner’s, else the one installed, else egui’s text. - with_
owner - Runs
fon the owner: the host’s, else the plugin’sLocalClipboard.Nonewithout the plugin, or while the host (owning the clipboard) runs a command.