gstreamer-rs/tutorials/src/tutorials-common.rs
Piotr Brzeziński eec3f18936 examples/tutorials: Use NSApp terminate() instead of sending an event
Has the same effect while being much more concise.
Unfortunately the cocoa crate doesn't (yet?) have bindings for this
function, so objc::msg_send! had to be used directly.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1163>
2022-12-12 13:23:46 +02:00

41 lines
1,010 B
Rust

/// macOS has a specific requirement that there must be a run loop running on the main thread in
/// order to open windows and use OpenGL, and that the global NSApplication instance must be
/// initialized.
/// On macOS this launches the callback function on a thread.
/// On other platforms it's just executed immediately.
#[cfg(not(target_os = "macos"))]
pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
where
T: Send + 'static,
{
main()
}
#[cfg(target_os = "macos")]
pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
where
T: Send + 'static,
{
use cocoa::appkit::NSApplication;
use objc::{msg_send, sel, sel_impl};
use std::thread;
unsafe {
let app = cocoa::appkit::NSApp();
let t = thread::spawn(|| {
let res = main();
let app = cocoa::appkit::NSApp();
let _: () = msg_send![app, terminate: cocoa::base::nil];
res
});
app.run();
t.join().unwrap()
}
}