refactor: use rwh

This commit is contained in:
Ashley Wulber 2024-03-29 15:48:29 -04:00
parent 42a888736b
commit c9e7a8e2fa
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
5 changed files with 89 additions and 18 deletions

View file

@ -6,11 +6,12 @@ edition = "2021"
[dependencies]
mime = { path = "../mime" }
bitflags = "2.5.0"
raw-window-handle = "0.6"
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies]
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd", features = [
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd", features = [
"dnd",
] }
sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", default-features = false, features = [
"calloop",
] }
], rev = "3bed072" }

View file

@ -1,10 +1,11 @@
use std::{
borrow::Cow,
ffi::c_void,
fmt::Debug,
sync::{mpsc::SendError, Arc},
};
use bitflags::bitflags;
use raw_window_handle::HasWindowHandle;
#[cfg(all(
unix,
@ -30,7 +31,7 @@ bitflags! {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum DndEvent<T> {
/// Dnd Offer event with the corresponding destination rectangle ID.
Offer(Option<u128>, OfferEvent<T>),
@ -38,7 +39,19 @@ pub enum DndEvent<T> {
Source(SourceEvent),
}
#[derive(Debug)]
impl<T> PartialEq for DndEvent<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(DndEvent::Offer(a, b), DndEvent::Offer(a2, b2)) => {
a == a2 && b == b2
}
(DndEvent::Source(a), DndEvent::Source(b)) => a == b,
_ => false,
}
}
}
#[derive(Debug, Clone)]
pub enum SourceEvent {
/// DnD operation ended.
Finished,
@ -54,7 +67,20 @@ pub enum SourceEvent {
Dropped,
}
#[derive(Debug)]
impl PartialEq for SourceEvent {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(SourceEvent::Finished, SourceEvent::Finished)
| (SourceEvent::Cancelled, SourceEvent::Cancelled)
| (SourceEvent::Dropped, SourceEvent::Dropped) => true,
(SourceEvent::Action(a), SourceEvent::Action(b)) => a == b,
(SourceEvent::Mime(a), SourceEvent::Mime(b)) => a == b,
_ => false,
}
}
}
#[derive(Debug, Clone)]
pub enum OfferEvent<T> {
Enter {
x: f64,
@ -82,6 +108,45 @@ pub enum OfferEvent<T> {
},
}
impl<T> PartialEq for OfferEvent<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
OfferEvent::Enter {
x,
y,
mime_types,
surface: _,
},
OfferEvent::Enter {
x: x2,
y: y2,
mime_types: mime_types2,
surface: _,
},
) => x == x2 && y == y2 && mime_types == mime_types2,
(
OfferEvent::Motion { x, y },
OfferEvent::Motion { x: x2, y: y2 },
) => x == x2 && y == y2,
(OfferEvent::LeaveDestination, OfferEvent::LeaveDestination)
| (OfferEvent::Leave, OfferEvent::Leave)
| (OfferEvent::Drop, OfferEvent::Drop) => true,
(OfferEvent::SelectedAction(a), OfferEvent::SelectedAction(b)) => {
a == b
}
(
OfferEvent::Data { data, mime_type },
OfferEvent::Data {
data: data2,
mime_type: mime_type2,
},
) => data == data2 && mime_type == mime_type2,
_ => false,
}
}
}
/// A rectangle with a logical location and size relative to a [`DndSurface`]
#[derive(Debug, Default, Clone)]
pub struct Rectangle {
@ -96,14 +161,6 @@ pub trait Sender<T> {
fn send(&self, t: DndEvent<T>) -> Result<(), SendError<DndEvent<T>>>;
}
pub trait RawSurface {
/// # Safety
///
/// returned pointer must be a valid pointer to the underlying surface, and
/// it must remain valid for as long as `RawSurface` object is alive.
unsafe fn get_ptr(&mut self) -> *mut c_void;
}
/// A rectangle with a logical location and size relative to a [`DndSurface`]
#[derive(Debug, Clone)]
pub struct DndDestinationRectangle {
@ -132,7 +189,15 @@ pub enum Icon {
}
#[derive(Clone)]
pub struct DndSurface(pub Arc<Box<dyn RawSurface + 'static + Send + Sync>>);
pub struct DndSurface(
pub Arc<Box<dyn HasWindowHandle + 'static + Send + Sync>>,
);
impl Debug for DndSurface {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DndSurface").finish()
}
}
#[derive(Clone)]
pub struct DataWrapper<T>(pub T);

View file

@ -1,6 +1,7 @@
use std::{borrow::Cow, ffi::c_void, sync::Arc};
use crate::{DataWrapper, DndAction, DndSurface, Icon};
use raw_window_handle::HasWindowHandle;
use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType};
impl<
@ -46,7 +47,11 @@ impl smithay_clipboard::dnd::RawSurface for DndSurface {
unsafe fn get_ptr(&mut self) -> *mut c_void {
// XXX won't panic because this is only called once before it could be
// cloned
Arc::get_mut(&mut self.0).unwrap().get_ptr()
Arc::get_mut(&mut self.0)
.unwrap()
.window_handle()
.unwrap()
.get_ptr()
}
}

View file

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies]
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd" }
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd" }

View file

@ -11,7 +11,7 @@ keywords = ["clipboard", "wayland"]
[dependencies]
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd", features = [
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd", features = [
"dnd",
] }
mime = { path = "../mime" }