Skip to content

Commit b3cccb8

Browse files
committed
Trace sendEvent: calls in AppKit
Most events in AppKit go through `sendEvent:`, and they contain a lot of information, so it's nice to surface this when debugging. We could override `sendEvent:` in UIKit and track this in there too, but that's much less important, since there the relevant events are fairly narrowly scoped, see the link below, other events go through CFRunLoop. https://developer.apple.com/documentation/uikit/uievent/eventtype
1 parent 29eb0dd commit b3cccb8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

winit-appkit/src/app.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#![allow(clippy::unnecessary_cast)]
22

33
use std::cell::Cell;
4-
use std::mem;
54
use std::rc::Rc;
5+
use std::{mem, ptr};
66

77
use dispatch2::MainThreadBound;
88
use objc2::runtime::{Imp, Sel};
99
use objc2::sel;
1010
use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType};
1111
use objc2_foundation::MainThreadMarker;
12+
use tracing::trace_span;
1213
use winit_core::event::{DeviceEvent, ElementState};
1314

1415
use super::app_state::AppState;
@@ -21,6 +22,8 @@ static ORIGINAL: MainThreadBound<Cell<Option<SendEvent>>> = {
2122
};
2223

2324
extern "C-unwind" fn send_event(app: &NSApplication, sel: Sel, event: &NSEvent) {
25+
// Pass `RUST_LOG='trace,winit_appkit::app=off'` if you want TRACE logs but not this.
26+
let _entered = trace_span!("sendEvent:", ?event).entered();
2427
let mtm = MainThreadMarker::from(app);
2528

2629
// Normally, holding Cmd + any key never sends us a `keyUp` event for that key.
@@ -48,7 +51,7 @@ extern "C-unwind" fn send_event(app: &NSApplication, sel: Sel, event: &NSEvent)
4851
original(app, sel, event)
4952
}
5053

51-
/// Override the [`sendEvent:`][NSApplication::sendEvent] method on the given application class.
54+
/// Intercept the [`sendEvent:`][NSApplication::sendEvent] method on the given application class.
5255
///
5356
/// The previous implementation created a subclass of [`NSApplication`], however we would like to
5457
/// give the user full control over their `NSApplication`, so we override the method here using
@@ -75,9 +78,7 @@ pub(crate) fn override_send_event(global_app: &NSApplication) {
7578
let overridden = unsafe { mem::transmute::<SendEvent, Imp>(send_event) };
7679

7780
// If we've already overridden the method, don't do anything.
78-
// FIXME(madsmtm): Use `std::ptr::fn_addr_eq` (Rust 1.85) once available in MSRV.
79-
#[allow(unknown_lints, unpredictable_function_pointer_comparisons)]
80-
if overridden == method.implementation() {
81+
if ptr::fn_addr_eq(overridden, method.implementation()) {
8182
return;
8283
}
8384

0 commit comments

Comments
 (0)