From f5384a32c0a7cef10f1ffedea6b871b7b31a86cb Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 22 Mar 2024 19:08:47 -0400 Subject: [PATCH] chore: refactor dnd offer mimes --- examples/clipboard.rs | 5 ++++- src/dnd/mod.rs | 5 ++--- src/dnd/state.rs | 16 +++++----------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/examples/clipboard.rs b/examples/clipboard.rs index de751c1..6a41b84 100644 --- a/examples/clipboard.rs +++ b/examples/clipboard.rs @@ -78,7 +78,10 @@ fn main() { DndDestinationRectangle { id: 0, rectangle: Rectangle { x: 0., y: 0., width: 256., height: 256. }, - mime_types: ALLOWED_TEXT_MIME_TYPES.iter().map(|m| Cow::from(m.to_string())).collect(), + mime_types: ALLOWED_TEXT_MIME_TYPES + .iter() + .map(|m| MimeType::from(Cow::from(m.to_string()))) + .collect(), actions: DndAction::all(), preferred: DndAction::Copy, }, diff --git a/src/dnd/mod.rs b/src/dnd/mod.rs index 0e90ce7..99c80da 100644 --- a/src/dnd/mod.rs +++ b/src/dnd/mod.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::ffi::c_void; use std::fmt::Debug; use std::sync::mpsc::SendError; @@ -9,7 +8,7 @@ use sctk::reexports::client::protocol::wl_surface::WlSurface; use sctk::reexports::client::{Connection, Proxy}; use wayland_backend::client::{InvalidId, ObjectId}; -use crate::mime::AsMimeTypes; +use crate::mime::{AsMimeTypes, MimeType}; use crate::Clipboard; pub mod state; @@ -118,7 +117,7 @@ pub struct DndDestinationRectangle { /// The rectangle representing this destination. pub rectangle: Rectangle, /// Accepted mime types in this rectangle - pub mime_types: Vec>, + pub mime_types: Vec, /// Accepted actions in this rectangle pub actions: DndAction, /// Prefered action in this rectangle diff --git a/src/dnd/state.rs b/src/dnd/state.rs index 8d6d23b..3ac0d04 100644 --- a/src/dnd/state.rs +++ b/src/dnd/state.rs @@ -27,7 +27,7 @@ pub(crate) struct DndState { active_surface: Option<(DndSurface, Option)>, source_actions: DndAction, selected_action: DndAction, - selected_mime: Option, + selected_mime: Option, pub(crate) source_content: Box, pub(crate) source_mime_types: Rc>, } @@ -82,13 +82,10 @@ where }); let mime = dnd_state.as_ref().and_then(|dnd_state| { r.mime_types.iter().find(|m| { - dnd_state.with_mime_types(|mimes| mimes.iter().any(|a| &a == m)) + dnd_state.with_mime_types(|mimes| mimes.iter().any(|a| a == m.as_ref())) }) }); - dbg!(actions); - dbg!(mime); - (r.rectangle.contains(x, y) && (r.mime_types.is_empty() || mime.is_some()) && (r.actions.is_all() @@ -118,7 +115,7 @@ where (actions, mime, dnd_state.as_ref()) { dnd_state.set_actions(action, preferred_action); - self.dnd_state.selected_mime = Some(mime_type.to_string()); + self.dnd_state.selected_mime = Some(mime_type.clone()); dnd_state.accept_mime_type(dnd_state.serial, Some(mime_type.to_string())) } (s.clone(), Some(dest)) @@ -162,9 +159,9 @@ where }; dnd_state.set_actions(self.dnd_state.selected_action, self.dnd_state.selected_action); - dnd_state.accept_mime_type(dnd_state.serial, Some(mime.clone())); + dnd_state.accept_mime_type(dnd_state.serial, Some(mime.to_string())); - _ = self.load_dnd(MimeType::Other(mime.into())); + _ = self.load_dnd(mime); } pub(crate) fn offer_enter( @@ -267,9 +264,6 @@ where .get_mut(latest) .ok_or_else(|| Error::new(ErrorKind::Other, "active seat lost"))?; - if !seat.has_focus { - return Err(Error::new(ErrorKind::Other, "client doesn't have focus")); - } let offer = seat .data_device .as_ref()