Apple: Use tracing spans instead of custom trace_scope! macro

Spans are more powerful, and can even optionally be emitted as events by
using `.with_span_events(tracing_subscriber::fmt::format::FmtSpan::*)`.
This commit is contained in:
Mads Marquart
2026-02-18 03:01:07 +01:00
parent 7adb805011
commit ca7735f10b
7 changed files with 78 additions and 99 deletions

View File

@@ -99,7 +99,6 @@ impl AppState {
// NOTE: This notification will, globally, only be emitted once, // NOTE: This notification will, globally, only be emitted once,
// no matter how many `EventLoop`s the user creates. // no matter how many `EventLoop`s the user creates.
pub fn did_finish_launching(self: &Rc<Self>, _notification: &NSNotification) { pub fn did_finish_launching(self: &Rc<Self>, _notification: &NSNotification) {
trace_scope!("NSApplicationDidFinishLaunchingNotification");
self.is_launched.set(true); self.is_launched.set(true);
let app = NSApplication::sharedApplication(self.mtm); let app = NSApplication::sharedApplication(self.mtm);
@@ -154,7 +153,6 @@ impl AppState {
} }
pub fn will_terminate(self: &Rc<Self>, _notification: &NSNotification) { pub fn will_terminate(self: &Rc<Self>, _notification: &NSNotification) {
trace_scope!("NSApplicationWillTerminateNotification");
let app = NSApplication::sharedApplication(self.mtm); let app = NSApplication::sharedApplication(self.mtm);
notify_windows_of_exit(&app); notify_windows_of_exit(&app);
self.event_handler.terminate(); self.event_handler.terminate();

View File

@@ -12,6 +12,7 @@ use objc2_app_kit::{
use objc2_core_foundation::{CFIndex, CFRunLoopActivity, kCFRunLoopCommonModes}; use objc2_core_foundation::{CFIndex, CFRunLoopActivity, kCFRunLoopCommonModes};
use objc2_foundation::{NSNotificationCenter, NSObjectProtocol}; use objc2_foundation::{NSNotificationCenter, NSObjectProtocol};
use rwh_06::HasDisplayHandle; use rwh_06::HasDisplayHandle;
use tracing::debug_span;
use winit_common::core_foundation::{MainRunLoop, MainRunLoopObserver, tracing_observers}; use winit_common::core_foundation::{MainRunLoop, MainRunLoopObserver, tracing_observers};
use winit_core::application::ApplicationHandler; use winit_core::application::ApplicationHandler;
use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource};
@@ -204,6 +205,7 @@ impl EventLoop {
// `applicationDidFinishLaunching:` // `applicationDidFinishLaunching:`
unsafe { NSApplicationDidFinishLaunchingNotification }, unsafe { NSApplicationDidFinishLaunchingNotification },
move |notification| { move |notification| {
let _entered = debug_span!("NSApplicationDidFinishLaunchingNotification").entered();
if let Some(app_state) = weak_app_state.upgrade() { if let Some(app_state) = weak_app_state.upgrade() {
app_state.did_finish_launching(notification); app_state.did_finish_launching(notification);
} }
@@ -216,6 +218,7 @@ impl EventLoop {
// `applicationWillTerminate:` // `applicationWillTerminate:`
unsafe { NSApplicationWillTerminateNotification }, unsafe { NSApplicationWillTerminateNotification },
move |notification| { move |notification| {
let _entered = debug_span!("NSApplicationWillTerminateNotification").entered();
if let Some(app_state) = weak_app_state.upgrade() { if let Some(app_state) = weak_app_state.upgrade() {
app_state.will_terminate(notification); app_state.will_terminate(notification);
} }

View File

@@ -1,37 +1,10 @@
use objc2_core_graphics::CGError; use objc2_core_graphics::CGError;
use tracing::trace;
use winit_core::error::OsError; use winit_core::error::OsError;
macro_rules! os_error { macro_rules! os_error {
($error:expr) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }}; ($error:expr) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }};
} }
macro_rules! trace_scope {
($s:literal) => {
let _crate = $crate::util::TraceGuard::new(module_path!(), $s);
};
}
pub(crate) struct TraceGuard {
module_path: &'static str,
called_from_fn: &'static str,
}
impl TraceGuard {
#[inline]
pub(crate) fn new(module_path: &'static str, called_from_fn: &'static str) -> Self {
trace!(target = module_path, "Triggered `{}`", called_from_fn);
Self { module_path, called_from_fn }
}
}
impl Drop for TraceGuard {
#[inline]
fn drop(&mut self) {
trace!(target = self.module_path, "Completed `{}`", self.called_from_fn);
}
}
#[track_caller] #[track_caller]
pub(crate) fn cgerr(err: CGError) -> Result<(), OsError> { pub(crate) fn cgerr(err: CGError) -> Result<(), OsError> {
if err == CGError::Success { Ok(()) } else { Err(os_error!(format!("CGError {err:?}"))) } if err == CGError::Success { Ok(()) } else { Err(os_error!(format!("CGError {err:?}"))) }

View File

@@ -16,6 +16,7 @@ use objc2_foundation::{
NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString, NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString,
NSNotFound, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger, NSNotFound, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger,
}; };
use tracing::{debug_span, trace_span};
use winit_core::event::{ use winit_core::event::{
DeviceEvent, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, DeviceEvent, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta,
PointerKind, PointerSource, TouchPhase, WindowEvent, PointerKind, PointerSource, TouchPhase, WindowEvent,
@@ -153,7 +154,7 @@ define_class!(
// Not a normal method on `NSView`, it's triggered by `NSViewFrameDidChangeNotification`. // Not a normal method on `NSView`, it's triggered by `NSViewFrameDidChangeNotification`.
#[unsafe(method(viewFrameDidChangeNotification:))] #[unsafe(method(viewFrameDidChangeNotification:))]
fn frame_did_change(&self, _notification: Option<&AnyObject>) { fn frame_did_change(&self, _notification: Option<&AnyObject>) {
trace_scope!("NSViewFrameDidChangeNotification"); let _entered = debug_span!("NSViewFrameDidChangeNotification").entered();
// Emit resize event here rather than from windowDidResize because: // Emit resize event here rather than from windowDidResize because:
// 1. When a new window is created as a tab, the frame size may change without a window // 1. When a new window is created as a tab, the frame size may change without a window
@@ -168,7 +169,7 @@ define_class!(
#[unsafe(method(drawRect:))] #[unsafe(method(drawRect:))]
fn draw_rect(&self, _rect: NSRect) { fn draw_rect(&self, _rect: NSRect) {
trace_scope!("drawRect:"); let _entered = debug_span!("drawRect:").entered();
self.ivars().app_state.handle_redraw(window_id(&self.window())); self.ivars().app_state.handle_redraw(window_id(&self.window()));
@@ -177,7 +178,7 @@ define_class!(
#[unsafe(method(acceptsFirstResponder))] #[unsafe(method(acceptsFirstResponder))]
fn accepts_first_responder(&self) -> bool { fn accepts_first_responder(&self) -> bool {
trace_scope!("acceptsFirstResponder"); let _entered = trace_span!("acceptsFirstResponder").entered();
true true
} }
@@ -191,13 +192,13 @@ define_class!(
// extension for using `NSTouchBar` // extension for using `NSTouchBar`
#[unsafe(method_id(touchBar))] #[unsafe(method_id(touchBar))]
fn touch_bar(&self) -> Option<Retained<NSObject>> { fn touch_bar(&self) -> Option<Retained<NSObject>> {
trace_scope!("touchBar"); let _entered = debug_span!("touchBar").entered();
None None
} }
#[unsafe(method(resetCursorRects))] #[unsafe(method(resetCursorRects))]
fn reset_cursor_rects(&self) { fn reset_cursor_rects(&self) {
trace_scope!("resetCursorRects"); let _entered = debug_span!("resetCursorRects").entered();
let bounds = self.bounds(); let bounds = self.bounds();
let cursor_state = self.ivars().cursor_state.borrow(); let cursor_state = self.ivars().cursor_state.borrow();
// We correctly invoke `addCursorRect` only from inside `resetCursorRects` // We correctly invoke `addCursorRect` only from inside `resetCursorRects`
@@ -212,13 +213,13 @@ define_class!(
unsafe impl NSTextInputClient for WinitView { unsafe impl NSTextInputClient for WinitView {
#[unsafe(method(hasMarkedText))] #[unsafe(method(hasMarkedText))]
fn has_marked_text(&self) -> bool { fn has_marked_text(&self) -> bool {
trace_scope!("hasMarkedText"); let _entered = debug_span!("hasMarkedText").entered();
self.ivars().marked_text.borrow().length() > 0 self.ivars().marked_text.borrow().length() > 0
} }
#[unsafe(method(markedRange))] #[unsafe(method(markedRange))]
fn marked_range(&self) -> NSRange { fn marked_range(&self) -> NSRange {
trace_scope!("markedRange"); let _entered = debug_span!("markedRange").entered();
let length = self.ivars().marked_text.borrow().length(); let length = self.ivars().marked_text.borrow().length();
if length > 0 { if length > 0 {
NSRange::new(0, length) NSRange::new(0, length)
@@ -230,7 +231,7 @@ define_class!(
#[unsafe(method(selectedRange))] #[unsafe(method(selectedRange))]
fn selected_range(&self) -> NSRange { fn selected_range(&self) -> NSRange {
trace_scope!("selectedRange"); let _entered = debug_span!("selectedRange").entered();
// Documented to return `{NSNotFound, 0}` if there is no selection. // Documented to return `{NSNotFound, 0}` if there is no selection.
NSRange::new(NSNotFound as NSUInteger, 0) NSRange::new(NSNotFound as NSUInteger, 0)
} }
@@ -243,7 +244,7 @@ define_class!(
_replacement_range: NSRange, _replacement_range: NSRange,
) { ) {
// TODO: Use _replacement_range, requires changing the event to report surrounding text. // TODO: Use _replacement_range, requires changing the event to report surrounding text.
trace_scope!("setMarkedText:selectedRange:replacementRange:"); let _entered = debug_span!("setMarkedText:selectedRange:replacementRange:").entered();
let (marked_text, string) = if let Some(string) = let (marked_text, string) = if let Some(string) =
string.downcast_ref::<NSAttributedString>() string.downcast_ref::<NSAttributedString>()
@@ -297,7 +298,7 @@ define_class!(
#[unsafe(method(unmarkText))] #[unsafe(method(unmarkText))]
fn unmark_text(&self) { fn unmark_text(&self) {
trace_scope!("unmarkText"); let _entered = debug_span!("unmarkText").entered();
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new(); *self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
let input_context = self.inputContext().expect("input context"); let input_context = self.inputContext().expect("input context");
@@ -314,7 +315,7 @@ define_class!(
#[unsafe(method_id(validAttributesForMarkedText))] #[unsafe(method_id(validAttributesForMarkedText))]
fn valid_attributes_for_marked_text(&self) -> Retained<NSArray<NSAttributedStringKey>> { fn valid_attributes_for_marked_text(&self) -> Retained<NSArray<NSAttributedStringKey>> {
trace_scope!("validAttributesForMarkedText"); let _entered = trace_span!("validAttributesForMarkedText").entered();
NSArray::new() NSArray::new()
} }
@@ -324,13 +325,14 @@ define_class!(
_range: NSRange, _range: NSRange,
_actual_range: *mut NSRange, _actual_range: *mut NSRange,
) -> Option<Retained<NSAttributedString>> { ) -> Option<Retained<NSAttributedString>> {
trace_scope!("attributedSubstringForProposedRange:actualRange:"); let _entered =
trace_span!("attributedSubstringForProposedRange:actualRange:").entered();
None None
} }
#[unsafe(method(characterIndexForPoint:))] #[unsafe(method(characterIndexForPoint:))]
fn character_index_for_point(&self, _point: NSPoint) -> NSUInteger { fn character_index_for_point(&self, _point: NSPoint) -> NSUInteger {
trace_scope!("characterIndexForPoint:"); let _entered = debug_span!("characterIndexForPoint:").entered();
0 0
} }
@@ -340,7 +342,7 @@ define_class!(
_range: NSRange, _range: NSRange,
_actual_range: *mut NSRange, _actual_range: *mut NSRange,
) -> NSRect { ) -> NSRect {
trace_scope!("firstRectForCharacterRange:actualRange:"); let _entered = debug_span!("firstRectForCharacterRange:actualRange:").entered();
// Guard when the view is no longer in a window during teardown. // Guard when the view is no longer in a window during teardown.
let Some(window) = (**self).window() else { let Some(window) = (**self).window() else {
@@ -356,7 +358,7 @@ define_class!(
#[unsafe(method(insertText:replacementRange:))] #[unsafe(method(insertText:replacementRange:))]
fn insert_text(&self, string: &NSObject, _replacement_range: NSRange) { fn insert_text(&self, string: &NSObject, _replacement_range: NSRange) {
// TODO: Use _replacement_range, requires changing the event to report surrounding text. // TODO: Use _replacement_range, requires changing the event to report surrounding text.
trace_scope!("insertText:replacementRange:"); let _entered = debug_span!("insertText:replacementRange:").entered();
let string = if let Some(string) = string.downcast_ref::<NSAttributedString>() { let string = if let Some(string) = string.downcast_ref::<NSAttributedString>() {
string.string().to_string() string.string().to_string()
@@ -381,7 +383,7 @@ define_class!(
// "human readable" character happens, i.e. newlines, tabs, and Ctrl+C. // "human readable" character happens, i.e. newlines, tabs, and Ctrl+C.
#[unsafe(method(doCommandBySelector:))] #[unsafe(method(doCommandBySelector:))]
fn do_command_by_selector(&self, command: Sel) { fn do_command_by_selector(&self, command: Sel) {
trace_scope!("doCommandBySelector:"); let _entered = debug_span!("doCommandBySelector:").entered();
// We shouldn't forward any character from just committed text, since we'll end up // We shouldn't forward any character from just committed text, since we'll end up
// sending it twice with some IMEs like Korean one. We'll also always send // sending it twice with some IMEs like Korean one. We'll also always send
@@ -420,7 +422,7 @@ define_class!(
impl WinitView { impl WinitView {
#[unsafe(method(keyDown:))] #[unsafe(method(keyDown:))]
fn key_down(&self, event: &NSEvent) { fn key_down(&self, event: &NSEvent) {
trace_scope!("keyDown:"); let _entered = debug_span!("keyDown:").entered();
{ {
let mut prev_input_source = self.ivars().input_source.borrow_mut(); let mut prev_input_source = self.ivars().input_source.borrow_mut();
let current_input_source = self.current_input_source(); let current_input_source = self.current_input_source();
@@ -479,7 +481,7 @@ define_class!(
#[unsafe(method(keyUp:))] #[unsafe(method(keyUp:))]
fn key_up(&self, event: &NSEvent) { fn key_up(&self, event: &NSEvent) {
trace_scope!("keyUp:"); let _entered = debug_span!("keyUp:").entered();
let event = replace_event(event, self.option_as_alt()); let event = replace_event(event, self.option_as_alt());
self.update_modifiers(&event, false); self.update_modifiers(&event, false);
@@ -496,14 +498,14 @@ define_class!(
#[unsafe(method(flagsChanged:))] #[unsafe(method(flagsChanged:))]
fn flags_changed(&self, event: &NSEvent) { fn flags_changed(&self, event: &NSEvent) {
trace_scope!("flagsChanged:"); let _entered = debug_span!("flagsChanged:").entered();
self.update_modifiers(event, true); self.update_modifiers(event, true);
} }
#[unsafe(method(insertTab:))] #[unsafe(method(insertTab:))]
fn insert_tab(&self, _sender: Option<&AnyObject>) { fn insert_tab(&self, _sender: Option<&AnyObject>) {
trace_scope!("insertTab:"); let _entered = debug_span!("insertTab:").entered();
let window = self.window(); let window = self.window();
if let Some(first_responder) = window.firstResponder() { if let Some(first_responder) = window.firstResponder() {
if *first_responder == ***self { if *first_responder == ***self {
@@ -514,7 +516,7 @@ define_class!(
#[unsafe(method(insertBackTab:))] #[unsafe(method(insertBackTab:))]
fn insert_back_tab(&self, _sender: Option<&AnyObject>) { fn insert_back_tab(&self, _sender: Option<&AnyObject>) {
trace_scope!("insertBackTab:"); let _entered = debug_span!("insertBackTab:").entered();
let window = self.window(); let window = self.window();
if let Some(first_responder) = window.firstResponder() { if let Some(first_responder) = window.firstResponder() {
if *first_responder == ***self { if *first_responder == ***self {
@@ -528,7 +530,7 @@ define_class!(
#[unsafe(method(cancelOperation:))] #[unsafe(method(cancelOperation:))]
fn cancel_operation(&self, _sender: Option<&AnyObject>) { fn cancel_operation(&self, _sender: Option<&AnyObject>) {
let mtm = MainThreadMarker::from(self); let mtm = MainThreadMarker::from(self);
trace_scope!("cancelOperation:"); let _entered = debug_span!("cancelOperation:").entered();
let event = NSApplication::sharedApplication(mtm) let event = NSApplication::sharedApplication(mtm)
.currentEvent() .currentEvent()
@@ -557,71 +559,73 @@ define_class!(
#[unsafe(method(mouseDown:))] #[unsafe(method(mouseDown:))]
fn mouse_down(&self, event: &NSEvent) { fn mouse_down(&self, event: &NSEvent) {
trace_scope!("mouseDown:"); let _entered = debug_span!("mouseDown:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[unsafe(method(mouseUp:))] #[unsafe(method(mouseUp:))]
fn mouse_up(&self, event: &NSEvent) { fn mouse_up(&self, event: &NSEvent) {
trace_scope!("mouseUp:"); let _entered = debug_span!("mouseUp:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Released); self.mouse_click(event, ElementState::Released);
} }
#[unsafe(method(rightMouseDown:))] #[unsafe(method(rightMouseDown:))]
fn right_mouse_down(&self, event: &NSEvent) { fn right_mouse_down(&self, event: &NSEvent) {
trace_scope!("rightMouseDown:"); let _entered = debug_span!("rightMouseDown:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[unsafe(method(rightMouseUp:))] #[unsafe(method(rightMouseUp:))]
fn right_mouse_up(&self, event: &NSEvent) { fn right_mouse_up(&self, event: &NSEvent) {
trace_scope!("rightMouseUp:"); let _entered = debug_span!("rightMouseUp:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Released); self.mouse_click(event, ElementState::Released);
} }
#[unsafe(method(otherMouseDown:))] #[unsafe(method(otherMouseDown:))]
fn other_mouse_down(&self, event: &NSEvent) { fn other_mouse_down(&self, event: &NSEvent) {
trace_scope!("otherMouseDown:"); let _entered = debug_span!("otherMouseDown:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[unsafe(method(otherMouseUp:))] #[unsafe(method(otherMouseUp:))]
fn other_mouse_up(&self, event: &NSEvent) { fn other_mouse_up(&self, event: &NSEvent) {
trace_scope!("otherMouseUp:"); let _entered = debug_span!("otherMouseUp:").entered();
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, ElementState::Released); self.mouse_click(event, ElementState::Released);
} }
// No tracing on these because that would be overly verbose
#[unsafe(method(mouseMoved:))] #[unsafe(method(mouseMoved:))]
fn mouse_moved(&self, event: &NSEvent) { fn mouse_moved(&self, event: &NSEvent) {
let _entered = debug_span!("mouseMoved:").entered();
self.mouse_motion(event); self.mouse_motion(event);
} }
#[unsafe(method(mouseDragged:))] #[unsafe(method(mouseDragged:))]
fn mouse_dragged(&self, event: &NSEvent) { fn mouse_dragged(&self, event: &NSEvent) {
let _entered = debug_span!("mouseDragged:").entered();
self.mouse_motion(event); self.mouse_motion(event);
} }
#[unsafe(method(rightMouseDragged:))] #[unsafe(method(rightMouseDragged:))]
fn right_mouse_dragged(&self, event: &NSEvent) { fn right_mouse_dragged(&self, event: &NSEvent) {
let _entered = debug_span!("rightMouseDragged:").entered();
self.mouse_motion(event); self.mouse_motion(event);
} }
#[unsafe(method(otherMouseDragged:))] #[unsafe(method(otherMouseDragged:))]
fn other_mouse_dragged(&self, event: &NSEvent) { fn other_mouse_dragged(&self, event: &NSEvent) {
let _entered = debug_span!("otherMouseDragged:").entered();
self.mouse_motion(event); self.mouse_motion(event);
} }
#[unsafe(method(mouseEntered:))] #[unsafe(method(mouseEntered:))]
fn mouse_entered(&self, event: &NSEvent) { fn mouse_entered(&self, event: &NSEvent) {
trace_scope!("mouseEntered:"); let _entered = debug_span!("mouseEntered:").entered();
let position = self.mouse_view_point(event).to_physical(self.scale_factor()); let position = self.mouse_view_point(event).to_physical(self.scale_factor());
@@ -635,7 +639,7 @@ define_class!(
#[unsafe(method(mouseExited:))] #[unsafe(method(mouseExited:))]
fn mouse_exited(&self, event: &NSEvent) { fn mouse_exited(&self, event: &NSEvent) {
trace_scope!("mouseExited:"); let _entered = debug_span!("mouseExited:").entered();
let position = self.mouse_view_point(event).to_physical(self.scale_factor()); let position = self.mouse_view_point(event).to_physical(self.scale_factor());
@@ -649,7 +653,7 @@ define_class!(
#[unsafe(method(scrollWheel:))] #[unsafe(method(scrollWheel:))]
fn scroll_wheel(&self, event: &NSEvent) { fn scroll_wheel(&self, event: &NSEvent) {
trace_scope!("scrollWheel:"); let _entered = debug_span!("scrollWheel:").entered();
self.mouse_motion(event); self.mouse_motion(event);
@@ -688,7 +692,7 @@ define_class!(
#[unsafe(method(magnifyWithEvent:))] #[unsafe(method(magnifyWithEvent:))]
fn magnify_with_event(&self, event: &NSEvent) { fn magnify_with_event(&self, event: &NSEvent) {
trace_scope!("magnifyWithEvent:"); let _entered = debug_span!("magnifyWithEvent:").entered();
self.mouse_motion(event); self.mouse_motion(event);
@@ -710,7 +714,7 @@ define_class!(
#[unsafe(method(smartMagnifyWithEvent:))] #[unsafe(method(smartMagnifyWithEvent:))]
fn smart_magnify_with_event(&self, event: &NSEvent) { fn smart_magnify_with_event(&self, event: &NSEvent) {
trace_scope!("smartMagnifyWithEvent:"); let _entered = debug_span!("smartMagnifyWithEvent:").entered();
self.mouse_motion(event); self.mouse_motion(event);
@@ -719,7 +723,7 @@ define_class!(
#[unsafe(method(rotateWithEvent:))] #[unsafe(method(rotateWithEvent:))]
fn rotate_with_event(&self, event: &NSEvent) { fn rotate_with_event(&self, event: &NSEvent) {
trace_scope!("rotateWithEvent:"); let _entered = debug_span!("rotateWithEvent:").entered();
self.mouse_motion(event); self.mouse_motion(event);
@@ -741,7 +745,7 @@ define_class!(
#[unsafe(method(pressureChangeWithEvent:))] #[unsafe(method(pressureChangeWithEvent:))]
fn pressure_change_with_event(&self, event: &NSEvent) { fn pressure_change_with_event(&self, event: &NSEvent) {
trace_scope!("pressureChangeWithEvent:"); let _entered = debug_span!("pressureChangeWithEvent:").entered();
self.queue_event(WindowEvent::TouchpadPressure { self.queue_event(WindowEvent::TouchpadPressure {
device_id: None, device_id: None,
@@ -755,13 +759,13 @@ define_class!(
// https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816 // https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816
#[unsafe(method(_wantsKeyDownForEvent:))] #[unsafe(method(_wantsKeyDownForEvent:))]
fn wants_key_down_for_event(&self, _event: &NSEvent) -> bool { fn wants_key_down_for_event(&self, _event: &NSEvent) -> bool {
trace_scope!("_wantsKeyDownForEvent:"); let _entered = debug_span!("_wantsKeyDownForEvent:").entered();
true true
} }
#[unsafe(method(acceptsFirstMouse:))] #[unsafe(method(acceptsFirstMouse:))]
fn accepts_first_mouse(&self, _event: &NSEvent) -> bool { fn accepts_first_mouse(&self, _event: &NSEvent) -> bool {
trace_scope!("acceptsFirstMouse:"); let _entered = debug_span!("acceptsFirstMouse:").entered();
self.ivars().accepts_first_mouse self.ivars().accepts_first_mouse
} }
} }

View File

@@ -8,6 +8,7 @@ use objc2::rc::{Retained, autoreleasepool};
use objc2::{MainThreadMarker, Message, define_class}; use objc2::{MainThreadMarker, Message, define_class};
use objc2_app_kit::{NSPanel, NSResponder, NSWindow}; use objc2_app_kit::{NSPanel, NSResponder, NSWindow};
use objc2_foundation::NSObject; use objc2_foundation::NSObject;
use tracing::trace_span;
use winit_core::cursor::Cursor; use winit_core::cursor::Cursor;
use winit_core::error::RequestError; use winit_core::error::RequestError;
use winit_core::icon::Icon; use winit_core::icon::Icon;
@@ -350,13 +351,13 @@ define_class!(
impl WinitWindow { impl WinitWindow {
#[unsafe(method(canBecomeMainWindow))] #[unsafe(method(canBecomeMainWindow))]
fn can_become_main_window(&self) -> bool { fn can_become_main_window(&self) -> bool {
trace_scope!("canBecomeMainWindow"); let _entered = trace_span!("canBecomeMainWindow").entered();
true true
} }
#[unsafe(method(canBecomeKeyWindow))] #[unsafe(method(canBecomeKeyWindow))]
fn can_become_key_window(&self) -> bool { fn can_become_key_window(&self) -> bool {
trace_scope!("canBecomeKeyWindow"); let _entered = trace_span!("canBecomeKeyWindow").entered();
true true
} }
} }
@@ -374,7 +375,7 @@ define_class!(
// it doesn't if window doesn't have NSWindowStyleMask::Titled // it doesn't if window doesn't have NSWindowStyleMask::Titled
#[unsafe(method(canBecomeKeyWindow))] #[unsafe(method(canBecomeKeyWindow))]
fn can_become_key_window(&self) -> bool { fn can_become_key_window(&self) -> bool {
trace_scope!("canBecomeKeyWindow"); let _entered = trace_span!("canBecomeKeyWindow").entered();
true true
} }
} }

View File

@@ -41,7 +41,7 @@ use objc2_foundation::{
NSObjectNSDelayedPerforming, NSObjectNSKeyValueObserverRegistration, NSObjectProtocol, NSPoint, NSObjectNSDelayedPerforming, NSObjectNSKeyValueObserverRegistration, NSObjectProtocol, NSPoint,
NSRect, NSSize, NSString, ns_string, NSRect, NSSize, NSString, ns_string,
}; };
use tracing::{trace, warn}; use tracing::{debug_span, trace, warn};
use winit_common::core_foundation::MainRunLoop; use winit_common::core_foundation::MainRunLoop;
use winit_core::cursor::Cursor; use winit_core::cursor::Cursor;
use winit_core::error::{NotSupportedError, RequestError}; use winit_core::error::{NotSupportedError, RequestError};
@@ -121,14 +121,14 @@ define_class!(
unsafe impl NSWindowDelegate for WindowDelegate { unsafe impl NSWindowDelegate for WindowDelegate {
#[unsafe(method(windowShouldClose:))] #[unsafe(method(windowShouldClose:))]
fn window_should_close(&self, _: Option<&AnyObject>) -> bool { fn window_should_close(&self, _: Option<&AnyObject>) -> bool {
trace_scope!("windowShouldClose:"); let _entered = debug_span!("windowShouldClose:").entered();
self.queue_event(WindowEvent::CloseRequested); self.queue_event(WindowEvent::CloseRequested);
false false
} }
#[unsafe(method(windowWillClose:))] #[unsafe(method(windowWillClose:))]
fn window_will_close(&self, _: Option<&AnyObject>) { fn window_will_close(&self, _: Option<&AnyObject>) {
trace_scope!("windowWillClose:"); let _entered = debug_span!("windowWillClose:").entered();
// `setDelegate:` retains the previous value and then autoreleases it // `setDelegate:` retains the previous value and then autoreleases it
autoreleasepool(|_| { autoreleasepool(|_| {
// Since El Capitan, we need to be careful that delegate methods can't // Since El Capitan, we need to be careful that delegate methods can't
@@ -140,14 +140,14 @@ define_class!(
#[unsafe(method(windowDidResize:))] #[unsafe(method(windowDidResize:))]
fn window_did_resize(&self, _: Option<&AnyObject>) { fn window_did_resize(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidResize:"); let _entered = debug_span!("windowDidResize:").entered();
// NOTE: WindowEvent::SurfaceResized is reported using NSViewFrameDidChangeNotification. // NOTE: WindowEvent::SurfaceResized is reported using NSViewFrameDidChangeNotification.
self.emit_move_event(); self.emit_move_event();
} }
#[unsafe(method(windowWillStartLiveResize:))] #[unsafe(method(windowWillStartLiveResize:))]
fn window_will_start_live_resize(&self, _: Option<&AnyObject>) { fn window_will_start_live_resize(&self, _: Option<&AnyObject>) {
trace_scope!("windowWillStartLiveResize:"); let _entered = debug_span!("windowWillStartLiveResize:").entered();
let increments = self.ivars().surface_resize_increments.get(); let increments = self.ivars().surface_resize_increments.get();
self.set_resize_increments_inner(increments); self.set_resize_increments_inner(increments);
@@ -155,20 +155,20 @@ define_class!(
#[unsafe(method(windowDidEndLiveResize:))] #[unsafe(method(windowDidEndLiveResize:))]
fn window_did_end_live_resize(&self, _: Option<&AnyObject>) { fn window_did_end_live_resize(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidEndLiveResize:"); let _entered = debug_span!("windowDidEndLiveResize:").entered();
self.set_resize_increments_inner(NSSize::new(1., 1.)); self.set_resize_increments_inner(NSSize::new(1., 1.));
} }
// This won't be triggered if the move was part of a resize. // This won't be triggered if the move was part of a resize.
#[unsafe(method(windowDidMove:))] #[unsafe(method(windowDidMove:))]
fn window_did_move(&self, _: Option<&AnyObject>) { fn window_did_move(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidMove:"); let _entered = debug_span!("windowDidMove:").entered();
self.emit_move_event(); self.emit_move_event();
} }
#[unsafe(method(windowDidChangeBackingProperties:))] #[unsafe(method(windowDidChangeBackingProperties:))]
fn window_did_change_backing_properties(&self, _: Option<&AnyObject>) { fn window_did_change_backing_properties(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidChangeBackingProperties:"); let _entered = debug_span!("windowDidChangeBackingProperties:").entered();
let scale_factor = self.scale_factor(); let scale_factor = self.scale_factor();
if scale_factor == self.ivars().previous_scale_factor.get() { if scale_factor == self.ivars().previous_scale_factor.get() {
return; return;
@@ -184,7 +184,7 @@ define_class!(
#[unsafe(method(windowDidBecomeKey:))] #[unsafe(method(windowDidBecomeKey:))]
fn window_did_become_key(&self, _: Option<&AnyObject>) { fn window_did_become_key(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidBecomeKey:"); let _entered = debug_span!("windowDidBecomeKey:").entered();
// TODO: center the cursor if the window had mouse grab when it // TODO: center the cursor if the window had mouse grab when it
// lost focus // lost focus
self.queue_event(WindowEvent::Focused(true)); self.queue_event(WindowEvent::Focused(true));
@@ -192,7 +192,7 @@ define_class!(
#[unsafe(method(windowDidResignKey:))] #[unsafe(method(windowDidResignKey:))]
fn window_did_resign_key(&self, _: Option<&AnyObject>) { fn window_did_resign_key(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidResignKey:"); let _entered = debug_span!("windowDidResignKey:").entered();
// It happens rather often, e.g. when the user is Cmd+Tabbing, that the // It happens rather often, e.g. when the user is Cmd+Tabbing, that the
// NSWindowDelegate will receive a didResignKey event despite no event // NSWindowDelegate will receive a didResignKey event despite no event
// being received when the modifiers are released. This is because // being received when the modifiers are released. This is because
@@ -208,7 +208,7 @@ define_class!(
/// Invoked when before enter fullscreen /// Invoked when before enter fullscreen
#[unsafe(method(windowWillEnterFullScreen:))] #[unsafe(method(windowWillEnterFullScreen:))]
fn window_will_enter_fullscreen(&self, _: Option<&AnyObject>) { fn window_will_enter_fullscreen(&self, _: Option<&AnyObject>) {
trace_scope!("windowWillEnterFullScreen:"); let _entered = debug_span!("windowWillEnterFullScreen:").entered();
self.ivars().maximized.set(self.is_zoomed()); self.ivars().maximized.set(self.is_zoomed());
let mut fullscreen = self.ivars().fullscreen.borrow_mut(); let mut fullscreen = self.ivars().fullscreen.borrow_mut();
@@ -236,7 +236,7 @@ define_class!(
/// Invoked when before exit fullscreen /// Invoked when before exit fullscreen
#[unsafe(method(windowWillExitFullScreen:))] #[unsafe(method(windowWillExitFullScreen:))]
fn window_will_exit_fullscreen(&self, _: Option<&AnyObject>) { fn window_will_exit_fullscreen(&self, _: Option<&AnyObject>) {
trace_scope!("windowWillExitFullScreen:"); let _entered = debug_span!("windowWillExitFullScreen:").entered();
self.ivars().in_fullscreen_transition.set(true); self.ivars().in_fullscreen_transition.set(true);
} }
@@ -247,7 +247,7 @@ define_class!(
_: Option<&AnyObject>, _: Option<&AnyObject>,
proposed_options: NSApplicationPresentationOptions, proposed_options: NSApplicationPresentationOptions,
) -> NSApplicationPresentationOptions { ) -> NSApplicationPresentationOptions {
trace_scope!("window:willUseFullScreenPresentationOptions:"); let _entered = debug_span!("window:willUseFullScreenPresentationOptions:").entered();
// Generally, games will want to disable the menu bar and the dock. Ideally, // Generally, games will want to disable the menu bar and the dock. Ideally,
// this would be configurable by the user. Unfortunately because of our // this would be configurable by the user. Unfortunately because of our
// `CGShieldingWindowLevel() + 1` hack (see `set_fullscreen`), our window is // `CGShieldingWindowLevel() + 1` hack (see `set_fullscreen`), our window is
@@ -270,7 +270,7 @@ define_class!(
/// Invoked when entered fullscreen /// Invoked when entered fullscreen
#[unsafe(method(windowDidEnterFullScreen:))] #[unsafe(method(windowDidEnterFullScreen:))]
fn window_did_enter_fullscreen(&self, _: Option<&AnyObject>) { fn window_did_enter_fullscreen(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidEnterFullScreen:"); let _entered = debug_span!("windowDidEnterFullScreen:").entered();
self.ivars().initial_fullscreen.set(false); self.ivars().initial_fullscreen.set(false);
self.ivars().in_fullscreen_transition.set(false); self.ivars().in_fullscreen_transition.set(false);
if let Some(target_fullscreen) = self.ivars().target_fullscreen.take() { if let Some(target_fullscreen) = self.ivars().target_fullscreen.take() {
@@ -281,7 +281,7 @@ define_class!(
/// Invoked when exited fullscreen /// Invoked when exited fullscreen
#[unsafe(method(windowDidExitFullScreen:))] #[unsafe(method(windowDidExitFullScreen:))]
fn window_did_exit_fullscreen(&self, _: Option<&AnyObject>) { fn window_did_exit_fullscreen(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidExitFullScreen:"); let _entered = debug_span!("windowDidExitFullScreen:").entered();
self.restore_state_from_fullscreen(); self.restore_state_from_fullscreen();
self.ivars().in_fullscreen_transition.set(false); self.ivars().in_fullscreen_transition.set(false);
@@ -308,7 +308,7 @@ define_class!(
/// work you may have done to prepare to enter full-screen mode. /// work you may have done to prepare to enter full-screen mode.
#[unsafe(method(windowDidFailToEnterFullScreen:))] #[unsafe(method(windowDidFailToEnterFullScreen:))]
fn window_did_fail_to_enter_fullscreen(&self, _: Option<&AnyObject>) { fn window_did_fail_to_enter_fullscreen(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidFailToEnterFullScreen:"); let _entered = debug_span!("windowDidFailToEnterFullScreen:").entered();
self.ivars().in_fullscreen_transition.set(false); self.ivars().in_fullscreen_transition.set(false);
self.ivars().target_fullscreen.replace(None); self.ivars().target_fullscreen.replace(None);
if self.ivars().initial_fullscreen.get() { if self.ivars().initial_fullscreen.get() {
@@ -327,7 +327,7 @@ define_class!(
// Invoked when the occlusion state of the window changes // Invoked when the occlusion state of the window changes
#[unsafe(method(windowDidChangeOcclusionState:))] #[unsafe(method(windowDidChangeOcclusionState:))]
fn window_did_change_occlusion_state(&self, _: Option<&AnyObject>) { fn window_did_change_occlusion_state(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidChangeOcclusionState:"); let _entered = debug_span!("windowDidChangeOcclusionState:").entered();
let visible = self.window().occlusionState().contains(NSWindowOcclusionState::Visible); let visible = self.window().occlusionState().contains(NSWindowOcclusionState::Visible);
self.queue_event(WindowEvent::Occluded(!visible)); self.queue_event(WindowEvent::Occluded(!visible));
@@ -348,7 +348,7 @@ define_class!(
#[unsafe(method(windowDidChangeScreen:))] #[unsafe(method(windowDidChangeScreen:))]
fn window_did_change_screen(&self, _: Option<&AnyObject>) { fn window_did_change_screen(&self, _: Option<&AnyObject>) {
trace_scope!("windowDidChangeScreen:"); let _entered = debug_span!("windowDidChangeScreen:").entered();
let is_simple_fullscreen = self.ivars().is_simple_fullscreen.get(); let is_simple_fullscreen = self.ivars().is_simple_fullscreen.get();
if is_simple_fullscreen { if is_simple_fullscreen {
if let Some(screen) = self.window().screen() { if let Some(screen) = self.window().screen() {
@@ -362,7 +362,7 @@ define_class!(
/// Invoked when the dragged image enters destination bounds or frame /// Invoked when the dragged image enters destination bounds or frame
#[unsafe(method(draggingEntered:))] #[unsafe(method(draggingEntered:))]
fn dragging_entered(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool { fn dragging_entered(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
trace_scope!("draggingEntered:"); let _entered = debug_span!("draggingEntered:").entered();
use std::path::PathBuf; use std::path::PathBuf;
@@ -393,7 +393,7 @@ define_class!(
#[unsafe(method(wantsPeriodicDraggingUpdates))] #[unsafe(method(wantsPeriodicDraggingUpdates))]
fn wants_periodic_dragging_updates(&self) -> bool { fn wants_periodic_dragging_updates(&self) -> bool {
trace_scope!("wantsPeriodicDraggingUpdates:"); let _entered = debug_span!("wantsPeriodicDraggingUpdates:").entered();
true true
} }
@@ -401,7 +401,7 @@ define_class!(
/// modification of the dragging operation or mouse-pointer position. /// modification of the dragging operation or mouse-pointer position.
#[unsafe(method(draggingUpdated:))] #[unsafe(method(draggingUpdated:))]
fn dragging_updated(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool { fn dragging_updated(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
trace_scope!("draggingUpdated:"); let _entered = debug_span!("draggingUpdated:").entered();
let dl = sender.draggingLocation(); let dl = sender.draggingLocation();
let dl = self.view().convertPoint_fromView(dl, None); let dl = self.view().convertPoint_fromView(dl, None);
@@ -416,14 +416,14 @@ define_class!(
/// Invoked when the image is released /// Invoked when the image is released
#[unsafe(method(prepareForDragOperation:))] #[unsafe(method(prepareForDragOperation:))]
fn prepare_for_drag_operation(&self, _sender: &NSObject) -> bool { fn prepare_for_drag_operation(&self, _sender: &NSObject) -> bool {
trace_scope!("prepareForDragOperation:"); let _entered = debug_span!("prepareForDragOperation:").entered();
true true
} }
/// Invoked after the released image has been removed from the screen /// Invoked after the released image has been removed from the screen
#[unsafe(method(performDragOperation:))] #[unsafe(method(performDragOperation:))]
fn perform_drag_operation(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool { fn perform_drag_operation(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
trace_scope!("performDragOperation:"); let _entered = debug_span!("performDragOperation:").entered();
use std::path::PathBuf; use std::path::PathBuf;
@@ -455,13 +455,13 @@ define_class!(
/// Invoked when the dragging operation is complete /// Invoked when the dragging operation is complete
#[unsafe(method(concludeDragOperation:))] #[unsafe(method(concludeDragOperation:))]
fn conclude_drag_operation(&self, _sender: Option<&NSObject>) { fn conclude_drag_operation(&self, _sender: Option<&NSObject>) {
trace_scope!("concludeDragOperation:"); let _entered = debug_span!("concludeDragOperation:").entered();
} }
/// Invoked when the dragging operation is cancelled /// Invoked when the dragging operation is cancelled
#[unsafe(method(draggingExited:))] #[unsafe(method(draggingExited:))]
fn dragging_exited(&self, sender: Option<&ProtocolObject<dyn NSDraggingInfo>>) { fn dragging_exited(&self, sender: Option<&ProtocolObject<dyn NSDraggingInfo>>) {
trace_scope!("draggingExited:"); let _entered = debug_span!("draggingExited:").entered();
let position = sender.map(|sender| { let position = sender.map(|sender| {
let dl = sender.draggingLocation(); let dl = sender.draggingLocation();
@@ -483,7 +483,7 @@ define_class!(
change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>, change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>,
_context: *mut c_void, _context: *mut c_void,
) { ) {
trace_scope!("observeValueForKeyPath:ofObject:change:context:"); let _entered = debug_span!("observeValueForKeyPath:ofObject:change:context:").entered();
// NOTE: We don't _really_ need to check the key path, as there should only be one, but // NOTE: We don't _really_ need to check the key path, as there should only be one, but
// in the future we might want to observe other key paths. // in the future we might want to observe other key paths.
if key_path == Some(ns_string!("effectiveAppearance")) { if key_path == Some(ns_string!("effectiveAppearance")) {

View File

@@ -88,7 +88,7 @@ impl EventLoopBuilder {
)] )]
#[inline] #[inline]
pub fn build(&mut self) -> Result<EventLoop, EventLoopError> { pub fn build(&mut self) -> Result<EventLoop, EventLoopError> {
let _span = tracing::debug_span!("winit::EventLoopBuilder::build").entered(); let _entered = tracing::debug_span!("winit::EventLoopBuilder::build").entered();
// Certain platforms accept a mutable reference in their API. // Certain platforms accept a mutable reference in their API.
#[allow(clippy::unnecessary_mut_passed)] #[allow(clippy::unnecessary_mut_passed)]
@@ -262,7 +262,7 @@ impl EventLoop {
/// ///
/// [`DeviceEvent`]: crate::event::DeviceEvent /// [`DeviceEvent`]: crate::event::DeviceEvent
pub fn listen_device_events(&self, allowed: DeviceEvents) { pub fn listen_device_events(&self, allowed: DeviceEvents) {
let _span = tracing::debug_span!( let _entered = tracing::debug_span!(
"winit::EventLoop::listen_device_events", "winit::EventLoop::listen_device_events",
allowed = ?allowed allowed = ?allowed
) )