macos_platform
only.Expand description
§macOS / AppKit
Winit has an OS requirement of macOS 10.11 or higher (same as Rust itself), and is regularly tested on macOS 10.14.
§Window initialization
A lot of functionality expects the application to be ready before you start doing anything; this includes creating windows, fetching monitors, drawing, and so on, see issues #2238, #2051 and #2087.
If you encounter problems, you should try doing your initialization inside
ApplicationHandler::resumed
.
§Custom NSApplicationDelegate
Winit usually handles everything related to the lifecycle events of the application. Sometimes, though, you might want to do more niche stuff, such as handle when the user re-activates the application. Such functionality is not exposed directly in Winit, since it would increase the API surface by quite a lot.
Instead, Winit guarantees that it will not register an application delegate, so the solution is
to register your own application delegate, as outlined in the following example (see
objc2-app-kit
for more detailed information).
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_app_kit::{NSApplication, NSApplicationDelegate};
use objc2_foundation::{NSArray, NSURL, MainThreadMarker, NSObject, NSObjectProtocol};
use winit::event_loop::EventLoop;
declare_class!(
struct AppDelegate;
unsafe impl ClassType for AppDelegate {
type Super = NSObject;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "MyAppDelegate";
}
impl DeclaredClass for AppDelegate {}
unsafe impl NSObjectProtocol for AppDelegate {}
unsafe impl NSApplicationDelegate for AppDelegate {
#[method(application:openURLs:)]
fn application_openURLs(&self, application: &NSApplication, urls: &NSArray<NSURL>) {
// Note: To specifically get `application:openURLs:` to work, you _might_
// have to bundle your application. This is not done in this example.
println!("open urls: {application:?}, {urls:?}");
}
}
);
impl AppDelegate {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
unsafe { msg_send_id![super(mtm.alloc().set_ivars(())), init] }
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let event_loop = EventLoop::new()?;
let mtm = MainThreadMarker::new().unwrap();
let delegate = AppDelegate::new(mtm);
// Important: Call `sharedApplication` after `EventLoop::new`,
// doing it before is not yet supported.
let app = NSApplication::sharedApplication(mtm);
app.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
// event_loop.run_app(&mut my_app);
Ok(())
}
Enums§
- Activation
Policy - Corresponds to
NSApplicationActivationPolicy
. - Option
AsAlt - Option as alt behavior.
Traits§
- Active
Event Loop ExtMacOS - Additional methods on
ActiveEventLoop
that are specific to macOS. - Event
Loop Builder ExtMacOS - Monitor
Handle ExtMacOS - Additional methods on
MonitorHandle
that are specific to MacOS. - Window
Attributes ExtMacOS - Additional methods on
WindowAttributes
that are specific to MacOS. - Window
ExtMacOS - Additional methods on
Window
that are specific to MacOS.