-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wayland: register drag & drop events
- Loading branch information
Showing
4 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src/platform_impl/linux/wayland/seat/data_device/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
use std::path::PathBuf; | ||
|
||
use ahash::{AHashMap, RandomState}; | ||
use sctk::data_device_manager::{ | ||
data_device::DataDeviceHandler, | ||
data_offer::{DataOfferHandler, DragOffer}, | ||
data_source::DataSourceHandler, WritePipe, | ||
}; | ||
use sctk::reexports::client::protocol::wl_data_source::WlDataSource; | ||
use sctk::reexports::client::protocol::wl_data_device::WlDataDevice; | ||
use sctk::reexports::client::protocol::wl_data_device_manager::DndAction; | ||
use sctk::reexports::client::protocol::wl_surface::WlSurface; | ||
use sctk::reexports::client::{Connection, QueueHandle}; | ||
|
||
use crate::event::WindowEvent; | ||
use crate::platform_impl::wayland::seat::ObjectId; | ||
use crate::platform_impl::wayland::seat::WinitSeatState; | ||
use crate::platform_impl::wayland::state::WinitState; | ||
use crate::platform_impl::wayland; | ||
|
||
fn seat_from_wl_data_device<'a>(seats: &'a mut AHashMap<ObjectId, WinitSeatState, RandomState>, wl_data_device: &WlDataDevice) -> Option<&'a mut WinitSeatState> { | ||
let Some((_, seat_state )) = seats.iter_mut().find(|(_, seat_state)| match &seat_state.data_device { | ||
Some(data_device) => data_device.inner() == wl_data_device, | ||
None => false | ||
}) else { | ||
return None; | ||
}; | ||
return Some(seat_state); | ||
} | ||
|
||
impl DataDeviceHandler for WinitState { | ||
fn enter( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
wl_data_device: &WlDataDevice, | ||
_x: f64, | ||
_y: f64, | ||
wl_surface: &WlSurface, | ||
) { | ||
let Some(seat_state) = seat_from_wl_data_device(&mut self.seats, wl_data_device) else { | ||
return; | ||
}; | ||
|
||
let Some(data_device) = &seat_state.data_device else { | ||
return; | ||
}; | ||
|
||
let Some(drag_offer) = data_device.data().drag_offer() else { | ||
return; | ||
}; | ||
|
||
// Accept any mime type, the API consumer can figure this out | ||
let mime_type = drag_offer.with_mime_types(|mime_types| { | ||
if let Some(mime) = mime_types.get(0) { | ||
Some(mime.clone()) | ||
} else { | ||
None | ||
} | ||
}); | ||
drag_offer.accept_mime_type(0, mime_type.clone()); | ||
|
||
drag_offer.set_actions(DndAction::Copy, DndAction::Copy); | ||
|
||
let window_id = wayland::make_wid(&wl_surface); | ||
seat_state.hovered_window = Some(window_id); | ||
self.events_sink.push_window_event(WindowEvent::HoveredFile(PathBuf::new()), window_id); | ||
} | ||
|
||
fn leave(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, wl_data_device: &WlDataDevice) { | ||
let Some(seat_state) = seat_from_wl_data_device(&mut self.seats, wl_data_device) else { | ||
return; | ||
}; | ||
if let Some(window_id) = seat_state.hovered_window { | ||
seat_state.hovered_window = None; | ||
self.events_sink.push_window_event(WindowEvent::HoveredFileCancelled, window_id); | ||
} | ||
} | ||
|
||
fn motion( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_wl_data_device: &WlDataDevice, | ||
_x: f64, | ||
_y: f64, | ||
) { | ||
|
||
} | ||
|
||
fn selection(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _wl_data_device: &WlDataDevice) { | ||
|
||
} | ||
|
||
fn drop_performed( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
wl_data_device: &WlDataDevice, | ||
) { | ||
|
||
let Some(seat_state) = seat_from_wl_data_device(&mut self.seats, wl_data_device) else { | ||
return; | ||
}; | ||
if let Some(window_id) = seat_state.hovered_window { | ||
seat_state.hovered_window = None; | ||
self.events_sink.push_window_event(WindowEvent::DroppedFile(PathBuf::new()), window_id); | ||
} | ||
} | ||
} | ||
|
||
// Inbound | ||
impl DataOfferHandler for WinitState { | ||
fn source_actions( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
offer: &mut DragOffer, | ||
_actions: DndAction, | ||
) { | ||
offer.set_actions(DndAction::Copy, DndAction::Copy); | ||
} | ||
|
||
fn selected_action( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_offer: &mut DragOffer, | ||
_actions: DndAction, | ||
) { | ||
|
||
} | ||
} | ||
|
||
// Outbound | ||
impl DataSourceHandler for WinitState { | ||
fn accept_mime( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_wl_data_source: &WlDataSource, | ||
_mime: Option<String>, | ||
) { | ||
|
||
} | ||
|
||
fn send_request( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_wl_data_source: &WlDataSource, | ||
_mime: String, | ||
_fd: WritePipe, | ||
) { | ||
|
||
} | ||
|
||
fn cancelled(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _wl_data_source: &WlDataSource) { | ||
|
||
} | ||
|
||
fn dnd_dropped(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _wl_data_source: &WlDataSource) { | ||
|
||
|
||
} | ||
|
||
fn dnd_finished(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _wl_data_source: &WlDataSource) { | ||
|
||
} | ||
|
||
fn action( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_wl_data_source: &WlDataSource, | ||
_action: DndAction, | ||
) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters