Files
Claude 76a23f7816
CI / test (push) Canceled after 4m0s
Drag and drop: files can be dragged between windows and within one
The compositor ignored wl_data_device.start_drag, so no drag ever reached a
window: Dolphin, GNOME Files, Chrome tabs and text could not be dropped
anywhere, not even inside the window they started in.

Now (compositor/wayland/dnd.mbt, compositor/host/dnd.mbt):
- start_drag while a button is held on the client turns the pointer into the
  drag (the origin gets wl_pointer.leave); a request that arrives after the
  release is cancelled at once.
- The surface under the pointer (windows, subsurfaces, menus) gets
  data_offer + offer + source_actions + enter, then motion and leave; a drag
  without a source stays inside its own client.
- accept -> source.target, set_actions -> copy/move/ask agreed and sent as
  offer.action and source.action (version 3), version 1/2 clients copy.
- Release drops when the target accepted a type and an action: drop,
  dnd_drop_performed, and finish -> dnd_finished; otherwise cancelled + leave.
  Escape cancels. The window dropped on becomes active, so Dolphin's
  Copy/Move Here menu opens as a menu of a focused window.
- The drag icon is drawn under the pointer at its hotspot (attach offsets and
  wl_surface.offset), above windows and menus.
- A source, icon or client going away ends or updates the drag.

Tests: compositor/host/dnd_test.mbt (fail on the previous commit). Checked
headless with real Dolphin 23.08 (Qt 5.15) and GNOME Files 46 on both the
native build and the wasm build: Files -> Dolphin, Dolphin -> Files,
Dolphin -> Dolphin (Move/Copy/Link menu) and a move into a folder inside Files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjWb2dWbR1K7qoMgWP
2026-11-26 05:31:33 +00:00

261 lines
12 KiB
MoonBit

