cacao/src/dragdrop.rs
Mads Marquart 094ed59a04
Initial conversion to objc2 (#30)
* Use objc2

* Replace `objc_id`

* Remove sel_impl import

* Fix `add_method` calls

* Fix accessing raw FFI functions

* Fix Encode impl

* Fix message sends arguments that do not implement `Encode`

* Use immutable reference in a few places where now necessary

See https://github.com/madsmtm/objc2/pull/150 for a bit of background

* Add a few Send + Sync bounds where examples require it

This is something we'll need to look into properly

* Use `&'static Class` instead of `*const Class`

Safer and more ergonomic. Also required for `msg_send_id!` macro

* Use msg_send_id! and rc::Id

* Update objc2 to v0.3.0-beta.2

* Replace `BOOL` with `Bool` when declaring delegates

This makes cacao compile on Aarch64 again

* Remove a few impossible to use correctly `into_inner` functions

These consumed `self`, and hence also dropped `Id` variable that was responsible for keeping the returned pointer alive

* Remove a few impossible to use correctly `From` implementations

* Quickly fix UB with using BACKGROUND_COLOR ivar

* Fix double-freeing of windows

* Fix double freeing of strings

* Fix a few remaining double-frees
2023-09-11 09:59:21 -07:00

68 lines
2 KiB
Rust

//! This module contains various bits and pieces for drag and drop operations. They're shared
//! across the codebase, hence why they're here - they're not currently exhaustive, so feel free to
//! tinker and pull request.
use objc::rc::{Id, Shared};
use objc::runtime::Object;
use objc::{msg_send, sel};
use crate::foundation::NSUInteger;
use crate::pasteboard::Pasteboard;
/// Represents operations that can happen for a given drag/drop scenario.
#[derive(Copy, Clone, Debug)]
pub enum DragOperation {
/// No drag operations are allowed.
None,
/// The data represented by the image can be copied.
Copy,
/// The data can be shared.
Link,
/// The operation can be defined by the destination.
Generic,
/// The operation is negotiated privately between the source and the destination.
Private,
/// The data can be moved.
Move,
/// The data can be deleted.
Delete // All of the above.
// @TODO: NSUIntegerMax, a tricky beast
// Every
}
impl From<DragOperation> for NSUInteger {
fn from(op: DragOperation) -> Self {
match op {
DragOperation::None => 0,
DragOperation::Copy => 1,
DragOperation::Link => 2,
DragOperation::Generic => 4,
DragOperation::Private => 8,
DragOperation::Move => 16,
DragOperation::Delete => 32
}
}
}
/// A wrapper for `NSDraggingInfo`. As this is a protocol/type you should never create yourself,
/// this only provides getters - merely a Rust-y way to grab what you need.
#[derive(Clone, Debug)]
pub struct DragInfo {
pub info: Id<Object, Shared>
}
impl DragInfo {
/// Returns a wrapped Pasteboard instance, enabling you to get the contents of whatever is
/// being pasted/dragged/dropped/etc.
///
/// Note: in general, you should not store pasteboards.
pub fn get_pasteboard(&self) -> Pasteboard {
unsafe { Pasteboard::with(msg_send![&*self.info, draggingPasteboard]) }
}
}