Drag and drop: files can be dragged between windows and within one
CI / test (push) Canceled after 4m0s

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
This commit is contained in:
Claude committed 2026-11-26 05:31:33 +00:00
1 parent 80af76fb15
commit 76a23f7816
5 files changed
+1046 -4

No files matched your search

+68 -4
View File
@@ -40,6 +40,17 @@ pub(all) enum CompositorEvent {
SubsurfaceCommitted(UInt64, UInt, Int, Int, @scene.ShmBufferSnapshot?)
/// The subsurface was unmapped (null buffer) or destroyed: (window, child wl_surface id).
SubsurfaceUnmapped(UInt64, UInt)
/// A client started a drag and drop (wl_data_device.start_drag) from this window (0 when its
/// origin surface has no window). The host checks a button is held on it, then moves the drag
/// focus with the pointer (`drag_focus`, `drag_motion`) and drops on release (`drag_drop`).
DragStarted(UInt64)
/// The drag icon's new buffer (None: no icon) and where its top-left goes relative to the
/// pointer.
DragIcon(@scene.ShmBufferSnapshot?, Int, Int)
/// The drag icon's hotspot moved without a new buffer.
DragIconMoved(Int, Int)
/// The drag ended on the client's side (its source or the client went away).
DragEnded
}
///| wl_subsurface state, keyed by the child wl_surface id.
@@ -129,6 +140,10 @@ struct SurfaceState {
/// org_kde_kwin_server_decoration mode Server: Chandra draws this window's frame (GTK 3 apps
/// without their own header bar, like Webby).
mut kde_ssd : Bool
/// Pending surface offset since the last commit (attach x/y, wl_surface.offset): the drag
/// icon's hotspot moves by it.
mut pend_dx : Int
mut pend_dy : Int
}
///|
@@ -218,6 +233,10 @@ struct ClientConn {
prim_devices : Array[UInt]
/// zwp_primary_selection_source_v1 id -> mime types it offers.
prim_sources : Map[UInt, Array[String]]
/// wl_data_device_manager version the client bound (dnd actions are since 3).
mut data_ver : UInt
/// wl_data_source id -> dnd actions it set (set_actions; missing = never set).
source_actions : Map[UInt, UInt]
/// Next server-allocated id for a wl_data_offer.
mut next_offer : UInt
/// wp_cursor_shape_v1 shape the client last asked for (0 = never set).
@@ -249,6 +268,8 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
data_sources: {},
prim_devices: [],
prim_sources: {},
data_ver: 1U,
source_actions: {},
next_offer: 0xff000000U,
cursor_shape: 0U,
alive: true,
@@ -335,6 +356,9 @@ pub(all) struct WaylandServer {
transfers : Array[(Int, Int, UInt64)]
/// CHANDRA_DEBUG_CONFIGURE=1: log each configure, ack and committed buffer size to stderr.
mut debug_configure : Bool
/// Drag and drop in progress (dnd.mbt), and a drop its target is still taking.
mut drag : DragState?
mut drop_pending : DropState?
}
///|
@@ -366,6 +390,8 @@ pub fn WaylandServer::listen(
own_data: Bytes::from_array([]),
transfers: [],
debug_configure: @sys.getenv("CHANDRA_DEBUG_CONFIGURE").length() > 0,
drag: None,
drop_pending: None,
})
}
@@ -621,6 +647,7 @@ fn WaylandServer::dispatch_msg(
c.objects.set(new_id, ObjCursorShapeMgr)
} else if iface == "wl_data_device_manager" {
c.objects.set(new_id, ObjDataManager)
c.data_ver = ver
} else if iface == "zwp_primary_selection_device_manager_v1" {
c.objects.set(new_id, ObjPrimaryManager)
} else if iface == "wl_subcompositor" {
@@ -643,6 +670,8 @@ fn WaylandServer::dispatch_msg(
buffer_scale: 1,
kde_ssd: false,
attached_null: false,
pend_dx: 0,
pend_dy: 0,
})
} else if opcode == 1U {
let rid = reader.u32()
@@ -660,14 +689,17 @@ fn WaylandServer::dispatch_msg(
None => ()
}
}
self.dnd_icon_gone(c, sid, events)
c.surfaces.remove(sid)
c.objects.remove(sid)
send_delete_id(c, sid)
} else if opcode == 1U {
// attach(buffer, x, y)
let buf_id = reader.u32()
let _x = reader.i32()
let _y = reader.i32()
let ax = reader.i32()
let ay = reader.i32()
surf.pend_dx = surf.pend_dx + ax
surf.pend_dy = surf.pend_dy + ay
surf.attached_null = buf_id == 0U
surf.attached_buffer = if buf_id == 0U {
None
@@ -678,12 +710,19 @@ fn WaylandServer::dispatch_msg(
// frame(callback)
let cb_id = reader.u32()
surf.pending_frame_cb = Some(cb_id)
} else if opcode == 10U {
// offset(x, y) (version 5): moves the surface; for a drag icon, its hotspot.
surf.pend_dx = surf.pend_dx + reader.i32()
surf.pend_dy = surf.pend_dy + reader.i32()
} else if opcode == 8U {
// set_buffer_scale(scale)
let sc = reader.i32()
surf.buffer_scale = if sc > 0 { sc } else { 1 }
} else if opcode == 6U {
// commit
self.dnd_icon_commit(c, sid, surf, events)
surf.pend_dx = 0
surf.pend_dy = 0
if surf.window_id is None && c.subsurfaces.contains(sid) {
match sub_root(c, sid) {
Some((wid, ax, ay)) =>
@@ -816,9 +855,14 @@ fn WaylandServer::dispatch_msg(
Some(m) => m.push(mime)
None => ()
}
} else if opcode == 2U {
// set_actions(dnd_actions) (version 3)
c.source_actions.set(obj_id, reader.u32() & 7U)
} else if opcode == 1U {
// destroy
self.dnd_source_gone(c, obj_id, events)
c.data_sources.remove(obj_id)
c.source_actions.remove(obj_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
if self.selection == Some((c.client_id, obj_id)) {
@@ -827,7 +871,13 @@ fn WaylandServer::dispatch_msg(
}
}
ObjDataDevice =>
if opcode == 1U {
if opcode == 0U {
// start_drag(source or null, origin, icon or null, serial)
let src = reader.u32()
let origin = reader.u32()
let icon = reader.u32()
self.start_drag(c, src, origin, icon, events)
} else if opcode == 1U {
// set_selection(source or null, serial)
let src = reader.u32()
let old = self.selection
@@ -854,7 +904,19 @@ fn WaylandServer::dispatch_msg(
send_delete_id(c, obj_id)
}
ObjDataOffer(ocid, osid) =>
if opcode == 1U {
if opcode == 0U {
// accept(serial, mime or null): the drag target can take this type here
let _serial = reader.u32()
self.dnd_accept(c, obj_id, reader.str())
} else if opcode == 3U {
// finish: the drop target has the data
self.dnd_finish(c, obj_id)
} else if opcode == 4U {
// set_actions(dnd_actions, preferred_action)
let actions = reader.u32()
let preferred = reader.u32()
self.dnd_set_actions(c, obj_id, actions, preferred)
} else if opcode == 1U {
// receive(mime_type, fd): ask the owner to write the data into the fd
let mime = reader.str()
let fd = c.take_fd()
@@ -883,6 +945,7 @@ fn WaylandServer::dispatch_msg(
}
} else if opcode == 2U {
// destroy
self.dnd_offer_gone(c, obj_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
@@ -1563,6 +1626,7 @@ pub fn WaylandServer::poll_events(
events.push(ToplevelClosed(wid))
}
c.close_all()
self.dnd_client_gone(c.client_id, events)
if self.kbd_client == Some(c.client_id) {
self.kbd_client = None
}