1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Wayland drag-and-drop (DnD) types.
//!
//! This module provides the event types and request structures for
//! Wayland-native drag-and-drop. The design mirrors the `wl_data_device`
//! protocol: a **source** initiates a drag with offered MIME types and
//! supported actions, while one or more **destinations** receive enter /
//! motion / leave / drop events and can accept or reject the operation.
//!
//! # Event flow
//!
//! 1. User starts dragging → widget publishes [`Request::StartDrag`] via
//! [`Shell::start_dnd`].
//! 2. Runtime calls `wl_data_device.start_drag(…)` on the Wayland
//! connection.
//! 3. The compositor delivers DnD protocol events which the runtime
//! converts to [`Event`] variants and pushes them into the iced event
//! queue.
//! 4. Widgets (or the application) handle [`Event::Enter`],
//! [`Event::Motion`], [`Event::Leave`], [`Event::Drop`], and
//! [`Event::SourceEvent`] to update visual feedback and perform the
//! drop action.
use bitflags::bitflags;
bitflags! {
/// The set of actions supported or accepted for a drag-and-drop
/// operation.
///
/// Maps directly to `wl_data_device_manager::dnd_action` in the
/// Wayland protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DndAction: u32 {
/// No action (reject).
const NONE = 0;
/// Copy the dragged data.
const COPY = 1;
/// Move the dragged data (source should delete after drop).
const MOVE = 2;
/// Ask the user which action to perform.
const ASK = 4;
}
}
/// A drag-and-drop event delivered to widgets.
///
/// These events are produced by the runtime when the Wayland compositor
/// sends `wl_data_device` / `wl_data_offer` / `wl_data_source` events.
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
/// A drag offer entered the application surface.
///
/// `mime_types` lists the MIME types offered by the drag source.
/// `x` / `y` are surface-local coordinates.
Enter {
/// Surface-local X coordinate.
x: f64,
/// Surface-local Y coordinate.
y: f64,
/// MIME types offered by the source.
mime_types: Vec<String>,
},
/// The drag cursor moved within our surface.
Motion {
/// Surface-local X coordinate.
x: f64,
/// Surface-local Y coordinate.
y: f64,
},
/// The drag offer left our surface without a drop.
Leave,
/// The user dropped the data on our surface.
///
/// After receiving this, the destination should request the data
/// for the desired MIME type via [`Request::RequestData`] and then
/// call [`Request::FinishDnd`].
Drop,
/// Data was received for a previously requested MIME type.
///
/// This is the response to [`Request::RequestData`].
DataReceived {
/// The MIME type of the received data.
mime_type: String,
/// The raw bytes of the data.
data: Vec<u8>,
},
/// The selected (negotiated) action for the current drag.
SelectedAction(DndAction),
/// An event for the **drag source** side of the operation.
SourceEvent(SourceEvent),
}
/// Events delivered to the drag **source** (the initiator of the drag).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceEvent {
/// The compositor is requesting data for the given MIME type.
///
/// The source should respond by writing the data. For internal
/// (same-app) drags this is handled automatically by the runtime.
SendRequested,
/// The drag was cancelled (e.g. user pressed Escape).
Cancelled,
/// The drop was performed by the destination.
DropPerformed,
/// The destination has finished processing the dropped data.
/// The source may now delete the original data if the action was
/// `Move`.
Finished,
/// The negotiated action changed.
Action(DndAction),
}
/// Icon for the drag visual.
///
/// The runtime uses this to create the `wl_surface` that the compositor
/// displays under the cursor during a Wayland drag.
pub enum DndIcon {
/// Pre-rendered pixel data in **pre-multiplied ARGB** format
/// (little-endian), which is the native Wayland `wl_shm` pixel
/// format (`Argb8888`).
Pixels {
/// Width in pixels.
width: u32,
/// Height in pixels.
height: u32,
/// Pre-multiplied ARGB pixel data (4 bytes per pixel, row-major).
pixels: Vec<u8>,
/// Buffer scale for HiDPI support (e.g., 2 for 2x rendering).
scale: i32,
},
/// A type-erased iced `Element` that the runtime will render
/// offscreen to produce the icon pixels.
///
/// The element is stored as `Box<dyn Any>` to avoid generic
/// parameters leaking into the core DnD types. The runtime
/// downcasts to the concrete `(Element<'static, (), Theme, Renderer>,
/// widget::tree::State)` tuple when rendering.
Element(DndIconSurface),
}
impl std::fmt::Debug for DndIcon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Pixels {
width,
height,
scale,
..
} => f
.debug_struct("DndIcon::Pixels")
.field("width", width)
.field("height", height)
.field("scale", scale)
.finish(),
Self::Element(_) => f
.debug_tuple("DndIcon::Element")
.field(&"<opaque>")
.finish(),
}
}
}
/// A type-erased iced [`Element`] that can be rendered offscreen to
/// produce DnD icon pixels.
///
/// Created via [`DndIconSurface::new`] on the application/widget side
/// and downcast back via [`DndIconSurface::downcast`] in the runtime.
pub struct DndIconSurface {
inner: Box<dyn std::any::Any>,
}
impl DndIconSurface {
/// Wrap an iced Element (mapped to message type `()`) together with
/// its initial widget tree state.
pub fn new<Theme: 'static, Renderer: 'static>(
element: crate::Element<'static, (), Theme, Renderer>,
state: crate::widget::tree::State,
) -> Self {
Self {
inner: Box::new(DndIconSurfaceInner::<Theme, Renderer> { element, state }),
}
}
/// Downcast back to the concrete types. Returns `None` if the
/// type parameters don't match.
pub fn downcast<Theme: 'static, Renderer: 'static>(
self,
) -> Option<(
crate::Element<'static, (), Theme, Renderer>,
crate::widget::tree::State,
)> {
self.inner
.downcast::<DndIconSurfaceInner<Theme, Renderer>>()
.ok()
.map(|inner| (inner.element, inner.state))
}
}
struct DndIconSurfaceInner<Theme, Renderer> {
element: crate::Element<'static, (), Theme, Renderer>,
state: crate::widget::tree::State,
}
/// A drag-and-drop request that widgets can issue through [`Shell`].
///
/// [`Shell`]: crate::Shell
pub enum Request {
/// Start a Wayland drag-and-drop operation.
///
/// The runtime will create a `wl_data_source`, offer the given
/// MIME types, and call `wl_data_device.start_drag(…)`.
StartDrag {
/// Whether this is an internal-only drag (same application).
/// When `true`, the runtime may short-circuit data transfer.
internal: bool,
/// MIME types the source is willing to provide.
mime_types: Vec<String>,
/// Supported DnD actions.
actions: DndAction,
/// Opaque drag data, pre-serialized per MIME type.
///
/// The outer `Vec` parallels `mime_types` — entry `i` is the
/// data for `mime_types[i]`. If empty, the runtime will send
/// a `SourceEvent::SendRequested` when the destination asks
/// for data.
data: Vec<Vec<u8>>,
/// Optional icon for the drag visual.
///
/// If `None`, the runtime provides a default generic icon.
icon: Option<DndIcon>,
/// Optional icon hotspot in logical coordinates relative to the icon's
/// top-left — the point of the icon kept under the cursor. `None` places
/// the icon's top-left at the cursor.
hotspot: Option<(i32, i32)>,
},
/// Accept the current drag offer for a specific MIME type.
///
/// This tells the compositor (and the drag source) which MIME type
/// the destination wants. Pass `None` to reject the current
/// serial.
AcceptMimeType(Option<String>),
/// Set the accepted DnD actions for the current offer.
SetActions {
/// Bitfield of accepted actions.
actions: DndAction,
/// The preferred action.
preferred: DndAction,
},
/// Request the data for a given MIME type from the current offer.
///
/// The runtime will call `wl_data_offer.receive(mime_type, fd)`
/// and deliver the result as [`Event::DataReceived`].
RequestData {
/// The MIME type to request.
mime_type: String,
},
/// Signal that the destination has finished processing the drop.
///
/// Must be called after [`Event::Drop`] once all data has been
/// read and applied.
FinishDnd,
/// Cancel / end the current DnD operation from the source side.
EndDnd,
}
impl std::fmt::Debug for Request {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::StartDrag {
internal,
mime_types,
actions,
icon,
..
} => f
.debug_struct("StartDrag")
.field("internal", internal)
.field("mime_types", mime_types)
.field("actions", actions)
.field("icon", icon)
.finish(),
Self::AcceptMimeType(m) => f.debug_tuple("AcceptMimeType").field(m).finish(),
Self::SetActions { actions, preferred } => f
.debug_struct("SetActions")
.field("actions", actions)
.field("preferred", preferred)
.finish(),
Self::RequestData { mime_type } => f
.debug_struct("RequestData")
.field("mime_type", mime_type)
.finish(),
Self::FinishDnd => write!(f, "FinishDnd"),
Self::EndDnd => write!(f, "EndDnd"),
}
}
}