#5620·egui

Expose NSApp on macOS to add app delegates (e.g. "Open With ... " functionality)

Author: hacknusCreated Jan 20, 2025Updated Sep 24, 2026

Is your feature request related to a problem? Please describe. I want to create a GUI application that can open CSV files. On macOS, if you put the following lines in the info.plist of the Application.app:

 <key>CFBundleDocumentTypes</key>
  <array>
      <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
          <string>csv</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>Comma separated values</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
      </dict>
    </array>

This then tells Finder that you can "Open With ..." and drag/drop files on the icon of the app (not in the window - this is handled by egui with dropped_files).

However, upon dropping or using "Open With ...", it fails because internally no app-delegate is defined.

Describe the solution you'd like

As far as I understand, an app-delegate somewhat like this:

rust
unsafe {
      // Initialize NSApplication
      let app = NSApp();
      app.setActivationPolicy(cocoa::appkit::NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular);

      // Set up the delegate
      let delegate = create_app_delegate();
      let _: () = msg_send![app, setDelegate: delegate];

      // Run the macOS application (This is blocking)
      app.run();
  }

would need to be defined. But as mentioned in #3411 this cannot be done outside of egui, since egui already creates the NSApp.

Describe alternatives you've considered I tried putting the code outside of egui in a thread in my main() function and this is the error:

winit requires control over the principal class. You must create the event loop before other parts of your application initialize NSApplication

Additional context It probably needs to be handled inside egui somewhere around https://github.com/emilk/egui/blob/cf965aaa30987a5b6fa2380f37c0ce8cb869347d/crates/eframe/src/native/app_icon.rs#L202 and exposing the possibility to add app-delegates can also then open up the possibility to add native menu items (#3411)

I am not sure if my approach is going in the correct direction, but I would be fond of the possibility to easily implement "Open With ..." and "Menu Items".