///| Drag and drop on the host side (the protocol is compositor/wayland/dnd.mbt): once a client /// starts a drag with a button held, the pointer stops going to clients as wl_pointer events and /// becomes the drag: the surface under it is the drag focus, the drag icon follows it, Escape /// cancels and the release drops. ///| A client asked to start a drag from window `win` (0: unknown). It is only real while a button /// pressed on that client is still held; otherwise (a click that was already released) it is /// cancelled at once, so the app doesn't wait for a drop that can't come. fn ChandraCompositor::dnd_start(self : ChandraCompositor, win : UInt64, time_ms : UInt) -> Unit { let held = self.ptr_grab && self.grab_buttons > 0 || self.press_fwd let same_client = win == 0UL || (match self.ptr_win { Some(w) => w == win || self.server.same_client(w, win) None => false }) if !held || !same_client { self.server.drag_cancel() return } // The origin loses the pointer (wl_pointer.leave) and gets the drag instead. self.dnd = true self.leave_pointer() self.press_fwd = false self.dnd_motion(time_ms) } ///| The pointer moved during a drag: move the drag focus to what is under it. fn ChandraCompositor::dnd_motion(self : ChandraCompositor, time_ms : UInt) -> Unit { let xi = self.ptr_x.to_int() let yi = self.ptr_y.to_int() let over = match self.popup_at(xi, yi) { Some(p) => { let (ox, oy) = { let r = self.popup_rect(p); (r.x, r.y) } Some((p.id, 0U, self.ptr_x - ox.to_double(), self.ptr_y - oy.to_double())) } None => if self.shell.hit(xi, yi) { None // the panel, the launcher: nothing there takes a drop } else { match self.window_at(xi, yi) { Some((wid, true)) => match self.windows.get(wid) { Some(win) => { let (sub, lx, ly) = win.surface_at(self.ptr_x, self.ptr_y) Some((wid, sub, lx, ly)) } None => None } _ => None } } } self.dnd_over = match over { Some((wid, _, _, _)) => Some(wid) None => None } match over { Some((wid, sub, lx, ly)) => { self.server.drag_focus(Some(wid), sub, lx, ly) self.server.drag_motion(time_ms, lx, ly) } None => self.server.drag_focus(None, 0U, 0.0, 0.0) } if self.dnd_icon is Some(_) { self.damage.mark_full() } } ///| The drag is over (dropped, cancelled, or ended by the client): back to normal pointer /// routing, the pointer entering whatever is under it. fn ChandraCompositor::dnd_finish(self : ChandraCompositor, time_ms : UInt) -> Unit { self.dnd = false self.dnd_over = None release_snapshot(self.dnd_icon) self.dnd_icon = None self.ptr_grab = false self.grab_buttons = 0 self.press_fwd = false self.damage.mark_full() self.handle_input(PointerMove(self.ptr_x, self.ptr_y), time_ms) } ///| Input while a drag is going: true when it was the drag's (motion, release, Escape, wheel). fn ChandraCompositor::dnd_input(self : ChandraCompositor, ev : @nested.InputEvent, time_ms : UInt) -> Bool { if !self.dnd { return false } if !self.server.drag_active() { // The client ended it (its source or the client went away). self.dnd_finish(time_ms) return false } match ev { PointerMove(x, y) => { self.ptr_x = x self.ptr_y = y self.dnd_motion(time_ms) true } PointerButton(_, false) => { let target = self.dnd_over let dropped = self.server.drag_drop() self.dnd_finish(time_ms) if dropped { // The window it was dropped on becomes the active one: the drop is now its work, and // what it shows next (Dolphin's Copy Here / Move Here menu) is a menu of a focused // window, not a window of its own. let wid = match target { Some(w) => match self.popup_root(w) { Some(r) => r; None => w } None => 0UL } if self.windows.contains(wid) { self.activate_window(wid) } } true } PointerButton(_, true) => true Scroll(_, _, _) => true Key(1U, true) => { // Escape cancels the drag. self.server.drag_cancel() self.dnd_finish(time_ms) true } Key(1U, false) => true _ => false } } ///| Scene: the drag icon, above everything clients draw, its hotspot at the pointer. fn ChandraCompositor::dnd_icon_node(self : ChandraCompositor) -> (@scene.Rect, @scene.ShmBufferSnapshot)? { guard self.dnd && self.dnd_icon is Some(snap) else { return None } let x = self.ptr_x.to_int() + self.dnd_icon_x let y = self.ptr_y.to_int() + self.dnd_icon_y Some((@scene.Rect::new(x, y, snap.width, snap.height), snap)) }