-
Notifications
You must be signed in to change notification settings - Fork 921
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
Double-click opening a file on macOS #1751
Comments
I'm willing to implement this but first it would be nice to get some feedback from someone who's made macOS apps that can open associated files and has an idea of how this should be implemented in winit. My plan is to handle both of pub enum WindowEvent {
// ...
/// A file was requested to be opened. This must be handled to allow a macOS app to open associated files that are
/// double-clicked. Other systems usually pass the file path as an argument to the program. (See `std::env::args()`)
OpenFiles(Vec<PathBuf>),
} |
@chrisduerr and @vbogaevsky as you are listed to be the contributors/maintainers for this platform, I'd like to ask you to give an opinion on the solution I suggested above. I'd be happy to tackle this 😊 |
I've never built any macOS applications, so I cannot tell you if the API is correct or even sensible to use. Though as stated previously it would be a pretty strange API for something like this, rather than an application reentry. But I see no reason what would stop anyone from just sending a PR to add these APIs, it all seems very straight-forward to me. |
Using |
I still don't understand what the point here. You track clicks and you have some sort of file view in your application, then you make this click and it goes through your ui view hierarchy. Then you check if you point on file and open a file, why winit should be even involved here, opening files in UI toolkit has nothing to do with winit imho. |
@alvinhochun Correct, these are limited to macOS as far as I can tell (Windows and GNOME will just forward the path as an argument and I assume so does every other Linux desktop envornment). If a variant shouldn't be added, what would be the solution you'd suggest? @kchibisov No that's not what I'm trying to do. This refers to using the native file browser, "Finder" on macOS and double clicking there. The OS then figures out which application to open and the OS opens the application and calls this ominous |
Can't you just get |
@ArturKovacs I'm not sure if there are existing patterns in winit for handling platform-specific events. @kchibisov My guess is that winit subclasses |
Line 246 in 3a077ff
If something like that works, I'm curious whether using that method will work for file opening. |
Well, I was going to suggest adding |
Thanks for the pointer @kchibisov, I'll try to experiment a bit to see if it's possible without having to add anything to winit. Yeah the platform specific extension trait would definitely be less intrusive, I like that idea, @alvinhochun. What I'm a bit concerned about is the timing of things, specifically if it's going to be possible for the user to call the appropriate function in time to actually receive the callback. Although this can be mitigated by keeping the last |
I looked into this and as you probably know, winit defines the Later I'll explore the possible implementation of an extension trait that allows registering a callback. |
Hm, maybe we should have some event loop extension builder, I'd need something like that for Wayland actually. |
You mean something like this? enum Event {
WindowEvent {...}
// All the other events events
Extra(ExtraEvent)
}
// On Linux
enum WaylandEvent {
...
}
impl ExtraEventWaylandExt for ExtraEvent {
fn into_wayland_event(self) -> WaylandEvent {...}
} |
No, for building event loop, not for events. Like you don't need event extension if you provide function pointers to winit when you're creating event loop or something like that. |
I see! Would that be identical to registering callback functions with the |
I don't claim to know anything about GUI programming on macOS at all, but I think as long as the callback is registered before the |
I don't know why I wanted to place the function on the |
Triage: The PR implementing this is several years old, and the approach was one I was never happy with (changing the application delegate class at runtime), since it is harder to ensure soundness, and it may interfere with downstream applications that want to override the application delegate themselves (e.g. accesskit does this for Instead, I think we should:
I will probably implement this myself at some point (if so, the I will assign myself to the issue), though my priorities are elsewhere at the moment, so if someone wants to do it first, feel free! |
Likely that this will get easier after #2903, if so then I'll probably tackle it |
That's great news @madsmtm! Thank you for the update and please let me know if I can help somehow like testing or anything. |
I ended up taking a completely different path with this: Instead of trying to expose all the functionality on So after #3758, listening to these events will be possible by implementing a custom (Wow, it really was a rollercoaster getting this done!) |
That's awesome! I'm glad to hear that :) Btw, I coincidentally spent the last days working on a native macOS menu bar with winit. |
Beware that I'm still not convinced that we should do native menu bars in Winit, see if the |
Oh no, I just meant a project using winit which should also get a native menubar, not contributing to winit itself! Sorry for the confusion 😅 |
No problem! The short answer though is that listening for menu bar events was possible before too, you just had to use any object, it didn't have to be the application delegate.
Yeah, I can see how that might've been a bit confusing - I really should write some better introductory docs on this in |
Thanks for the explanation! :) |
When a file is associated with an app on macOS, the
application:openFile:
or theapplication:openFiles:
function gets called instead of passing the target filepath as an argument. The application must handle theese calls otherwise the os will display an error message to the user that the appilcation doesn't support opening the file type.Winit currently doesn't seem to handle either of these and there doesn't seem to be any other way of making a winit application able to handle opening a file for when the user double-clicks on an associated file.
The text was updated successfully, but these errors were encountered: