Skip to main content

Module clipboard

Module clipboard 

Source
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.

Experimental: may change in any release (docs/stability.md).
  • 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, ClipboardPlugin keeps a LocalClipboard in memory, shared by the application’s windows. It publishes clipboard.changed (the slot, the type, the revision; never the payload).
  • Sources and targets are the components’: a component registers an endpoint by name (register, or Ctx::clip_endpoint) with a ClipSource (what a copy takes, what a cut deletes) and a ClipTarget (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.copy and clipboard.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) and edit.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 !textInputFocus and the keys canCut, 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’s SystemClipboardPlugin), run_native turns 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 (with format, transform, parts, if_rev and dry_run), clipboard.offers, clipboard.read, clipboard.write, each in the session’s own slot unless the owner allows otherwise; the clipboard collector.

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§

ClipItem
What one copy made: a typed item with its readable fallbacks.
ClipOrigin
Where an item came from.
ClipType
A type of item a plugin declares and owns.
ClipboardPlugin
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, the clipboard collector, egui-pop’s own sanitiser of external text and its HTML to text transform.
CopyRequest
A copy, as the clipboard.copy step’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.
EguiText
The default backend: text through egui (Context::copy_text, which eframe hands to its own clipboard at the end of the pass), nothing read.
FormatId
A format: a MIME-like type, type/subtype with optional parameters.
LocalClipboard
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.
PasteRequest
A paste, as the clipboard.paste step’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.
TitleKey
A title as a catalogue message: its key and its arguments.
Transform
A transform a plugin declares and owns.
TransformSpec
A transform a plugin declares: it makes to from from.
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’s SystemClipboardPlugin, whose backend reads the system clipboard: with it added, run_native turns 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§

ClipSource
What a copy takes from a component.
ClipTarget
Where a paste goes in a component.
Clipboard
The owner of the clipboard, as the interface reaches it.
SystemBackend
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’s LocalClipboard.
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_COPY step. 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_PASTE step. 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, text and element).
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_shortcuts off), so the keymap runs them everywhere: edit.copy, edit.cut and edit.paste then also run while a text field has the focus, and give it egui’s own Event::Copy, Event::Cut and Event::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, text and element; its format, transform and parts ignored).
with_backend
Runs f on the system backend: the owner’s, else the one installed, else egui’s text.
with_owner
Runs f on the owner: the host’s, else the plugin’s LocalClipboard. None without the plugin, or while the host (owning the clipboard) runs a command.