transfer.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Package transfer contains operations and events for brokering data transfers.
  2. //
  3. // The transfer protocol is as follows:
  4. //
  5. // - Data sources use [SourceFilter] to receive an [InitiateEvent] when a drag
  6. // is initiated, and an [RequestEvent] for each initiation of a data transfer.
  7. // Sources respond to requests with [OfferCmd].
  8. // - Data targets use [TargetFilter] to receive an [DataEvent] for receiving data.
  9. // The target must close the data event after use.
  10. //
  11. // When a user initiates a pointer-guided drag and drop transfer, the
  12. // source as well as all potential targets receive an InitiateEvent.
  13. // Potential targets are targets with at least one MIME type in common
  14. // with the source. When a drag gesture completes, a CancelEvent is sent
  15. // to the source and all potential targets.
  16. //
  17. // Note that the RequestEvent is sent to the source upon drop.
  18. package transfer
  19. import (
  20. "io"
  21. "gioui.org/io/event"
  22. )
  23. // OfferCmd is used by data sources as a response to a RequestEvent.
  24. type OfferCmd struct {
  25. Tag event.Tag
  26. // Type is the MIME type of Data.
  27. // It must be the Type from the corresponding RequestEvent.
  28. Type string
  29. // Data contains the offered data. It is closed when the
  30. // transfer is complete or cancelled.
  31. // Data must be kept valid until closed, and it may be used from
  32. // a goroutine separate from the one processing the frame.
  33. Data io.ReadCloser
  34. }
  35. func (OfferCmd) ImplementsCommand() {}
  36. // SourceFilter filters for any [RequestEvent] that match a MIME type
  37. // as well as [InitiateEvent] and [CancelEvent].
  38. // Use multiple filters to offer multiple types.
  39. type SourceFilter struct {
  40. // Target is a tag included in a previous event.Op.
  41. Target event.Tag
  42. // Type is the MIME type supported by this source.
  43. Type string
  44. }
  45. // TargetFilter filters for any [DataEvent] whose type matches a MIME type
  46. // as well as [CancelEvent]. Use multiple filters to accept multiple types.
  47. type TargetFilter struct {
  48. // Target is a tag included in a previous event.Op.
  49. Target event.Tag
  50. // Type is the MIME type accepted by this target.
  51. Type string
  52. }
  53. // RequestEvent requests data from a data source. The source must
  54. // respond with an OfferCmd.
  55. type RequestEvent struct {
  56. // Type is the first matched type between the source and the target.
  57. Type string
  58. }
  59. func (RequestEvent) ImplementsEvent() {}
  60. // InitiateEvent is sent to a data source when a drag-and-drop
  61. // transfer gesture is initiated.
  62. //
  63. // Potential data targets also receive the event.
  64. type InitiateEvent struct{}
  65. func (InitiateEvent) ImplementsEvent() {}
  66. // CancelEvent is sent to data sources and targets to cancel the
  67. // effects of an InitiateEvent.
  68. type CancelEvent struct{}
  69. func (CancelEvent) ImplementsEvent() {}
  70. // DataEvent is sent to the target receiving the transfer.
  71. type DataEvent struct {
  72. // Type is the MIME type of Data.
  73. Type string
  74. // Open returns the transfer data. It is only valid to call Open in the frame
  75. // the DataEvent is received. The caller must close the return value after use.
  76. Open func() io.ReadCloser
  77. }
  78. func (DataEvent) ImplementsEvent() {}
  79. func (SourceFilter) ImplementsFilter() {}
  80. func (TargetFilter) ImplementsFilter() {}