UIKit: Add tracing spans

This commit is contained in:
Mads Marquart
2026-02-18 04:53:10 +01:00
parent a630b5333c
commit 98692641c4
4 changed files with 50 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ use objc2_ui_kit::{
UIApplicationWillResignActiveNotification, UIApplicationWillTerminateNotification, UIScreen, UIApplicationWillResignActiveNotification, UIApplicationWillTerminateNotification, UIScreen,
}; };
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, CustomCursorSource}; use winit_core::cursor::{CustomCursor, CustomCursorSource};
@@ -160,6 +161,7 @@ impl EventLoop {
// `application:didFinishLaunchingWithOptions:` // `application:didFinishLaunchingWithOptions:`
unsafe { UIApplicationDidFinishLaunchingNotification }, unsafe { UIApplicationDidFinishLaunchingNotification },
move |_| { move |_| {
let _entered = debug_span!("UIApplicationDidFinishLaunchingNotification").entered();
app_state::did_finish_launching(mtm); app_state::did_finish_launching(mtm);
}, },
); );
@@ -167,19 +169,27 @@ impl EventLoop {
&center, &center,
// `applicationDidBecomeActive:` // `applicationDidBecomeActive:`
unsafe { UIApplicationDidBecomeActiveNotification }, unsafe { UIApplicationDidBecomeActiveNotification },
move |_| app_state::handle_resumed(mtm), move |_| {
let _entered = debug_span!("UIApplicationDidBecomeActiveNotification").entered();
app_state::handle_resumed(mtm)
},
); );
let _will_resign_active_observer = create_observer( let _will_resign_active_observer = create_observer(
&center, &center,
// `applicationWillResignActive:` // `applicationWillResignActive:`
unsafe { UIApplicationWillResignActiveNotification }, unsafe { UIApplicationWillResignActiveNotification },
move |_| app_state::handle_suspended(mtm), move |_| {
let _entered = debug_span!("UIApplicationWillResignActiveNotification").entered();
app_state::handle_suspended(mtm)
},
); );
let _will_enter_foreground_observer = create_observer( let _will_enter_foreground_observer = create_observer(
&center, &center,
// `applicationWillEnterForeground:` // `applicationWillEnterForeground:`
unsafe { UIApplicationWillEnterForegroundNotification }, unsafe { UIApplicationWillEnterForegroundNotification },
move |notification| { move |notification| {
let _entered =
debug_span!("UIApplicationWillEnterForegroundNotification").entered();
let app = notification.object().expect( let app = notification.object().expect(
"UIApplicationWillEnterForegroundNotification to have application object", "UIApplicationWillEnterForegroundNotification to have application object",
); );
@@ -194,6 +204,7 @@ impl EventLoop {
// `applicationDidEnterBackground:` // `applicationDidEnterBackground:`
unsafe { UIApplicationDidEnterBackgroundNotification }, unsafe { UIApplicationDidEnterBackgroundNotification },
move |notification| { move |notification| {
let _entered = debug_span!("UIApplicationDidEnterBackgroundNotification").entered();
let app = notification.object().expect( let app = notification.object().expect(
"UIApplicationDidEnterBackgroundNotification to have application object", "UIApplicationDidEnterBackgroundNotification to have application object",
); );
@@ -208,6 +219,7 @@ impl EventLoop {
// `applicationWillTerminate:` // `applicationWillTerminate:`
unsafe { UIApplicationWillTerminateNotification }, unsafe { UIApplicationWillTerminateNotification },
move |notification| { move |notification| {
let _entered = debug_span!("UIApplicationWillTerminateNotification").entered();
let app = notification let app = notification
.object() .object()
.expect("UIApplicationWillTerminateNotification to have application object"); .expect("UIApplicationWillTerminateNotification to have application object");
@@ -221,7 +233,11 @@ impl EventLoop {
&center, &center,
// `applicationDidReceiveMemoryWarning:` // `applicationDidReceiveMemoryWarning:`
unsafe { UIApplicationDidReceiveMemoryWarningNotification }, unsafe { UIApplicationDidReceiveMemoryWarningNotification },
move |_| app_state::handle_memory_warning(mtm), move |_| {
let _entered =
debug_span!("UIApplicationDidReceiveMemoryWarningNotification").entered();
app_state::handle_memory_warning(mtm)
},
); );
let main_loop = MainRunLoop::get(mtm); let main_loop = MainRunLoop::get(mtm);

View File

@@ -13,7 +13,7 @@ use objc2_ui_kit::{
UIResponder, UIRotationGestureRecognizer, UITapGestureRecognizer, UITextInputTraits, UITouch, UIResponder, UIRotationGestureRecognizer, UITapGestureRecognizer, UITextInputTraits, UITouch,
UITouchPhase, UITouchType, UITraitEnvironment, UIView, UITouchPhase, UITouchType, UITraitEnvironment, UIView,
}; };
use tracing::debug; use tracing::{debug, debug_span, trace_span};
use winit_core::event::{ use winit_core::event::{
ButtonSource, ElementState, FingerId, Force, KeyEvent, PointerKind, PointerSource, ButtonSource, ElementState, FingerId, Force, KeyEvent, PointerKind, PointerSource,
TabletToolAngle, TabletToolButton, TabletToolData, TabletToolKind, TouchPhase, WindowEvent, TabletToolAngle, TabletToolButton, TabletToolData, TabletToolKind, TouchPhase, WindowEvent,
@@ -48,6 +48,7 @@ define_class!(
impl WinitView { impl WinitView {
#[unsafe(method(drawRect:))] #[unsafe(method(drawRect:))]
fn draw_rect(&self, rect: CGRect) { fn draw_rect(&self, rect: CGRect) {
let _entered = debug_span!("drawRect:").entered();
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
let window = self.window().unwrap(); let window = self.window().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::Window { app_state::handle_nonuser_event(mtm, EventWrapper::Window {
@@ -59,6 +60,7 @@ define_class!(
#[unsafe(method(layoutSubviews))] #[unsafe(method(layoutSubviews))]
fn layout_subviews(&self) { fn layout_subviews(&self) {
let _entered = debug_span!("layoutSubviews").entered();
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
let _: () = unsafe { msg_send![super(self), layoutSubviews] }; let _: () = unsafe { msg_send![super(self), layoutSubviews] };
@@ -79,6 +81,7 @@ define_class!(
#[unsafe(method(setContentScaleFactor:))] #[unsafe(method(setContentScaleFactor:))]
fn set_content_scale_factor(&self, untrusted_scale_factor: CGFloat) { fn set_content_scale_factor(&self, untrusted_scale_factor: CGFloat) {
let _entered = debug_span!("setContentScaleFactor:").entered();
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
let _: () = let _: () =
unsafe { msg_send![super(self), setContentScaleFactor: untrusted_scale_factor] }; unsafe { msg_send![super(self), setContentScaleFactor: untrusted_scale_factor] };
@@ -124,6 +127,7 @@ define_class!(
#[unsafe(method(safeAreaInsetsDidChange))] #[unsafe(method(safeAreaInsetsDidChange))]
fn safe_area_changed(&self) { fn safe_area_changed(&self) {
let _entered = debug_span!("safeAreaInsetsDidChange").entered();
debug!("safeAreaInsetsDidChange was called, requesting redraw"); debug!("safeAreaInsetsDidChange was called, requesting redraw");
// When the safe area changes we want to make sure to emit a redraw event // When the safe area changes we want to make sure to emit a redraw event
self.setNeedsDisplay(); self.setNeedsDisplay();
@@ -131,26 +135,31 @@ define_class!(
#[unsafe(method(touchesBegan:withEvent:))] #[unsafe(method(touchesBegan:withEvent:))]
fn touches_began(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) { fn touches_began(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) {
let _entered = debug_span!("touchesBegan:withEvent:").entered();
self.handle_touches(touches) self.handle_touches(touches)
} }
#[unsafe(method(touchesMoved:withEvent:))] #[unsafe(method(touchesMoved:withEvent:))]
fn touches_moved(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) { fn touches_moved(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) {
let _entered = debug_span!("touchesMoved:withEvent:").entered();
self.handle_touches(touches) self.handle_touches(touches)
} }
#[unsafe(method(touchesEnded:withEvent:))] #[unsafe(method(touchesEnded:withEvent:))]
fn touches_ended(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) { fn touches_ended(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) {
let _entered = debug_span!("touchesEnded:withEvent:").entered();
self.handle_touches(touches) self.handle_touches(touches)
} }
#[unsafe(method(touchesCancelled:withEvent:))] #[unsafe(method(touchesCancelled:withEvent:))]
fn touches_cancelled(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) { fn touches_cancelled(&self, touches: &NSSet<UITouch>, _event: Option<&UIEvent>) {
let _entered = debug_span!("touchesCancelled:withEvent:").entered();
self.handle_touches(touches) self.handle_touches(touches)
} }
#[unsafe(method(pinchGesture:))] #[unsafe(method(pinchGesture:))]
fn pinch_gesture(&self, recognizer: &UIPinchGestureRecognizer) { fn pinch_gesture(&self, recognizer: &UIPinchGestureRecognizer) {
let _entered = debug_span!("pinchGesture:").entered();
let window = self.window().unwrap(); let window = self.window().unwrap();
let (phase, delta) = match recognizer.state() { let (phase, delta) = match recognizer.state() {
@@ -185,6 +194,7 @@ define_class!(
#[unsafe(method(doubleTapGesture:))] #[unsafe(method(doubleTapGesture:))]
fn double_tap_gesture(&self, recognizer: &UITapGestureRecognizer) { fn double_tap_gesture(&self, recognizer: &UITapGestureRecognizer) {
let _entered = debug_span!("doubleTapGesture:").entered();
let window = self.window().unwrap(); let window = self.window().unwrap();
if recognizer.state() == UIGestureRecognizerState::Ended { if recognizer.state() == UIGestureRecognizerState::Ended {
@@ -200,6 +210,7 @@ define_class!(
#[unsafe(method(rotationGesture:))] #[unsafe(method(rotationGesture:))]
fn rotation_gesture(&self, recognizer: &UIRotationGestureRecognizer) { fn rotation_gesture(&self, recognizer: &UIRotationGestureRecognizer) {
let _entered = debug_span!("rotationGesture:").entered();
let window = self.window().unwrap(); let window = self.window().unwrap();
let (phase, delta) = match recognizer.state() { let (phase, delta) = match recognizer.state() {
@@ -244,6 +255,7 @@ define_class!(
#[unsafe(method(panGesture:))] #[unsafe(method(panGesture:))]
fn pan_gesture(&self, recognizer: &UIPanGestureRecognizer) { fn pan_gesture(&self, recognizer: &UIPanGestureRecognizer) {
let _entered = debug_span!("panGesture:").entered();
let window = self.window().unwrap(); let window = self.window().unwrap();
let translation = recognizer.translationInView(Some(self)); let translation = recognizer.translationInView(Some(self));
@@ -296,6 +308,7 @@ define_class!(
#[unsafe(method(canBecomeFirstResponder))] #[unsafe(method(canBecomeFirstResponder))]
fn can_become_first_responder(&self) -> bool { fn can_become_first_responder(&self) -> bool {
let _entered = trace_span!("canBecomeFirstResponder").entered();
true true
} }
} }
@@ -309,6 +322,10 @@ define_class!(
_gesture_recognizer: &UIGestureRecognizer, _gesture_recognizer: &UIGestureRecognizer,
_other_gesture_recognizer: &UIGestureRecognizer, _other_gesture_recognizer: &UIGestureRecognizer,
) -> bool { ) -> bool {
let _entered = trace_span!(
"gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:"
)
.entered();
true true
} }
} }
@@ -318,16 +335,19 @@ define_class!(
unsafe impl UIKeyInput for WinitView { unsafe impl UIKeyInput for WinitView {
#[unsafe(method(hasText))] #[unsafe(method(hasText))]
fn has_text(&self) -> bool { fn has_text(&self) -> bool {
let _entered = debug_span!("hasText").entered();
true true
} }
#[unsafe(method(insertText:))] #[unsafe(method(insertText:))]
fn insert_text(&self, text: &NSString) { fn insert_text(&self, text: &NSString) {
let _entered = debug_span!("insertText:").entered();
self.handle_insert_text(text) self.handle_insert_text(text)
} }
#[unsafe(method(deleteBackward))] #[unsafe(method(deleteBackward))]
fn delete_backward(&self) { fn delete_backward(&self) {
let _entered = debug_span!("deleteBackward").entered();
self.handle_delete_backward() self.handle_delete_backward()
} }
} }

View File

@@ -7,6 +7,7 @@ use objc2_ui_kit::{
UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle, UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle,
UIUserInterfaceIdiom, UIView, UIViewController, UIUserInterfaceIdiom, UIView, UIViewController,
}; };
use tracing::trace_span;
use crate::{ScreenEdge, StatusBarStyle, ValidOrientations, WindowAttributesIos}; use crate::{ScreenEdge, StatusBarStyle, ValidOrientations, WindowAttributesIos};
@@ -28,31 +29,37 @@ define_class!(
impl WinitViewController { impl WinitViewController {
#[unsafe(method(shouldAutorotate))] #[unsafe(method(shouldAutorotate))]
fn should_autorotate(&self) -> bool { fn should_autorotate(&self) -> bool {
let _entered = trace_span!("shouldAutorotate").entered();
true true
} }
#[unsafe(method(prefersStatusBarHidden))] #[unsafe(method(prefersStatusBarHidden))]
fn prefers_status_bar_hidden(&self) -> bool { fn prefers_status_bar_hidden(&self) -> bool {
let _entered = trace_span!("prefersStatusBarHidden").entered();
self.ivars().prefers_status_bar_hidden.get() self.ivars().prefers_status_bar_hidden.get()
} }
#[unsafe(method(preferredStatusBarStyle))] #[unsafe(method(preferredStatusBarStyle))]
fn preferred_status_bar_style(&self) -> UIStatusBarStyle { fn preferred_status_bar_style(&self) -> UIStatusBarStyle {
let _entered = trace_span!("preferredStatusBarStyle").entered();
self.ivars().preferred_status_bar_style.get() self.ivars().preferred_status_bar_style.get()
} }
#[unsafe(method(prefersHomeIndicatorAutoHidden))] #[unsafe(method(prefersHomeIndicatorAutoHidden))]
fn prefers_home_indicator_auto_hidden(&self) -> bool { fn prefers_home_indicator_auto_hidden(&self) -> bool {
let _entered = trace_span!("prefersHomeIndicatorAutoHidden").entered();
self.ivars().prefers_home_indicator_auto_hidden.get() self.ivars().prefers_home_indicator_auto_hidden.get()
} }
#[unsafe(method(supportedInterfaceOrientations))] #[unsafe(method(supportedInterfaceOrientations))]
fn supported_orientations(&self) -> UIInterfaceOrientationMask { fn supported_orientations(&self) -> UIInterfaceOrientationMask {
let _entered = trace_span!("supportedInterfaceOrientations").entered();
self.ivars().supported_orientations.get() self.ivars().supported_orientations.get()
} }
#[unsafe(method(preferredScreenEdgesDeferringSystemGestures))] #[unsafe(method(preferredScreenEdgesDeferringSystemGestures))]
fn preferred_screen_edges_deferring_system_gestures(&self) -> UIRectEdge { fn preferred_screen_edges_deferring_system_gestures(&self) -> UIRectEdge {
let _entered = trace_span!("preferredScreenEdgesDeferringSystemGestures").entered();
self.ivars().preferred_screen_edges_deferring_system_gestures.get() self.ivars().preferred_screen_edges_deferring_system_gestures.get()
} }
} }

View File

@@ -16,7 +16,7 @@ use objc2_ui_kit::{
UIApplication, UICoordinateSpace, UIEdgeInsets, UIResponder, UIScreen, UIApplication, UICoordinateSpace, UIEdgeInsets, UIResponder, UIScreen,
UIScreenOverscanCompensation, UIViewController, UIWindow, UIScreenOverscanCompensation, UIViewController, UIWindow,
}; };
use tracing::{debug, warn}; use tracing::{debug, debug_span, warn};
use winit_core::cursor::Cursor; use winit_core::cursor::Cursor;
use winit_core::error::{NotSupportedError, RequestError}; use winit_core::error::{NotSupportedError, RequestError};
use winit_core::event::WindowEvent; use winit_core::event::WindowEvent;
@@ -46,6 +46,7 @@ define_class!(
impl WinitUIWindow { impl WinitUIWindow {
#[unsafe(method(becomeKeyWindow))] #[unsafe(method(becomeKeyWindow))]
fn become_key_window(&self) { fn become_key_window(&self) {
let _entered = debug_span!("becomeKeyWindow").entered();
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::Window { app_state::handle_nonuser_event(mtm, EventWrapper::Window {
window_id: self.id(), window_id: self.id(),
@@ -56,6 +57,7 @@ define_class!(
#[unsafe(method(resignKeyWindow))] #[unsafe(method(resignKeyWindow))]
fn resign_key_window(&self) { fn resign_key_window(&self) {
let _entered = debug_span!("resignKeyWindow").entered();
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::Window { app_state::handle_nonuser_event(mtm, EventWrapper::Window {
window_id: self.id(), window_id: self.id(),