Skip to content

Commit

Permalink
Data offer source actions (#372)
Browse files Browse the repository at this point in the history
* fix: track source actions before data_device enter event

* cleanup: remove unused variable
  • Loading branch information
wash2 authored Apr 5, 2023
1 parent 67b5b52 commit 389a4f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/data_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl DataDeviceWindow {

impl DataDeviceHandler for DataDeviceWindow {
fn enter(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, data_device: DataDevice) {
let mut drag_offer = data_device.drag_offer().unwrap();
let drag_offer = data_device.drag_offer().unwrap();
println!("data offer entered x: {:.2} y: {:.2}", drag_offer.x, drag_offer.y);

// accept the first mime type we support
Expand Down
25 changes: 13 additions & 12 deletions src/data_device_manager/data_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::{DataDeviceManagerState, ReadPipe};
#[derive(Debug, Clone)]
pub struct UndeterminedOffer {
pub(crate) data_offer: Option<WlDataOffer>,
pub actions: DndAction,
}
impl PartialEq for UndeterminedOffer {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -60,8 +61,6 @@ pub struct DragOffer {
pub y: f64,
/// the timestamp a motion event was received in millisecond granularity
pub time: Option<u32>,
/// accepted mime type
pub accepted_mime_type: Option<String>,
/// the advertised drag actions
pub source_actions: DndAction,
/// the compositor selected drag action
Expand Down Expand Up @@ -100,8 +99,7 @@ impl DragOffer {
/// Accept the given mime type, or None to reject the offer.
/// In version 2, this request is used for feedback, but doesn't affect the final result of the drag-and-drop operation.
/// In version 3, this request determines the final result of the drag-and-drop operation.
pub fn accept_mime_type(&mut self, serial: u32, mime_type: Option<String>) {
self.accepted_mime_type = mime_type.clone();
pub fn accept_mime_type(&self, serial: u32, mime_type: Option<String>) {
self.data_offer.accept(serial, mime_type);
}

Expand Down Expand Up @@ -162,7 +160,10 @@ pub enum DataDeviceOffer {

impl Default for DataDeviceOffer {
fn default() -> Self {
DataDeviceOffer::Undetermined(UndeterminedOffer { data_offer: None })
DataDeviceOffer::Undetermined(UndeterminedOffer {
data_offer: None,
actions: DndAction::empty(),
})
}
}

Expand Down Expand Up @@ -196,15 +197,15 @@ impl DataDeviceOffer {
receive(inner, mime_type).map_err(DataOfferError::Io)
}

pub fn accept_mime_type(&mut self, serial: u32, mime_type: Option<String>) {
pub fn accept_mime_type(&self, serial: u32, mime_type: Option<String>) {
match self {
DataDeviceOffer::Drag(o) => o.accept_mime_type(serial, mime_type),
DataDeviceOffer::Selection(_) => {}
DataDeviceOffer::Undetermined(_) => {} // error?
};
}

pub fn set_actions(&mut self, actions: DndAction, preferred_action: DndAction) {
pub fn set_actions(&self, actions: DndAction, preferred_action: DndAction) {
match self {
DataDeviceOffer::Drag(o) => o.set_actions(actions, preferred_action),
DataDeviceOffer::Selection(_) => {}
Expand Down Expand Up @@ -234,7 +235,7 @@ impl DataOfferData {
match &mut inner.deref_mut().offer {
DataDeviceOffer::Drag(ref mut o) => o.source_actions = action,
DataDeviceOffer::Selection(_) => {}
DataDeviceOffer::Undetermined(_) => {}
DataDeviceOffer::Undetermined(ref mut o) => o.actions = action,
};
}

Expand Down Expand Up @@ -266,14 +267,16 @@ impl DataOfferData {
pub(crate) fn init_undetermined_offer(&self, offer: &WlDataOffer) {
let mut inner = self.inner.lock().unwrap();
match &mut inner.deref_mut().offer {
DataDeviceOffer::Drag(_) => {
DataDeviceOffer::Drag(o) => {
inner.offer = DataDeviceOffer::Undetermined(UndeterminedOffer {
data_offer: Some(offer.clone()),
actions: o.source_actions,
});
}
DataDeviceOffer::Selection(_) => {
inner.offer = DataDeviceOffer::Undetermined(UndeterminedOffer {
data_offer: Some(offer.clone()),
actions: DndAction::empty(),
});
}
DataDeviceOffer::Undetermined(o) => {
Expand All @@ -296,7 +299,6 @@ impl DataOfferData {
DataDeviceOffer::Selection(o) => {
inner.offer = DataDeviceOffer::Drag(DragOffer {
data_offer: o.data_offer.clone(),
accepted_mime_type: None,
source_actions: DndAction::empty(),
selected_action: DndAction::empty(),
serial,
Expand All @@ -309,8 +311,7 @@ impl DataOfferData {
DataDeviceOffer::Undetermined(o) => {
inner.offer = DataDeviceOffer::Drag(DragOffer {
data_offer: o.data_offer.clone().unwrap(),
accepted_mime_type: None,
source_actions: DndAction::empty(),
source_actions: o.actions,
selected_action: DndAction::empty(),
serial,
surface,
Expand Down

0 comments on commit 389a4f2

Please # to comment.