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>
This commit is contained in:
Piotr Brzeziński 2022-12-11 22:09:54 +01:00 committed by Sebastian Dröge
parent a8dd4aa165
commit eec3f18936
2 changed files with 3 additions and 18 deletions

View file

@ -21,6 +21,7 @@ termion = { version = "2", optional = true }
[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.24"
objc = "0.2.7"
[features]
tutorial5 = ["gtk", "gdk", "gst-video"]

View file

@ -18,6 +18,7 @@ where
T: Send + 'static,
{
use cocoa::appkit::NSApplication;
use objc::{msg_send, sel, sel_impl};
use std::thread;
@ -27,24 +28,7 @@ where
let res = main();
let app = cocoa::appkit::NSApp();
app.stop_(cocoa::base::nil);
// Stopping the event loop requires an actual event
let event = cocoa::appkit::NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
cocoa::base::nil,
cocoa::appkit::NSEventType::NSApplicationDefined,
cocoa::foundation::NSPoint { x: 0.0, y: 0.0 },
cocoa::appkit::NSEventModifierFlags::empty(),
0.0,
0,
cocoa::base::nil,
cocoa::appkit::NSEventSubtype::NSApplicationActivatedEventType,
0,
0,
);
app.postEvent_atStart_(event, cocoa::base::YES);
std::process::exit(0);
let _: () = msg_send![app, terminate: cocoa::base::nil];
res
});