use std::cell::Cell;
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2::{define_class, msg_send, MainThreadMarker, MainThreadOnly};
use objc2_foundation::{NSPoint, NSRect};
use objc2_web_kit::{WKWebView, WKWebViewConfiguration};
thread_local! {
static DRAG_SUPPRESSED: Cell<bool> = const { Cell::new(false) };
}
define_class!(
#[unsafe(super = WKWebView)]
#[thread_kind = MainThreadOnly]
#[name = "VsHeadlessWebView"]
pub(super) struct HeadlessWebView;
impl HeadlessWebView {
#[unsafe(method(beginDraggingSessionWithItems:event:source:))]
fn begin_dragging_session(
&self,
_items: *mut AnyObject,
_event: *mut AnyObject,
_source: *mut AnyObject,
) -> *mut AnyObject {
DRAG_SUPPRESSED.with(|f| f.set(true));
std::ptr::null_mut()
}
}
);
impl HeadlessWebView {
pub(super) fn create(
mtm: MainThreadMarker,
frame: NSRect,
config: &WKWebViewConfiguration,
) -> Retained<WKWebView> {
let this: Retained<Self> = unsafe {
msg_send![
Self::alloc(mtm),
initWithFrame: frame,
configuration: config,
]
};
this.into_super()
}
}
pub(super) fn end_suppressed_drag(web_view: &WKWebView, at: NSPoint) {
if !DRAG_SUPPRESSED.with(|f| f.replace(false)) {
return;
}
let session: *mut AnyObject = std::ptr::null_mut();
unsafe {
let _: () = msg_send![
web_view,
draggingSession: session,
endedAtPoint: at,
operation: 0usize,
];
}
}