///| Drag and drop (Dolphin, GNOME Files, Chrome): a client starts a drag with the button held,
/// the window under the pointer gets enter / motion / drop with an offer of the dragged types,
/// and the two agree on copy or move.
///| Like Dolphin dragging a file: a source offering text/uri-list that can be copied or moved,
/// then start_drag from `surf` (with `icon`, 0 for none). Returns the source id.
fn start_file_drag(c : TestWaylandClient, mgr : UInt, dev : UInt, surf : UInt, icon? : UInt = 0U) -> UInt {
let src = c.alloc_id()
let w1 = @wayland.WireWriter::new()
w1.u32(src)
c.send(w1.finish_msg(mgr, 0U)) // create_data_source
let w2 = @wayland.WireWriter::new()
w2.str("text/uri-list")
c.send(w2.finish_msg(src, 0U)) // offer
let w3 = @wayland.WireWriter::new()
w3.u32(3U)
c.send(w3.finish_msg(src, 2U)) // set_actions(copy | move)
let w4 = @wayland.WireWriter::new()
w4.u32(src)
w4.u32(surf)
w4.u32(icon)
w4.u32(1U)
c.send(w4.finish_msg(dev, 0U)) // start_drag
src
}
///|
test "a file dragged from one window drops on another, which moves it" {
let socket_path = "/tmp/wayland-moon-dnd1-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let (b, shm_b, surf_b, xdg_b, _, _) = open_and_configure_client(comp, socket_path, "Files", "org.gnome.Nautilus")
commit_shm_frame(comp, b, shm_b, surf_b, xdg_b, 2UL)
let ptr_a = pointer_for(comp, a)
let ptr_b = pointer_for(comp, b)
let (mgr_a, dev_a) = data_device_for(a)
let (_, dev_b) = data_device_for(b)
pump(comp, 5)
let g = comp.windows.get(1UL).unwrap().geometry
let gb = comp.windows.get(2UL).unwrap().geometry
// Press on a file in window 1 and move a little: Dolphin starts the drag.
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U)
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 50).to_double(), (g.y + 45).to_double()), 12U)
ignore(drain_args(a))
ignore(drain_args(b))
let src = start_file_drag(a, mgr_a, dev_a, surf_a)
pump(comp, 5)
let ma = drain_args(a)
assert_eq(count_msgs(ma, ptr_a, 1U), 1) // wl_pointer.leave: the pointer is the drag now
// The drag enters its own window first (Dolphin can drop into a folder in the same view).
let own_enter = ma.iter().find_first(m => m.0 == dev_a && m.1 == 1U).unwrap()
assert_eq(own_enter.2[1], surf_a)
// Over window 2: it gets an offer of the file list, its actions, then enter.
comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 60).to_double(), (gb.y + 70).to_double()), 13U)
pump(comp, 5)
assert_eq(count_msgs(drain_args(a), dev_a, 2U), 1) // window 1 got leave
let mb = drain_args(b)
assert_eq(count_msgs(mb, ptr_b, 0U), 0) // no wl_pointer.enter during the drag
let offer = mb.iter().find_first(m => m.0 == dev_b && m.1 == 0U).unwrap().2[0]
assert_eq(count_msgs(mb, offer, 0U), 1) // offer("text/uri-list")
let sa = mb.iter().find_first(m => m.0 == offer && m.1 == 1U).unwrap()
assert_eq(sa.2[0], 3U) // source_actions(copy | move)
let enter = mb.iter().find_first(m => m.0 == dev_b && m.1 == 1U).unwrap()
let (ox, oy) = comp.windows.get(2UL).unwrap().surface_origin()
assert_eq(enter.2[1], surf_b)
assert_eq(enter.2[2].reinterpret_as_int(), (gb.x + 60 - ox) * 256)
assert_eq(enter.2[3].reinterpret_as_int(), (gb.y + 70 - oy) * 256)
assert_eq(enter.2[4], offer)
// Files accepts the list and prefers to move it.
let wacc = @wayland.WireWriter::new()
wacc.u32(enter.2[0])
wacc.str("text/uri-list")
b.send(wacc.finish_msg(offer, 0U))
let wact = @wayland.WireWriter::new()
wact.u32(3U)
wact.u32(2U)
b.send(wact.finish_msg(offer, 4U))
pump(comp, 5)
let ma2 = drain_args(a)
assert_eq(count_msgs(ma2, src, 0U), 1) // target("text/uri-list")
assert_eq(ma2.iter().find_first(m => m.0 == src && m.1 == 5U).unwrap().2[0], 2U) // action(move)
let mb2 = drain_args(b)
assert_eq(mb2.iter().find_first(m => m.0 == offer && m.1 == 2U).unwrap().2[0], 2U) // offer.action(move)
// Motion inside window 2 reaches it as wl_data_device.motion.
comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 80).to_double(), (gb.y + 90).to_double()), 14U)
pump(comp, 5)
let mot = drain_args(b).iter().find_first(m => m.0 == dev_b && m.1 == 3U).unwrap()
assert_eq(mot.2[0], 14U)
assert_eq(mot.2[1].reinterpret_as_int(), (gb.x + 80 - ox) * 256)
// Release: drop on window 2, drop_performed to the source.
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 15U)
pump(comp, 5)
let mb3 = drain_args(b)
assert_eq(count_msgs(mb3, dev_b, 4U), 1) // drop
assert_eq(count_msgs(mb3, dev_b, 2U), 0) // and no leave before it is read
assert_eq(count_msgs(mb3, ptr_b, 0U), 1) // the pointer is back to normal, over window 2
assert_eq(count_msgs(mb3, ptr_b, 3U), 0) // without a stray release
let ma3 = drain_args(a)
assert_eq(count_msgs(ma3, src, 3U), 1) // dnd_drop_performed
assert_eq(count_msgs(ma3, src, 2U), 0) // not cancelled
// Files reads the list: receive(mime, fd) reaches Dolphin as send(mime, fd).
let fd = @sys.memfd_create("dnd-uri-list", 16)
let wr = @wayland.WireWriter::new()
wr.str("text/uri-list")
b.send(wr.finish_msg(offer, 1U), send_fd=fd)
@sys.close_fd(fd)
pump(comp, 5)
assert_eq(count_msgs(drain_args(a), src, 1U), 1) // send
// finish -> dnd_finished: Dolphin deletes the moved original.
b.send(@wayland.WireWriter::new().finish_msg(offer, 3U))
pump(comp, 5)
assert_eq(count_msgs(drain_args(a), src, 4U), 1)
comp.close()
}
///|
test "a drag released where nothing takes it is cancelled" {
let socket_path = "/tmp/wayland-moon-dnd2-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let (b, shm_b, surf_b, xdg_b, _, _) = open_and_configure_client(comp, socket_path, "Files", "org.gnome.Nautilus")
commit_shm_frame(comp, b, shm_b, surf_b, xdg_b, 2UL)
ignore(pointer_for(comp, a))
let (mgr_a, dev_a) = data_device_for(a)
let (_, dev_b) = data_device_for(b)
pump(comp, 5)
let g = comp.windows.get(1UL).unwrap().geometry
let gb = comp.windows.get(2UL).unwrap().geometry
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
let src = start_file_drag(a, mgr_a, dev_a, surf_a)
pump(comp, 5)
// Over window 2, which doesn't accept anything, then onto the panel and released there.
comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 60).to_double(), (gb.y + 70).to_double()), 3U)
pump(comp, 5)
assert_eq(count_msgs(drain_args(b), dev_b, 1U), 1) // enter
comp.handle_input(@nested.InputEvent::PointerMove(640.0, 2.0), 4U)
pump(comp, 5)
assert_eq(count_msgs(drain_args(b), dev_b, 2U), 1) // leave
ignore(drain_args(a))
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 5U)
pump(comp, 5)
let ma = drain_args(a)
assert_eq(count_msgs(ma, src, 2U), 1) // cancelled
assert_eq(count_msgs(ma, src, 3U), 0) // no drop
assert_eq(count_msgs(drain_args(b), dev_b, 4U), 0)
// A drop over a window that never accepted the type is cancelled too.
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 6U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 7U)
let src2 = start_file_drag(a, mgr_a, dev_a, surf_a)
pump(comp, 5)
comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 60).to_double(), (gb.y + 70).to_double()), 8U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 9U)
pump(comp, 5)
assert_eq(count_msgs(drain_args(a), src2, 2U), 1)
let mb = drain_args(b)
assert_eq(count_msgs(mb, dev_b, 4U), 0) // no drop
assert_eq(count_msgs(mb, dev_b, 2U), 1) // leave
comp.close()
}
///|
test "Escape cancels a drag, and a drag asked for after the release never starts" {
let socket_path = "/tmp/wayland-moon-dnd3-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
let ptr_a = pointer_for(comp, a)
let (mgr_a, dev_a) = data_device_for(a)
pump(comp, 5)
let g = comp.windows.get(1UL).unwrap().geometry
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
let src = start_file_drag(a, mgr_a, dev_a, surf_a)
pump(comp, 5)
ignore(drain_args(a))
comp.handle_input(@nested.InputEvent::Key(1U, true), 3U)
comp.handle_input(@nested.InputEvent::Key(1U, false), 4U)
pump(comp, 5)
let ma = drain_args(a)
assert_eq(count_msgs(ma, src, 2U), 1) // cancelled
assert_eq(count_msgs(ma, dev_a, 2U), 1) // leave
assert_eq(count_msgs(ma, ptr_a, 0U), 1) // the pointer is the window's again
// The release that follows is not a drop and doesn't reach the window as a stray release.
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 5U)
pump(comp, 5)
let mr = drain_args(a)
assert_eq(count_msgs(mr, dev_a, 4U), 0)
// A click that was already released when the drag request arrives: cancelled at once.
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 6U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 7U)
let late = start_file_drag(a, mgr_a, dev_a, surf_a)
pump(comp, 5)
let ml = drain_args(a)
assert_eq(count_msgs(ml, late, 2U), 1) // cancelled
assert_eq(count_msgs(ml, dev_a, 1U), 0) // never entered
comp.close()
}
///|
test "the drag icon follows the pointer, its hotspot under it" {
let socket_path = "/tmp/wayland-moon-dnd4-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL)
ignore(pointer_for(comp, a))
let (mgr_a, dev_a) = data_device_for(a)
let comp_id = bind_global(a, 2U, "wl_compositor", 5U)
let icon = a.alloc_id()
let wcs = @wayland.WireWriter::new()
wcs.u32(icon)
a.send(wcs.finish_msg(comp_id, 0U))
pump(comp, 5)
let g = comp.windows.get(1UL).unwrap().geometry
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
ignore(start_file_drag(a, mgr_a, dev_a, surf_a, icon~))
pump(comp, 5)
// A 32x24 icon whose hotspot is 16, 12 (wl_surface.offset(-16, -12), as Qt and GTK do).
let memfd = @sys.memfd_create("dnd-icon", 32 * 24 * 4)
ignore(@sys.pwrite_bytes(memfd, Bytes::make(32 * 24 * 4, b'\xff'), 0))
let pool = a.alloc_id()
let wp = @wayland.WireWriter::new()
wp.u32(pool)
wp.i32(32 * 24 * 4)
a.send(wp.finish_msg(shm_a, 0U), send_fd=memfd)
@sys.close_fd(memfd)
let buf = a.alloc_id()
let wb = @wayland.WireWriter::new()
wb.u32(buf)
wb.i32(0)
wb.i32(32)
wb.i32(24)
wb.i32(32 * 4)
wb.u32(0U)
a.send(wb.finish_msg(pool, 0U))
let wa = @wayland.WireWriter::new()
wa.u32(buf)
wa.i32(0)
wa.i32(0)
a.send(wa.finish_msg(icon, 1U)) // attach
let wo = @wayland.WireWriter::new()
wo.i32(-16)
wo.i32(-12)
a.send(wo.finish_msg(icon, 10U)) // offset
a.send(@wayland.WireWriter::new().finish_msg(icon, 6U)) // commit
pump(comp, 5)
comp.handle_input(@nested.InputEvent::PointerMove(500.0, 300.0), 3U)
let scene = comp.compose_scene().unwrap()
let r = scene.popups.last().unwrap().0
assert_eq((r.x, r.y, r.w, r.h), (484, 288, 32, 24))
// Dropped: the icon is gone.
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
let scene2 = comp.compose_scene().unwrap()
assert_eq(scene2.popups.length(), 0)
comp.close()
}