UXPasteboard
UXPasteboard holds one payload per type, so a copy can offer the same
content several ways and a paste can take the richest form it understands. It
has the shape of NSPasteboard and is the basis of both copy/paste and drag and
drop.
#use <UXKit> // or #import "UXPasteboard.xc"Overview
Section titled “Overview”UXPasteboard* pb = UXPasteboard.general(); // the clipboard
pb.clearContents();pb.setString((u8*)"<b>Report</b>", (u8*)"public.html");pb.writeText((u8*)"Report"); // public.utf8-plain-textBoth forms are now on the board. What comes back depends on what the reader can handle, not on the order they were written:
pb.preferredType((u8*)"public.html", (u8*)"public.utf8-plain-text")// -> "public.html" an HTML-capable paste
pb.preferredType((u8*)"public.rtf", (u8*)"public.utf8-plain-text")// -> "public.utf8-plain-text" a plain-text-only pasteA copy that wrote only its richest form would paste as nothing into a plain-text field. One that wrote only plain text would lose the formatting everywhere.
Type names are UTIs: "public.utf8-plain-text", "public.file-url", or your
application’s own. Nothing validates them; the contract is an agreed string.
Topics
Section titled “Topics”general · setString · writeText · stringForType · text · hasType · preferredType · typeCount · typeAt · clearContents · entryForType
general
Section titled “general”static UXPasteboard* general(void)The shared clipboard, made on first use.
A drag does not use this. A drag session carries its own fresh pasteboard,
so dragging something never destroys what the user copied earlier. See
UXDragSession.
setString
Section titled “setString”void setString(u8* s, u8* type)Write one payload under one type. Writing a type that is already present replaces it.
writeText
Section titled “writeText”void writeText(u8* s)Shorthand for setString(s, "public.utf8-plain-text"). Almost everything can
read this form, so write it alongside any richer one.
stringForType
Section titled “stringForType”u8* stringForType(u8* type) // 0 when absentAn absent type returns null instead of an error, so the only guard needed is a check of the result.
u8* text(void)Shorthand for the plain-text payload.
hasType
Section titled “hasType”bool hasType(u8* type)Whether a type is on offer. A drag destination asks this cheap question on every
pointer move; see
UXDragDestination.
preferredType
Section titled “preferredType”u8* preferredType(u8* a, u8* b) // 0 when neither is presentThe richer of two types, preferring a. This covers richest-first paste in the
common two-way case; for more than two, ask hasType in your own order of
preference.
typeCount / typeAt
Section titled “typeCount / typeAt”i32 typeCount(void)u8* typeAt(i32 i)Enumerate what is on offer, for a paste-special menu or for debugging what a copy wrote.
clearContents
Section titled “clearContents”void clearContents(void)entryForType
Section titled “entryForType”UXPasteboardEntry* entryForType(u8* type)The raw entry, when you want the type and data together.
changeCount
Section titled “changeCount”i32 changeCountTicks on every write. Use it to notice that someone else wrote to the clipboard since you last looked. A paste menu item that greys itself, or a panel that refreshes a preview, reads this instead of polling contents.
Example
Section titled “Example”types offered: 2 public.html public.utf8-plain-textan HTML-capable paste takes: public.html -> <b>Report</b>a plain-text-only paste takes: public.utf8-plain-text -> Reporthas public.file-url: no value: (null)after clear: types=0 change count 3 -> 4drag pasteboard is separate: clipboard types=0 drag types=1The program is website/site/examples/uxkit/pasteboard.xc. The doc-examples
gate compiles it, and the output above is what it prints.
Data is string-typed
Section titled “Data is string-typed”Payloads are strings: text, serialized forms, URLs. Raw bytes are not supported yet, so binary content needs an encoding you choose (and a type name that says so).
See also
Section titled “See also”UXDragSession: carries one of these through a dragUXDragDestination: askshasTypeto decide whether it can accept a dropUXPasteboardEntry: one type and its payload