Skip to content

[Question] Responding to open file events using App Delegate. #818

@JamesMoreau

Description

@JamesMoreau

Hi, I am writing an egui application for macos. I would like for the user to be able to right click a file of a given type then "Open With" my app.

First I added CFBundleDocumentTypes and related subfields to the info.plist so that my application shows up in the menu list.

Now, following the example code I need to create and register the AppDelegate:

fn main() {
    let mtm: MainThreadMarker = MainThreadMarker::new().unwrap();

    let app = NSApplication::sharedApplication(mtm);
    app.setActivationPolicy(NSApplicationActivationPolicy::Regular);

    // configure the application delegate
    let delegate = AppDelegate::new(42, true, mtm);
    let object = ProtocolObject::from_ref(&*delegate);
    app.setDelegate(Some(object));

    // run the app
    app.run();
}

My issue is with this last line: app.run();. If i call this, the control is taken away from my program. If I don't call it, I can't seem to get the method callbacks, such as applicationDidFinishLaunching and application:openFiles to fire.

Instead I would like to register the app delegate, then return to my app egui code.

fn main() -> eframe::Result {
    env_logger::init(); // Log to stderr (if run with `RUST_LOG=debug`).

    let icon_data = icon_data::from_png_bytes(include_bytes!("../assets/icon.png")).expect("The icon data must be valid");

    let options = NativeOptions {
        viewport: ViewportBuilder::default(),
        event_loop_builder: Some(Box::new(|_| {
            #[cfg(target_os = "macos")]
            install_app_delegate(); // <-- setup app delegate here.
        })),
        ..Default::default()
    };
    run_native("My App", options, Box::new(|cc| Ok(Box::new(my_app_setup(cc)))))
}

Is this possible, or is my approach wrong?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frameworkAffects the framework crates and the translator for themquestionThere is no such thing as a dumb one!

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions