Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/docs/configuration/drag-and-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,15 @@ COMPOSITOR.dnd.event.onStart((drag) => {

Remember to give the cursor back with `overrideKind(null)` when the drag
ends.


## Dragging a window with the data

A browser or toolkit can tear a tab off into a new window and dock it back by
dragging the window itself along with the data
([xdg-toplevel-drag-v1](https://wayland.app/protocols/xdg-toplevel-drag-v1)).
ShojiWM moves that window with the pointer as an interactive move, so your
config gets `onWindowMove` events with `source: "toplevel-drag"` and its usual
snapping and workspace rules apply. The window is never the drop target: the
drop lands on what is under it. When the drag ends, the window stays where
the move left it.
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ COMPOSITOR.dnd.event.onStart((drag) => {
```

ドラッグが終わったら、`overrideKind(null)` でカーソルを戻すのを忘れないでください。


## データと一緒にウィンドウをドラッグする

ブラウザーやツールキットは、データと一緒にウィンドウそのものをドラッグして、タブを
新しいウィンドウに切り離したり、元のウィンドウに戻したりできます
([xdg-toplevel-drag-v1](https://wayland.app/protocols/xdg-toplevel-drag-v1))。
ShojiWM はそのウィンドウを対話的な移動としてポインターと一緒に動かします。設定には
`source: "toplevel-drag"` の `onWindowMove` イベントが届き、いつものスナップや
ワークスペースのルールがそのまま働きます。そのウィンドウ自身はドロップ先になりません。
ドロップはその下にあるものに届きます。ドラッグが終わると、ウィンドウは移動が終わった
位置に残ります。
7 changes: 6 additions & 1 deletion packages/shoji_wm/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ export interface WindowMoveRect {
height: number;
}

export type WindowMoveSource = "ssd" | "modifier" | "client-csd" | "xwayland";
export type WindowMoveSource =
| "ssd"
| "modifier"
| "client-csd"
| "xwayland"
| "toplevel-drag";
export type WindowMovePhase = "start" | "update" | "end" | "cancel";

export interface WindowMoveEvent {
Expand Down
18 changes: 16 additions & 2 deletions src/shojiwm_lib/src/dnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
//! of the negotiated action, the target window or the icon, end). A config
//! that does not listen gets the built-in view: the icon at the pointer,
//! gone when the drag ends.
//!
//! A drag can also carry a window (xdg-toplevel-drag-v1, see
//! `crate::toplevel_drag`).

use std::{
any::Any,
Expand All @@ -32,14 +35,15 @@ use smithay::{
dnd::{DnDGrab, DndAction, GrabType, Source, SourceMetadata},
pointer::Focus,
},
reexports::wayland_server::protocol::wl_surface::WlSurface,
reexports::wayland_server::protocol::{wl_data_source::WlDataSource, wl_surface::WlSurface},
utils::{IsAlive, Logical, Point, Rectangle, Serial},
};
use tracing::{debug, warn};

use crate::{
ssd::{DecorationNode, DecorationTree, StandaloneDecoration, window_model::WindowIconSnapshot},
state::ShojiWM,
toplevel_drag::ToplevelDragGrab,
};

/// How long a view may stay after its drag ended without the config
Expand Down Expand Up @@ -639,6 +643,11 @@ impl ShojiWM {
};

let metadata = source.metadata();
// A `wl_data_device` drag hands over its `wl_data_source`, which may
// carry a toplevel (xdg-toplevel-drag-v1).
let data_source = (&source as &dyn Any)
.downcast_ref::<WlDataSource>()
.cloned();
let source_window = start_data
.focus
.as_ref()
Expand All @@ -660,7 +669,10 @@ impl ShojiWM {
self.end_drag(previous, DragEndKind::Cancelled, false, position);
}
let grab = DnDGrab::new_pointer(&self.display_handle, start_data, tracked, seat);
pointer.set_grab(self, grab, serial, Focus::Keep);
pointer.set_grab(self, ToplevelDragGrab { inner: grab }, serial, Focus::Keep);
if let Some(data_source) = data_source {
self.begin_toplevel_drag(&data_source);
}

let now = Instant::now();
let drag = ActiveDrag {
Expand Down Expand Up @@ -717,6 +729,7 @@ impl ShojiWM {
accepted: bool,
position: Point<f64, Logical>,
) {
self.mark_toplevel_drag_ended();
let Some(drag_id) = self.dnd.active.as_ref().map(|active| active.id) else {
return;
};
Expand All @@ -742,6 +755,7 @@ impl ShojiWM {
return;
}
self.sync_drag_state();
self.end_toplevel_drag();
let Some(active) = self.dnd.end(Instant::now()) else {
return;
};
Expand Down
138 changes: 95 additions & 43 deletions src/shojiwm_lib/src/grabs/move_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,62 @@ use smithay::{
utils::{Logical, Point},
};

pub struct MoveSurfaceGrab {
pub start_data: PointerGrabStartData<ShojiWM>,
/// A window that follows the pointer: the part of a move that does not
/// depend on what drives it, a [`MoveSurfaceGrab`] or a toplevel drag
/// (`crate::toplevel_drag`).
#[derive(Debug)]
pub struct WindowMove {
pub window: Window,
pub initial_window_location: Point<i32, Logical>,
pub initial_event_rect: smithay::utils::Rectangle<i32, Logical>,
pub source: WindowMoveSourceSnapshot,
pub runtime_managed: bool,
pub last_pointer: Point<f64, Logical>,
start_pointer: Point<f64, Logical>,
initial_window_location: Point<i32, Logical>,
initial_event_rect: smithay::utils::Rectangle<i32, Logical>,
source: WindowMoveSourceSnapshot,
runtime_managed: bool,
last_pointer: Point<f64, Logical>,
}

impl MoveSurfaceGrab {
pub fn start(
start_data: PointerGrabStartData<ShojiWM>,
impl WindowMove {
/// A move of `window`, which sits at `initial_window_location` (and
/// `initial_event_rect` for the config) while the pointer is at
/// `start_pointer`.
pub fn new(
window: Window,
start_pointer: Point<f64, Logical>,
initial_window_location: Point<i32, Logical>,
initial_event_rect: smithay::utils::Rectangle<i32, Logical>,
source: WindowMoveSourceSnapshot,
) -> Self {
let last_pointer = start_data.location;
Self {
start_data,
window,
start_pointer,
initial_window_location,
initial_event_rect,
source,
runtime_managed: false,
last_pointer,
last_pointer: start_pointer,
}
}

pub fn last_pointer(&self) -> Point<f64, Logical> {
self.last_pointer
}

/// Tell the config the move starts. The config answers at once, so this
/// must not run inside a pointer grab: what it asks for in reply takes the
/// pointer's lock.
pub fn notify_start(&mut self, data: &mut ShojiWM) {
data.cursor_override = Some(CursorIcon::Grabbing);
data.schedule_redraw();
self.runtime_managed = self.invoke_runtime_event(
data,
WindowMovePhaseSnapshot::Start,
self.start_data.location,
);
self.runtime_managed =
self.invoke_runtime_event(data, WindowMovePhaseSnapshot::Start, self.start_pointer);
}

/// Tell the config the move ended, once the caller has returned (see
/// `queue_grab_end_dispatch`).
pub fn finish(&self, data: &mut ShojiWM) {
if self.runtime_managed {
let window_id = data.snapshot_window(&self.window).id;
let event = self.runtime_event(data, WindowMovePhaseSnapshot::End, self.last_pointer);
data.queue_grab_end_dispatch(PendingGrabMotion::WindowMove { window_id, event });
}
}

fn invoke_runtime_event(
Expand All @@ -83,7 +101,7 @@ impl MoveSurfaceGrab {
phase: WindowMovePhaseSnapshot,
current_pointer: Point<f64, Logical>,
) -> WindowMoveEventSnapshot {
let start_pointer = self.start_data.location;
let start_pointer = self.start_pointer;
let delta = current_pointer - start_pointer;
let current_rect = move_rect_for_delta(self.initial_event_rect, delta);
let output_name = data
Expand Down Expand Up @@ -114,28 +132,20 @@ impl MoveSurfaceGrab {
timestamp: std::time::Duration::from(data.clock.now()).as_millis() as u64,
}
}
}

impl PointerGrab<ShojiWM> for MoveSurfaceGrab {
fn motion(
&mut self,
data: &mut ShojiWM,
handle: &mut PointerInnerHandle<'_, ShojiWM>,
_focus: Option<(WlSurface, Point<f64, Logical>)>,
event: &MotionEvent,
) {
// While the grab is active, no client has pointer focus
handle.motion(data, None, event);
self.last_pointer = event.location;
/// The pointer moved to `location`: move the window with it. Safe inside
/// a pointer grab, since the config hears about it later.
pub fn motion(&mut self, data: &mut ShojiWM, location: Point<f64, Logical>) {
self.last_pointer = location;

if self.runtime_managed {
let window_id = data.snapshot_window(&self.window).id;
let event = self.runtime_event(data, WindowMovePhaseSnapshot::Update, event.location);
let event = self.runtime_event(data, WindowMovePhaseSnapshot::Update, location);
data.queue_grab_motion_dispatch(PendingGrabMotion::WindowMove { window_id, event });
return;
}

let delta = event.location - self.start_data.location;
let delta = location - self.start_pointer;
let new_location = self.initial_window_location.to_f64() + delta;
// Unlike the runtime-managed path above, this fallback places the
// window directly in the `Space`, whose element locations are whole
Expand Down Expand Up @@ -221,6 +231,53 @@ impl PointerGrab<ShojiWM> for MoveSurfaceGrab {
data.schedule_redraw();
}
}
}

pub struct MoveSurfaceGrab {
pub start_data: PointerGrabStartData<ShojiWM>,
pub window_move: WindowMove,
}

impl MoveSurfaceGrab {
pub fn start(
start_data: PointerGrabStartData<ShojiWM>,
window: Window,
initial_window_location: Point<i32, Logical>,
initial_event_rect: smithay::utils::Rectangle<i32, Logical>,
source: WindowMoveSourceSnapshot,
) -> Self {
let window_move = WindowMove::new(
window,
start_data.location,
initial_window_location,
initial_event_rect,
source,
);
Self {
start_data,
window_move,
}
}

pub fn notify_start(&mut self, data: &mut ShojiWM) {
data.cursor_override = Some(CursorIcon::Grabbing);
data.schedule_redraw();
self.window_move.notify_start(data);
}
}

impl PointerGrab<ShojiWM> for MoveSurfaceGrab {
fn motion(
&mut self,
data: &mut ShojiWM,
handle: &mut PointerInnerHandle<'_, ShojiWM>,
_focus: Option<(WlSurface, Point<f64, Logical>)>,
event: &MotionEvent,
) {
// While the grab is active, no client has pointer focus
handle.motion(data, None, event);
self.window_move.motion(data, event.location);
}

fn relative_motion(
&mut self,
Expand All @@ -247,13 +304,8 @@ impl PointerGrab<ShojiWM> for MoveSurfaceGrab {
if !handle.current_pressed().contains(&BTN_LEFT) {
// No more buttons are pressed, release the grab.
handle.unset_grab(self, data, event.serial, event.time, true);
if self.runtime_managed {
let window_id = data.snapshot_window(&self.window).id;
let event =
self.runtime_event(data, WindowMovePhaseSnapshot::End, self.last_pointer);
data.queue_grab_end_dispatch(PendingGrabMotion::WindowMove { window_id, event });
}
data.update_decoration_cursor_icon(self.last_pointer);
self.window_move.finish(data);
data.update_decoration_cursor_icon(self.window_move.last_pointer());
}
}

Expand Down Expand Up @@ -348,7 +400,7 @@ impl PointerGrab<ShojiWM> for MoveSurfaceGrab {

fn unset(&mut self, data: &mut ShojiWM) {
data.pointer_dispatch.discard_grab_motion();
data.update_decoration_cursor_icon(self.last_pointer);
data.update_decoration_cursor_icon(self.window_move.last_pointer());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_lib/src/handlers/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ impl CompositorHandler for ShojiWM {
xdg_shell::handle_commit(self, surface);
layer_shell::handle_commit(self, surface, layer_commit_damage);
resize_grab::handle_commit(&mut self.space, surface);
self.toplevel_drag_commit(surface);

if !self.idle_inhibited_surfaces.is_empty() {
self.refresh_idle_inhibit_state();
Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_lib/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ crate::delegate_hyprland_toplevel_export!(ShojiWM);
crate::delegate_tearing_control!(ShojiWM);
crate::delegate_output_power!(ShojiWM);
crate::delegate_gamma_control!(ShojiWM);
crate::delegate_toplevel_drag!(ShojiWM);
crate::delegate_virtual_pointer!(ShojiWM);
crate::delegate_virtual_keyboard!(ShojiWM);

Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_lib/src/handlers/xdg_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl XdgShellHandler for ShojiWM {

fn toplevel_destroyed(&mut self, surface: ToplevelSurface) {
let wl_surface = surface.wl_surface();
self.toplevel_drag_toplevel_destroyed(surface.xdg_toplevel());
let Some(window) = self
.space
.elements()
Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub mod runtime_process;
pub mod runtime_workspace;
pub mod ssd;
pub mod state;
pub mod toplevel_drag;
pub mod virtual_input;
pub mod virtual_keyboard;
pub mod window_decoration;
Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_lib/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod output_power;
pub mod plasma_shell;
pub mod screencopy;
pub mod tearing_control;
pub mod toplevel_drag;
pub mod virtual_keyboard;
pub mod virtual_pointer;
Loading