Skip to content

Commit

Permalink
Ensure that winit initializes NSApplication (#3069)
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm authored and kchibisov committed Oct 21, 2023
1 parent ee4ec43 commit b278aa8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use core_foundation::runloop::{
};
use icrate::Foundation::MainThreadMarker;
use objc2::rc::{autoreleasepool, Id};
use objc2::runtime::NSObjectProtocol;
use objc2::{msg_send_id, ClassType};
use raw_window_handle::{AppKitDisplayHandle, RawDisplayHandle};

Expand Down Expand Up @@ -109,7 +110,10 @@ impl<T> EventLoopWindowTarget<T> {

pub struct EventLoop<T: 'static> {
/// Store a reference to the application for convenience.
app: Id<WinitApplication>,
///
/// We intentially don't store `WinitApplication` since we want to have
/// the possiblity of swapping that out at some point.
app: Id<NSApplication>,
/// The delegate is only weakly referenced by NSApplication, so we keep
/// it around here as well.
_delegate: Id<ApplicationDelegate>,
Expand Down Expand Up @@ -152,15 +156,15 @@ impl<T> EventLoop<T> {
attributes: &PlatformSpecificEventLoopAttributes,
) -> Result<Self, EventLoopError> {
let mtm = MainThreadMarker::new()
.expect("On macOS, `EventLoop` must be created on the main thread!");
.expect("on macOS, `EventLoop` must be created on the main thread!");

// This must be done before `NSApp()` (equivalent to sending
// `sharedApplication`) is called anywhere else, or we'll end up
// with the wrong `NSApplication` class and the wrong thread could
// be marked as main.
let app: Id<WinitApplication> =
let app: Id<NSApplication> =
unsafe { msg_send_id![WinitApplication::class(), sharedApplication] };

if !app.is_kind_of::<WinitApplication>() {
panic!("`winit` requires control over the principal class. You must create the event loop before other parts of your application initialize NSApplication");
}

use NSApplicationActivationPolicy::*;
let activation_policy = match attributes.activation_policy {
ActivationPolicy::Regular => NSApplicationActivationPolicyRegular,
Expand Down

0 comments on commit b278aa8

Please sign in to comment.