Skip to content

Commit

Permalink
macOS RAII trace guards (rust-windowing#2150)
Browse files Browse the repository at this point in the history
* Add TraceGuard to make tracing simpler

* Add SharedStateMutexGuard to make tracing simpler

* Add trace_scope macro

* Add missing let binding in trace_scope!
  • Loading branch information
madsmtm authored Jan 23, 2022
1 parent 51bb6b7 commit 9229e2d
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 135 deletions.
3 changes: 1 addition & 2 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ extern "C" fn dealloc(this: &Object, _: Sel) {
}

extern "C" fn did_finish_launching(this: &Object, _: Sel, _: id) {
trace!("Triggered `applicationDidFinishLaunching`");
trace_scope!("applicationDidFinishLaunching:");
AppState::launched(this);
trace!("Completed `applicationDidFinishLaunching`");
}
4 changes: 3 additions & 1 deletion src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![cfg(target_os = "macos")]

#[macro_use]
mod util;

mod app;
mod app_delegate;
mod app_state;
Expand All @@ -9,7 +12,6 @@ mod ffi;
mod menu;
mod monitor;
mod observer;
mod util;
mod view;
mod window;
mod window_delegate;
Expand Down
19 changes: 11 additions & 8 deletions src/platform_impl/macos/util/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use objc::runtime::{BOOL, NO};

use crate::{
dpi::LogicalSize,
platform_impl::platform::{ffi, util::IdRef, window::SharedState},
platform_impl::platform::{
ffi,
util::IdRef,
window::{SharedState, SharedStateMutexGuard},
},
};

// Unsafe wrapper type that allows us to dispatch things that aren't Send.
Expand Down Expand Up @@ -111,10 +115,11 @@ pub unsafe fn toggle_full_screen_async(
if !curr_mask.contains(required) {
set_style_mask(*ns_window, *ns_view, required);
if let Some(shared_state) = shared_state.upgrade() {
trace!("Locked shared state in `toggle_full_screen_callback`");
let mut shared_state_lock = shared_state.lock().unwrap();
let mut shared_state_lock = SharedStateMutexGuard::new(
shared_state.lock().unwrap(),
"toggle_full_screen_callback",
);
(*shared_state_lock).saved_style = Some(curr_mask);
trace!("Unlocked shared state in `toggle_full_screen_callback`");
}
}
}
Expand Down Expand Up @@ -144,8 +149,8 @@ pub unsafe fn set_maximized_async(
let shared_state = MainThreadSafe(shared_state);
Queue::main().exec_async(move || {
if let Some(shared_state) = shared_state.upgrade() {
trace!("Locked shared state in `set_maximized`");
let mut shared_state_lock = shared_state.lock().unwrap();
let mut shared_state_lock =
SharedStateMutexGuard::new(shared_state.lock().unwrap(), "set_maximized");

// Save the standard frame sized if it is not zoomed
if !is_zoomed {
Expand All @@ -171,8 +176,6 @@ pub unsafe fn set_maximized_async(
};
ns_window.setFrame_display_(new_rect, NO);
}

trace!("Unlocked shared state in `set_maximized`");
}
});
}
Expand Down
35 changes: 30 additions & 5 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cocoa::{
foundation::{NSPoint, NSRect, NSString, NSUInteger},
};
use core_graphics::display::CGDisplay;
use objc::runtime::{Class, Object, Sel, BOOL, YES};
use objc::runtime::{Class, Object};

use crate::dpi::LogicalPosition;
use crate::platform_impl::platform::ffi;
Expand Down Expand Up @@ -79,6 +79,35 @@ impl Clone for IdRef {
}
}

macro_rules! trace_scope {
($s:literal) => {
let _crate = $crate::platform_impl::platform::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);
}
}

// For consistency with other platforms, this will...
// 1. translate the bottom-left window corner into the top-left window corner
// 2. translate the coordinate from a bottom-left origin coordinate system to a top-left one
Expand Down Expand Up @@ -129,10 +158,6 @@ pub unsafe fn open_emoji_picker() {
let () = msg_send![NSApp(), orderFrontCharacterPalette: nil];
}

pub extern "C" fn yes(_: &Object, _: Sel) -> BOOL {
YES
}

pub unsafe fn toggle_style_mask(window: id, view: id, mask: NSWindowStyleMask, on: bool) {
use cocoa::appkit::NSWindow;

Expand Down
Loading

0 comments on commit 9229e2d

Please sign in to comment.