From 5657d8b391f6a5158a1bfff74ef44040c9bae50b Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Tue, 28 May 2024 19:04:20 +0200 Subject: [PATCH] Add Event::OpenFiles --- src/application.rs | 6 ++++++ src/event.rs | 13 +++++++++++-- src/platform_impl/macos/app_delegate.rs | 19 ++++++++++++++++++- src/platform_impl/macos/event_loop.rs | 1 + 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/application.rs b/src/application.rs index 118ba6e528..9860ccc220 100644 --- a/src/application.rs +++ b/src/application.rs @@ -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; @@ -222,4 +224,8 @@ pub trait ApplicationHandler { fn memory_warning(&mut self, event_loop: &ActiveEventLoop) { let _ = event_loop; } + + fn open_files(&mut self, files: Vec) { + let _ = files; + } } diff --git a/src/event.rs b/src/event.rs index 5cd3877a26..240eb1eb49 100644 --- a/src/event.rs +++ b/src/event.rs @@ -67,12 +67,18 @@ pub enum Event { /// 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. /// @@ -103,6 +109,8 @@ pub enum Event { /// /// [`ApplicationHandler::memory_warning`]: crate::application::ApplicationHandler::memory_warning MemoryWarning, + + OpenFiles(Vec), } impl Event { @@ -119,6 +127,7 @@ impl Event { Suspended => Ok(Suspended), Resumed => Ok(Resumed), MemoryWarning => Ok(MemoryWarning), + OpenFiles(path) => Ok(OpenFiles(path)), } } } diff --git a/src/platform_impl/macos/app_delegate.rs b/src/platform_impl/macos/app_delegate.rs index 9de572aa55..a18b8bcc25 100644 --- a/src/platform_impl/macos/app_delegate.rs +++ b/src/platform_impl/macos/app_delegate.rs @@ -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; @@ -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}; @@ -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,) -> bool { + trace_scope!("applicationOpenFiles:"); + self.handle_event(Event::OpenFiles( + filenames.iter().map(|filename| format!("{filename}")).map(PathBuf::from).collect() + )); + true + } } ); diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 68f411cef9..e7155833ce 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -178,6 +178,7 @@ fn map_user_event>( 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), } }