Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Event::OpenFiles #3713

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! End user application handling.

use std::path::PathBuf;

use crate::event::{DeviceEvent, DeviceId, StartCause, WindowEvent};
use crate::event_loop::ActiveEventLoop;
use crate::window::WindowId;
Expand Down Expand Up @@ -222,4 +224,8 @@ pub trait ApplicationHandler<T: 'static = ()> {
fn memory_warning(&mut self, event_loop: &ActiveEventLoop) {
let _ = event_loop;
}

fn open_files(&mut self, files: Vec<PathBuf>) {
let _ = files;
}
}
13 changes: 11 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ pub enum Event<T: 'static> {
/// See [`ApplicationHandler::window_event`] for details.
///
/// [`ApplicationHandler::window_event`]: crate::application::ApplicationHandler::window_event
WindowEvent { window_id: WindowId, event: WindowEvent },
WindowEvent {
window_id: WindowId,
event: WindowEvent,
},

/// See [`ApplicationHandler::device_event`] for details.
///
/// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event
DeviceEvent { device_id: DeviceId, event: DeviceEvent },
DeviceEvent {
device_id: DeviceId,
event: DeviceEvent,
},

/// See [`ApplicationHandler::user_event`] for details.
///
Expand Down Expand Up @@ -103,6 +109,8 @@ pub enum Event<T: 'static> {
///
/// [`ApplicationHandler::memory_warning`]: crate::application::ApplicationHandler::memory_warning
MemoryWarning,

OpenFiles(Vec<PathBuf>),
}

impl<T> Event<T> {
Expand All @@ -119,6 +127,7 @@ impl<T> Event<T> {
Suspended => Ok(Suspended),
Resumed => Ok(Resumed),
MemoryWarning => Ok(MemoryWarning),
OpenFiles(path) => Ok(OpenFiles(path)),
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::mem;
use std::path::PathBuf;
use std::rc::Weak;
use std::sync::{Arc, Mutex};
use std::time::Instant;
Expand All @@ -9,7 +10,7 @@ use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate};
use objc2_foundation::{MainThreadMarker, NSObject, NSObjectProtocol, NSSize};
use objc2_foundation::{MainThreadMarker, NSArray, NSObject, NSObjectProtocol, NSSize, NSString};

use super::event_handler::EventHandler;
use super::event_loop::{stop_app_immediately, ActiveEventLoop, PanicInfo};
Expand Down Expand Up @@ -124,6 +125,22 @@ declare_class!(
// TODO: Notify every window that it will be destroyed, like done in iOS?
self.internal_exit();
}

#[method(application:openFile:)]
unsafe fn application_open_file(&self, _: &NSApplication, filename: &NSString) -> bool {
trace_scope!("applicationOpenFile:");
self.handle_event(Event::OpenFiles(vec![PathBuf::from(format!("{filename}"))]));
true
}

#[method(application:openFiles:)]
unsafe fn application_open_files(&self, _: &NSApplication, filenames: &NSArray<NSString>,) -> bool {
trace_scope!("applicationOpenFiles:");
self.handle_event(Event::OpenFiles(
filenames.iter().map(|filename| format!("{filename}")).map(PathBuf::from).collect()
));
true
}
}
);

Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn map_user_event<T: 'static, A: ApplicationHandler<T>>(
Event::AboutToWait => app.about_to_wait(window_target),
Event::LoopExiting => app.exiting(window_target),
Event::MemoryWarning => app.memory_warning(window_target),
Event::OpenFiles(files) => app.open_files(files),
}
}

Expand Down
Loading