use crate::reexports::client::{
protocol::{
wl_data_device_manager::DndAction,
wl_data_source::{self, WlDataSource},
wl_surface::WlSurface,
},
Connection, Dispatch, Proxy, QueueHandle, WEnum,
};
use super::{data_device::DataDevice, DataDeviceManagerState, WritePipe};
#[derive(Debug, Default)]
pub struct DataSourceData {}
pub trait DataSourceDataExt: Send + Sync {
fn data_source_data(&self) -> &DataSourceData;
}
impl DataSourceDataExt for DataSourceData {
fn data_source_data(&self) -> &DataSourceData {
self
}
}
pub trait DataSourceHandler: Sized {
fn accept_mime(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
source: &WlDataSource,
mime: Option<String>,
);
fn send_request(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
source: &WlDataSource,
mime: String,
fd: WritePipe,
);
fn cancelled(&mut self, conn: &Connection, qh: &QueueHandle<Self>, source: &WlDataSource);
fn dnd_dropped(&mut self, conn: &Connection, qh: &QueueHandle<Self>, source: &WlDataSource);
fn dnd_finished(&mut self, conn: &Connection, qh: &QueueHandle<Self>, source: &WlDataSource);
fn action(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
source: &WlDataSource,
action: DndAction,
);
}
impl<D, U> Dispatch<wl_data_source::WlDataSource, U, D> for DataDeviceManagerState
where
D: Dispatch<wl_data_source::WlDataSource, U> + DataSourceHandler,
U: DataSourceDataExt,
{
fn event(
state: &mut D,
source: &wl_data_source::WlDataSource,
event: <wl_data_source::WlDataSource as wayland_client::Proxy>::Event,
_data: &U,
conn: &wayland_client::Connection,
qh: &wayland_client::QueueHandle<D>,
) {
match event {
wl_data_source::Event::Target { mime_type } => {
state.accept_mime(conn, qh, source, mime_type)
}
wl_data_source::Event::Send { mime_type, fd } => {
state.send_request(conn, qh, source, mime_type, fd.into());
}
wl_data_source::Event::Cancelled => {
state.cancelled(conn, qh, source);
}
wl_data_source::Event::DndDropPerformed => {
state.dnd_dropped(conn, qh, source);
}
wl_data_source::Event::DndFinished => {
state.dnd_finished(conn, qh, source);
}
wl_data_source::Event::Action { dnd_action } => match dnd_action {
WEnum::Value(dnd_action) => {
state.action(conn, qh, source, dnd_action);
}
WEnum::Unknown(_) => {}
},
_ => unimplemented!(),
};
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CopyPasteSource {
pub(crate) inner: WlDataSource,
}
impl CopyPasteSource {
pub fn set_selection(&self, device: &DataDevice, serial: u32) {
device.device.set_selection(Some(&self.inner), serial);
}
pub fn inner(&self) -> &WlDataSource {
&self.inner
}
}
impl Drop for CopyPasteSource {
fn drop(&mut self) {
self.inner.destroy();
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DragSource {
pub(crate) inner: WlDataSource,
}
impl DragSource {
pub fn start_drag(
&self,
device: &DataDevice,
origin: &WlSurface,
icon: Option<&WlSurface>,
serial: u32,
) {
device.device.start_drag(Some(&self.inner), origin, icon, serial);
}
pub fn start_internal_drag(
device: &DataDevice,
origin: &WlSurface,
icon: Option<&WlSurface>,
serial: u32,
) {
device.device.start_drag(None, origin, icon, serial);
}
pub fn set_actions(&self, dnd_actions: DndAction) {
if self.inner.version() >= 3 {
self.inner.set_actions(dnd_actions);
}
self.inner.set_actions(dnd_actions);
}
pub fn inner(&self) -> &WlDataSource {
&self.inner
}
}
impl Drop for DragSource {
fn drop(&mut self) {
self.inner.destroy();
}
}