-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Desktop: Implement native file handler on Mac #4106
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -102,7 +102,16 @@ pub fn start() { | |||||
| } | ||||||
| }; | ||||||
|
|
||||||
| let app = App::new(Box::new(cef_context), cef_view_info_sender, wgpu_context, app_event_receiver, app_event_scheduler, prefs, cli); | ||||||
| let app = App::new( | ||||||
| Box::new(cef_context), | ||||||
| cef_view_info_sender, | ||||||
| wgpu_context, | ||||||
| app_event_receiver, | ||||||
| app_event_scheduler, | ||||||
| prefs, | ||||||
| cli.files, | ||||||
| cli.disable_ui_acceleration, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: App::new is passed Prompt for AI agents
Suggested change
|
||||||
| ); | ||||||
|
|
||||||
| let exit_reason = app.run(event_loop); | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,22 @@ | ||||||
| use objc2::{ClassType, define_class, msg_send}; | ||||||
| use objc2_app_kit::{NSApplication, NSEvent, NSEventType, NSResponder}; | ||||||
| use objc2_foundation::NSObject; | ||||||
| use std::ffi::CStr; | ||||||
| use std::ffi::OsStr; | ||||||
| use std::ops::Deref; | ||||||
| use std::os::unix::ffi::OsStrExt; | ||||||
| use std::path::PathBuf; | ||||||
| use std::sync::{Mutex, Once}; | ||||||
|
|
||||||
| use objc2::rc::Retained; | ||||||
| use objc2::runtime::ProtocolObject; | ||||||
| use objc2::{ClassType, MainThreadMarker, MainThreadOnly, define_class, msg_send}; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| use objc2_app_kit::{NSApplication, NSApplicationDelegate, NSEvent, NSEventType, NSResponder}; | ||||||
| use objc2_foundation::{NSArray, NSObject, NSObjectProtocol, NSURL}; | ||||||
|
|
||||||
| use crate::event::{AppEvent, AppEventScheduler}; | ||||||
|
|
||||||
| static APP_EVENT_SCHEDULER: Mutex<Option<AppEventScheduler>> = Mutex::new(None); | ||||||
| static INSTALL_DELEGATE: Once = Once::new(); | ||||||
|
|
||||||
| static LAUNCH_DOCUMENTS: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new()); | ||||||
|
|
||||||
| define_class!( | ||||||
| #[unsafe(super(NSApplication, NSResponder, NSObject))] | ||||||
|
|
@@ -20,12 +36,66 @@ define_class!( | |||||
| } | ||||||
| ); | ||||||
|
|
||||||
| define_class!( | ||||||
| #[unsafe(super(NSObject))] | ||||||
| #[thread_kind = MainThreadOnly] | ||||||
| #[name = "GraphiteApplicationDelegate"] | ||||||
| struct GraphiteApplicationDelegate; | ||||||
|
|
||||||
| unsafe impl NSObjectProtocol for GraphiteApplicationDelegate {} | ||||||
|
|
||||||
| unsafe impl NSApplicationDelegate for GraphiteApplicationDelegate { | ||||||
| #[unsafe(method(application:openURLs:))] | ||||||
| fn application_open_urls(&self, _application: &NSApplication, urls: &NSArray<NSURL>) { | ||||||
| let Some(app_event_scheduler) = APP_EVENT_SCHEDULER.lock().ok() else { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The error message here is misleading: Prompt for AI agents |
||||||
| tracing::error!("Received macOS open URL event before the app event scheduler was initialized"); | ||||||
| return; | ||||||
| }; | ||||||
|
Comment on lines
+50
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message here is misleading. Additionally, returning early on a poisoned lock prevents the paths from being added to the buffer. It is better to collect the paths first and then attempt to schedule the event if the scheduler is available. fn application_open_urls(&self, _application: &NSApplication, urls: &NSArray<NSURL>) {
let mut paths = Vec::new();
for index in 0..urls.count() {
let url = urls.objectAtIndex(index);
if !url.isFileURL() {
tracing::error!("Ignoring macOS open URL event for non-file URL: {:?}", url);
continue;
}
let path = unsafe { CStr::from_ptr(url.fileSystemRepresentation().as_ptr()) };
let path = PathBuf::from(OsStr::from_bytes(path.to_bytes()));
paths.push(path);
}
if paths.is_empty() {
return;
}
let mut pending_paths_to_open = LAUNCH_DOCUMENTS.lock().unwrap();
pending_paths_to_open.extend(paths);
if let Ok(scheduler_guard) = APP_EVENT_SCHEDULER.lock() {
if let Some(app_event_scheduler) = scheduler_guard.as_ref() {
app_event_scheduler.schedule(AppEvent::AddLaunchDocuments(std::mem::take(&mut pending_paths_to_open)));
}
}
} |
||||||
|
|
||||||
| let mut pending_paths_to_open = LAUNCH_DOCUMENTS.lock().unwrap(); | ||||||
|
|
||||||
| for index in 0..urls.count() { | ||||||
| let url = urls.objectAtIndex(index); | ||||||
| if !url.isFileURL() { | ||||||
| tracing::error!("Ignoring macOS open URL event for non-file URL: {:?}", url); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| let path = unsafe { CStr::from_ptr(url.fileSystemRepresentation().as_ptr()) }; | ||||||
| let path = PathBuf::from(OsStr::from_bytes(path.to_bytes())); | ||||||
|
|
||||||
| pending_paths_to_open.push(path); | ||||||
| } | ||||||
|
|
||||||
| if let Some(app_event_scheduler) = app_event_scheduler.deref() { | ||||||
| app_event_scheduler.schedule(AppEvent::AddLaunchDocuments(std::mem::take(&mut pending_paths_to_open))); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| fn instance() -> objc2::rc::Retained<NSApplication> { | ||||||
| unsafe { msg_send![GraphiteApplication::class(), sharedApplication] } | ||||||
| } | ||||||
|
|
||||||
| pub(super) fn init() { | ||||||
| let _ = instance(); | ||||||
|
|
||||||
| INSTALL_DELEGATE.call_once(|| { | ||||||
| let mtm = MainThreadMarker::new().expect("only ever called from main thread"); | ||||||
| let delegate: Retained<GraphiteApplicationDelegate> = unsafe { msg_send![super(GraphiteApplicationDelegate::alloc(mtm).set_ivars(())), init] }; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of
Suggested change
|
||||||
| instance().setDelegate(Some(ProtocolObject::from_ref(&*delegate))); | ||||||
| std::mem::forget(delegate); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| pub(super) fn setup(app_event_scheduler: AppEventScheduler) { | ||||||
| let mut app_event_scheduler_guard = APP_EVENT_SCHEDULER.lock().unwrap(); | ||||||
|
|
||||||
| let mut pending_paths_to_open = LAUNCH_DOCUMENTS.lock().unwrap(); | ||||||
| app_event_scheduler.schedule(AppEvent::AddLaunchDocuments(std::mem::take(&mut pending_paths_to_open))); | ||||||
|
|
||||||
| *app_event_scheduler_guard = Some(app_event_scheduler); | ||||||
| } | ||||||
|
|
||||||
| pub(super) fn hide() { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a small typo in the error message: "send" should be "sent".