Fill context menu of Zed macOS dock icon with recent projects (#8952)

Fixes https://github.com/zed-industries/zed/issues/8416

Release Notes:

- Added recent projects into Zed's macOS dock icon context menu ([8416](https://github.com/zed-industries/zed/issues/8416))

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
Daniel Zhu 2024-03-15 07:22:43 -07:00 committed by GitHub
parent 5bf0c8ed2d
commit cb16003133
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 121 additions and 23 deletions

View file

@ -1132,6 +1132,17 @@ impl AppContext {
self.platform.set_menus(menus, &self.keymap.borrow());
}
/// Adds given path to list of recent paths for the application.
/// The list is usually shown on the application icon's context menu in the dock,
/// and allows to open the recent files via that context menu.
pub fn add_recent_documents(&mut self, paths: &[PathBuf]) {
self.platform.add_recent_documents(paths);
}
/// Clears the list of recent paths from the application.
pub fn clear_recent_documents(&mut self) {
self.platform.clear_recent_documents();
}
/// Dispatch an action to the currently active window or global action handler
/// See [action::Action] for more information on how actions work
pub fn dispatch_action(&mut self, action: &dyn Action) {

View file

@ -118,6 +118,8 @@ pub(crate) trait Platform: 'static {
fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>);
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
fn add_recent_documents(&self, _paths: &[PathBuf]) {}
fn clear_recent_documents(&self) {}
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);

View file

@ -137,6 +137,7 @@ unsafe fn build_classes() {
sel!(application:openURLs:),
open_urls as extern "C" fn(&mut Object, Sel, id, id),
);
decl.register()
}
}
@ -766,6 +767,29 @@ impl Platform for MacPlatform {
}
}
fn add_recent_documents(&self, paths: &[PathBuf]) {
for path in paths {
let Some(path_str) = path.to_str() else {
log::error!("Not adding to recent documents a non-unicode path: {path:?}");
continue;
};
unsafe {
let document_controller: id =
msg_send![class!(NSDocumentController), sharedDocumentController];
let url: id = NSURL::fileURLWithPath_(nil, ns_string(path_str));
let _: () = msg_send![document_controller, noteNewRecentDocumentURL:url];
}
}
}
fn clear_recent_documents(&self) {
unsafe {
let document_controller: id =
msg_send![class!(NSDocumentController), sharedDocumentController];
let _: () = msg_send![document_controller, clearRecentDocuments:nil];
}
}
fn local_timezone(&self) -> UtcOffset {
unsafe {
let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone];
@ -1062,7 +1086,6 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
unsafe {
let app: id = msg_send![APP_CLASS, sharedApplication];
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
let platform = get_mac_platform(this);
let callback = platform.0.lock().finish_launching.take();
if let Some(callback) = callback {

View file

@ -241,6 +241,10 @@ impl Platform for TestPlatform {
fn set_menus(&self, _menus: Vec<crate::Menu>, _keymap: &Keymap) {}
fn add_recent_documents(&self, _paths: &[PathBuf]) {}
fn clear_recent_documents(&self) {}
fn on_app_menu_action(&self, _callback: Box<dyn FnMut(&dyn crate::Action)>) {}
fn on_will_open_app_menu(&self, _callback: Box<dyn FnMut()>) {}