Skip to content

Class ClipboardOffer ​

One thing put on a seat's clipboard, from seat.clipboard:current() or the changed event. Once something else is copied, the old offer can no longer be read.

This keeps the last 20 texts copied in apps, newest first:

lua
local clipboard_history = {}

local function offers_type(offer, wanted_type)
	for _, mime_type in ipairs(offer:mime_types()) do
		if mime_type == wanted_type then return true end
	end
	return false
end

seat.clipboard:on("changed", function(offer)
	if not offer or offer.origin ~= "wayland" then return end
	if not offers_type(offer, "text/plain;charset=utf-8") then return end
	offer:read("text/plain;charset=utf-8", 64 * 1024, function(text)
		if not text then return end
		table.insert(clipboard_history, 1, text)
		clipboard_history[21] = nil
	end)
end)

Properties ​

offer.origin: "wayland" | "lua"origin read only ​

Who put it on the clipboard: "wayland" for an app, "lua" for seat.clipboard:set.

offer.seat: Seat?seat read only ​

The seat whose clipboard it is on.

  • nil when that seat is gone.

Methods ​

offer:mime_types() -> string[]mime_types ​

The types the data can be read in, such as text/plain;charset=utf-8 or image/png.

  • A new table on every call.
  • Sorted for an offer from Lua. An app's offer keeps the app's order.

offer:is_current() -> booleanis_current ​

Whether this offer is still what is on its seat's clipboard.

offer:read(mime_type: string, max_bytes: integer, callback: function(data: string?, err: string?)) -> nilread ​

Asks for the data in one of the offered types. callback gets the data as a Lua string, or nil and err.

  • callback always runs later, never before read returns.
  • An app's data can take a moment to arrive.

err is set when:

  • the type isn't one of the offered ones
  • the data is longer than max_bytes
  • the offer isn't current any more, or something else was copied before the data arrived