transfer

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 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 describes data exchanged through the clipboard and native drag and drop. Values contain explicit representations, never Go object references. The immutable model is shared by both integrations.

Index

Constants

This section is empty.

Variables

View Source
var ErrFormat = errors.New("transfer: representation unavailable")

ErrFormat means the requested item or representation is not available.

View Source
var ErrProviderPanic = errors.New("transfer: provider panicked")

ErrProviderPanic means a lazy provider panicked. The panic is contained at the data boundary, rather than escaping through a native callback.

Functions

This section is empty.

Types

type Data

type Data struct {
	// contains filtered or unexported fields
}

Data is an immutable collection of items. Its zero value is empty.

func FileData

func FileData(paths ...string) (Data, error)

FileData offers existing file paths as file URLs, one item per path. Paths must be absolute. It neither reads nor deletes the files.

func New

func New(items ...Item) Data

New makes data from items, preserving their order.

func TextData

func TextData(text string) Data

TextData offers a plain text item.

func URLData

func URLData(raw string) (Data, error)

URLData offers a URL as a URI list and as plain text.

func (Data) Files

func (d Data) Files() ([]string, error)

Files returns local file paths only. Remote file authorities and other URL schemes are ignored, so a network URL never becomes a filesystem path.

func (Data) Formats

func (d Data) Formats() []Format

Formats lists all formats, in item order, without invoking providers.

func (Data) Items

func (d Data) Items() []Item

Items returns the items. Modifying the returned slice does not change Data.

func (Data) Materialize

func (d Data) Materialize(formats []Format) (Data, error)

Materialize copies matching representations into data with no providers. A failed representation rejects the transfer; an empty match is ErrFormat.

func (Data) Preferred

func (d Data) Preferred(formats ...Format) (Format, bool)

Preferred chooses the first preferred format advertised by the data, without invoking any provider. The caller supplies its own preference order (for example, custom JSON before HTML before plain text).

func (Data) Read

func (d Data) Read(format Format) ([]byte, error)

Read reads a format for platforms with a single selection/data object. Text items are joined with newlines and URI lists with CRLF. Other formats use the first item offering them; macOS preserves separate items.

func (Data) Snapshot

func (d Data) Snapshot() Data

Snapshot starts a new provider cache, as each clipboard write or drag does.

func (Data) URLs

func (d Data) URLs() ([]string, error)

URLs returns the URLs of all URI-list items, ignoring comments and blanks.

type DragOptions

type DragOptions struct {
	Operations Operation
	// Preview is copied at drag start. Hotspot is in image pixels; the
	// preview is displayed in DIPs (one pixel per DIP).
	Preview image.Image
	Hotspot image.Point
	// Done runs on the main thread exactly once, including cancellation,
	// startup failure and source-window destruction. Delete or remove the
	// original only after a successful Move; Golet never deletes files.
	Done func(Result)
}

DragOptions configures a native source. The default operation is copy.

type Drop

type Drop struct {
	Data      Data
	Operation Operation
}

Drop is a completed transfer. Its data is independent of the native session and can be read later, from any goroutine.

type DropOptions

type DropOptions struct {
	Formats    []Format
	Operations Operation
}

DropOptions describes a destination. At least one offered format must match Formats. Empty Formats accepts none; zero Operations permits copy.

type Format

type Format string

Format names a representation. Use a MIME type for custom serialized data.

const (
	Text    Format = "text/plain"
	HTML    Format = "text/html"
	URIList Format = "text/uri-list"
	// FileList is a URI list known to contain local files. Backends expose
	// it as the system's file-list format as well as text/uri-list.
	FileList Format = "application/x-golet-file-list"
	PNG      Format = "image/png"
	// Markdown is Markdown's text (RFC 7763), net.daringfireball.markdown
	// on macOS.
	Markdown Format = "text/markdown"
)

func PreferredFormat

func PreferredFormat(offered []Format, preferred ...Format) (Format, bool)

PreferredFormat negotiates formats from a clipboard or drag offer without requesting data. Receiver preference order wins over source item order.

type Item

type Item struct {
	// contains filtered or unexported fields
}

Item is a value with alternative representations, in preference order. For example, one item can offer HTML and plain text of the same content.

func NewItem

func NewItem(reps ...Representation) Item

NewItem makes an item. Duplicate formats and zero representations panic.

func (Item) Formats

func (i Item) Formats() []Format

Formats lists the item's advertised formats without invoking providers.

func (Item) Read

func (i Item) Read(format Format) ([]byte, error)

Read returns a copy of the requested bytes. It is safe from any goroutine.

type Offer

type Offer struct {
	Formats    []Format
	Operations Operation
	Suggested  Operation
}

Offer describes a drag during hover, without reading its bytes.

func (Offer) Preferred

func (o Offer) Preferred(formats ...Format) (Format, bool)

Preferred selects a format without requesting any representation bytes.

type Operation

type Operation uint8

Operation is a native drag effect, or a bitmask of allowed effects.

const (
	None Operation = 0
	Copy Operation = 1
	Move Operation = 2
)

func Negotiate

func Negotiate(source, destination, suggested Operation) Operation

Negotiate chooses one effect shared by source and destination. A source's modifier-key suggestion wins when allowed; otherwise copy precedes move.

func (Operation) Allowed

func (o Operation) Allowed() Operation

Allowed supplies the safe default (copy) and removes unsupported effects.

type Representation

type Representation struct {
	// contains filtered or unexported fields
}

Representation is one format of an item, made with Bytes or Lazy.

func Bytes

func Bytes(format Format, b []byte) Representation

Bytes makes a representation, copying b. Empty data is valid.

func Lazy

func Lazy(format Format, provider func() ([]byte, error)) Representation

Lazy advertises a format without producing its bytes. The provider runs at most once per snapshot, when a destination requests this format; failures are cached too. Native integrations call it on the main thread. It must return promptly and must not wait for another main-thread action. Prepare expensive data on a goroutine before offering it.

type Result

type Result struct {
	Operation Operation
	// Canceled reports that the drag ended without a drop: let go over
	// no target, or canceled.
	Canceled bool
	// Escaped reports that a canceled drag was given up (Escape,
	// Window.CancelDataDrag, or the source window closing), rather than
	// let go over no target.
	Escaped bool
	// Point is where the drag ended, in screen coordinates, as
	// Screen.CursorScreenPoint reports them.
	Point image.Point
	Err   error
}

Result reports the destination's final effect. None means no drop.

Jump to

Keyboard shortcuts

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