plat/dnd.rs#
Source: lib/plat/dnd.rs
dnd -- drag and drop between programs: what a drag carries, the target hook, the pending
drop and the app's own outgoing drag (the parts every OS shares) (docs/GUIDE.md §7)
DT_FILES / DT_TEXT / DT_URL / DT_HTML / DT_IMAGE kinds of content (bits, DT_STD = all);
DT_USER0 << k: the app's own kinds (internal drags only)
DE_COPY / DE_MOVE / DE_LINK effects (bits)
DragData the content of one drag (paths, text, URLs, HTML, an image, the
app's own bytes) and the effects
drag_hook(Some(f)) f(pw, x, y, types) -> (accepted types, accepted effects): what the
UI takes at a point (px) of platform window pw. The OS layers
ask it synchronously while something is dragged over a window
(IDropTarget::DragOver, XdndPosition, wl_data_device.motion).
drag_query(pw, x, y, types, allowed, mods) -> (types, effect) (OS layers) the hook's answer
with one effect picked; without a hook: files, copy (as before).
Files are never moved: a source deletes what it moved, and
the app gets only the paths (copy or link instead). The hook
answers per drop target (ui: drop_effect_set): a target that
takes no effect refuses the drag there (the system's pointer
shows it)
drag_query_all(...) -> (types, effect, effects) drag_query and every effect the target
takes there (Wayland's set_actions names them all)
drag_decide(types, effects, offered, allowed, mods) -> (types, effect, effects) what
drag_query makes of a hook's answer (tests ask it headless)
drag_effect_pick(allowed, mods) -> effect Ctrl copy, Shift move, Ctrl+Shift link; else
the first of copy / move / link the mask allows
drop_data_take() -> DragData the pending drop of E_DROP: the paths of drop_take and the rest
drop_hold(on) (the UI) the program answers drops itself: where the platform
allows a later answer, a drop's answer to its source waits after
E_DROP for drop_finish (Windows: a source whose data object is in
IDataObjectAsyncCapability's async mode; X11: XdndFinished;
Wayland: wl_data_offer.finish); elsewhere it went at the drop
drop_waiting() -> serial the drop whose answer waits (0: none), read after drop_data_take
drop_allowed() -> DE_* the effects the source of that drop allows, as the platform gives
them with the drop (0: it does not say), read after drop_data_take
drop_finish(serial, effect) -> answered answer that drop's source: the effect it was taken
with, 0 = not taken
drop_set_text / drop_set_html / drop_add_url / drop_set_image / drop_set_effect /
drop_set_allowed (OS layers) fill the pending drop (drop_reset empties it)
drop_text_from(bytes, utf16) / drop_url_list(text) decoding helpers of the OS layers
drag_start(data) -> bool drag data out of the app while the left button is held (the UI
calls it when an internal drag leaves its window); false when
the platform cannot (headless, browser) or a drag runs.
E_DRAG_END follows (data = the effect, 0 = cancelled / refused).
drag_running() -> bool an outgoing drag runs
Events: E_DRAG (x, y = position in window px, data = DT_* offered | DE_* allowed << 8; data 0:
the drag left the window, was dropped or cancelled) while another program's drag (or our
own outgoing one) is over a window; E_DROP (x, y, data = the number of paths) when it is
dropped there; E_DRAG_END when our own outgoing drag ended.
Windows: OLE (win_dnd.rs: IDropTarget per window, DoDragDrop with our IDataObject / IDropSource);
X11: Xdnd 5 target and source (linux_x11_dnd.rs); Wayland: wl_data_device (linux_wl_clip.rs).
Everything that comes from another program is bounded (sizes, counts) and checked.
kinds of content (bits)
DT_FILES: u32 = 1 // paths (file:// URIs, CF_HDROP)
DT_TEXT: u32 = 2 // UTF-8 text
DT_URL: u32 = 4 // URLs that are not local files
DT_HTML: u32 = 8 // a UTF-8 HTML fragment
DT_IMAGE: u32 = 16 // an image file (PNG; a DIB arrives as BMP)
DT_STD: u32 = 31 // every kind other programs know
DT_USER0: u32 = 1 << 8 // the app's own kinds: DT_USER0 << k, k = 0..23
effects (bits). An empty set of the effects a source allows (DragData::effects of a drag
// out, drag_query's and drag_decide's `allowed`, the DE_* of E_DRAG, drop_allowed) means the
// source does not say: every effect, DE_ALL -- the same everywhere (ui: drag_allowed)
DE_COPY: u32 = 1 // the target copies (DROPEFFECT_COPY, XdndActionCopy)
DE_MOVE: u32 = 2 // the target moves: the source deletes its original
DE_LINK: u32 = 4 // the target links to it (not on Wayland)
DE_ALL: u32 = 7 // every effect
struct DragDatathe content of one drag (a drop that arrives, or what the app drags)files: Vec<String>-- paths of files and folderstext: Option<String>-- plain texturls: Vec<String>-- URLs (not local files)html: Option<String>-- an HTML fragment (UTF-8)image: Option<Vec<u8>>-- an image file: PNG (a drop of a DIB / image/bmp arrives as a BMP file)bmp: Option<Vec<u8>>-- (drag out) the same image as a BMP file, offered as DIB / image/bmp too (optional)app: u32-- the app's own kind (DT_USER0 << k bits) and bytes: internal drags onlybytes: Vec<u8>label: String-- what the drag shows next to the pointer inside the app (empty: from the content)effects: u32-- drag out: the DE_* effects the source allows (0: it does not say, every effect); a drop: the effect chosen
impl DragData
fn types(&self) -> u32the DT_* kinds it carries (the app's own kinds included)fn is_empty(&self) -> boolnothing to dragfn of_text(s: &str) -> DragDatatext onlyfn of_files(paths: &[&str]) -> DragDatapaths onlyfn of_app(kind: u32, bytes: &[u8]) -> DragDatathe app's own kind (DT_USER0 << k) and bytesfn allowed(&self) -> u32the effects a drag out allows:effects, every one (DE_ALL) when it names nonetype DragHook = fn(i32, i32, i32, u32) -> (u32, u32)what the UI takes at a point: f(platform window, x, y px, offered DT_*) -> (accepted DT_*, accepted DE_*)fn drag_hook(f: Option<DragHook>)install (Some) or remove (None) the drop target hook (the UI does it for its windows)fn drag_effect_pick(allowed: u32, mods: u8) -> u32the effect of a drop: the modifier keys choose (Ctrl copy, Shift move, Ctrl+Shift link), else the first of copy / move / link thatallowedhas (0: none)fn drag_query(pw: i32, x: i32, y: i32, types: u32, allowed: u32, mods: u8) -> (u32, u32)(OS layers) what platform window pw takes at (x, y) px of a drag offeringtypeswith the effectsallowed(0: the source does not say, every effect) -> (accepted types, the effect); (0, 0) = refusedfn drag_query_all(pw: i32, x: i32, y: i32, types: u32, allowed: u32, mods: u8) -> (u32, u32, u32)(OS layers) drag_query and every DE_* effect the target there takes of the allowed ones (Wayland's wl_data_offer.set_actions) -> (accepted types, the effect, the effects); (0, 0, 0) = refusedfn drag_decide(t: u32, e: u32, types: u32, allowed: u32, mods: u8) -> (u32, u32, u32)what drag_query makes of the hook's answer (accepted types t, effects e) to a drag offeringtypeswith the effectsallowed(0: not said, every effect) and the modifier keysmods-> (accepted types, the effect, every effect taken); (0, 0, 0) = refused (no kind or no effect in common). Files are never moved. For the OS layers and headless tests (ui'sdragover expect)fn drop_set_text(bytes: &[u8]) -> bool(OS layers) the pending drop's text (UTF-8 bytes; cut at a NUL) -> false when too largefn drop_set_html(bytes: &[u8]) -> bool(OS layers) the pending drop's HTML fragment (UTF-8 bytes) -> false when too largefn drop_add_url(url: &str) -> bool(OS layers) one URL of the pending drop ("scheme:...", no blanks or control characters, bounded, no duplicates) -> addedfn drop_set_image(bytes: Vec<u8>) -> bool(OS layers) the pending drop's image file (PNG or BMP bytes) -> false when too largefn drop_set_effect(effect: u32)(OS layers) the effect the drop was made with (DE_*)fn drop_set_allowed(effects: u32)(OS layers) the DE_* effects the source of the pending drop allows, as the platform gives them with the drop: Windows IDropTarget::Drop's pdwEffect, X11 the source's XdndActionList with the action of its last XdndPosition (every effect without a list), Wayland the offer's source_actions, macOS the drag's operation mask. Not called (the browser, Android): 0fn drop_data_take() -> DragDatathe pending drop (E_DROP): its paths (drop_take) and the rest; both start empty againfn drop_allowed() -> u32drop_allowed() -> the DE_* effects the source of the drop drop_data_take took last allows, as the platform gave them with the drop (drop_set_allowed: the same the drag said in E_DRAG over the window); 0: the platform does not say (the browser, Android). Read it right after drop_data_takefn drop_text_from(bytes: &[u8], utf16: bool) -> Vec<u8>(OS layers) text in other programs' encodings -> UTF-8 bytes: UTF-16 LE (utf16, up to a NUL unit) or bytes as they are (up to a NUL)fn drop_url_list(text: &[u8]) -> u32(OS layers) the lines of a URL list (text/uri-list, text/x-moz-url, UniformResourceLocator): '#' lines skipped, every other line one URL; local files are left to drop_uri_list -> URLs added (also used for the URL a browser drags)fn drop_hold(on: bool)drop_hold(on): (the UI, at its start) the program answers drops itself. On, the OS layers keep a drop's answer to its source open after E_DROP where the platform allows a later answer, until drop_finish: Windows when the source's data object offers IDataObjectAsyncCapability in async mode (StartOperation in IDropTarget::Drop, EndOperation later), X11 (XdndFinished), Wayland (wl_data_offer.finish); everywhere else, and on Windows for every other source, the answer went at the drop (drop_waiting says 0). Off (the default): every drop is answered at once, as beforefn drop_waiting() -> u32drop_waiting() -> the serial of the last drop whose answer to its source waits for drop_finish; 0: none (it was answered at the drop, or no drop came). Read it right after drop_data_takefn drop_finish(serial: u32, effect: u32) -> booldrop_finish(serial, effect) -> answered: the source of dropserial(drop_waiting) learns how the drop ended: the DE_* effect it was taken with, 0 = not taken. false: that drop does not wait (answered already, or a newer drop replaced it). Windows: EndOperation(S_OK, effect), not taken EndOperation(E_ABORT, DROPEFFECT_NONE); X11: XdndFinished with "accepted" and the effect's action; Wayland: taken wl_data_offer.finish (the action stays the one negotiated before the drop), not taken the offer destroyed without finishfn drag_start(mut data: DragData) -> booldragdataout of the app (call while the left button is held) -> started; E_DRAG_END (data = the effect, 0 = cancelled or refused) follows. Only the standard kinds leave the app; files only when the manifest lets the app read them (false otherwise), a move only when it may write them all (else the drag offers copy and link).fn drag_running() -> boolan outgoing drag runs (until its E_DRAG_END is queued)