transfer

package
v0.2.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 14 Imported by: 3 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package transfer is the content shape every data transfer shares: clipboard claims, the primary selection, and drag and drop all offer a payload as mime types, best first, with a writer for each, and all read one back by mime preference. The builders cover the formats the desktop speaks - text, images (PNG and JPEG), uri lists (file copies), HTML (formatted text) - and encode up front, so a peer's transfer deadline never runs during encoding.

Index

Constants

View Source
const (
	MimeText    = "text/plain;charset=utf-8"
	MimeURIList = "text/uri-list"
	MimeHTML    = "text/html"
	MimePNG     = "image/png"
	MimeJPEG    = "image/jpeg"
	MimeWebP    = "image/webp"
	MimeGIF     = "image/gif"
)

Well-known mime types.

Variables

ImageMimes are the image types accepted, best first: PNG round-trips exactly, the rest decode lossy or flattened.

View Source
var TextMimes = []string{
	MimeText,
	"UTF8_STRING",
	"text/plain",
	"COMPOUND_TEXT",
	"TEXT",
	"STRING",
}

TextMimes are the text types offered and accepted, best first: the X11 atoms after the mime types, for clients bridged through Xwayland.

Functions

func DecodeHTML

func DecodeHTML(data []byte) string

DecodeHTML returns an HTML payload as a string: UTF-8 as sent, or UTF-16 when it opens with a byte-order mark (some browsers write their HTML that way).

func FilePaths

func FilePaths(uris []string) []string

FilePaths keeps the local file uris of uris, as paths.

func FileURI

func FileURI(path string) string

FileURI encodes an absolute local path as a file:// uri, percent escapes included; anything else returns unchanged, for the consumer to reject.

func ParseURIList

func ParseURIList(data []byte) []string

ParseURIList reads a uri list: one uri per line, comment lines (starting with #) and blank lines skipped, CR tolerated.

func Pick

func Pick(prefs []string, present func(string) bool) string

Pick returns the first of prefs that present reports, "" when none.

func URIPath

func URIPath(uri string) (string, bool)

URIPath decodes a local file:// uri to its path; remote hosts, other schemes, bad escapes, and empty paths are not local files.

Types

type Action

type Action uint8

Action is a drag-and-drop action, a set when offered: what becomes of the payload at the source once dropped.

const (
	ActionNone Action = 0
	// ActionCopy leaves the source's data as it was.
	ActionCopy Action = 1
	// ActionMove means the source deletes its data once the drop
	// finished.
	ActionMove Action = 2
	// ActionAsk lets the destination ask the user (a drop menu).
	ActionAsk Action = 4
)

Actions, the wl_data_device_manager dnd_action bits.

func Prefer

func Prefer(offered Action, prefs ...Action) Action

Prefer picks the action to take from offered: the first of prefs offered, else copy or move (copy first), else none.

type Content

type Content struct {
	Mimes []string
	Write func(mime string, w io.Writer) error
}

Content is one transfer's payload: the mimes it is offered as, best first, and Write, which writes the bytes for one of them. Write runs off the event loop, possibly several at once, so it must not touch widgets.

func Bytes

func Bytes(mime string, data []byte) Content

Bytes is data offered as one mime.

func Files

func Files(paths ...string) Content

Files is local paths as a uri list of file URIs - what a file manager copies and takes; relative paths resolve against the working directory.

func HTML

func HTML(html, plain string) Content

HTML is formatted text: html for rich targets, plain for the rest.

func Image

func Image(img image.Image) (Content, error)

Image is img offered as PNG (exact) and JPEG (flattened onto white: JPEG has no alpha), both encoded now.

func Merge

func Merge(parts ...Content) Content

Merge offers every part's mimes in order; a mime two parts share is served by the first. Formatted text merges HTML before plain text, files their uri list before the paths as text.

func Payloads

func Payloads(mimes []string, data map[string][]byte) Content

Payloads is content from finished bytes per mime, offered in the order given; the writers copy, so the content may be served any number of times.

func Text

func Text(s string) Content

Text is s offered under every text mime.

func URIs

func URIs(uris ...string) Content

URIs is a uri list (RFC 2483: CRLF-terminated lines), with the uris one per line as plain text for targets that only take text.

func (Content) Bytes

func (c Content) Bytes(mime string) ([]byte, error)

Bytes writes c's payload for mime into a buffer - the same-process path, where no pipe is needed.

func (Content) Empty

func (c Content) Empty() bool

Empty reports whether c offers nothing.

func (Content) Has

func (c Content) Has(mime string) bool

Has reports whether c offers mime.

type Drag

type Drag struct {
	Content
	// Actions are the actions offered; zero offers copy.
	Actions Action
	// OnFeedback, optional, hears every change of acceptance and
	// negotiated action.
	OnFeedback func(Feedback)
	// OnDone, optional, fires once the drag concluded: the action the
	// destination finished with (ActionMove: delete the source data),
	// or ActionNone for a cancelled or rejected drag.
	OnDone func(done Action)
}

Drag is a drag's payload and its source-side hooks.

func (Drag) Offered

func (d Drag) Offered() Action

Offered is the action set the drag offers, copy when none was set.

type Feedback

type Feedback struct {
	Mime   string // "" while nothing accepts
	Action Action
}

Feedback is what a drag's source hears while the drag runs: whether the destination under the pointer accepts (and as which mime), and the action the compositor negotiated with it - for a drag icon or cursor that tells copy from move, or a source that dims what a move would take.

func (Feedback) Accepted

func (f Feedback) Accepted() bool

Accepted reports whether a destination accepts the drag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL